/*
  slideshow.js
  version 1.0
  
  - changed am, added SLIDESHOW_DELAY_LOCAL

  Adds an automatic slideshow effect on top of the rollover effect.
  Uses the jQuery Timers plugin (http://plugins.jquery.com/project/timers).
*/

var SLIDESHOW_DELAY_LOCAL = '5s'; // either a milliseconds integer or a human-readable string ("2s", "250ms", etc.) - see the plugin doc
var SLIDESHOW_ITERATIONS = 0; // number of times the slideshow will loop (0 for unlimited) - see the plugin doc
var SLIDESHOW_ITEMS = 4; // number of items used for the slideshow

var slideshow_previous_item = -1;
var slideshow_current_item = 0;
var slideshow_running = true;
var mouse_on_main_visual = false;

var slideshow_control = {};
var SPECIAL_EFFECTS_COOKIE_NAME = 'specialEffects';
var expiresDateObj = new Date();

function isdefined( variable){  return (typeof(window[variable]) == "undefined")?  false: true;}


expiresDateObj.setYear( expiresDateObj.getFullYear() + 1 );
slideshow_control.cookieOptions = { path: '/', expires: expiresDateObj };

slideshow_control.useSpecialEffects = function(){
	if(jQuery.url.param("specialEffects") == "off") {
		$.cookie(SPECIAL_EFFECTS_COOKIE_NAME, "off", slideshow_control.cookieOptions);
		return false;
	}
	if(jQuery.url.param("specialEffects") == "on") {
		$.cookie(SPECIAL_EFFECTS_COOKIE_NAME, null, slideshow_control.cookieOptions);
		return true;
	}
	if($.cookie(SPECIAL_EFFECTS_COOKIE_NAME) == "off") {
		return false;
	}
	return true;
}

slideshow_control.pngFixSettings = { blankgif:'/chrome/common/images/blank.gif' };
slideshow_control.pngFix = function(){
	$(".figure").pngFix(slideshow_control.pngFixSettings);
	$(".view").pngFix(slideshow_control.pngFixSettings);
}

function activeTopic() {
	var number =  $('ul.selectionTopics').attr('currentnumber');
	return $('#topPR ul.selectionTopics li[mvnumber='+number+']');
	
}

function isTopicActive(topic) {
	
	return $('ul.selectionTopics').attr('currentnumber') == $(topic).attr('mvnumber');
	
	
}

function setThumbsArrows() {
	$('#topPR ul.selectionTopics li').each(function(i, li) {
		var span = $($(li).find('div.figure span')[0]);
		if (isTopicActive(li)) {
			/*show arrow*/
			span.removeClass("none"); span.addClass("arrow");;
		} else {
			/*hide arrow*/
			span.removeClass("arrow"); span.addClass("none");
		}
	});
	
	slideshow_control.pngFix();
}

// Initialize the crossfade between the main visuals
// Based on code from the Bridgestone global website
function initializeCrossfade() {
	// Initialize the thumbnails position counter
	$("#topPR ul.selectionTopics").attr("currentNumber","0");
	
	$("#topPR ul.selectionTopics li")
		// Initialize the thumbnails position number
		.each(function(myPosition) {
			$(this).attr("mvNumber", myPosition);
		})
		
		// Handle the crossfade on thumbnail click
		.bind( "mouseenter", function(myEv){
			var lastNumber = $(this.parentNode).attr("currentNumber") - 0;
			var targetNumber = $(this).attr("mvNumber") - 0;

			if (lastNumber === targetNumber) return false;

			$("#mainVisual div.view").css("zIndex","1");
			$($("#mainVisual div.view").get(lastNumber)).css("zIndex","2");
			var target = $($("#mainVisual div.view").get(targetNumber))
					.css("display","none")
					.css("zIndex","3");
					
			if(slideshow_control.useSpecialEffects()) {
				target.fadeIn()//Show big images with fadeIn
			} else {
				target.show()//Show big images without fadeIn
			}

			$(this.parentNode).attr("currentNumber",$(this).attr("mvNumber"));
			
			return false;
		});

	var li = $($('#topPR ul.selectionTopics li')[slideshow_current_item]);
	var span = $($(li).find('div.figure span')[0]);
	span.removeClass("none"); span.addClass("arrow");
	slideshow_control.pngFix();
}

$(window).load(function () {
	initializeCrossfade();
	
	/* check id the global value was defined at the page */
	if (isdefined('SLIDESHOW_DELAY')){
		SLIDESHOW_DELAY_LOCAL = SLIDESHOW_DELAY;
		
	}
	
	// Main slideshow loop
	$(document).everyTime(SLIDESHOW_DELAY_LOCAL, function(i) {
		if (slideshow_running && !mouse_on_main_visual && !video_playing) {
			// Next item
			slideshow_previous_item = slideshow_current_item;
			slideshow_current_item += 1;
			if (slideshow_current_item == SLIDESHOW_ITEMS) slideshow_current_item = 0;

			// Trigger thumbnails mouseout
			$($('#topPR ul.selectionTopics li')[slideshow_previous_item]).trigger('mouseleave', true);
			
			// Trigger thumbnails click
			$($('#topPR ul.selectionTopics li')[slideshow_current_item]).trigger('mouseenter', true);
		}
	}, SLIDESHOW_ITERATIONS);

	// Observe the thumbnails click
	$('#topPR ul.selectionTopics li').bind('mouseenter', function(event, triggered_by_slideshow) {
		// Stop playing all videos
		if(parseInt($(this).attr('mvnumber')) != slideshow_current_item) {
			stopVideo("flashplayer03");
		}

		// Stop the slideshow when the user manually hover the thumbnail
		if (!triggered_by_slideshow) {
			slideshow_running = false;
			
			slideshow_previous_item = slideshow_current_item;
			slideshow_current_item = parseInt($(this).attr('mvnumber'));
		}

		// Handle the white arrow on the thumbnails
		setThumbsArrows();
	});

	// Observe the thumbnails mouseout
	$('#topPR ul.selectionTopics li').bind('mouseleave', function(event, triggered_by_slideshow) {
		// Restart the slideshow from the current item when the user moves away from the thumbnail
		if (!triggered_by_slideshow) {
			slideshow_running = true;
		}

		// Keep the white arrow only on the selected thumbnail
		setThumbsArrows();
		
		//return false; // skip the original script's binding
	});

	// Observe the click on the main visual
	$('#topPR #mainVisual, #topPR .selectionTopics').bind('click', function(event, triggered_by_slideshow) {
		// Stop the slideshow
		slideshow_running = false;
		mouse_on_main_visual = true;

		// Play the video
		if (activeTopic().hasClass('video') && !video_playing) {
			playVideo("flashplayer03");
		}
	});

	// Observe the mouseover on the main visual
	$('#topPR #mainVisual, #topPR .selectionTopics').bind('mouseenter', function(event, triggered_by_slideshow) {
		mouse_on_main_visual = true;
	});

	// Observe the mouseout on the main visual
	$('#topPR #mainVisual, #topPR .selectionTopics').bind('mouseleave', function(event, triggered_by_slideshow) {
		mouse_on_main_visual = false;
	});
});

