if ( !featurePathStub )
	featurePathStub = '';

var bcExp;
var modVP;
var modExp;
var modCon;

var userSelectedFeature = false;
	 
// called when template loads, this function stores a reference to the player and modules.
function onTemplateLoaded(experienceID) {
 	//alert(experienceID);
    bcExp = brightcove.getExperience(experienceID);
 
    modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
    modExp = bcExp.getModule(APIModules.EXPERIENCE);
    modCon = bcExp.getModule(APIModules.CONTENT);
	
	modExp.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);
    modExp.addEventListener(BCExperienceEvent.CONTENT_LOAD, onContentLoad);
    modCon.addEventListener(BCContentEvent.VIDEO_LOAD, onVideoLoad);
	modVP.addEventListener(BCMediaEvent.PLAY,onVideoPlay);
	
	// check to make sure that the default feature is still the current/desired feature
	if (Kmart.FeatureGallery.userSelectedFeature) {
		Kmart.FeatureGallery.setCurrentFeature( Kmart.FeatureGallery.userSelectedFeature );
		// no timer
	} else {
		// let's go
		Kmart.FeatureGallery.startTimer();
	}
	
	
}
function onTemplateReady(evt) {
    //alert("EVENT: TEMPLATE_READY"); 
}
function onContentLoad(evt) {
}
function onVideoPlay(evt) {
	Kmart.FeatureGallery.stopTimer();
	var gaStub = jQuery('#feature-gallery div.thumb.current').eq(0).find('a.feature-ga-stub').eq(0).attr('href').toString();
	pageTracker._trackPageview(gaStub+'click');
	pageTracker._trackPageview('/'+featurePathStub+'/video/'+evt.media.id);
}
function onVideoLoad(evt) {
    // Cue video that was just loaded
    modVP.cueVideo(evt.video.id);
}

var Kmart = {
	FeatureGallery : {}
}

Kmart.FeatureGallery.init = function()
{
	Kmart.FeatureGallery.featureRotationTime = 7;
	Kmart.FeatureGallery.canRotate = true;
	Kmart.FeatureGallery.canSwitchPages = true;
	
	var featureThumbWidth = Kmart.FeatureGallery.featureThumbWidth = 141;
	var featurePerPage = Kmart.FeatureGallery.featurePerPage = 4;
	var pageWidth = Kmart.FeatureGallery.pageWidth = featureThumbWidth * featurePerPage;
	Kmart.FeatureGallery.thumbs = jQuery('#feature-gallery .feature-thumbs .thumb');
	var featureCount = Kmart.FeatureGallery.featureCount = Math.round( jQuery(Kmart.FeatureGallery.thumbs).length );
	var pageCount = Kmart.FeatureGallery.pageCount = Math.ceil( featureCount / featurePerPage );
	
	Kmart.FeatureGallery.featureContentContainer = jQuery('#feature-gallery .feature .content').eq(0);
	Kmart.FeatureGallery.featureCaptionContainer = jQuery('#feature-gallery .feature .caption').eq(0);
	var slider = Kmart.FeatureGallery.slider = jQuery('#feature-gallery .feature-thumbs .inner .slider').eq(0);
	Kmart.FeatureGallery.nextPageBtnLarge = jQuery('#feature-gallery .feature-thumbs a.next-page').eq(0);
	Kmart.FeatureGallery.prevPageBtnLarge = jQuery('#feature-gallery .feature-thumbs a.prev-page').eq(0);
	
	jQuery( Kmart.FeatureGallery.featureContentContainer ).click( Kmart.FeatureGallery.stopTimer );
	jQuery( Kmart.FeatureGallery.featureCaptionContainer ).click( Kmart.FeatureGallery.stopTimer );
	
	jQuery( slider ).css( 'width', pageWidth * pageCount );
	
	jQuery( Kmart.FeatureGallery.nextPageBtnLarge ).click( Kmart.FeatureGallery.nextPage );
	jQuery( Kmart.FeatureGallery.prevPageBtnLarge ).click( Kmart.FeatureGallery.prevPage );
	jQuery( '#feature-gallery .pagination a.next-page' ).click( Kmart.FeatureGallery.nextPage );
	jQuery( '#feature-gallery .pagination a.prev-page' ).click( Kmart.FeatureGallery.prevPage );
	
	jQuery( Kmart.FeatureGallery.thumbs ).hover(
		Kmart.FeatureGallery.highlight,
		Kmart.FeatureGallery.unhighlight
	);
	
	jQuery( Kmart.FeatureGallery.thumbs ).click( Kmart.FeatureGallery.onFeatureThumbClick );
	
	if (featureCount > 1) {
		// Timer now starts when Brightcove API is ready. See function onTemplateLoaded
		//Kmart.FeatureGallery.startTimer();
		Kmart.FeatureGallery.preload();
	} else {
		Kmart.FeatureGallery.canRotate = false;
	}
}


Kmart.FeatureGallery.startTimer = function()
{
	if ( Kmart.FeatureGallery.canRotate )
		Kmart.FeatureGallery.featureRotateTimer = setInterval( Kmart.FeatureGallery.rotate, Kmart.FeatureGallery.featureRotationTime * 1000 );
}


Kmart.FeatureGallery.stopTimer = function()
{
	clearInterval( Kmart.FeatureGallery.featureRotateTimer );
}

Kmart.FeatureGallery.onPageTransitionComplete = function()
{
	Kmart.FeatureGallery.canSwitchPages = true;
	Kmart.FeatureGallery.updatePagination();
}

Kmart.FeatureGallery.updatePagination = function()
{
	///alert(jQuery( Kmart.FeatureGallery.slider ).position().left);
	var paginationTextContainer = jQuery('#feature-gallery .pagination .current-thumbs').eq(0);
	var page = Kmart.FeatureGallery.getCurrentPage();
	var startThumbNum = ( page == 1 ) ? page : 1 + ( (page-1) * Kmart.FeatureGallery.featurePerPage );
	var remaining = jQuery('#feature-gallery .feature-thumbs .thumb').slice(startThumbNum).length;
	var endThumbNum = (remaining <= 3) ? remaining : 3;
		endThumbNum += startThumbNum;
	paginationString =  startThumbNum + ' - ' + endThumbNum;
	jQuery(paginationTextContainer).text(paginationString);
}


Kmart.FeatureGallery.getCurrentPage = function()
{
	var pageLeft = jQuery( Kmart.FeatureGallery.slider ).position().left;
	if ( pageLeft == 0 ) {
		return 1;
	} else {
		var page = Math.round( ( Math.abs(pageLeft) / Kmart.FeatureGallery.pageWidth ) ) + 1;
		return page;
	}
}


Kmart.FeatureGallery.isOnPageBoundary = function()
{
	var pageLeft = Math.ceil( Math.abs( jQuery( Kmart.FeatureGallery.slider ).position().left) );
	return (pageLeft == 0) ? true : pageLeft % Kmart.FeatureGallery.pageWidth == 0;
}


Kmart.FeatureGallery.nextPage = function()
{
	var pageLeft = jQuery( Kmart.FeatureGallery.slider ).position().left;
	var boundaryLeft = -( ( Kmart.FeatureGallery.pageCount - 1 ) * Kmart.FeatureGallery.pageWidth );
	if (pageLeft > boundaryLeft && Kmart.FeatureGallery.canSwitchPages) {
		Kmart.FeatureGallery.canSwitchPages = false;
		var left = pageLeft - Kmart.FeatureGallery.pageWidth;
		jQuery(Kmart.FeatureGallery.slider).animate({
			'left': left
		}, 500, null, Kmart.FeatureGallery.onPageTransitionComplete);
	}
	return false;
}


Kmart.FeatureGallery.prevPage = function()
{
	var pageLeft = jQuery( Kmart.FeatureGallery.slider ).position().left;
	if (pageLeft < 0 && Kmart.FeatureGallery.canSwitchPages ) {
		Kmart.FeatureGallery.canSwitchPages = false;
		var left = pageLeft + Kmart.FeatureGallery.pageWidth;
		jQuery(Kmart.FeatureGallery.slider).animate({
			'left': left
		}, 500, null, Kmart.FeatureGallery.onPageTransitionComplete );
	}
	return false;
}


Kmart.FeatureGallery.firstPage = function()
{
	jQuery(Kmart.FeatureGallery.slider).animate({
		'left': 0
	}, 500, null, Kmart.FeatureGallery.updatePagination);
}


Kmart.FeatureGallery.setCurrentFeature = function( element )
{
	Kmart.FeatureGallery.setCurrentFeatureThumb( element );
	Kmart.FeatureGallery.showFeature( element );
}


Kmart.FeatureGallery.setCurrentFeatureThumb = function(element)
{
	jQuery( Kmart.FeatureGallery.thumbs ).removeClass('current');
	jQuery(element).addClass('current');
}


Kmart.FeatureGallery.updateCurrentFeatureCopy = function( copyHTML )
{
	jQuery( Kmart.FeatureGallery.featureCaptionContainer ).html( copyHTML );
}


Kmart.FeatureGallery.showFeature = function( element )
{
	var copyHTML = jQuery(element).find('div.full-copy').eq(0).html();
	var featureContentLink = jQuery(element).find('a.feature-content').eq(0);
	var contentPath = jQuery(featureContentLink).attr('href');
	var contentType = jQuery(featureContentLink).attr('rel')
	if ( contentPath && contentType ) {
		switch ( contentType ) {
			case 'image':
				if(typeof modVP != 'undefined')
					modVP.pause();
				jQuery('#feature-video-player').css('visibility','hidden');
				var imgClickAnchor = jQuery(copyHTML).find('p a');
				var anchor = jQuery(element).find('div.full-copy p a').eq(0).clone();
				var img = document.createElement('img');
				img.setAttribute('src',contentPath.toString().substr(contentPath.toString().indexOf('#')+1));
				jQuery(anchor).html(img);
				jQuery(anchor).css('display','none');
				jQuery(Kmart.FeatureGallery.featureContentContainer).html(anchor);
				// jQuery(anchor).fadeIn('slow');
				jQuery(anchor).show();
				break;
			case 'brightcove':
				var videoId = contentPath.toString().substr(contentPath.toString().indexOf('#')+1);
				jQuery('#feature-video-player').css('visibility','visible');
				modCon.getMediaAsynch(Number(videoId));
				break;
		} 
	}
	Kmart.FeatureGallery.updateCurrentFeatureCopy( copyHTML )
}


Kmart.FeatureGallery.rotate = function()
{
	var currentFeatureThumb = jQuery('#feature-gallery .feature-thumbs .thumb.current');
	var nextFeatureThumb = jQuery( currentFeatureThumb ).next( 'div.thumb' );
	var featurePageRight = jQuery( '#feature-gallery .feature-thumbs .inner' ).eq(0).offset().left + Kmart.FeatureGallery.pageWidth;
	//alert(jQuery(nextFeatureThumb).length);
	if (jQuery(nextFeatureThumb).length) {
		Kmart.FeatureGallery.setCurrentFeature(nextFeatureThumb);
		var featureLeft = jQuery(nextFeatureThumb).offset().left;
		if ( featureLeft >= featurePageRight) {
			Kmart.FeatureGallery.nextPage();
		} else if ( featureLeft < 0 ) {
			Kmart.FeatureGallery.prevPage();
		}
	} else {
		Kmart.FeatureGallery.setCurrentFeature( jQuery('#feature-gallery .feature-thumbs .thumb').eq(0) );
		Kmart.FeatureGallery.firstPage();
	}
}


Kmart.FeatureGallery.highlight = function()
{
	Kmart.FeatureGallery.stopTimer();
	jQuery(Kmart.FeatureGallery.thumbs).removeClass('over');
	jQuery(this).addClass('over');
}


Kmart.FeatureGallery.unhighlight = function()
{
	// Kmart.FeatureGallery.startTimer();
	jQuery(this).removeClass('over');
}


Kmart.FeatureGallery.onFeatureThumbClick = function()
{
	Kmart.FeatureGallery.canRotate = false;
	Kmart.FeatureGallery.stopTimer();
	
	// store selected feature -- most useful for clicks during initial Brightcove load
	Kmart.FeatureGallery.userSelectedFeature = this;
	var gaStub = jQuery(this).find('a.feature-ga-stub').eq(0).attr('href').toString();
	pageTracker._trackPageview(gaStub+'view');
	Kmart.FeatureGallery.setCurrentFeature(this);
}


Kmart.FeatureGallery.preload = function()
{
	var imgs = Array();
	jQuery('#feature-gallery .feature-thumbs a.feature-content').each(
		function(){
			var featureContentLink = jQuery(this);
			var contentType = jQuery(featureContentLink).attr('rel');
			if (contentType && contentType == 'image') {
				var contentPath = jQuery(featureContentLink).attr('href');
				if ( contentPath.indexOf('#') != -1 )
					contentPath = contentPath.substr(contentPath.indexOf('#')+1);
				//alert(contentPath);
				var img = new Image();
				img.src = contentPath;
				imgs.push(img);
			}
		}
	);
}


jQuery( Kmart.FeatureGallery.init() );