// GLOBAL VARS (Sorry!!)

var currentThumbIndex = 0;
var numThumbs = 0;
if(!window.slider) 
	var slider={};

slider.data = [
	{"id":"slide-img-1","client":"Biowallet","desc":"Splash Screen"},
	{"id":"slide-img-2","client":"Biowallet","desc":"Signature"},
	{"id":"slide-img-3","client":"Biowallet","desc":"Menu"},
	{"id":"slide-img-4","client":"Biowallet","desc":"Explorer"},
	{"id":"slide-img-5","client":"Biowallet","desc":"Search"}
];

$(document).ready(function(){
    
    // Initialize phone slider
	initializePhoneSlider ();
	
	// Initialize numeric slider control
	initializeNumericSlider();
	
	//I have no words...
	hackSexyButtonsForIE();
  });


/**************************************************************************************************************************/
function initializePhoneSlider () {
	$(".btn-slide").click(function(){
		$("#thumb_list").slideToggle("slow");
		return false;
	});
}

function initializeNumericSlider () {
	$(".numeric_control").mouseover(function(){
      var rel = this.rel;
      changeNumericSlidePosition (rel);
      showAssociatedText (rel);
      gotoThumb(rel);
    });
    
    //Showing text of first slide and assigning it as current one
	var divToShow = $("#stages .stage:eq(0)");
		
	// Showing the new one and assigning current
	$(divToShow).css("display","block");
	$(divToShow).addClass("current");

	// Setting first link in numeric slider as current one
	var firstLink = $("#numeric_controls .numeric_control:first");
	$(firstLink).addClass("current");
	//console.debug (firstLink);
}

function changeNumericSlidePosition (which) {
	var offset = 38;
	var blockOffset = which * offset;
	$(".block").stop().animate({"left": blockOffset + "px"}, "slow");
}


function showAssociatedText (which) {

	// Checking if incoming is equal than current. If yes, do nothing.
	var currentLink = $("#numeric_controls .current");
	var currentElement = $(currentLink).attr("rel");
	
	if (currentElement != which) {
		// Taking the element to hide (that is the previous shown)
		var divToHide = $("#stages .stage.current");
		$(currentLink).addClass("current");
		// If exists, hiding the previous shown div and deassigning current class
		if (divToHide.html()!=null) {
			$(divToHide).css("display", "none");
			$(divToHide).removeClass("current");
			$(currentLink).removeClass("current");
		}
		
		// Taking the new element to show
		var divToShow = $("#stages .stage:eq("+which+")");
			
		// Showing the new one and assigning current
		$(divToShow).stop(false,true).fadeIn("slow");
		$(divToShow).addClass("current");
		
		var newCurrenLink = $("#numeric_controls .numeric_control:eq("+which+")");
		$(newCurrenLink).addClass("current");
	}
}
/**************************************************************************************************************************/
/**************************************************************************************************************************/
/**************************************************************************************************************************/
/**************************************************************************************************************************/
/**************************************************************************************************************************/

function gotoThumb (index) {/*Simulate click on slide toolbar for changing image*/$('#slide-nav a:eq('+index+')').click();}
function gotoFirst() {gotoThumb(0)}
function gotoPrevious() {gotoThumb(getPreviousElement());}
function gotoNext() {gotoThumb(getNextElement());}
function gotoLast() {gotoThumb(numThumbs - 1);}
function activateFirstThumb () {
 	var thumbToActivate = $('#thumb_list ul li:eq(0)');
 	thumbToActivate.addClass('active').stop().animate ({
 		opacity:'1.0',
 		top:'-20px'
 	},250);
}
function getThumbIndex (element) {
	var imageSrc = $(element).find('img').attr('src');
	var imageSrcLength = imageSrc.length;
	var imageIndex = imageSrc.substring(imageSrcLength-6,imageSrcLength-4);
	var thumbIndex = imageIndex - 1;
	return thumbIndex;
}
function getPreviousElement () {
	var previousElement = 0;
	if (currentThumbIndex != 0) {
		previousElement = currentThumbIndex - 1;
	} else {
		previousElement = 	 - 1;
	}
	return previousElement;
}
function getNextElement () {
	var nextElement = 0;
	if (currentThumbIndex != numThumbs - 1) {
		nextElement = currentThumbIndex + 1;
	} 
	return nextElement;
}
function play_pause() {
	if (slider.ar == true) {
		slider.ar = false;
	} else {
		slider.ar = true;
		setTimeout("gotoNext()",200);
	}
}
function updateThumb (pos) {
 	var activeThumb = $('.active');
 	activeThumb.removeClass('active').stop().animate ({
 		opacity:'0.5',
 		top:0
 	},250)
 	
 	var thumbToActivate = $('#thumb_list ul li:eq('+pos+')');
 	thumbToActivate.addClass('active').stop().animate ({
 		opacity:'1.0',
 		top:'-20px'
 	},250);

	// Updating current thumb variable
	currentThumbIndex = pos;
}
/**************************************************************************************************************************/


/******************************************************************************************
* This function allows button element to download the parent link associated file 
	(for making this method tottaly unobstrusive include a href="no_script.html" in link)
*******************************************************************************************/
function hackSexyButtonsForIE () {
	if ($.browser.msie) {
		$('.productButtons a, #pre-download_link a').each(function () {
		
			var button_url = $(this).attr('href');
			var concrete_button = $(this).find('button');
			// Set onclick event 
			 $(concrete_button).click(function (e){
			 	e.preventDefault();  //stop the browser from following
				window.location.href = button_url;
			 });
		 });
	}
}
