// Plugin base object
jQuery.Carousel = function()
{
	
}
	
jQuery.Carousel.Initialize = function(imageArray, viewAllObj)
{
	/**
	 * Preload Images
	 **/
	
	for (var i=0, len=imageArray.length; i<len; i++)
	{
		jQuery('<div class="CarouselImage" />').append(jQuery('<img />').attr('src', imageArray[i].path).attr('width', '113').attr('height','54'));
	}

	/**
	 * Init Vars
	 **/
	jQuery.imageStartLoc = imageStartLoc = 3; 	// position of the left most visible image
	jQuery.imageWidth 	= imageWidth = 113;		// width of an individual slide
	jQuery.scrollAmount 	= imageWidth * 7;		// (directionless) amount to move the slides
	jQuery.imageArray	= imageArray;			// all slide images
	jQuery.viewAllObj	= viewAllObj;			// Object that contains the rightmost slide.  This slide is tacked on to every every set.
	jQuery.isAnimating	= false;				// this tracks whether the slides are currently moving.
	jQuery.startLoc;								// Initial position of the image holder... this is set below.
	
	/**
	 * Layout the Carousel
	 **/
	
	
	jQuery('#Carousel').append('<div id="CarouselMask"></div>');
	jQuery('#CarouselMask').append('<div id="CarouselImageHolder"></div>');
		
	// Add the slide color overlay
	
	for (var i=0; i<7; i++)
		jQuery('#Carousel').append('<div class="CarouselOverlay" style="left:'+(58+(113*i))+'px;"><img id="overlay'+i+'" src="/en_US/images/brand_strip/Roll_Over.jpg" width="113" height="54"/><div>');
	
	

	// Add the arrow color overlays

	jQuery('#Carousel').append('<div id="CarouselLeftOverlay" class="CarouselArrowOverlay" href="#"><img src="/en_US/images/brand_strip/ViewAll_Button.jpg" width="13" height="19" /></div>');
	jQuery('#Carousel').append('<div id="CarouselRightOverlay" class="CarouselArrowOverlay" href="#"><img src="/en_US/images/brand_strip/ViewAll_Button.jpg" width="13" height="19" /></div>');
	
	jQuery('#Carousel').append(jQuery('#CarouselBar'));
	//jQuery('#Carousel').append('<div id="CarouselBar"></div>');
	jQuery('#Carousel').append('<a id="CarouselLeftButton" class="CarouselArrowButton" href="#"><img src="/en_US/images/brand_strip/blank.gif" width="13" height="19" /></a>');
	jQuery('#Carousel').append('<a id="CarouselRightButton" class="CarouselArrowButton" href="#"><img src="/en_US/images/brand_strip/blank.gif" width="13" height="19" /></a>');
	
	// Add the stroke
	for (var i=0; i<7; i++)
		jQuery('#Carousel').append('<div class="CarouselItemStroke" style="left:'+(61+(113*i))+'px" ><img id="stroke'+i+'" src="/en_US/images/brand_strip/Blue_Outline.png" width="113" height="57" /></div>');

	// Add a transparent button overlay that corresponds to the Slide
	for (var i=0; i<7; i++)
		jQuery('#Carousel').append('<a href="'+jQuery.imageArray[i].link+'" id="image'+i+'" class="ImageButton" style="left:'+(66+(113*i))+'px;"><img src="/en_US/images/brand_strip/blank.gif" width="105" height="54" /></a>');
		
	jQuery('#image6').attr('href', jQuery.viewAllObj.link);
	// onclick="bs_tracking_Click(\''+jQuery.imageArray[i].title+'\')"

	/**
	 * Hide stuff
	 **/
	
	jQuery('#CarouselLeftOverlay').css('opacity',0.0);
	jQuery('#CarouselRightOverlay').css('opacity',0.0);
	//jQuery('.CarouselOverlay').css('opacity',1.0);

	for (var i=0; i<7; i++)
	{
		jQuery('#overlay'+i).css('opacity', 0.0);
		jQuery('#stroke'+i).css('opacity', 0.0);
	}

	jQuery('#CarouselMask').css('overflow','hidden');
	
	
	// Store the initial location of the image holder
	jQuery.startLoc = jQuery('#CarouselImageHolder').css('left');
	
	/**
	 * Mouse Actions
	 **/
	
	jQuery('.CarouselArrowButton').hover(jQuery.Carousel.mouseEntersArrow, jQuery.Carousel.mouseLeavesArrow);
	jQuery('.ImageButton').hover(jQuery.Carousel.mouseEntersItem, jQuery.Carousel.mouseLeavesItem).click(jQuery.Carousel.imageSelect);
	jQuery('#CarouselLeftButton').click(jQuery.Carousel.showPrevious);
	jQuery('#CarouselRightButton').click(jQuery.Carousel.showNext);

	// Add the initial set of images
	jQuery.Carousel.addImages(0);
};

jQuery.Carousel.showNext = function()
{
	if(jQuery.isAnimating) return;
	jQuery.isAnimating = true;
	
	// Rotate the array so that next set of items is at the start of the array
	for ( var i=0; i<6; ++i ) {
		jQuery.imageArray.push(jQuery.imageArray.shift());
	}
		
	jQuery.Carousel.addImages(jQuery.scrollAmount);
	
	// animate 
	jQuery('#CarouselImageHolder').animate( {left:'-='+jQuery.scrollAmount},
										750, 
										'swing',
										jQuery.Carousel.resetPositioning);
};

jQuery.Carousel.showPrevious = function()
{
	if(jQuery.isAnimating) return;
	jQuery.isAnimating = true;
	
	// Rotate the array so that previous set of items is at the start of the array
	for ( var i=0; i<6; ++i ) {
		jQuery.imageArray.unshift(jQuery.imageArray.pop());
		
	}
	
	jQuery.Carousel.addImages(-1*jQuery.scrollAmount);
	
	// animate 
	jQuery('#CarouselImageHolder').animate( {left:'+='+jQuery.scrollAmount}, 
										750, 
									 	'swing', 
									  	jQuery.Carousel.resetPositioning);
};

jQuery.Carousel.addImages = function(relativeStartLoc)
{
	var left;
	for (var i=0; i<6; i++)
	{
		left = (relativeStartLoc + jQuery.imageStartLoc + (i*jQuery.imageWidth))+'px';
		jQuery('#CarouselImageHolder').append('<div class="CarouselImage" style="left:'+left+'" ><img src="'+jQuery.imageArray[i].path+'" width="'+jQuery.imageWidth+'" height="54" /></div>');
	
		jQuery('#image'+i).attr('href', jQuery.imageArray[i].link);
		//console.log(jQuery('#image'+i).attr('href'));
	}
	left = (relativeStartLoc + jQuery.imageStartLoc + (6*jQuery.imageWidth))+'px';
	

	var offset = parseInt(jQuery.viewAllObj.title_offset_y);
	var top = 19 + offset;
	
	jQuery('#CarouselImageHolder').append('<div class="CarouselImage" style="left:'+left+'" ><img src="'+jQuery.viewAllObj.path+'" width="'+jQuery.imageWidth+'" height="54" /><div class="copy" style="left:8px; top:'+top+'px">'+jQuery.viewAllObj.title+'</div></div>');

};

jQuery.Carousel.resetPositioning = function()
{
	// Remove all images
	jQuery('.CarouselImage').remove();
	jQuery('#CarouselImageHolder').css('left', jQuery.startLoc);
	jQuery.Carousel.addImages(0);
	jQuery.isAnimating = false;
};

jQuery.Carousel.imageSelect = function()
{
	if (jQuery.isAnimating) return;
	
	var id = parseInt((this).id.charAt(5));
	
	// Clear Overlays
	jQuery('#copy'+id).remove();
	jQuery('#overlay'+id).clearQueue().css('opacity','0');
	jQuery('#stroke'+id).css('opacity', 0);
	
	// Go to the link
	pz_tracking_Click(jQuery.imageArray[id].title, jQuery.imageArray[id].title +' brand strip');
	
	/*track.ad_click = jQuery.imageArray[id].title;
	track.activity = jQuery.imageArray[id].title +' brand strip';
	trackEvent.event('event.link',track);
	setTimeout(window.location.assign(id<6?jQuery.imageArray[id].link:jQuery.viewAllObj.link), 500 );*/
	
	//window.location.assign(id<6?jQuery.imageArray[id].link:jQuery.viewAllObj.link);	
	//return false;
};

jQuery.Carousel.mouseEntersArrow = function()
{
	jQuery("#"+(this).id.split("Button")[0] + "Overlay").clearQueue().animate( {opacity: 1}, 200, 'swing');
};

jQuery.Carousel.mouseLeavesArrow = function()
{
	jQuery("#"+(this).id.split("Button")[0] + "Overlay").clearQueue().animate( {opacity: 0}, 200, 'swing');
};

jQuery.Carousel.mouseEntersItem = function()
{
	if(jQuery.isAnimating) return;
	var id = parseInt((this).id.charAt(5));
	
	// dynamically add the title to the slide
	var title = (id<6)?jQuery.imageArray[id].title:jQuery.viewAllObj.title;
	var offset = parseInt((id<6)?jQuery.imageArray[id].title_offset_y:jQuery.viewAllObj.title_offset_y);
	//var yValue = 18 + offset
	jQuery('#overlay'+id).parent().append('<div id="copy'+id+'" class="copy" style="top:'+(18+offset)+'px">'+title+'</div>');
	
	var copy = jQuery('#copy'+id);
	
	// show the stroke
	jQuery('#stroke'+id).css('opacity', 1.0);
	
	//fade in the blue overlay
	jQuery('#overlay'+id).clearQueue().animate({opacity: 0.7}, 200,'swing');
};

jQuery.Carousel.mouseLeavesItem = function()
{
	if(jQuery.isAnimating) return;

	// remove the dynamically added copy
	jQuery('#copy'+(this).id.charAt(5)).remove();
	
	var id = (this).id.charAt(5);
	
	// hide the stroke
	jQuery('#stroke'+id).css('opacity', 0.0);
	
	// fade out the blue overlay
	jQuery('#overlay'+id).clearQueue().animate( {opacity: 0}, 200, 'swing');
};

