﻿var crestCarousel = function() {
	
	// private variables
	var staticPanels = new Array();
	var activePanelID;
	
	return {
		
		/*
		 * getStaticPanelsHTML
		 * loads the static <li> HTML content from the staticCarousel list
		 * 	into an array for later processing. Subsequently removes the
		 * 	staticCarousel list from the DOM
		*/
		getStaticPanelsHTML : function (panelArray){
			$.each (
				panelArray,
				function( intIndex, panel  ){
					staticPanels[intIndex] = panel.innerHTML;
				}
			);
			$("#staticCarousel").remove();
		},
		
		/*
		 * itemVisibleInCallback
		 * handles carousel display when an item moves into view
		*/
		itemVisibleInCallback : function(carousel, item, i, state, evt) {
			
			// The index() method calculates the index from a
			// given index who is out of the actual item range.
			
			var idx = carousel.index(i, staticPanels.length);
			carousel.add(i, staticPanels[idx - 1]);
		},
		
		/*
		 * itemVisibleOutCallback
		 * handles carousel display when an item moves out of view
		*/
		itemVisibleOutCallback : function(carousel, item, i, state, evt) {
			carousel.remove(i);
		},
		
		/*
		 * activateSlot
		 * enlarges the images moved into the first position within the carousel 
		*/
		activateSlot : function(slotIndex){
		
			activePanelID = $("#dynamicCarousel").children("li")[slotIndex].firstChild.firstChild.id	
				
			// shrink any previously enlarged images
			/*$(".active").animate({ 
				height: "120px",
				width: "50px",
				marginTop:"40px"
			}, 150 );
			*/
			
			// remove class any previous active images
			$(".active").removeClass("active")
			
			// enlarge the active image
			/*$("#"+activePanelID).animate({ 
				height: "210px",
				width: "88px",
				marginTop:"0px"
			}, 500 );
			*/
		
			// set class of the active image
			$("#"+activePanelID).addClass("active");
		}
	};
}();

function initSlot(){
	 // enlarge the 1st active image
	/*$("#panel_1").animate({ 
		height: "210px",
		width: "88px",
		marginTop:"0px"
	}, 500 );
	$("#panel_1").addClass("active");
	
	$('ul#dynamicCarousel A[rel*="event"]').click( function() {
		var ary = this.name.split("~");
		var section = "";
		var subsection = "";
		var event = "";
		if(ary[0] != undefined)
			section = ary[0];
		if(ary[1] != undefined)
			subsection = ary[1];
		if(ary[2] != undefined)
			event = ary[2];
			
        trackEvent(section, subsection, event);
	});
	*/
	
	//show the carousel only after it has fully loaded 
	//so it doesn't show wonky styles before it's finished (disabled in favor of jsEnabled feature for jquery)
	//document.getElementById("carouselContainer").style.marginLeft = "auto";
}

jQuery(document).ready(function() {

	// get the html from the staticCarousel
    crestCarousel.getStaticPanelsHTML($("#staticCarousel").children());
    
    // create the dynamicCarousel
    var ul = document.createElement('ul');
    ul.setAttribute('id',"dynamicCarousel");
    document.getElementById("carouselContainer").appendChild(ul);
    
    // set up the carousel
    jQuery('#dynamicCarousel').jcarousel({
        wrap: 'circular',
        itemVisibleInCallback: {onBeforeAnimation: crestCarousel.itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: crestCarousel.itemVisibleOutCallback}
    });
    
    var initSlot1 = setTimeout ( initSlot, 500 );
    
   // $("#actionbutton").attr("src","/images/"+localPath+"/_global/search-action.png")
    
        

});



