var photoslide_count;
var photoslide_cur;
var photoslide_pos;
var photoslide_timer;
var photoslide_timeout;
var stopAfter;

$(document).ready(function() { 
	photoslide_count	= $('.photoslide div.elem').length;
	photoslide_cur		= 1;
	photoslide_pos		= [0,-960,-1920,-2880,-3840];
	
	if (!photoslide_timer) {
		photoslide_timer = [6000, 6000, 6000, 6000, 6000];
		//var photoslide_timer = [30000, 6000, 10000, 6000, 6000];
	}
	stopAfter = false;
	
	if (photoslide_count > 1) {
		// bind click events
		hideFlashFeature();
		showFlashFeature(1);
		$('.btn-show-last:not(.btn-disabled), .btn-show-next:not(.btn-disabled)').one('click', photoSlider);
		photoslide_timeout	= setTimeout("photoSlideShow()", photoslide_timer[photoslide_cur-1]);
	}
	
	//slideshow deeplinking
	$.address.change(function(event) {
    	if(event.value != "/"){
    		var y = event.value;
    		var key = y.substr(1, 5);
    		//show slide by slide number passed as variable "slide" + slide number. (ex. 'slide5')
    		if(key == 'slide'){
    			var x = y.substr(6);
    			if(x > photoslide_count || x <= 0){
	    			$('.navtray .item1').trigger('click');
	    		}else{
	    			var itemNum = '.navtray .item'+x;
	    			$(itemNum).trigger('click');
	    		}
    		}
    		//find slide by slide class instead of slide number (ex. 'teamNBInspiration')
    		//the creative for this hub needs to be wrapped in a div and have the class assigned to it
    		else{ 
    			var x = y.substr(1);
    			var classToShow = '.photoslide .'+x;
    			var pos = $('.elem').index($(classToShow).parent('.elem'));
    			x = pos+1;
    			if(x > photoslide_count || x <= 0){
	    			$('.navtray .item1').trigger('click');
	    		}else{
	    			var itemNum = '.navtray .item'+x;
	    			$(itemNum).trigger('click');
	    		}
    		}
    		
		}
    });
	
});

$(function() {
	// change navtray text on hover
	var $navtray = $('.navtray');
	$navtray.find('li a').hover(function() {
		$navtray.find('.display_txt').html($(this).attr('rel'));
	});

	// shift image on click
	$navtray.find('li a').live('click', function() {
		// clear the existing timeout
		clearTimeout(photoslide_timeout);
		// remove previously selected
		$navtray.find('li a').removeClass('current');
		// update current element
		var $this = $(this);
		$this.addClass('current');
		photoslide_cur = $this.text();

		hideFlashFeature(photoslide_cur);
		// shift the photoslide
		var animateTo = photoslide_pos[photoslide_cur-1];
		$('.photoslide .photos').animate({ marginLeft: animateTo }, 750, null, function() {
			// update the last and next button states
			photoslideBtnState();
			// update the click handler
			$('.btn-show-last:not(.btn-disabled), .btn-show-next:not(.btn-disabled)').one('click', photoSlider);
			showFlashFeature(photoslide_cur);
		});
		return false;
	});
	$('.photoslide .elem a, .photoslide .elem map, .photoslide .elem object').click(function(){
		trackHubClick();
	});
});

function trackHubClick(){
	var sAjax=s_gi(s_account);
	sAjax.events='event36';
	sAjax.prop34=$('.navtray li .item'+photoslide_cur).attr("rel");//creative
	sAjax.prop35=photoslide_cur;//position
	sAjax.prop36=PAGE_TITLE;//current page
	sAjax.prop37=PAGE_TITLE+"> "+$('.navtray li .item'+photoslide_cur).attr("rel")+" ("+photoslide_cur+")";//everything
	sAjax.linkTrackVars='events,prop34,prop35,prop36,prop37';
	sAjax.linkTrackEvents='event36';
	sAjax.tl(this,'o','Hub Page Click');
}

// slideshow
function photoSlideShow() {
	// clear the existing timeout
	clearTimeout(photoslide_timeout);

	// get the current element
	var $current = $('.navtray li a.current');
	if ($current.length == 0) {
		$current = $('.navtray li:first a');
	}

	// determine the next element
	var count = parseInt($current.text());
	count = ((count + 1) > photoslide_count) ? 1 : count + 1;
	photoslide_cur = count;

	// shift to the next element
	var $next = $('.navtray li .item'+count);
	$current.removeClass('current');
	$next.addClass('current');

	// update the text
	$('.navtray .display_txt').html($next.attr('rel'));
	// update the image
	
	hideFlashFeature(photoslide_cur);
	var animateTo = photoslide_pos[photoslide_cur-1] + 'px';
	$('.photoslide .photos').animate({ marginLeft: animateTo }, 750, null, function() {
		// update the last and next button states
		photoslideBtnState();
		// update the click handler
		$('.btn-show-last:not(.btn-disabled), .btn-show-next:not(.btn-disabled)').one('click', photoSlider);
		showFlashFeature(photoslide_cur);
	});

	// set the next slide timeout
	//if (photoslide_cur != photoslide_count) {
		if(photoslide_cur == photoslide_count){
			//photoslide_cur = 0;
			photoslide_timeout = setTimeout("photoSlideShow()", photoslide_timer[0]);
			stopAfter = true;
		}else{
			if (stopAfter == false) {
				photoslide_timeout = setTimeout("photoSlideShow()", photoslide_timer[photoslide_cur - 1]);
			}	
		}
		
	//}
}

function photoSlider() {
	// unbind the auto slide
	clearTimeout(photoslide_timeout);
	//make sure number is an integer
	photoslide_cur = parseInt(photoslide_cur);
	// show the next
	if ($(this).hasClass('btn-show-next')) {
		if (photoslide_cur + 1 <= photoslide_count) {
			photoslide_cur += 1;
			var animateTo = photoslide_pos[photoslide_cur-1];
		} else {
			$('.btn-show-last:not(.btn-disabled), .btn-show-next:not(.btn-disabled)').one('click', photoSlider);
			return false;
		}
	} else {
		// show the previous image
		if (photoslide_cur - 1 >= 1) {
			photoslide_cur -= 1;
			var animateTo = photoslide_pos[photoslide_cur-1] + 'px';
		} else {
			$('.btn-show-last:not(.btn-disabled), .btn-show-next:not(.btn-disabled)').one('click', photoSlider);
			return false;
		}
	}

	// update the last and next button states
	photoslideBtnState();

	// get current element
	$elem = $('.navtray a.item'+photoslide_cur);

	// update text and count
	$('.navtray .display_txt').html($elem.attr('rel'));
	$('.navtray li a').removeClass('current');
	$elem.addClass('current');

	hideFlashFeature(photoslide_cur);
	// slide to photo
	$('.photoslide .photos').animate({ marginLeft: animateTo }, 750, null, function() {
		// update the click handler
		$('.btn-show-last:not(.btn-disabled), .btn-show-next:not(.btn-disabled)').one('click', photoSlider);
		showFlashFeature(photoslide_cur);
	});

	return false;
}

function stopPhotoSlider(){
	clearTimeout(photoslide_timeout);
}

function photoslideBtnState() {
	if (photoslide_cur == 1) {
		$('.btn-show-last').addClass('btn-disabled');
	} else {
		$('.btn-show-last').removeClass('btn-disabled');
	}

	if (photoslide_cur == photoslide_count) {
		$('.btn-show-next').addClass('btn-disabled');
	} else {
		$('.btn-show-next').removeClass('btn-disabled');
	}
}

// ITEMSLIDER
var itemslide_count 	= $('.itemslide .col-233').length;
var itemslide_elem_w 	= 242; // width plus margin
var itemslide_cur 		= 1;

if (itemslide_count > 4) {
	$('.btn-show-newer, .btn-show-older').click(function() { return false; });
	$('.btn-show-newer:not(.btn-disabled), .btn-show-older:not(.btn-disabled)').one('click', itemSlider);
}

function itemSlider() {
	// store the parent element
	var $elem = $('.itemslide');

	// show older (slide left)
	if ($(this).hasClass('btn-show-older')) {
		// not yet on last element
		if (itemslide_cur < itemslide_count) {
			// see if we can fully shift by 4
			if ((itemslide_cur + 7) < itemslide_count) {
				// shift by 4
				itemslide_cur += 4;
				var animateTo = -((itemslide_cur - 1) * itemslide_elem_w) + 'px';
				// enable state of button
				$('.btn-show-newer').removeClass('btn-disabled');
			} else {
				// partial shift to the end
				itemslide_cur = itemslide_count - 3;
				var shift = (itemslide_cur + 4 == itemslide_count) ? 9 : 0;
				var animateTo = -(((itemslide_cur - 1) * itemslide_elem_w) - shift) + 'px';
				// disable state of button
				$('.btn-show-older').addClass('btn-disabled');
			}
		} else {
			$('.btn-show-newer:not(.btn-disabled), .btn-show-older:not(.btn-disabled)').one('click', itemSlider);
			return false;
		}
	}
	// show newer (slide right)
	else {
		// not yet on first element
		if (itemslide_cur > 1) {
			// see if we can fully shift by 4
			if ((itemslide_cur - 4) > 1) {
				itemslide_cur -= 4;
				var animateTo = -((itemslide_cur - 1) * itemslide_elem_w) + 'px';
				// enable state of button
				$('.btn-show-newer').removeClass('btn-disabled');
			} else {
				// partial shift to beginning
				itemslide_cur = 1;
				var animateTo = '0px';
				// disable state of button
				$('.btn-show-newer').addClass('btn-disabled');
			}

		} else {
			$('.btn-show-newer:not(.btn-disabled), .btn-show-older:not(.btn-disabled)').one('click', itemSlider);
			return false;
		}
	}

	// enable buttons if necessary
	if (itemslide_cur > 1) $('.btn-show-newer').removeClass('btn-disabled');
	if ((itemslide_cur + 4) < itemslide_count) $('.btn-show-older').removeClass('btn-disabled');

	// animate
	//hideFlashFeature(itemslide_cur);
	
	$elem.animate({ marginLeft: animateTo }, 750, null, function() {
		$('.btn-show-newer:not(.btn-disabled), .btn-show-older:not(.btn-disabled)').one('click', itemSlider);
		//showFlashFeature(itemslide_cur);
	});

	return false;
}

function hideFlashFeature(itemslide_cur){
	$('.flash_feature').hide();
}

function showFlashFeature(cur){
	$('.photoSlide'+cur+' .flash_feature').show();
	//$('.photoSlide'+cur+' .flash_feature').css('display', 'none').css('display', 'block');
}

