var j$=jQuery.noConflict();
  jQuery(document).ready(function(){
//$(function() {
	var totalPanels			= j$(".scrollContainer").children().size();
	
//	alert("totalPanels:	"+totalPanels);
		
	var regWidth			= j$(".panel").css("width");
	var regImgWidth			= j$(".panel img").css("width");
	var regTitleSize		= j$(".panel h2").css("font-size");
	var regParSize			= j$(".panel p").css("font-size");
	
	var movingDistance	    = 99;
	
	var curWidth			= 250;
	var curImgWidth			= 250;
	
/*
	var movingDistance	    = 300;
	
	var curWidth			= 350;
	var curImgWidth			= 326;
*/
	
	var curTitleSize		= "12px";
	var curParSize			= "11px";
	
	var smallTitleSize		= "4px";
	var smallParSize		= "4px";

	var $panels				= j$('#slider .scrollContainer > div');
	var $container			= j$('#slider .scrollContainer');

	$panels.css({'float' : 'left','position' : 'relative'});
    
	j$("#slider").data("currentlyMoving", false);

	$container
		.css('width', ($panels.attr('offsetWidth') * $panels.length) + 157 )
/*		.css('width', ($panels[0].offsetWidth * $panels.length) + 157 )*/		
		.css('left', '-99px');
/*		.css('left', "-350px"); */		

	var scroll = j$('#slider .scroll').css('overflow', 'hidden');

	function returnToNormal(element) {
		j$(element)
			.animate({ width: regWidth, top: 90   })
			.find("img")
			.animate({ width: regImgWidth, opacity: '0.5' })
		    .end()
			.find("h2")
			.animate({ fontSize: smallTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: smallParSize })
			.end()
			.find('.np_counterCT')
			.hide();


	/*	j$(element).animate({ top: 100 })
			.find("h2")
			.animate({ fontSize: regTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: regParSize });			
	*/
	};
	
	function growBigger(element) {
		j$(element)
			.animate({ width: curWidth, top: 0   })
			.find("img")
			.animate({ width: curImgWidth, opacity: '1' })
		    .end()
			.find("h2")
			.animate({ fontSize: curTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: curParSize })
			.end()
			.find('.np_counterCT')
			.show();
			
			
			
	}
	
	//direction true = right, false = left
	function change(direction) {
	   
	    //if not at the first or last panel
		if((direction && !(curPanel < totalPanels)) || (!direction && (curPanel <= 1))) { 
//			alert("hit end or start!");
			return false; 
		}	
        
        //if not currently moving
        if ((j$("#slider").data("currentlyMoving") == false)) {
            
			j$("#slider").data("currentlyMoving", true);
			
			var next         = direction ? curPanel + 1 : curPanel - 1;
			var leftValue    = j$(".scrollContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;
		
			j$(".scrollContainer")
				.stop()
				.animate({
					"left": movement
				}, function() {
					j$("#slider").data("currentlyMoving", false);
				});
			
			returnToNormal("#panel_"+curPanel);
			growBigger("#panel_"+next);
			
			curPanel = next;
			
			//remove all previous bound functions
			j$("#panel_"+(curPanel+1)).unbind();	
		
			//go forward
			j$("#panel_"+(curPanel+1)).click(function(){ j$("#panel_"+(curPanel+1)) ? change(true) : null; });
			
		
            //remove all previous bound functions															
			j$("#panel_"+(curPanel-1)).unbind();
			
			//go back
			j$("#panel_"+(curPanel-1)).click(function(){ j$("#panel_"+(curPanel-1)) ? change(false) : null; }); 
			
			//remove all previous bound functions
			j$("#panel_"+curPanel).unbind();
		}
	}
	
	// Set up "Current" panel and next and prev
	growBigger("#panel_3");	
	var curPanel = 3;
	
	j$("#panel_"+(curPanel+1)).click(function(){ change(true); });
	j$("#panel_"+(curPanel-1)).click(function(){ change(false); });
	
	//when the left/right arrows are clicked
	j$(".right").click(function(){ change(true); });	
	j$(".left").click(function(){ change(false); });
	
	j$(window).keydown(function(event){
	  switch (event.keyCode) {
			case 13: //enter
				j$(".right").click();
				break;
			case 32: //space
				j$(".right").click();
				break;
			case 37: //left arrow
				j$(".left").click();
				break;
			case 39: //right arrow
				j$(".right").click();
				break;
	  }
	});
	
});
