$(function() {
	
	var totalPanels			= $(".scrollContainer").children().size();
		a = 0;
	var regWidth			= $(".panel").css("width");
	var regImgWidth			= $(".panel img").css("width");
	var regTitleSize		= $(".panel h2").css("font-size");
	var regParSize			= $(".panel p").css("font-size");
	
	movingDistance	    	= 154;
	var curWidth			= 200;
	
	if($.browser.msie ) {
		var vers = $.browser.version;
		if ( vers == '7.0' ) {
		var curImgWidth		= 227;
		}
		else {
			var curImgWidth		= 190;	
		}
	}
	else {
		var curImgWidth		= 190;
	}
	var regImgHeight		= 95;
	var curTitleSize		= "20px";
	var curParSize			= "15px";

	var $panels				= $('#slider .scrollContainer > div');
	var $container			= $('#slider .scrollContainer');

	$panels.css({'float' : 'left','position' : 'relative'});
    
	$("#slider").data("currentlyMoving", false);

	$container
		.css('width', ($panels[0].offsetWidth * $panels.length) + 100 )
		.css('left', "0px");

	var scroll = $('#slider .scroll').css('overflow', 'hidden');

	function returnToNormal(element) {
		$(element)
			.addClass("unSelected")
			.animate({ width: regWidth })
			.find("img")
			.animate({ width: regImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: regTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: regParSize });
	};
	
	function growBigger(element) {
		$(element)
			.removeClass("unSelected")
			.animate({ width: curWidth })
			.find("img")
			.animate({ width: curImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: curTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: curParSize });
		elementReplaced = element.replace("#","");
		description = 'descriptionOf' + elementReplaced;			
		document.getElementById('slideDetail').innerHTML = document.getElementById(description).innerHTML; 
	}
	
	//direction true = right, false = left
	function change(direction) {
		if (-parseFloat($(".scrollContainer").css("left")) >= movingDistance  * (totalPanels - 2) ) {
			$container
			.css('left', "154px");
			
			
		}
		if (curPanel >= totalPanels ) {
			returnToNormal("#panel_"+curPanel);
			curPanel = 1;
			cyrcleLeftValue = '154px';
		}
		else {
			cyrcleLeftValue = 'zero';
		}
		
	    //if not at the first or last panel
		if( (!direction && (curPanel <= 1))) { return false; }
		
		/*if (curPanel <= 0 ) {
			returnToNormal("#panel_"+curPanel);
			curPanel = totalPanels;
			cyrcleLeftValue = -154*totalPanels;
		}
		else {
			cyrcleLeftValue = 'zero';
		}*/
        
        //if not currently moving
        if (($("#slider").data("currentlyMoving") == false)) {
        	
    				
			$("#slider").data("currentlyMoving", true);
			
			var next         = direction ? curPanel + 1 : curPanel - 1;
			var leftValue    = (cyrcleLeftValue == 'zero' ) ? $(".scrollContainer").css("left") : cyrcleLeftValue;
			
			var movement	 = direction ? parseFloat(leftValue) - movingDistance : (parseFloat(leftValue)) + movingDistance;
			//alert(curPanel);
			//alert(movement);
			

			$(".scrollContainer")
				.stop()
				.animate({
					"left": movement
				}, function() {
					$("#slider").data("currentlyMoving", false);
				});
			
			returnToNormal("#panel_"+curPanel);
			growBigger("#panel_"+next);
			
			curPanel = next;
			
			//remove all previous bound functions
			$("#panel_"+(curPanel+1)).unbind();	
			
			//go forward
			$("#panel_"+(curPanel+1)).click(function(){ change(true); });
			
            //remove all previous bound functions															
			$("#panel_"+(curPanel-1)).unbind();
			
			//go back
			$("#panel_"+(curPanel-1)).click(function(){ change(false); }); 
			
			//remove all previous bound functions
			$("#panel_"+curPanel).unbind();
		}
       
		if (-parseFloat($(".scrollContainer").css("left")) >=  movingDistance  * (totalPanels - 2) ) {
			$container.css('left', "154px");
			
		}
		
	}
	
	// Set up "Current" panel and next and prev
	growBigger("#panel_2");	
	var curPanel = 2;
	
	$("#panel_"+(curPanel+1)).click(function(){ change(true); });
	$("#panel_"+(curPanel-1)).click(function(){ change(false); });
	
	//when the left/right arrows are clicked
	$(".right").click(function(){ change(true); });	
	$(".left").click(function(){ change(false); });
	
	function movePanels() {
		change(true);
		setTimeout(movePanels,5000);
	}
	setTimeout(movePanels,5000);
	
	

});


function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
        // Alternatively you could use:
        // (new Image()).src = this;
    });
}

