// Effects Library written by Dave Hone
// version 1.1a 23/11/10
// Dependency: jQuery 1.4 for .delay()
// Extensive use of jQuery markup and callback functions see http://jquery.com for docs

var root = "/"; // portability variable for the document "root" (staging / live)

function SetupVars () { // called on doc load
	SetUpRollovers();
	SetupAjax(true); // comment out for kiosk mode
	SetupAnimations();
	//jQuery(".button, .sumbit").css({cursor: "hand"}); // optional mouse icon, nice to have but doesn't validate
}
function getUrlVars (loc) { // returns hash object from ampersand separated url search args, or null (loc = full url)
	if (!loc) return;
   	var vars = [], hash;
	var anc = loc.indexOf('#');
	var que = loc.indexOf('?')+1;
	if (que < 0) return;
    var hashes = (anc < 0)? loc.slice(que) : loc.slice( que, anc);
	hashes = hashes.split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

(function($) { // background animate position plugin for jquery
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
	
	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};
	
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step.backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			
			fx.start = [start[0],start[2]];
			
			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);

function SetUpRollovers () { // define event handlers added at doc load
	// **** non-destructive JS.  This gets added on top of basic functionality only if it's supported ****
	
	// for the menu
	jQuery('div#menu li').mouseenter(function(e) {
		jQuery(this).animate({ marginLeft:'-=5px', marginRight:'+=10px'}, 100);
    }).mouseleave(function(e) {
		jQuery(this).animate({ marginLeft:'+=5px', marginRight:'-=10px'}, 100);
	});
	
	// for the thumbs
	jQuery('#thumbs li a').css({backgroundPosition:'left -330px'}); // must be set before 1st animation to avoid flicker
	jQuery('#thumbs li a').mouseenter(function(e) {
		jQuery(this).stop().animate({backgroundPosition:'left -440px'}, 200);
		var sp = jQuery(this).children("span");
		sp.css({display: "none"});
	}).mouseleave(function(e) {
		jQuery(this).stop().animate({backgroundPosition:'left -330px'}, 200, function() {
			var sp = jQuery(this).children("span");
			sp.css({display: "inline"});
		});
	});
	
	// for the lists
	jQuery(".topTab").attr("class","jqTab").click(function(e){
		jQuery(".jqTab").not(jQuery(this)).removeClass("activeTab").children("ul").stop().hide();
		jQuery(this).not(".activeTab").addClass("activeTab").children("ul").slideToggle("fast");
	});
	jQuery(".jqTab:first-child").addClass("activeTab").children("ul").slideToggle("fast");
}
function SetupAjax (init) { // AJAX is added at doc load, init=true called once at doc load then init=false on AJAX load
	// **** non-destructive JS.  This gets added on top of basic functionality only if it's supported ****
	var anchor = jQuery("a");
	anchor.attr('href', function(){
		var link = jQuery(this).attr("href");
		if (link) {
			if (link.indexOf("popup.php") < 0) return link;
			var vars = getUrlVars(link);
			return "javascript:GalleryLoad("+vars["g"]+","+vars["p"]+")";
		}
	});
	
	// Ajax for the popup
	jQuery(".popupClose").attr("href", "javascript:HideGallery();").text("Close");
	
	if (init) {
		jQuery("form").submit(function() {
			var name = jQuery("input#name").val();
			var email = jQuery("input#email").val();
			var body = jQuery("textarea#body").val();
			var dataString = 'name='+ name + '&email=' + email + '&body=' + body +'&jx=1';
			if (name != "" &&  email != "" && body != "") {
				jQuery.ajax({
					type: "POST",
					url: root+"pages/contact.php", //?g="+gallery+"&p="+page,
					data: dataString,
					success: FormSendComplete
				});
				return false;
			} else {
				FormSendComplete(2);
			}
			return false;
		});
		// ajax for the contact form
		jQuery("a.contact").css({"display":"block"});
		jQuery("#example a.contact").click(function(){
			ToggleContact(true);
			return false;
		});
		jQuery("#contact a.contact").click(function(){
			ToggleContact(false);
			return false;
		});
	}
}
function SetupAnimations () { // DHTML animations and interaction added at doc load
	jQuery("li.thumbImg div").hide();
	
	var titleSlides = jQuery("#intro h2").children();
	var titleCount = titleSlides.length;
	titleSlides.css({visibility:"hidden","margin-left":"30px"});
	titleSlides.each(
		function (i) {
			var item = jQuery(this);
			setTimeout (function() {
				item.css({visibility:"visible",opacity: 0});
				item.animate({"margin-left": 0, "opacity":1}, 400,
					function () {
						if (i == titleCount-2) { // last title animation complete
							ToggleContact(false);
							titleSlides.css({visibility:"visible"});
							jQuery("li.thumbImg div").each(
								function (i) {
									var item = jQuery(this);	
									setTimeout (function() {
										item.fadeIn("slow");
									}, 100*i);
								}
							);
						}
					}
				);
			}, 200*i);
		}
	);
}
function ToggleContact (state) { // shows hides contact pane, show=true
	if (state) {
		//jQuery("#contact div").css({"display":"none"});
		jQuery("#contact").stop(true,true).show().animate({marginLeft:"560px"},300, function(){jQuery("#contact div").css({"display":"block"})});
		jQuery("#example a.contact").css({"display":"none"});
	} else {
		jQuery("#contact").stop(true,true).animate({marginLeft:"200px"}, 600, function(){jQuery(this).hide()}).children("div").css({"display":"none"});
		jQuery("#example a.contact").css({"display":"block"});
	}
}
function FormSendComplete (e) { // handler for form receipt, e = error number (0-2)
	// **** non-destructive JS.  This gets added on top of basic functionality only if it's supported ****
	if (e == 2) {
		jQuery("#mailError").empty().load(root+"pages/error-2.php", FormLoadComplete); // error message, js form validation failed form not sent
	} else if (e == 1) {
		jQuery("#mailError").empty().load(root+"pages/error-1.php", FormLoadComplete); // error message, receipt failed server not responding
	} else {
		jQuery("#mailError").empty().load(root+"pages/error-0.php", FormLoadComplete); // ok message, sent with no error returned
		
		jQuery("input#name").val("");
		jQuery("input#email").val("");
		jQuery("textarea#body").val("");
	}
}
function FormLoadComplete () { // handler for form response show message
	jQuery("#mailError").stop(true,true).hide().slideToggle("fast");
}
function GalleryLoad (g, p) { // ajax loader for gallery loads on thumbnail click, g= gallery number(0-n), p= page number(0-n)
	// **** non-destructive JS.  This gets added on top of basic functionality only if it's supported ****
	var target = jQuery("#contentPopup");
	var loading = jQuery("#loading");
	var gallery = jQuery("#backgroundPopup");
	var page = root+"pages/page-"+g+"-"+p+".php";
	var themeIndex = jQuery("body").attr("class").indexOf("theme");
	var theme = themeIndex> -1? jQuery("body").attr("class").substr(themeIndex+5,1) : "";
	var bodyclass = "home gallery" +g+ " page" +p+ " theme" +theme;
	
	loading.fadeIn('fast');
	target.fadeOut(200, function(){
		jQuery("body").attr("class", "").addClass("gallery"+g+" page"+p);
		jQuery(this).empty().load(page, GalleryLoadComplete);
	});
}
function GalleryLoadComplete () { // handler for ajax load complete (clears and populates div with new content)
	// **** non-destructive JS.  This gets added on top of basic functionality only if it's supported ****
	SetupAjax(false);
	ShowGallery();
}
function ShowGallery () { // fades in gallery popup (overlay)
	// **** non-destructive JS.  This gets added on top of basic functionality only if it's supported ****
	var target = jQuery("#contentPopup");
	var loading = jQuery("#loading");
	var gallery = jQuery("#backgroundPopup");
	
	target.delay(500).fadeIn('slow');
}
function HideGallery () { // fades out gallery popup (overlay)
	// **** non-destructive JS.  This gets added on top of basic functionality only if it's supported ****
	var target = jQuery("#contentPopup");
	var loading = jQuery("#loading");
	var gallery = jQuery("#backgroundPopup");
	target.fadeOut('slow');
	loading.hide();
	gallery.fadeOut('slow');
}

