
// 'stacks' is the Stacks global object.
// All of the other Stacks related Javascript will 
// be attatched to it.
var stacks = {};


// this call to jQuery gives us access to the globaal
// jQuery object. 
// 'noConflict' removes the '$' variable.
// 'true' removes the 'jQuery' variable.
// removing these globals reduces conflicts with other 
// jQuery versions that might be running on this page.
stacks.jQuery = jQuery.noConflict(true);

// Javascript for stacks_in_13_page1
// ---------------------------------------------------------------------

// Each stack has its own object with its own namespace.  The name of
// that object is the same as the stack's id.
stacks.stacks_in_13_page1 = {};

// A closure is defined and assined to the stack's object.  The object
// is also passed in as 'stack' which gives you a shorthand for refering
// to this object from elsewhere.
stacks.stacks_in_13_page1 = (function(stack) {

	// When jQuery is used it will be available as $ and jQuery but only
	// inside the closure.
	var jQuery = stacks.jQuery;
	var $ = jQuery;
	

//-- MovingBox Stack v1.5.0 by Joe Workman --//
$(document).ready(function($) {
	
	var num_slides = ($(".scrollContainer").children().size()) - 1;

	var small_slide_width	= $(".panel").css("width");
	var small_img_width		= $(".panel img").css("width");
	var small_title_size	= $(".panel h2").css("font-size");
	var small_text_size		= $(".panel p").css("font-size");
			
	var big_slide_width		= 291;
	var big_img_width		= 269;
	var big_title_size		= "19px";
	var big_text_size		= "14px";

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

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

	$container
		.css('left', ($container[0].offsetWidth / 2 ) - (($panels[0].offsetWidth * (2 - 1)) + (big_slide_width / 2) + 5) )
		.css('width', ($panels[0].offsetWidth * $panels.length) + 100 );

//		Old math for start slide = 2
//		.css('left', ($container[0].offsetWidth / 2 ) - ($panels[0].offsetWidth + (big_slide_width / 2)) )

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

	var moving_distance	= $panels[0].offsetWidth;

	function shrink_slide(element) {
		$(element)
			.animate({ width: small_slide_width })
			.find("img")
			.animate({ width: small_img_width })
		    .end()
			.find("h2")
			.animate({ fontSize: small_title_size })
			.end()
			.find("p")
			.animate({ fontSize: small_text_size });
	};
	
	function grow_slide(element) {
		$(element)
			.animate({ width: big_slide_width })
			.find("img")
			.animate({ width: big_img_width })
		    .end()
			.find("h2")
			.animate({ fontSize: big_title_size })
			.end()
			.find("p")
			.animate({ fontSize: big_text_size });
	}
	
	//direction true = right, false = left
	function change(direction) {
	   
	    //if not at the first or last panel
		if((direction && !(current_slide < num_slides)) || (!direction && (current_slide <= 1))) { return false; }	
        
        //if not currently moving
        if (($("#slider").data("currentlyMoving") == false)) {
            
			$("#slider").data("currentlyMoving", true);
			
			var next         = direction ? current_slide + 1 : current_slide - 1;
			var leftValue    = $(".scrollContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - moving_distance : parseFloat(leftValue, 10) + moving_distance;
		
			$(".scrollContainer")
				.stop()
				.animate({
					"left": movement
				}, function() {
					$("#slider").data("currentlyMoving", false);
				});
			
			shrink_slide("#panel_"+current_slide);
			grow_slide("#panel_"+next);
			
			current_slide = next;
			
			//remove all previous bound functions
			$("#panel_"+(current_slide+1)).unbind();	
			
			//go forward
			$("#panel_"+(current_slide+1)).click(function(){ change(true); });
			
            //remove all previous bound functions															
			$("#panel_"+(current_slide-1)).unbind();
			
			//go back
			$("#panel_"+(current_slide-1)).click(function(){ change(false); }); 
			
			//remove all previous bound functions
			$("#panel_"+current_slide).unbind();
		}
	}
	
	// Set up "Current" panel and next and prev
	grow_slide("#panel_2");	
	var current_slide = 2;
	
	$("#panel_"+(current_slide+1)).click(function(){ change(true); });
	$("#panel_"+(current_slide-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;
	  }
	});
	
});
//-- End MovingBox Stack --//

	return stack;
})(stacks.stacks_in_13_page1);



