var $jQ = jQuery.noConflict();

$jQ(function() {
	var totalPanels			= $jQ(".scrollContainer").children().size();
		
	var regWidth			= $jQ(".panel").css("width");
	var regImgWidth			= $jQ(".panel img.slider_img").css("width");
	var regImgHeight		= $jQ(".panel img.slider_img").css("height");
	var regImgBgWidth		= $jQ(".panel img.slider_pic_bg").css("width");
	var regImgBgHeight		= $jQ(".panel img.slider_pic_bg").css("height");
	var regTitleSize		= $jQ(".panel h2").css("font-size");
	var regParSize			= $jQ(".panel p").css("font-size");
	
	var movingDistance	    = 291;
	
	var curWidth			= 310;
	var curImgWidth			= 320;
	var curImgHeight		= 190;
	var curImgBgWidth		= 400;
	var curImgBgHeight		= 236;
	var curTitleSize		= "20px";
	var curParSize			= "15px";

	var $jQpanels				= $jQ('#slider .scrollContainer > div');
	var $jQcontainer			= $jQ('#slider .scrollContainer');

	$jQpanels.css({'float' : 'left','position' : 'relative'});
    
	$jQ("#slider").data("currentlyMoving", false);

	$jQcontainer
		.css('width', ($jQpanels[0].offsetWidth * $jQpanels.length) + 100 )
		.css('left', "-304px");

	var scroll = $jQ('#slider .scroll').css('overflow', 'hidden');

	function returnToNormal(element) {
		$jQ(element)
			.animate({ width: regWidth })
			.find("img.slider_img")
			.animate({ width: regImgWidth, height: regImgHeight })
		    .end()
			.find("img.slider_pic_bg")
			.animate({ width: regImgBgWidth, height: regImgBgHeight })
			.end()
			.find("div.inside_frame")
			.animate({ left: 0, top: 25 })
			.end()
			.find("div.inside")
			.animate({ left: -4, top: 25 })
			.end()
	};
	
	function growBigger(element) {
		$jQ(element)
			.animate({ width: curWidth })
			.find("img.slider_img")
			.animate({ width: curImgWidth, height: curImgHeight })
		    .end()
			.find("img.slider_pic_bg")
			.animate({ width: curImgBgWidth, height: curImgBgHeight })
			.end()
			.find("div")
			.animate({ left: 0, top: 0 })
			.end()
	}
	
	//direction true = right, false = left
	function change(direction) {
	   
	    //if not at the first or last panel
		if((direction && !(curPanel < totalPanels)) || (!direction && (curPanel <= 1))) { return false; }	
		
		if (direction && !(curPanel < totalPanels) ) {
				curPanel = totalPanels;
			} else {
				if ( curPanel < 1 ) { curPanel = 1; }
				if ( curPanel > totalPanels ) { curPanel = totalPanels; }
			}
        
        //if not currently moving
        if (($jQ("#slider").data("currentlyMoving") == false)) {
            
			$jQ("#slider").data("currentlyMoving", true);
			
			var next         = direction ? curPanel + 1 : curPanel - 1;
			var leftValue    = $jQ(".scrollContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;
		
			$jQ(".scrollContainer")
				.stop()
				.animate({
					"left": movement
				}, function() {
					$jQ("#slider").data("currentlyMoving", false);
				});
			
			returnToNormal("#panel_"+curPanel);
			growBigger("#panel_"+next);
			
			curPanel = next;
			
			//remove all previous bound functions
			$jQ("#panel_"+(curPanel+1)).unbind();	
			
			//go forward
			//$jQ("#panel_"+(curPanel+1)).click(function(){ change(true); });
			
            //remove all previous bound functions															
			$jQ("#panel_"+(curPanel-1)).unbind();
			
			//go back
			//$jQ("#panel_"+(curPanel-1)).click(function(){ change(false); }); 
			
			//remove all previous bound functions
			$jQ("#panel_"+curPanel).unbind();
		}
	}
	
	// Set up "Current" panel and next and prev
	growBigger("#panel_3");	
	var curPanel = 3;
	
	$jQ("#panel_"+(curPanel+1)).click(function(){ change(true); });
	$jQ("#panel_"+(curPanel-1)).click(function(){ change(false); });
	
	//when the left/right arrows are clicked
	$jQ(".right").click(function(){ change(true); });	
	$jQ(".left").click(function(){ change(false); });
	
	/*$jQ(window).keydown(function(event){
	  switch (event.keyCode) {
			case 13: //enter
				$jQ(".right").click();
				break;
			case 32: //space
				$jQ(".right").click();
				break;
	    case 37: //left arrow
				$jQ(".left").click();
				break;
			case 39: //right arrow
				$jQ(".right").click();
				break;
	  }
	});*/
	
});
