var doScroll = true;

$(document).ready( function() {
	
	var leftoffset=0;
	var imagewidth=940;
	var currentimage=0;
	var autoRotateTimer;
	var numimages=$("div#scroller-stage img").length;
	var currentlyFading=false;
	var degreeToRadian=0.0174532925;
	var scrollIncTime=5;
	var transitionTime=1500;
	var scrollIncSteps=250;
	var autoplay=true;
	
	var isFireFox=false;
	if (navigator.userAgent.indexOf("Firefox")!=-1) {
		isFireFox=true;
	}
	var isIE = window.ActiveXObject ? true : false; // ActiveX is only used in Internet Explorer
	

	// JQuery set-up functions
	$("div#scroller-stage").css("width", (numimages*imagewidth));
	$("div#scroller-stage img:eq(0)").attr("src",$("div#scroller-stage div:eq(0)").find("span.img-src").text()); 
	$("div#scroller-stage img:eq(1)").attr("src",$("div#scroller-stage div:eq(1)").find("span.img-src").text()); 
	$("div#scroller-stage img:eq("+(numimages-1)+")").attr("src",$("div#scroller-stage div:eq("+(numimages-1)+")").find("span.img-src").text()); 
	
	$("div#scroller-main-image-holder img").attr("src", $("div#scroller-stage div:eq(0)").find("span.img-src").text());
	
	// Auto animation routine
	autoRotateTimer=setTimeout(fadeportfolio=function (fadeDirection, number) {
		
		if (this.doScroll == false) {
			return false;
		}
		
		if (currentlyFading == true) {
			return false;
		};
		
		currentlyFading = true;
		
		if (number) {

			currentimage = number;
			
		} 
						
		if (fadeDirection=="backwards") {
			if(currentimage==0) {
					currentimage=numimages-1;
				}
			else {
				currentimage--;
			} 
			
			$('#dots div').removeClass('active');
			$('#dots div:eq('+currentimage+')').addClass('active');
		}
		else {
			if(currentimage>=(numimages-1)) {
				currentimage=0;
			}
			else {
				currentimage++;
			} 
			
			$('#dots div').removeClass('active');
			$('#dots div:eq('+currentimage+')').addClass('active');
		}

		$("div#scroller-stage").css("left", -(currentimage*imagewidth));		

		$("div#scroller-main-image-holder").animate({opacity: 0},transitionTime);
		
		$("div#scroller-stage").animate({opacity: 1}, transitionTime+50, function() {
			
			$("div#scroller-main-image-holder img").attr("src", $("div#scroller-stage div:eq("+(currentimage)+") img").attr("src"));
			
			var tempprevimg=currentimage-1;
			if (tempprevimg<0) {
				tempprevimg=numimages+tempprevimg;
			}
			var tempnextimg=currentimage+1;
			if (tempnextimg>(numimages-1)) {
				tempnextimg=0;
			}
			if ($("div#scroller-stage img:eq("+(tempprevimg)+")").attr("src").indexOf("empty.image") != -1) {
				$("div#scroller-stage img:eq("+(tempprevimg)+")").attr("src",$("div#scroller-stage div:eq("+(tempprevimg)+")").find("span.img-src").text());
			}
			if ($("div#scroller-stage img:eq("+(tempnextimg)+")").attr("src").indexOf("empty.image") != -1) {
				$("div#scroller-stage img:eq("+(tempnextimg)+")").attr("src",$("div#scroller-stage div:eq("+(tempnextimg)+")").find("span.img-src").text());
			}
			
			setTimeout(function() {
				$("div#scroller-main-image-holder").css({opacity:1});
				currentlyFading=false;
				if (autoplay) {
					autoRotateTimer=setTimeout(function() {
						fadeportfolio();
					}, 2000);
				}	
			},200);	
			
		});
		
		
		
	}, 3000);
	
	
	
	$("div#scroller-button-holder a.scroller-button-next").click( function() {
		if (currentlyFading) {
			return false;	
		}
		clearTimeout(autoRotateTimer);
		fadeportfolio();
		$("div#scroller-button-holder a.scroller-button-play").hide();
		$("div#scroller-button-holder a.scroller-button-pause").show();		
		return false;
	});

	$("div#scroller-button-holder a.scroller-button-prev").click( function() {
		if (currentlyFading) {
			return false;
		}
		clearTimeout(autoRotateTimer);
		fadeportfolio("backwards");
		$("div#scroller-button-holder a.scroller-button-play").hide();
		$("div#scroller-button-holder a.scroller-button-pause").show();		
		return false;
	});

	$("div#scroller-button-holder a.scroller-button-play").click( function() {

		if (autoplay) {
			return;
		}
		autoplay=true;
		if (!currentlyFading) {
			clearTimeout(autoRotateTimer);
			fadeportfolio();
		}
		
		$("div#scroller-button-holder a.scroller-button-play").hide();
		$("div#scroller-button-holder a.scroller-button-pause").show();		
		return false;
	});

	$("div#scroller-button-holder a.scroller-button-pause").click( function() {
		clearTimeout(autoRotateTimer);
		autoplay=false;
		$("div#scroller-button-holder a.scroller-button-play").show();
		$("div#scroller-button-holder a.scroller-button-pause").hide();
		return false;
	});
	
	$('#dots a').click(function() {
		
		if (currentlyFading == true) {
			return false;
		};
		
		clearTimeout(autoRotateTimer);
		
		//autoplay=false;
		
		var index =  $(this).parent().attr('rel');
				
		fadeportfolio(null, index-1);
		
		$('#dots div').removeClass('active');
		$('#dots div:eq('+index+')').addClass('active');
		
		return false;
	})

});
