var $totFrontItem = 0;

var tweenOver = function(event, $target) {
	var label_txt = $target.getChildren('.titleHolder');
	label_txt.set('tween', {duration: '200'});
	label_txt.tween('bottom','0px');
};


var tweenOut = function(event, $target) {
	var label_txt = $target.getChildren('.titleHolder');
	label_txt.set('tween', {duration: 'long'});
	label_txt.tween('bottom','-80px');
};


var landPortOver = function(event, $target) {
	var portTitle = $target.getElement('h3');
	portTitle.fade('show');
};


var landPortOut = function(event, $target) {
	var portTitle = $target.getElement('h3');
	portTitle.fade('out');
};



// FRONT PAGE FEATURED ELEMENTS SCRIPTS

var i = 1; 
var timer;

var frontFeatOver = function(event, $target) {
	
	$$('.info_box').each( function($e){
		$e.tween('top', 270);
		$e.addClass('darkBox');
	});
	
   clearInterval(timer);
   ffNavShow('.6');    
};


var frontFeatOut = function(event, $target) {
	
	$$('.info_box').each( function($e){
		$e.tween('top', 340);
		$e.removeClass('darkBox');
	});
	
	clearInterval(timer);
	timer = featureItemChange.periodical(7000);
	ffNavShow('out');
}


// FRONT FEATURED ITEM CARROUSSEL, FORWARD TRANSITION

var featureItemChange = function() {
	$$('.front_featured .item'+i).each( function($e){
		$e.fade('in');
	});
	
	if( i > 1){
	$$('.front_featured .item'+(i-1)).each( function($e){
		$e.fade('out');
	});
	
	if(i == ($totFrontItem + 1)){
		i = 1;
		$$('.front_featured .item'+i).each( function($f){
		$f.set('tween', {duration: 100});
		$f.fade('in');
	});
	}

	}
	 
	i++;
	}


// FRONT FEATURED ITEM CARROUSSEL, BACKWARD TRANSITION

var featureItemBack = function(){

	i--;

		
	if( i > 1){
	
	$$('.front_featured .item'+ i).each( function($e){
		$e.fade('out');
	});
	
	$$('.front_featured .item'+(i-1)).each( function($e){
		$e.fade('in');
	});
	}
	
	if(i == 1){
	i = $totFrontItem;
	$$('.front_featured .item'+ $totFrontItem).each( function($e){
		$e.fade('in');
	});
	
	$$('.front_featured .item1').each( function($e){
		$e.fade('out');
	});
	
	i++;
	
	}
	
};

var startFeatureItem = function(){
	clearInterval(timer);
	timer = featureItemChange.periodical(7000);
	featureItemChange();
}


var ffNavOver = function(event) {
	$e = event.target.getParent('h4');
	$e.set('opacity','1');
}

var ffNavOut = function(event) {
	$e = event.target.getParent('h4');
	$e.set('opacity','0.6');
}


var ffNavShow = function($e) {
	$$('.front_featured h4').each( function($f){
	$f.fade($e);
	});
}

var ff_leftNav = function(){
	clearInterval(timer);
	featureItemBack();	
}


var ff_rightNav = function(){
	clearInterval(timer);
	featureItemChange();
}



// PHOTO GALLERY CODE

var scale = 1;

var imageGallery = function($e){
	var pic = $e.getElement('.picthmb'); ;
	var size = pic.getSize().y;
	pic.set('height','150px');
}


var photoOver = function($e){
	var container = $e.target.getParent('.photo');
	var pic = container.getElement('.picthmb');
	pic.removeProperty('height')
}

var photoOut = function($e){
	var container = $e.target.getParent('.photo');
	var pic = container.getElement('.picthmb');
	pic.set('height','150px');
}


var picLinkShow = function($e){	
	var container = $e.target.getParent('.photo');
	var pic = container.getElement('.picLink');
	pic.fade('show');
	container.removeEvent('mouseenter',picLinkShow);
	container.addEvent('mouseleave', picLinkHide);
}

var picLinkHide = function($e){
	var container = $e.target.getParent('.photo');
	var pic = container.getElement('.picLink');
	pic.fade('hide');
	container.removeEvent('mouseleave',picLinkHide);
	container.addEvent('mouseenter', picLinkShow);
}

// PHOTO LANDING PAGE ROLLOVERS

var photoCatOver = function($e){
	var container = $e.target.getParent('.cat_photo');	
	var title = container.getElement('.photocat');
	title.fade('show');
	container.removeEvent('mouseenter', photoCatOver);
	container.addEvent('mouseleave', photoCatOut);
}

var photoCatOut = function($e){
	var container = $e.target.getParent('.cat_photo');
	var title = container.getElement('.photocat');
	title.fade('hide');
	container.removeEvent('mouseleave', photoCatOut)
	container.addEvent('mouseenter', photoCatOver);
}


var sizePage = function(event){
	 $$('#page').each( function(el){
	 	var $height = el.getSize().y;
	 	var winHeight = window.innerHeight;
	 	if(winHeight >= $height){
	 		el.setStyle('height',((winHeight - 550)+'px'))
	 	}
	 })
}



// Reply field rollovers

var replyFieldOpen = function(event){
	var container = event.target.getParent('#respond');
	var targ = container.getChildren('h3')
	container.tween('height','325px');
	targ.addClass('replyOver');
	targ.removeEvent('mouseenter', replyFieldOpen);
	targ.addEvent('click', replyFieldClose);
}

var replyFieldClose = function(event){
	var container = event.target.getParent( );
	var targ = container.getChildren('h3')
	container.tween('height','15px');
	targ.removeClass('replyOver');
	targ.removeEvent('click', replyFieldClose)
	targ.addEvent('mouseenter', replyFieldOpen)
}




window.addEvent('domready', function() {

	$$('.port_gallery li a img').each( function(el) {
		var captionText = ( el.getProperty('title')!=null )? el.getProperty('title') : el.getProperty('alt');
		
		if ( captionText!=null ) {
		  
		  var figure = new Element('div', {
			  'class' : 'figure',
			  'styles' : {
			  'width' : el.get('width').toInt() + 10
			}
		  });
		 
		  var caption = new Element('p', {
			'class' : 'caption',
			'html' : captionText
		  });
     	 
		 figure.inject(el,'after');
         caption.inject(figure);
		 
    	} else {
      		alert('The image: "'+el.getProperty('src')+'" needs a title or alt value.');
    	}

		
	});
	

	$$('#portfolio .item').each( function ($el) {
		$el.addEvent('mouseenter', tweenOver.bindWithEvent(this, [$el]));
		$el.addEvent('mouseleave', tweenOut.bindWithEvent(this, [$el]));
		var $f = $el.getChildren('.titleHolder')
		$f.setStyle('bottom','-80px')
	});
	
	$$('.port_front .item').each( function($land_port){
		$land_port.addEvent('mouseenter', landPortOver.bindWithEvent(this, [$land_port]));
		$land_port.addEvent('mouseleave', landPortOut.bindWithEvent(this, [$land_port]));
		var $e = $land_port.getChildren('h3');
		$e.fade('hide');
	
	});
	
	$$('.front_featured').each( function($ffeat){
		$ffeat.set('tween', { duration:100 });
		$ffeat.addEvent('mouseenter', frontFeatOver.bindWithEvent(this, [$ffeat]));
		$ffeat.addEvent('mouseleave', frontFeatOut.bindWithEvent(this, [$ffeat]));
		// $ffeat.addEvent('mouseleave', frontFeatOut);
	});
	
	
	$$('.front_featured .item').each( function($e){	
		$totFrontItem++;
		$e.fade('hide');
		});

	$$('.front_featured h4').each( function($f){
		$f.tween('opacity', 0.6);
		$f.addEvent('mouseenter', ffNavOver);
		$f.addEvent('mouseleave', ffNavOut);
		});

	$$('.ff_larrow').addEvent('click', ff_leftNav);	
	$$('.ff_rarrow').addEvent('click', ff_rightNav);	


	$$('.photogallery .photo').each( function($e){
		var $pic = $e.getChildren('.picthmb');
		var size = $pic.getSize();
		var height = size.y
		imageGallery($e);
		
	});
	

	$$('.photogallery .photo').each( function($e){
		var $pic = $e.getChildren('.picthmb');
		var size = $pic.getSize();
		var height = size.y
		var $plink = $e.getChildren('.picLink');
		$plink.fade('hide');
		$e.addEvent('mouseenter', picLinkShow);
		imageGallery($e);
		
		
	});
	

	$$('.picthmb').each(function(element,index){
		var content = element.get('title').split('::');
		element.store('tip:title',content[0]);
		element.store('tip:text',content[1]);
	});	
	
	var toolTips = new Tips('.picthmb', {
    	className: 'custom_tip'
	});
	
	

	var socLinks = $$('.soci_links a')
		
	var sociTips = new Tips(socLinks,{
		className: 'custom_tip2',	
	});
	
	
	$$('#photography .cat_photo a').each(function($e){		
		var container = $e.getParent('.cat_photo');
		var title = container.getChildren('.photocat');
		title.fade('hide');
		$e.addEvent('mouseenter', photoCatOver);
	});

	
/*	$$('#respond h3').each(function($e){
		$e.addEvent('mouseenter', replyFieldOpen);
		var targ = $e.getParent('#respond');
		targ.setStyle('height','15px')
		
		$e.setProperty('height','15px');
	});
	
	$$('#comment').each(function($e){
		$e.addEvent('focus', replyFieldOpen); 
	});

*/	
	window.addEvent('resize', sizePage);

	sizePage();
	ffNavShow('0')
	startFeatureItem();
});
