(function ($) {
	// VERTICALLY ALIGN FUNCTION
	$.fn.vAlign = function(off) {
	  return this.each(function(i){
		var ah = $(this).height();
		var ph = $(this).parent().height();
		if(off) {
			var offset = ah / 5;
		} else {
			offset = 0;
		}
		var mh = ((ph - ah) / 2) + offset;
		$(this).css('margin-top', mh);
	  });
	};
})(jQuery);

$(document).ready(function(){
	/* EXTERNAL LINKS */
	$('a[rel="external"], a[class="external"]').attr('target','_blank');
	$("a[href$='.jpg']").attr('target','_blank');
	
	/* VERTICAL ALIGN BOXES */
	//$('#nav-home li a').vAlign(true);
	$('#nav-more li a').vAlign(true);
	//$('#nav-home-stereo-3d li a').css('margin-top','67.5px');

	/*CREDITS EXTERNAL LINKS*/
	$('.credit-links a').hover(
		function(){ $(this).addClass('hover'); },
		function(){ $(this).removeClass('hover'); }
	);

	/*GALLERIES IN BUZZ POSTS*/
	$('.gallery').cycle({
		fx: 'fade',
		timeout:  5000,
    });

	var path = document.location.pathname.split('/');
	var loc = path[1];
	//console.log(path[1]+' '+path[2]+' '+path[3]);
	if(loc == '') {
		$('#photo-wrapper').cycle({
			fx: 'fade',
			timeout:  5000,
	    });
		$('.photo-project').hover(
			function() { $(this).css('cursor','pointer'); $('.project-credit', this).animate({marginTop: "270px"},500); },
			function() { $(this).css('cursor','default'); $('.project-credit', this).animate({marginTop: "193px"},500); }
		);
	} else if(loc == 'camera' || loc == 'lab' || loc == 'stereo-3d' || loc == 'more' || loc == 'screen' || loc == 'about') {
		if(path[2] != 'facility' && path[3] != 'facility') {
			$('#photo-wrapper').cycle({
				fx: 'fade',
				timeout:  4000,
			});
		} else {
			$('#photo-wrapper').cycle({
				fx:     'fade', 
				speed:  'slow', 
				timeout: 0, 
				pager:  '#inline-gallery', 
				 
				// callback fn that creates a thumbnail to use as pager anchor 
				pagerAnchorBuilder: function(idx, slide) {
					var theimg = slide.innerHTML.split('<img src="');
					var thesrc = theimg[1].split('" width');
					return '<li><a href="#"><img src="' + thesrc[0] + '" width="60" height="25" /></a></li>'; 
				} 
			});
		}
	    if(loc == 'screen') {
			/*$('form#screenform input, form#screenform textarea').focus(function(){
				$(this).css('color','#939598') .val("");
			});*/
			$('input#date').datepicker({
				inline: true
			});
	    	$('form#screenform input#go').hover(
	    		function(){ $(this).css('background-color','#939598').css('cursor','pointer'); },
	    		function(){ $(this).css('background-color','#E51B24').css('cursor','pointer'); }
			);
	    	$('form#screenform').submit(function(){
				/*var email = $("input#email").val();
				var name = $("input#name").val();
				var company = $("input#company").val();
				if((email === "") || (email == "Required")) {
					$("input#email").css('color','#E51B24') .val("Required");
					return false;
				} else if((name === "") || (name == "Required")) {
					$("input#name").css('color','#E51B24') .val("Required");
					return false;
				} else if((company === "") (company == "Required")) {
					$("input#company").css('color','#E51B24') .val("Required");
					return false;
				} else {*/
					var dataString = encodeURI($(this).serialize());
					var url = $('form#screenform').attr('action');
					//window.alert(url+'?'+dataString);
					$('p#screenstatus').load(url+'?'+dataString);
					return false;
				//}
	    	});    	
	    }
	    if(path[2] == 'client-center') {
	    	$('.file-wrapper').hover(
	    		function(){ $(this).css('background-color','#939598').css('cursor','pointer'); },
	    		function(){ $(this).css('background-color','#F0F0F0').css('cursor','pointer'); }
	    	);
	    	$('.file-wrapper').click(function(){
	    		var url = $('h2 a', this).attr('href');
	    		window.open(url);
	    	});
	    }
	} else if(loc == 'color') {
		var arr = [78,80];
		for(var i in arr) {
			$('#slideshow-'+arr[i]).cycle({
				fx: 'fade',
				timeout:  4000,
				prev: '#prev-'+arr[i],
				next: '#next-'+arr[i]
			});
		}
	    $('.prev, .next').hover(
    		function(){ $(this).parent().addClass('hover'); },
    		function(){ $(this).parent().removeClass('hover'); }
    	);
	}
});
