var intHeaderImgFadeSecs = 2;
var intHeaderImgLingerSecs = 6;

var intFBPadding = 10;
var overlayOpacity = 0.9;
var intElasticMS = 200;
var intFBMargin = 70;

(function ($) {
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function () {
		var i;
		var args_len = arguments.length;
		for (i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	};
})(jQuery);

jQuery(document).ready(function() {
	var intHeaderImgFadeMs = intHeaderImgFadeSecs * 1000;
	var intHeaderImgLingerMs = intHeaderImgLingerSecs * 1000;
	
	if ($("#home").length > 0) {
		$(".vimeo").click(function() {
			var strVimeoID = $(this).attr("href").split("vimeo.com/")[1]
			$.fancybox({
				'padding'		: intFBPadding,
				'autoScale'		: false,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'title'			: this.title,
				'width'			: 940,
				'height'		: 529,
				'content'		: "<iframe src=\"http://player.vimeo.com/video/" + strVimeoID + "?autoplay=1\" width=\"940\" height=\"529\" frameborder=\"0\"></iframe>",
				'overlayOpacity': overlayOpacity,
				'overlayColor'	: '#000',
				'speedIn'		: intElasticMS,
				'speedOut'		: intElasticMS,
				'margin'		: intFBMargin,
				'scrolling'		: 'no'
			});
			_gaq.push(["_trackEvent", "Video", "Play", $("h4, h5", $(this).parent()).text()]);
			return false;
		});
		$("#palehorse a").click(function() {
			$.fancybox([
				'video/palehorsepalerider/01.jpg',
				'video/palehorsepalerider/02.jpg',
				'video/palehorsepalerider/03.jpg',
				'video/palehorsepalerider/04.jpg',
				'video/palehorsepalerider/05.jpg',
				'video/palehorsepalerider/06.jpg',
				'video/palehorsepalerider/07.jpg',
				'video/palehorsepalerider/08.jpg',
				'video/palehorsepalerider/09.jpg',
				'video/palehorsepalerider/10.jpg',
				'video/palehorsepalerider/11.jpg',
				'video/palehorsepalerider/12.jpg',
				'video/palehorsepalerider/13.jpg',
				'video/palehorsepalerider/14.jpg',
				'video/palehorsepalerider/15.jpg',
				'video/palehorsepalerider/16.jpg'
			], {
				'padding'			: intFBPadding,
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'type'              : 'image',
				'changeFade'        : 0,
				'overlayOpacity'	: overlayOpacity,
				'overlayColor'		: '#000',
				'speedIn'		: intElasticMS,
				'speedOut'		: intElasticMS,
				'margin'		: intFBMargin
			});
			_gaq.push(["_trackEvent", "Image", "View", "Palehorse"]);
			return false;
		});
		$(".singleimg").fancybox({
			'padding'		: 	intFBPadding, 
			'overlayOpacity':	overlayOpacity, 
			'overlayColor'	:	'#000',
			'transitionIn'	: 	'elastic',
			'transitionOut'	: 	'elastic',
			'speedIn'		: 	intElasticMS,
			'speedOut'		: 	intElasticMS,
			'margin'		: 	intFBMargin
		});
		
		$("#thewell a").click(function() {
			_gaq.push(["_trackEvent", "Image", "View", "The Well"]);
			return true;
		});
				
		$(".no-touch #willswapforfood a").fancybox({
			'scrolling': false,
			'titleShow': false,
			'padding' : intFBPadding,
			'overlayColor'	: '#000',
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'autoScale'		: false,
			'speedIn'		: intElasticMS,
			'speedOut'		: intElasticMS,
			'margin'		: intFBMargin,
			'autoDimensions': false,
			'width': 720,
			'height': 480,
			'content': '<div id="player"></div>',
			'onComplete' : function() {
			 	var strVideoHTML = '<object id="flash_fallback_1" class="vjs-flash-fallback" width="720" height="480" type="application/x-shockwave-flash" data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf">';
				strVideoHTML += '<param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />';
				strVideoHTML += '<param name="allowfullscreen" value="true" />';
				strVideoHTML += '<param name="flashvars" value=\'config={"playlist":["http://theworkofadam.com/video/willswapforfood/willswapforfoodstill.jpg", {"url": "http://theworkofadam.com/video/willswapforfood/willswapforfood.mp4","autoPlay":true,"autoBuffering":true}]}\' />';
				strVideoHTML += '<img src="http://theworkofadam.com/video/willswapforfood/willswapforfoodstill.jpg" width="720" height="480" alt="Poster Image" title="No video playback capabilities." />';
				strVideoHTML += '</object>';
				$("#player").html(strVideoHTML);
				_gaq.push(["_trackEvent", "Video", "Play", "Will Swap for Food"]);
			 	
			},
			'onClosed': function () { $("#player").remove(); }
		});
		
		$(".no-touch #returntoparadise a").fancybox({
			'scrolling': false,
			'titleShow': false,
			'padding' : intFBPadding,
			'overlayColor'	: '#000',
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'autoScale'		: false,
			'speedIn'		: intElasticMS,
			'speedOut'		: intElasticMS,
			'margin'		: intFBMargin,
			'autoDimensions': false,
			'width'			: 714,
			'height'		: 330,
			'content': '<div id="player"></div>',
			'onComplete' : function() {
			 	var strVideoHTML = '<object id="flash_fallback_1" class="vjs-flash-fallback" width="714" height="330" type="application/x-shockwave-flash" data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf">';
				strVideoHTML += '<param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />';
				strVideoHTML += '<param name="allowfullscreen" value="true" />';
				strVideoHTML += '<param name="flashvars" value=\'config={"playlist":["http://theworkofadam.com/video/returntoparadise/returntoparadisestill.jpg", {"url": "http://theworkofadam.com/video/returntoparadise/returntoparadise.mp4","autoPlay":true,"autoBuffering":true}]}\' />';
				strVideoHTML += '<img src="http://theworkofadam.com/video/returntoparadise/returntoparadisestill.jpg" width="714" height="330" alt="Poster Image" title="No video playback capabilities." />';
				strVideoHTML += '</object>';
				$("#player").html(strVideoHTML);
			 	_gaq.push(["_trackEvent", "Video", "Play", "Return to Paradise"]);
			},
			'onClosed': function () { $("#player").remove(); }
		});
		
		$(".no-touch #antonialofaso a").fancybox({
			'scrolling': false,
			'titleShow': false,
			'padding' : intFBPadding,
			'overlayColor'	: '#000',
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'autoScale'		: false,
			'speedIn'		: intElasticMS,
			'speedOut'		: intElasticMS,
			'margin'		: intFBMargin,
			'autoDimensions': false,
			'width': 720,
			'height': 480,
			'content': '<div id="player"></div>',
			'onComplete' : function() {
			 	var strVideoHTML = '<object id="flash_fallback_1" class="vjs-flash-fallback" width="720" height="480" type="application/x-shockwave-flash" data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf">';
				strVideoHTML += '<param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />';
				strVideoHTML += '<param name="allowfullscreen" value="true" />';
				strVideoHTML += '<param name="flashvars" value=\'config={"playlist":["http://theworkofadam.com/video/antonialofaso/antonialofasostill.jpg", {"url": "http://theworkofadam.com/video/antonialofaso/antonialofaso.mp4","autoPlay":true,"autoBuffering":true}]}\' />';
				strVideoHTML += '<img src="http://theworkofadam.com/video/antonialofaso/antonialofasostill.jpg" width="720" height="480" alt="Poster Image" title="No video playback capabilities." />';
				strVideoHTML += '</object>';
				$("#player").html(strVideoHTML);
			 	_gaq.push(["_trackEvent", "Video", "Play", "Antonia Lofaso"]);
			},
			'onClosed': function () { $("#player").remove(); }
		});
		
		
		//homepage slideshow
		var intHdrImgLength = $("#home header .container .homehdrimg").length;
		var intHdrImg = 2;
		var bolFirstRun = true;
		
		function newHomeBanner() {
			$("#home header .container .homehdrimg:nth-child(" + intHdrImg + ")").stop(true, false).fadeOut(intHeaderImgFadeMs, "easeInOutSine");
			intHdrImg++;
			if (intHdrImg > intHdrImgLength + 1) {
				intHdrImg = 2;
			}
			$("#home header .container .homehdrimg:nth-child(" + intHdrImg + ")").stop(true, false).fadeTo(intHeaderImgFadeMs, 1, "easeInOutSine");
			if (bolFirstRun) {
				bolFirstRun = false;
				var iHdrImgInterval = setInterval(newHomeBanner, intHeaderImgLingerMs);
			}
		}
		
		var iHomeTimeOut = setTimeout(newHomeBanner, (intHeaderImgLingerMs - intHeaderImgFadeMs));
		
		jQuery.preLoadImages("/img/work/featured-work-highlight.jpg", "/img/work/palehorse-highlight.jpg", "/img/work/willswapforfood-highlight.jpg", "/img/work/returntoparadise-highlight.jpg", "/img/work/antonialofaso-highlight.jpg", "/img/work/intersection-highlight.jpg", "/img/work/throughtheviewfinder-highlight.jpg", "/img/work/thewell-highlight.jpg");
	}
	
	if ($("#photo").length > 0) {
		$("a[rel='photos']").fancybox({
			'padding'			: intFBPadding,
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'type'              : 'image',
			'changeFade'        : 0,
			'overlayOpacity'	: overlayOpacity,
			'overlayColor'		: '#000',
			'speedIn'		: intElasticMS,
			'speedOut'		: intElasticMS,
			'margin'		: intFBMargin
		});
	}
	
	if ($("#contact").length > 0) {
		var bolCanSubmit = true;
			
		$("#contact form").submit(function () {
			if (bolCanSubmit) {
				bolCanSubmit = false;
				
				//validation
				var bolErrors = false;
				var strVia = $.trim($("#via", this).val());
				var strMsg = $.trim($("#msg", this).val());
				var strNr = $.trim($("#nr", this).val());
				
				$(".emaillbl, .msglbl").removeClass("error");
				
				if ($.isValidEmailAddress(strVia) === false) {
					bolErrors = true;
					$(".emaillbl").addClass("error");
				}
				if (strMsg === "") {
					bolErrors = true;
					$(".msglbl").addClass("error");
				}
				
				if (bolErrors === true) {
					bolCanSubmit = true;
					return false;
				} else {
					//waiting indication
					$("#contact button").addClass("sending");
					$("#contact input, #contact textarea").attr("disabled", "disabled");
					
					$.post($(this).attr("action") + "?ajax=true", {fn: $("#fn", this).val(), ln: $("#ln", this).val(), via: strVia, msg: strMsg, nr: strNr, sent: "sent"}, function (data) {
						if (data === "SUCCESSFUL") {
							$("#contact button").removeClass("sending");
							$("#contact form").attr("action", "#").empty().html("<div style=\"display:none;\" id=\"thanks\">Thanks for contacting me. We'll be in touch.</div>");
							$("#contact #thanks").fadeIn("def");
							_gaq.push(["_trackEvent", "Contact", "Submit"]);
						} else {
							bolCanSubmit = true;
							$("#contact button").removeClass("sending");
							$("#contact input, #contact textarea").removeAttr("disabled");
							
							var arrResponse = data.replace("email", ".emaillbl").replace("message", ".msglbl").replace("ERRORS: ", "").split(" ");
							for (var i = 0; i < arrResponse.length; i++) {
								//error handling
								$(arrResponse[i]).addClass("error");
							}
						}
					});
					
				}
			}
			return false;
		});
	}
});
$.isValidEmailAddress = function (emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
};
