$(function() {
	
	$(".right").css({'z-index' : 202});
	$(".left").css({'z-index' : 202});
	
	$(".btn_home_right").hide();
	$(".btn_home_left").hide();
	$(".arrow_text_home_left").hide();
	$(".arrow_text_home_right").hide();
	$("#additional_close").hide();
	$("#arrow_text_workshops").show();
	
	var totalPanels			= 4
	var regWidth			= $(".panel").css("width");
	var movingDistance	    = 754;
	var hiddenDistance	    = -6100;
	var curWidth			= 754;
	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', "-751px");

	var scroll = $('#slider .scroll').css('overflow', 'hidden');
	
	//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);
				});
						
			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_3");
	curPanel = 2;
	
	$("#panel_"+(curPanel+1)).click(function(){ change(true); });
	$("#panel_"+(curPanel-1)).click(function(){ change(false); });
	
	
	
	// when the header button is pressed
	$("a.header_button").click(function(){ change(4);
		$(".right").fadeOut("fast");
		$(".left").fadeOut("fast");
		$("#button_blocker_left").fadeIn("fast");
		$("#button_blocker_right").fadeOut("fast");
		$("#arrow_text_films").fadeOut("fast");
		$("#arrow_text_workshops").fadeOut("fast");
		$(".btn_home_left").fadeIn("fast");
		$("#additional_close").show();
		$(".arrow_text_home_left").fadeIn("fast");
		//var curPanel = 4;
			$(".scrollContainer")
				.stop()
				.animate({
					"left": -2260
				});
				curPanel = 4;
	});	
	
	$("a.panel4_close").click(function() {  
		$(".right").fadeIn("fast");
		$(".left").fadeIn("fast");
		$("#button_blocker_left").fadeIn("fast");
		$("#button_blocker_right").fadeIn("fast");
		$(".btn_home_left").fadeOut("fast");
		$("#additional_close").fadeOut("fast");
		$(".arrow_text_home_left").fadeOut("fast");
		$("#arrow_text_films").fadeIn("fast");
		$("#arrow_text_workshops").fadeIn("fast");
		$(".scrollContainer")
					.stop()
					.animate({
						"left": -751
					}, function() {
						$("#slider").data("currentlyMoving", false);
					});	
		curPanel = 2;
  	});
	
	// when the header button is pressed
	$("a.testimonials_button").click(function(){ change(4);
		$(".right").fadeOut("fast");
		$(".left").fadeOut("fast");
		$("#button_blocker_left").fadeIn("fast");
		$("#button_blocker_right").fadeOut("fast");
		$("#arrow_text_films").fadeOut("fast");
		$("#arrow_text_workshops").fadeOut("fast");
		$(".btn_home_left").fadeIn("fast");
		$("#additional_close").show();
		$(".arrow_text_home_left").fadeIn("fast");
		//var curPanel = 4;
			$(".scrollContainer")
				.stop()
				.animate({
					"left": -3013
				});
				curPanel = 4;
	});	
	
	$("a.testimonials_close").click(function() {  
		$(".right").fadeIn("fast");
		$(".left").fadeIn("fast");
		$("#button_blocker_left").fadeIn("fast");
		$("#button_blocker_right").fadeIn("fast");
		$(".btn_home_left").fadeOut("fast");
		$("#additional_close").fadeOut("fast");
		$(".arrow_text_home_left").fadeOut("fast");
		$("#arrow_text_films").fadeIn("fast");
		$("#arrow_text_workshops").fadeIn("fast");
		$(".scrollContainer")
					.stop()
					.animate({
						"left": -751
					}, function() {
						$("#slider").data("currentlyMoving", false);
					});	
		curPanel = 2;
  	});
	
	//when the left/right arrows are clicked
	$(".right").click(function(){ change(true);
		if(curPanel == 3) {
			$("#button_blocker_right").fadeOut("fast");
			$(".btn_home_left").fadeIn("fast");
			$(".btn_films_left").fadeOut("fast");
			$(".arrow_text_home_left").fadeIn("fast");
			$(".left").fadeIn("fast");
			$(".right").fadeOut("fast");
			$("#arrow_text_films").fadeOut("fast");
			$("#arrow_text_workshops").fadeOut("fast");
		}
		if(curPanel == 2) {
			$("#button_blocker_left").fadeIn("fast");
			$("#button_blocker_right").fadeIn("fast");
			$(".btn_home_right").fadeOut("fast");
			$(".btn_home_left").fadeOut("fast");
			$(".btn_films_right").fadeIn("fast");
			$(".arrow_text_home_left").fadeOut("fast");
			$(".arrow_text_home_right").fadeOut("fast");
			$(".left").fadeIn("fast");
			$(".right").fadeIn("fast");
			$(".btn_films_right").fadeIn("fast");
			$("#arrow_text_films").fadeIn("fast");
			$("#arrow_text_workshops").fadeIn("fast");
		}
	});	
	$(".left").click(function(){ change(false); 
		if(curPanel < 2) {
			$("#button_blocker_left").fadeOut("fast");
			$(".btn_home_right").fadeIn("fast");
			$(".btn_films_right").fadeOut("fast");
			$(".arrow_text_home_right").fadeIn("fast");
			$(".left").fadeOut("fast");
			$(".right").fadeIn("fast");
			$("#arrow_text_films").fadeOut("fast");
			$("#arrow_text_workshops").fadeOut("fast");
		}
		if(curPanel == 2) {
			$("#button_blocker_left").fadeIn("fast");
			$("#button_blocker_right").fadeIn("fast");
			$(".btn_home_right").fadeOut("fast");
			$(".btn_home_left").fadeOut("fast");
			$(".btn_films_right").fadeIn("fast");
			$(".arrow_text_home_left").fadeOut("fast");
			$(".arrow_text_home_right").fadeOut("fast");
			$(".left").fadeIn("fast");
			$(".right").fadeIn("fast");
			$(".btn_films_right").fadeIn("fast");
			$("#arrow_text_films").fadeIn("fast");
			$("#arrow_text_workshops").fadeIn("fast");
		}
	});
	
});
