$(function() {
	
	var totalvpanels		= $(".vscrollContainer").children().size();
		
	var regWidth			= $(".vpanel").css("width");
	var regImgWidth			= $(".vpanel img").css("width");
	var regTitleSize		= $(".vpanel h2").css("font-size");
	var regParSize			= $(".vpanel p").css("font-size");
	
	var movingDistance	    = 270;
	
	var curWidth			= 270;
	var curImgWidth			= 65;
	var curTitleSize		= "10px";
	var curParSize			= "12px";

	var $vpanels			= $('#vslider .vscrollContainer > div');
	var $vcontainer			= $('#vslider .vscrollContainer');

	$vpanels.css({'float' : 'left','position' : 'relative'});
    
	$("#vslider").data("vcurrentlyMoving", false);

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

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

	function returnToNormal(element) {
		$(element)
			.animate({ width: regWidth })
			.find("img")
			.animate({ width: regImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: regTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: regParSize });
	};
	
	function growBigger(element) {
		$(element)
			.animate({ width: curWidth })
			.find("img")
			.animate({ width: curImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: curTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: curParSize });
	}
	
	//direction true = right, false = left
	function change(direction) {
	   
	    //if not at the first or last vpanel
		if((direction && !(curvpanel < totalvpanels)) || (!direction && (curvpanel <= 1))) { return false; }	
        
        //if not currently moving
        if (($("#vslider").data("vcurrentlyMoving") == false)) {
            
			$("#vslider").data("vcurrentlyMoving", true);
			
			var next         = direction ? curvpanel + 1 : curvpanel - 1;
			var leftValue    = $(".vscrollContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;
		
			$(".vscrollContainer")
				.stop()
				.animate({
					"left": movement
				}, function() {
					$("#vslider").data("vcurrentlyMoving", false);
				});
			
			returnToNormal("#vpanel_"+curvpanel);
			growBigger("#vpanel_"+next);
			
			curvpanel = next;
			
			//remove all previous bound functions
			$("#vpanel_"+(curvpanel+1)).unbind();	
			
			//go forward
			$("#vpanel_"+(curvpanel+1)).click(function(){ change(true); });
			
            //remove all previous bound functions															
			$("#vpanel_"+(curvpanel-1)).unbind();
			
			//go back
			$("#vpanel_"+(curvpanel-1)).click(function(){ change(false); }); 
			
			//remove all previous bound functions
			$("#vpanel_"+curvpanel).unbind();
		}
	}
	
	// Set up "Current" vpanel and next and prev
	growBigger("#vpanel_1");	
	var curvpanel = 1;
	
	$("#vpanel_"+(curvpanel+1)).click(function(){ change(true); });
	$("#vpanel_"+(curvpanel-1)).click(function(){ change(false); });
	
	//when the left/right arrows are clicked
	$(".vright").click(function(){ change(true); });	
	$(".vleft").click(function(){ change(false); });
	

});
