/**
 * jQuery.Preload - Multifunctional preloader
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com
 * Dual licensed under MIT and GPL.
 * Date: 3/25/2009
 * @author Ariel Flesler
 * @version 1.0.8
 */
;(function($){var h=$.preload=function(c,d){if(c.split)c=$(c);d=$.extend({},h.defaults,d);var f=$.map(c,function(a){if(!a)return;if(a.split)return d.base+a+d.ext;var b=a.src||a.href;if(typeof d.placeholder=='string'&&a.src)a.src=d.placeholder;if(b&&d.find)b=b.replace(d.find,d.replace);return b||null}),data={loaded:0,failed:0,next:0,done:0,total:f.length};if(!data.total)return finish();var g=$(Array(d.threshold+1).join('<img/>')).load(handler).error(handler).bind('abort',handler).each(fetch);function handler(e){data.element=this;data.found=e.type=='load';data.image=this.src;data.index=this.index;var a=data.original=c[this.index];data[data.found?'loaded':'failed']++;data.done++;if(d.enforceCache)h.cache.push($('<img/>').attr('src',data.image)[0]);if(d.placeholder&&a.src)a.src=data.found?data.image:d.notFound||a.src;if(d.onComplete)d.onComplete(data);if(data.done<data.total)fetch(0,this);else{if(g&&g.unbind)g.unbind('load').unbind('error').unbind('abort');g=null;finish()}};function fetch(i,a,b){if(a.attachEvent&&data.next&&data.next%h.gap==0&&!b){setTimeout(function(){fetch(i,a,1)},0);return!1}if(data.next==data.total)return!1;a.index=data.next;a.src=f[data.next++];if(d.onRequest){data.index=a.index;data.element=a;data.image=a.src;data.original=c[data.next-1];d.onRequest(data)}};function finish(){if(d.onFinish)d.onFinish(data)}};h.gap=14;h.cache=[];h.defaults={threshold:2,base:'',ext:'',replace:''};$.fn.preload=function(a){h(this,a);return this}})(jQuery);




/*
 * jQuery Color Animations
 * Copyright 2007 John Resig
 * Released under the MIT and GPL licenses.
 */

(function(jQuery){

	// We override the animation for all of these color styles
	jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
		jQuery.fx.step[attr] = function(fx){
			if ( fx.state == 0 ) {
				fx.start = getColor( fx.elem, attr );
				fx.end = getRGB( fx.end );
			}

			fx.elem.style[attr] = "rgb(" + [
				Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
			].join(",") + ")";
		}
	});

	// Color Conversion functions from highlightFade
	// By Blair Mitchelmore
	// http://jquery.offput.ca/highlightFade/

	// Parse strings looking for color tuples [255,255,255]
	function getRGB(color) {
		var result;

		// Check if we're already dealing with an array of colors
		if ( color && color.constructor == Array && color.length == 3 )
			return color;

		// Look for rgb(num,num,num)
		if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
			return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];

		// Look for rgb(num%,num%,num%)
		if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
			return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

		// Look for #a0b1c2
		if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
			return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

		// Look for #fff
		if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
			return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

		// Otherwise, we're most likely dealing with a named color
		return colors[jQuery.trim(color).toLowerCase()];
	}
	
	function getColor(elem, attr) {
		var color;

		do {
			color = jQuery.curCSS(elem, attr);

			// Keep going until we find an element that has color, or we hit the body
			if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") )
				break; 

			attr = "backgroundColor";
		} while ( elem = elem.parentNode );

		return getRGB(color);
	};
	
	// Some named colors to work with
	// From Interface by Stefan Petre
	// http://interface.eyecon.ro/

	var colors = {
		aqua:[0,255,255]
	};
	
})(jQuery);

// end color anim

/////////////////////////////////////////////////////////////////


jQuery(document).ready(function(){

current_image = 0;
image_holders = new Array(jQuery('#bg-1'),jQuery('#bg-2'));
current_holder = 0;
origW = 1680;
origH = 1050;
iR = origW / origH;

var animControl;
var isAnimating=false;

if(typeof background_images != 'undefined'){
	if(background_images.length>1){
		jQuery.preload(background_images, {
			enforceCache:true,
			onFinish:function(){
				galleryReady();
				animControl = setInterval(switch_bg, 6000);
				jQuery('#gallery-links p').hide();
				jQuery('#gallery-links ul').show();
			}
		});
	}
	if(background_images[0]!=""){
		image_holders[0].html('<img src="'+background_images[0]+'" />');
	}
	hiliteNav();
}

function nextHolder(){
	(current_holder == 1) ? current_holder=0 : current_holder=1;	
}

function nextImage(){
	((current_image+1) == background_images.length) ? current_image=0 : current_image++;	
}

function switch_bg(){
	image_holders[current_holder].css('z-index','-8');
	nextHolder();
	image_holders[current_holder].css({'z-index':'-9'}).show();
	nextImage();
	image_holders[current_holder].html('<img src="'+background_images[current_image]+'" />');
	resize_bgs();
	nextHolder();
	isAnimating=true;
	image_holders[current_holder].fadeOut('slow', function() {
    	isAnimating=false;
  	});
	hiliteNav();
	nextHolder();
}

function hiliteNav(){
	jQuery('#gallery-links li a').removeClass('active');
	jQuery('#gallery-link-'+(current_image+1)).addClass('active');
}


function galleryReady(){

if(typeof show_gallery_controls != 'undefined'){
	// we're on a single project page
	
	if(background_images[0]!=""){
		
		jQuery('#slide-nav-left').css('cursor','pointer');
		jQuery('#slide-nav-right').css('cursor','pointer');
		
		jQuery('#slide-nav-left').hover(
					function(){
						jQuery('#slide-nav-left-indicator').stop().animate({
							left: '20px'
						}, 300, 'swing');
					},
					function(){
						jQuery('#slide-nav-left-indicator').stop().animate({
							left: '-120px'
						}, 300, 'swing');
		});
		
		jQuery('#slide-nav-right').hover(
					function(){
						jQuery('#slide-nav-right-indicator').stop().animate({
							right: '20px'
						}, 300, 'swing');
					},
					function(){
						jQuery('#slide-nav-right-indicator').stop().animate({
							right: '-120px'
						}, 300, 'swing');
		});
		
		jQuery('#slide-nav-left').click(function(){
			if(isAnimating==true){
				return false;
			} else {
				clearInterval(animControl);
				jQuery('#anim-is-on').hide();
				jQuery('#anim-is-off').show();
				((current_image-2) < -1) ? current_image=(background_images.length-2) : current_image=current_image-2;
				switch_bg();
				return false;
			}
		});
		
		jQuery('#slide-nav-right').click(function(){
			if(isAnimating==true){
				return false;
			} else {
				clearInterval(animControl);
				jQuery('#anim-is-on').hide();
				jQuery('#anim-is-off').show();
				switch_bg();
				return false;
			}
		});
		
		
		jQuery('#anim-is-off').click(function(){
			jQuery(this).hide();
			jQuery('#anim-is-on').show();
			switch_bg();
			animControl = setInterval(switch_bg, 6000);
			return false;
		});
		
		jQuery('#anim-is-on').click(function(){
			jQuery(this).hide();
			jQuery('#anim-is-off').show();
			clearInterval(animControl);
			return false;
		});
		
		jQuery('#gallery-links li a').not('#gallery-links li#anim-is-on a, #gallery-links li#anim-is-off a').click(function(){
			if(isAnimating==true){
				return false;
			} else {
				clearInterval(animControl);
				jQuery('#anim-is-on').hide();
				jQuery('#anim-is-off').show();
				
				var thisID = jQuery(this).attr('id');
				thisID = thisID.substr(13);
				current_image = thisID-2;
				switch_bg();
				return false;
			}
		});
	
	}
	
}
// end show_gallery_controls

}


// project page details link

jQuery('#details-hide a').click(
	function(){
		jQuery('#right-col-content').slideUp('fast');
		jQuery('#details-hide').hide();
		jQuery('#details-show').show();
		jQuery('#slide-nav-right').show();
		return false;
	}
);

jQuery('#details-show a').click(
	function(){
		jQuery('#right-col-content').slideDown();
		jQuery('#details-show').hide();
		jQuery('#details-hide').show();
		jQuery('#slide-nav-right').hide();
		return false;
	}
);


// background resizing stuff

jQuery(window).bind('resize',function() {
	resize_bgs();
});

jQuery(window).bind('load',function() {
	resize_bgs();
});

function resize_bgs(){
	wW = jQuery(window).width();
	wH = jQuery(window).height();
	mH = jQuery("#main").height();
	mW = jQuery("#main").width();
	
	if(wH<mH){
		wH = mH;
	}
	
	if(wW<mW){
		wW = mW;
	}
	
	wR = wW / wH;
	
	if (iR > wR) {
		bHeight = wH;
		bWidth = bHeight * iR;
		bTop = 0;
		bLeft = (wW - bWidth)/2;
	} else {
		bWidth = wW;
		bHeight = bWidth / iR;
		bTop = (wH - bHeight)/2;
		bLeft = 0;
	}
	
	jQuery("#bg")
		.css('height',wH)
		.css('width',wW);
	
	jQuery("#bg-1,#bg-2")
		.css('height',bHeight)
		.css('width',bWidth);
	
	jQuery("#bg-1 img,,#bg-2 img")
		.css('height','100%')
		.css('width','100%');
		
	// project page stuff
	
	elemHight = wH-143;
	jQuery('#slide-nav-left').css({height : elemHight});
	jQuery('#slide-nav-right').css({height : elemHight});
	
	//jQuery('#slide-nav-right-indicator').css({height : elemHight});
	
	
	
}	

resize_bgs();




// main and secondary nav
/*
var navTimeOut;

jQuery('#nav-control').mouseenter(function(){
    clearTimeout(navTimeOut);
	jQuery('#main-nav').slideDown(400,function(){ resize_bgs(); });
	jQuery('#secondary-nav').slideUp(300);
	jQuery('#slide-nav-left').hide();
	jQuery('#nav-control h4').stop().animate({ color: "#fff" }, 200);
	jQuery('#roll-image').fadeTo(200,1);
	jQuery('#arrow-image').fadeTo(200,0);
}).mouseleave(function(){
    navTimeOut = setTimeout(
		function(){
			jQuery('#main-nav').slideUp(300);
			jQuery('#secondary-nav').slideDown(400,function(){ resize_bgs(); });
			jQuery('#slide-nav-left').show();
			jQuery('#search-holder').slideUp(200);
			jQuery('#nav-control h4').stop().animate({ color: "#4D6877" }, 300);
			jQuery('#roll-image').fadeTo(200,0);
			jQuery('#arrow-image').fadeTo(200,1);
			searchOpen = false;
		}, 3000);
});

jQuery('#main-nav').mouseenter(function(){
    clearTimeout(navTimeOut);
}).mouseleave(function(){
    navTimeOut = setTimeout(
			function(){
				jQuery('#main-nav').slideUp(300);
				jQuery('#secondary-nav').slideDown(400,function(){ resize_bgs(); });
				jQuery('#slide-nav-left').show();
				jQuery('#search-holder').slideUp(200);
				jQuery('#nav-control h4').stop().animate({ color: "#4D6877" }, 300);
				jQuery('#roll-image').fadeTo(200,0);
				jQuery('#arrow-image').fadeTo(200,1);
				searchOpen = false;
			}, 3000);
});

//clearTimeout(navTimeOut);
navTimeOut = setTimeout(
		function(){
			jQuery('#main-nav').slideUp(300);
			jQuery('#secondary-nav').slideDown(400,function(){ resize_bgs(); });
			jQuery('#slide-nav-left').show();
			jQuery('#search-holder').slideUp(200);
			jQuery('#nav-control h4').stop().animate({ color: "#4D6877" }, 300);
			jQuery('#roll-image').fadeTo(200,0);
			jQuery('#arrow-image').fadeTo(200,1);
			searchOpen = false;
}, 3000);
*/

// search

var searchOpen = false;

jQuery('[title="search"]').click(function(){
	if(searchOpen == false){
		jQuery('#search-holder').slideDown(200,function(){
			jQuery('#s').focus();
		});
		searchOpen = true;
	} else {
		jQuery('#search-holder').slideUp();
		searchOpen = false;
	}
	return false;
});

jQuery('#searchform').submit(function(){
	jQuery('#search-loading').show();
	var query = jQuery('#s').val();
	jQuery('#search-results').slideUp(300,function(){
		jQuery(this).html('');
		jQuery.get("/", { s: query },
		   function(data){
			 jQuery('#search-loading').hide();
			 jQuery('#search-results').append(data).slideDown(300);
		   });										   
	});
	return false;								  
});



// catalogue page menu

var catalogueTimeOut;


if (jQuery.browser.msie) {
// no fades for you IE!
 
		jQuery('.project_link').hover(
			function(){
				clearTimeout(catalogueTimeOut);
				jQuery(this).stop().animate({ color: "#54B247" }, 200);
				var thisID = jQuery(this).attr('ID');
				thisID = thisID.substr(13);
				//jQuery('.catalogue-thumb-frame').removeClass('hovered').addClass('tinted');
				//jQuery('#project_'+thisID).removeClass('tinted').addClass('hovered');
				
				jQuery('.catalogue-thumb-tinted').show();
				jQuery('.catalogue-thumb-frame').hide();
				jQuery('#project_'+thisID).siblings('.catalogue-thumb-frame').show();
				jQuery('#project_'+thisID).siblings('.catalogue-thumb-tinted').hide();
				
			},function(){
				jQuery(this).stop().animate({ color: "#fff" }, 300);
				catalogueTimeOut = setTimeout(function(){
													jQuery('.catalogue-thumb-tinted').hide();
													jQuery('.catalogue-thumb-frame').show(); }, 300);
												}						  
			);
		
		jQuery('.catalogue-thumb a').hover(
			function(){
				clearTimeout(catalogueTimeOut);
				var thisID = jQuery(this).attr('ID');
				thisID = thisID.substr(8);
				jQuery('.catalogue-thumb-tinted').show();
				jQuery('.catalogue-thumb-frame').hide();
				jQuery(this).siblings('.catalogue-thumb-frame').show();
				jQuery(this).siblings('.catalogue-thumb-tinted').hide();
				jQuery('#project_link_'+thisID).stop().animate({ color: "#54B247" }, 200);
			},function(){
				var thisID = jQuery(this).attr('ID');
				thisID = thisID.substr(8);
				jQuery('#project_link_'+thisID).stop().animate({ color: "#fff" }, 300);
				catalogueTimeOut = setTimeout(function(){
												jQuery('.catalogue-thumb-tinted').hide();
												jQuery('.catalogue-thumb-frame').show();		   
											}, 300);
			}						  
		);
		

} else {
// pretty fades for everyone else
	
		jQuery('.project_link').hover(
			function(){
				clearTimeout(catalogueTimeOut);
				jQuery(this).stop().animate({ color: "#54B247" }, 200);
				var thisID = jQuery(this).attr('ID');
				thisID = thisID.substr(13);
				//jQuery('.catalogue-thumb-frame').removeClass('hovered').addClass('tinted');
				//jQuery('#project_'+thisID).removeClass('tinted').addClass('hovered');
				
				jQuery('.catalogue-thumb-tinted').stop().fadeTo(200,1);
				jQuery('.catalogue-thumb-frame').stop().fadeTo(200,0);
				jQuery('#project_'+thisID).siblings('.catalogue-thumb-frame').stop().fadeTo(200,1);
				jQuery('#project_'+thisID).siblings('.catalogue-thumb-tinted').stop().fadeTo(200,0);
				
			},function(){
				jQuery(this).stop().animate({ color: "#fff" }, 300);
				catalogueTimeOut = setTimeout(function(){
													jQuery('.catalogue-thumb-tinted').stop().fadeTo(300,0);
													jQuery('.catalogue-thumb-frame').stop().fadeTo(300,1); }, 300);
												}						  
			);
		
		jQuery('.catalogue-thumb a').hover(
			function(){
				clearTimeout(catalogueTimeOut);
				var thisID = jQuery(this).attr('ID');
				thisID = thisID.substr(8);
				jQuery('.catalogue-thumb-tinted').stop().fadeTo(200,1);
				jQuery('.catalogue-thumb-frame').stop().fadeTo(200,0);
				jQuery(this).siblings('.catalogue-thumb-frame').stop().fadeTo(200,1);
				jQuery(this).siblings('.catalogue-thumb-tinted').stop().fadeTo(200,0);
				jQuery('#project_link_'+thisID).stop().animate({ color: "#54B247" }, 200);
			},function(){
				var thisID = jQuery(this).attr('ID');
				thisID = thisID.substr(8);
				jQuery('#project_link_'+thisID).stop().animate({ color: "#fff" }, 300);
				catalogueTimeOut = setTimeout(function(){
												jQuery('.catalogue-thumb-tinted').stop().fadeTo(300,0);
												jQuery('.catalogue-thumb-frame').stop().fadeTo(300,1);		   
											}, 300);
			}						  
		);
	
	
}

// page scroller

jQuery('#page-scroll-panel div').jScrollPane();

// people

jQuery('#people-scroll-panel div').jScrollPane();






// main menu rollover color stuff

jQuery('#footer h1 a').hover(
	function(){
		jQuery('#logo-on').stop().fadeTo(500,1);
		jQuery('#logo-off').stop().fadeTo(500,0);
		//jQuery('#logo-bg').stop().animate({ backgroundColor: "#ebe6e0" }, 500);
	},
	function(){
		jQuery('#logo-on').stop().fadeTo(300,0);
		jQuery('#logo-off').stop().fadeTo(300,1);
		//jQuery('#logo-bg').stop().animate({ backgroundColor: "#1A2732" }, 300);
	});

jQuery('#nav-panel ul li a').not('#nav-panel div ul li a[title="border"], #nav-panel div ul li a[title="no-border"], #nav-panel div ul li.current-menu-item a, #nav-panel div ul li.current-page-ancestor a, #nav-panel div ul li.current-post-ancestor a,#archive-page #nav-panel div ul li#menu-item-36 a,.archive-project #nav-panel div ul li#menu-item-36 a, #nav-panel div ul li.current_page_item a').hover(
	function(){
		jQuery(this).stop().animate({ color: "#54B247" }, 200);
	},
	function(){
		jQuery(this).stop().animate({ color: "#fff" }, 300);
	});


jQuery('#right-col a').hover(
	function(){
		jQuery(this).stop().animate({ color: "#54B247" }, 200);
	},
	function(){
		jQuery(this).stop().animate({ color: "#A5B2BC" }, 300);
	});


jQuery('a[title="search"]').parent().css({'position':'relative','width':'144px','height':'16px'}).append('<div id="search-over"></div><div id="search-off"></div>');
jQuery('a[title="brochure"]').parent().css({'position':'relative','width':'144px','height':'16px'}).append('<div id="dl-over"></div><div id="dl-off"></div>');

jQuery('a[title="search"]').hover(
	function(){
		jQuery('#search-over').stop().fadeTo(200,1);
		jQuery('#search-off').stop().fadeTo(200,0);
	},
	function(){
		jQuery('#search-over').stop().fadeTo(300,0);
		jQuery('#search-off').stop().fadeTo(300,1);
	});

jQuery('a[title="brochure"]').hover(
	function(){
		jQuery('#dl-over').stop().fadeTo(200,1);
		jQuery('#dl-off').stop().fadeTo(200,0);
	},
	function(){
		jQuery('#dl-over').stop().fadeTo(300,0);
		jQuery('#dl-off').stop().fadeTo(300,1);
	});


// hide all the tooltips

jQuery("a").not("[title=divider],[title=search],[title=brochure],[title=border],[title=no-border]").hover(
 function() {
  jQuery.data(this, 'title', jQuery(this).attr('title'));
  jQuery(this).attr("title","");
 },
 function() {
  jQuery(this).attr("title",jQuery.data(this).title);
 }
);


// menu hack, ugh

if(jQuery('body#archive-page').length>0){
	jQuery('#menu-item-34').removeClass('current-menu-item');
}

// end document.ready
});
