var activeform;
var use_height = $j(window).height() - $j("#footer").height();
var use_width = $j(window).width();

$j(document).ajaxStart(function() { loader(true); });
$j(document).ajaxStop(function() { loader(false); });
$j(window).resize(function() { });
$j(window).load(function() { 

	$j(".control.info").mouseover(function(){
		$j(this).find("span").fadeIn("fast");
	}).mouseout(function(){
		$j(this).find("span").fadeOut("fast");
    });

	$j("a.close").click(function() { toggle_page_contents(); });
	
	toggle_page_contents();
	
	function toggle_page_contents() {
		
		obj = $j("#middle");
		
		if (obj.hasClass("closed")) {
			bottom = $j("#footer").height();
			newclass = "opened";
		} else {
			bottom = -(obj.height() - $j("#footer").height());
			newclass = "closed";
		}
		
		obj.removeClass().addClass(newclass);
		
		obj.animate({
			bottom: bottom
		}, 1500, 'easeInOutExpo', function() {
		});
	}
	
	
	$j('.story').cycle({
		slideExpr: 'li',
		timeout: 6000,
		//easing: 'easeInOutExpo',
		sync: 0,
   		fx: 'custom', 
		speed: 3000,
		cssBefore: {  
			left: -700,
			display: 'block'
		}, 
	    animIn: {
			left: 0
		}, 
		animOut: {  
			left: -700
		},
		before: onAfterStory
	});
	
	function onAfterStory(curr, next, opts) { 
		//tt = $j(next);
		//if (tt.attr("class") == undefined) {
			$j('.slideshow').cycle('next');
		//}
	}
	
	$j('.slideshow').cycle({
		speed: 1500,
		next:   '.slide-control .next', 
    	prev:   '.slide-control .prev',
		fx: 'scrollHorz',
		slideExpr: 'li',
		timeout: 0,
		easing: 'easeInOutExpo',
		pager:  '.gallery-scroller', 
		pagerAnchorBuilder: function(idx, slide) { 
			return '.gallery-scroller li:eq(' + idx + ')'; 
		},
		before: onBefore
	});
	
	
	function onBefore(curr, next, opts) { 
        
        if (!opts.addSlide) 
            return; 
        
		next_obj = $j(next);
		next_obj_id = next_obj.attr("id");
		
		exist_to_slideshow = ($j(".slideshow > li#"+next_obj_id+" img").size() > 0) ? true : false;
		
		//console.log(exist_to_slideshow);
		
		if (!exist_to_slideshow) {
			img_src = $j(".gallery-scroller li#"+next_obj_id+"").find("a").attr("href");
			new_img_file = "timthumb.php?src="+img_src+"&w="+use_width+"&h="+use_height+"&cz=1&q=100";
			
			var img = new Image(); 
			img.src = new_img_file; 
			
			//console.log("Loading image...");
			
			loader(true);
			
			$j(img).bind('load', function() {
				
				//console.log("image loaded");
				
				$j(".slideshow li#"+next_obj_id+"").append("<img src=\""+new_img_file+"\"/>");
				loader(false);
				
				//console.log(next_obj_id);

				//$j(".page").smoothDivScroll("moveToElement", "number", next_obj.index());
			});
		}
    }; 
	
	$j(".back_slideshow").fadeIn("fast");
	$j("body > .loading").fadeOut("slow");
	
});

$j(document).ready(function() {
	
	if ($j("body").hasClass("location")) {
		
		c = $j(".back_slideshow li:first");
		c.find("img").remove();
		
		map = $j(".bodytext > iframe")
		
		map.appendTo(c);
		
		map.mouseenter(function(){
			$j(".gfx_top").fadeOut("fast");
		}).mouseleave(function(){
			$j(".gfx_top").fadeIn("fast");
		});
	}
	
	/* gallery scroll */
	
	$j("ul.story li:even").addClass("even");
	
	if ($j("body").hasClass("page-gallery")) {
		$j(".page").smoothDivScroll({
			scrollableArea: ".scrollableArea"
			//autoScrollDirection: "endlessloopright",
			//autoScroll: "onstart",
			//autoScrollStep: 1
		});
	}
	
	$j(".gallery-scroller li").each(function() {
		
		to = $j(this);
		id = to.attr("id");
		
		exist_to_slideshow = ($j(".slideshow > li#"+id+"").size() > 0) ? true : false;
		
		if (!exist_to_slideshow) {
			html = "<li id=\""+id+"\"></li>";
			$j(".slideshow").append(html);
		}
	});
	
	
	$j(".slideshow li").each(function() {
		to = $j(this);
		img = to.find("img").attr("src");
		
		to.css({
			width: use_width + "px",
			height: use_height + "px"
		})
		
		if (img) {
			new_img = img.replace("h=490", "h="+use_height+"");
			new_img = new_img.replace("w=960", "w="+use_width+"");
			to.find("img").attr("src", new_img);
		}
	});
	
	$j(".nav > li:not(.last), .footernavright > li:not(.last)").after('<li class="seperator"></li>');
	$j(".footernav li").before('<li class="seperator"></li>');
	
	$j("h1").prependTo('#middle .pos0');
	
	$j(".control.zoom").click(function() {
		$j(this).closest(".container").find("a").trigger("click");
		return false;
	});
	
	$j('body:not(.page-gallery) .thumbs').cycle({
		fx: 'fade',
		slideExpr: 'li',
		timeout: 3000
	});

	$j("ul.nav").supersubs({ 
		minWidth:    8,
		maxWidth:    20,
		extraWidth:  1
	}).superfish({
		delay: 1000,                    
		animation:  {opacity:'show',height:'show'},  
		speed: 'fast',   
		autoArrows:  false,                      
		dropShadows: false 
	}); 
	
	$j(".date").datepicker({
		defaultDate: "+1",
		dateFormat: "dd/mm/yy",
		minDate: 0
	});

	var walloptions = { beforeSubmit: form_validate, success: form_success }; 
	
	function form_validate(formData, jqForm) { 
		activeform = jqForm[0].id;
		return $j("#"+jqForm[0].id+"").xvalidate();
	}

	function form_success(responseText) { 
		fo = $j("form#"+activeform+"");
		message("Form Response", responseText);
		
		fo.resetForm();
		fo.clearForm();
	}
	
	$j('.ajax').ajaxForm(walloptions);
	
	$j('#searchform').submit(function() {
		return $j("#searchform").xvalidate();
	});
	
	$j("ul.thumbs a").colorbox({maxHeight: '90%', maxWidth: '90%', opacity: '0.9'});
	
	global_initializations();
});

function global_initializations() {
	if ($j.browser.webkit) { $j("body").addClass("webkit"); }
	if ($j.browser.mozilla) { $j("body").addClass("mozilla"); }
	if ($j.browser.msie) { $j("body").addClass("explorer"); }
	parselinks();
}

function getUrlVars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).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 parselinks() {
	$j('[icon^="ui-icon"]:not(:has(span))').each(function() { 
		$j(this).html('<span class="ui-icon '+$j(this).attr("icon")+'"></span><span class="text">'+$j(this).html()+'</span>');
		$j(this).addClass("myicon");
	});
	
	$j("button.uibutton").button();
}

function loader(x) { if (x) { $j(".loading").fadeIn("fast"); } else { $j(".loading").fadeOut("fast"); }}

/* ease */

jQuery.easing["jswing"]=jQuery.easing["swing"];jQuery.extend(jQuery.easing,{def:"easeOutQuad",swing:function(a,b,c,d,e){return jQuery.easing[jQuery.easing.def](a,b,c,d,e)},easeInQuad:function(a,b,c,d,e){return d*(b/=e)*b+c},easeOutQuad:function(a,b,c,d,e){return-d*(b/=e)*(b-2)+c},easeInOutQuad:function(a,b,c,d,e){if((b/=e/2)<1)return d/2*b*b+c;return-d/2*(--b*(b-2)-1)+c},easeInCubic:function(a,b,c,d,e){return d*(b/=e)*b*b+c},easeOutCubic:function(a,b,c,d,e){return d*((b=b/e-1)*b*b+1)+c},easeInOutCubic:function(a,b,c,d,e){if((b/=e/2)<1)return d/2*b*b*b+c;return d/2*((b-=2)*b*b+2)+c},easeInQuart:function(a,b,c,d,e){return d*(b/=e)*b*b*b+c},easeOutQuart:function(a,b,c,d,e){return-d*((b=b/e-1)*b*b*b-1)+c},easeInOutQuart:function(a,b,c,d,e){if((b/=e/2)<1)return d/2*b*b*b*b+c;return-d/2*((b-=2)*b*b*b-2)+c},easeInQuint:function(a,b,c,d,e){return d*(b/=e)*b*b*b*b+c},easeOutQuint:function(a,b,c,d,e){return d*((b=b/e-1)*b*b*b*b+1)+c},easeInOutQuint:function(a,b,c,d,e){if((b/=e/2)<1)return d/2*b*b*b*b*b+c;return d/2*((b-=2)*b*b*b*b+2)+c},easeInSine:function(a,b,c,d,e){return-d*Math.cos(b/e*(Math.PI/2))+d+c},easeOutSine:function(a,b,c,d,e){return d*Math.sin(b/e*(Math.PI/2))+c},easeInOutSine:function(a,b,c,d,e){return-d/2*(Math.cos(Math.PI*b/e)-1)+c},easeInExpo:function(a,b,c,d,e){return b==0?c:d*Math.pow(2,10*(b/e-1))+c},easeOutExpo:function(a,b,c,d,e){return b==e?c+d:d*(-Math.pow(2,-10*b/e)+1)+c},easeInOutExpo:function(a,b,c,d,e){if(b==0)return c;if(b==e)return c+d;if((b/=e/2)<1)return d/2*Math.pow(2,10*(b-1))+c;return d/2*(-Math.pow(2,-10*--b)+2)+c},easeInCirc:function(a,b,c,d,e){return-d*(Math.sqrt(1-(b/=e)*b)-1)+c},easeOutCirc:function(a,b,c,d,e){return d*Math.sqrt(1-(b=b/e-1)*b)+c},easeInOutCirc:function(a,b,c,d,e){if((b/=e/2)<1)return-d/2*(Math.sqrt(1-b*b)-1)+c;return d/2*(Math.sqrt(1-(b-=2)*b)+1)+c},easeInElastic:function(a,b,c,d,e){var f=1.70158;var g=0;var h=d;if(b==0)return c;if((b/=e)==1)return c+d;if(!g)g=e*.3;if(h<Math.abs(d)){h=d;var f=g/4}else var f=g/(2*Math.PI)*Math.asin(d/h);return-(h*Math.pow(2,10*(b-=1))*Math.sin((b*e-f)*2*Math.PI/g))+c},easeOutElastic:function(a,b,c,d,e){var f=1.70158;var g=0;var h=d;if(b==0)return c;if((b/=e)==1)return c+d;if(!g)g=e*.3;if(h<Math.abs(d)){h=d;var f=g/4}else var f=g/(2*Math.PI)*Math.asin(d/h);return h*Math.pow(2,-10*b)*Math.sin((b*e-f)*2*Math.PI/g)+d+c},easeInOutElastic:function(a,b,c,d,e){var f=1.70158;var g=0;var h=d;if(b==0)return c;if((b/=e/2)==2)return c+d;if(!g)g=e*.3*1.5;if(h<Math.abs(d)){h=d;var f=g/4}else var f=g/(2*Math.PI)*Math.asin(d/h);if(b<1)return-.5*h*Math.pow(2,10*(b-=1))*Math.sin((b*e-f)*2*Math.PI/g)+c;return h*Math.pow(2,-10*(b-=1))*Math.sin((b*e-f)*2*Math.PI/g)*.5+d+c},easeInBack:function(a,b,c,d,e,f){if(f==undefined)f=1.70158;return d*(b/=e)*b*((f+1)*b-f)+c},easeOutBack:function(a,b,c,d,e,f){if(f==undefined)f=1.70158;return d*((b=b/e-1)*b*((f+1)*b+f)+1)+c},easeInOutBack:function(a,b,c,d,e,f){if(f==undefined)f=1.70158;if((b/=e/2)<1)return d/2*b*b*(((f*=1.525)+1)*b-f)+c;return d/2*((b-=2)*b*(((f*=1.525)+1)*b+f)+2)+c},easeInBounce:function(a,b,c,d,e){return d-jQuery.easing.easeOutBounce(a,e-b,0,d,e)+c},easeOutBounce:function(a,b,c,d,e){if((b/=e)<1/2.75){return d*7.5625*b*b+c}else if(b<2/2.75){return d*(7.5625*(b-=1.5/2.75)*b+.75)+c}else if(b<2.5/2.75){return d*(7.5625*(b-=2.25/2.75)*b+.9375)+c}else{return d*(7.5625*(b-=2.625/2.75)*b+.984375)+c}},easeInOutBounce:function(a,b,c,d,e){if(b<e/2)return jQuery.easing.easeInBounce(a,b*2,0,d,e)*.5+c;return jQuery.easing.easeOutBounce(a,b*2-e,0,d,e)*.5+d*.5+c}})


