$(function() {

	var totalPanels			= $(".scrollContainer").children().size();
		
	var regWidth			= $(".panel").css("width");
	var regImgWidth			= $(".panel img").css("width");
	var regHeight			= $(".panel").css("height");
	var regImgHeight		= $(".panel img").css("height");
	
	//var movingDistance	= 165;
	var movingDistance		= 160;
	
	var curWidth			= 230;
	var curImgWidth			= 230;

	var curHeight			= 380;
	var curImgHeight		= 350;

	var normTop				= "60px";
	var bigTop				= "0px";

	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)
			.animate({ width: regWidth, height: regHeight })
			.find("img")
			.animate({ width: regImgWidth, top: normTop })
			.end();
	};
	
	function growBigger(element) {
		$(element)
			.animate({ width: curWidth, height: curHeight })
			.find("img")
			.animate({ width: curImgWidth, top: bigTop })
		    .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 not currently moving
        if (($("#slider").data("currentlyMoving") == false)) {
            
			$("#slider").data("currentlyMoving", true);
			
			var next         = direction ? curPanel + 1 : curPanel - 1;
			var leftValue    = $(".scrollContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;
		
			$(".scrollContainer")
				.stop()
				.animate({
					"left": movement
				}, function() {
					$("#slider").data("currentlyMoving", false);
				});
			
			document.getElementById('panel_play_'+curPanel).style.display = "none"; 

			returnToNormal("#panel_"+curPanel);

			growBigger("#panel_"+next);
			
			document.getElementById('panel_play_'+next).style.backgroundImage = "url('/extension/ezwebin/design/ezwebin/images/fava/play-mp3.png')"; 
			document.getElementById('panel_play_'+next).style.backgroundRepeat = "no-repeat"; 
			document.getElementById('panel_play_'+next).style.zIndex = "9"; 
			document.getElementById('panel_play_'+next).style.position = "absolute"; 
			document.getElementById('panel_play_'+next).style.top = "140px"; 
			document.getElementById('panel_play_'+next).style.left = "90px"; 
			document.getElementById('panel_play_'+next).style.width = "72px"; 
			document.getElementById('panel_play_'+next).style.height = "74px"; 
			document.getElementById('panel_play_'+next).style.display = "block"; 

			our = (next-1);

			if( document.getElementById('pcaudiofile') )
			{
				document.getElementById('pcaudiofile').href=filearr[our];
				document.getElementById('pcaudiofile').title=titlearr[our];
			}
			if( document.getElementById('audiodownbtn') )
			{
				if( filearr[our] )
				{
					document.getElementById('audiodownbtn').style.display="block";
				}
				else
				{
					document.getElementById('audiodownbtn').style.display="none";
				}
			}
			if( document.getElementById('pcimgfront') )
			{
				document.getElementById('pcimgfront').href=imgarr[our];
				document.getElementById('pcimgfront').title=titlearr[our];
				document.getElementById('pcimgfront').rel=relarr[our];
			}
			if( document.getElementById('pcimgback') )
			{
				document.getElementById('pcimgback').href=imgbackarr[our];
				document.getElementById('pcimgback').title=titlearr[our];
				document.getElementById('pcimgback').rel=relarr[our];
			}
			if( document.getElementById('imgbackdownbtn') )
			{
				if( imgbackarr[next+1] )
				{
					document.getElementById('imgbackdownbtn').style.display="block";
				}
				else
				{
					document.getElementById('imgbackdownbtn').style.display="none";
				}
			}
			
			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();
		}
	}
	
	// Set up "Current" panel and next and prev
	growBigger("#panel_2");	
	var curPanel = 2;

	document.getElementById('panel_play_'+curPanel).style.backgroundImage = "url('/extension/ezwebin/design/ezwebin/images/fava/play-mp3.png')"; 
	document.getElementById('panel_play_'+curPanel).style.backgroundRepeat = "no-repeat"; 
	document.getElementById('panel_play_'+curPanel).style.zIndex = "9999"; 
	document.getElementById('panel_play_'+curPanel).style.position = "absolute"; 
	document.getElementById('panel_play_'+curPanel).style.top = "140px"; 
	document.getElementById('panel_play_'+curPanel).style.left = "90px"; 
	document.getElementById('panel_play_'+curPanel).style.width = "72px"; 
	document.getElementById('panel_play_'+curPanel).style.height = "74px"; 
	document.getElementById('panel_play_'+curPanel).style.display = "block"; 

	$("#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); });
	
	$(window).keydown(function(event){
	  switch (event.keyCode) {
			case 13: //enter
				$(".right").click();
				break;
			case 32: //space
				$(".right").click();
				break;
	    case 37: //left arrow
				$(".left").click();
				break;
			case 39: //right arrow
				$(".right").click();
				break;
	  }
	});
	
});
