$(function(){
	$("#background-manual img").eq(0).scaleImage();
	$("#background img").eq(0).scaleImage();
	$("#background").cycleItem({limit:$("#background img").length, onCycle:rotatePhotos});
	$("#photoNav a").bind("click", caseStudyPhotos);

	var current = 0,
		limit = $("#background-manual img").length;

	function caseStudyPhotos(event){
		var obj = $(event.currentTarget),
			id = obj.attr("id"),
			lgphotos = $("#background-manual").find("img"),
			smphotos = $("#casestudyphoto").find("img");
			
		if(id === "photoprev"){
			controlCurrent("down");
		} else if(id === "photonext"){
			controlCurrent("up");
		}	
		
		lgphotos.eq(current).scaleImage();
		lgphotos.filter(":visible").fadeOut("fast");
		smphotos.filter(":visible").hide();
		smphotos.eq(current).show();
		
		return false;
	}

	function controlCurrent(dir){
		if (current === 0 && dir === "down") {
			current = limit-1;
		} else if (current === limit-1 && dir === "up") {
			current = 0;
		} else {
			(dir === "down") ? (current--) : (current++);
		}
	}

	function rotatePhotos(data){
		var elements = $("#background").find("img");
		elements.eq(data.next).scaleImage();
		elements.eq(data.current).fadeOut("fast");
	}
});


/**
* jQuery Scale Image
* Copyright (c) 2011 Kevin Doyle
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
**/

(function(a){var l={},c,i,j,e,f,g,k,h,d={init:function(b){b&&a.extend(l,b);return this.each(function(){c=a(this);a(window).bind("resize",function(){h||(h=!0,window.setTimeout(function(){d._scaleImage();h=!1},200))});d._redraw()})},calcNewImg:function(){return this.each(function(){c=a(this);d._redraw()})},_redraw:function(){setTimeout(function(){d._calcImgRatio();a(window).trigger("resize");setTimeout(function(){c.show()},200)},50)},_calcImgRatio:function(){i=c.height();j=c.width();e=i/j},_scaleImage:function(){f= a(window).height();g=a(window).width();k=f/g;k>e?c.css({height:f,width:f/e}):c.css({height:g*e,width:g})}};a.fn.scaleImage=function(b){if(d[b])return d[b].apply(this,Array.prototype.slice.call(arguments,1));else if(typeof b==="object"||!b)return d.init.apply(this,arguments);else a.error("Method "+b+" does not exist on scaleImage")}})(jQuery);

/**
* jQuery Cycle Item
* Copyright (c) 2011 Kevin Doyle
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
**/

(function(e){var a={delay:1E4,timeoutDelay:5E3,section:".cycleitem",initialIndex:0,navigation:!1,limit:null,onCycle:function(){}},j,b,c,g,m,h,k,n,l,i,d={init:function(f){f&&e.extend(a,f);return this.each(function(){j=e(this);if(!a.limit)a.limit=j.find(a.section).length;g=m=!1;i=!0;b=-1;c=0;a.limit>1&&d._createTimer();a.navigation&&e(a.navigation).bind("click",d._forceCycle)})},cycle:function(){a.navigation&&(e(a.navigation+".current").removeClass("current"),e(a.navigation).eq(c).addClass("current")); a.onCycle.call(j,{current:b,next:c});g&&(g=!1,h=!0)},resetCycle:function(){b=a.initialIndex-1;c=0},disableNavigation:function(){i=!1},enableNavigation:function(){i=!0},_controlCurrent:function(f){h?(b=c,c=b+1,b===a.limit-1&&(c=0),h=!1):b===a.limit-1&&f==="up"?(b=0,c=1):(b+=1,f==="up"&&b===0?c=1:f==="up"&&b===a.limit-1?c=0:f==="up"&&b>0&&b<a.limit-1&&(c=b+1))},_createTimer:function(){rotate=setInterval(d._autoCycle,a.delay);g&&(g=!1,h=!0)},_autoCycle:function(){d._controlCurrent("up");d.cycle()},_forceCycle:function(f){!g&& i&&(d._delayTimer(),n=e(f.currentTarget),l=e(a.navigation).index(n),c!==l&&(g=!0,b=c,c=l,d.cycle()));return!1},_stopTimer:function(){clearInterval(rotate);typeof k!=="undefined"&&clearTimeout(k)},_delayTimer:function(){d._stopTimer();m||(k=setTimeout(d._createTimer,a.timeoutDelay))}};e.fn.cycleItem=function(a){if(d[a])return d[a].apply(this,Array.prototype.slice.call(arguments,1));else if(typeof a==="object"||!a)return d.init.apply(this,arguments);else e.error("Method "+a+" does not exist on cycleItem")}})(jQuery);

