/**
 * slideshow.js
 *
 * @author		Jonathan Pitre
 * @description	This file contains the various JavaScript functions which control the slideshow
 *				of the www.builditsoftware.com home page. For modifications, scroll down to the
 *				section labelled "Modify ONLY this section".
 * @changelog
 * --------------------------------------------------------------------------------------------------
 * Jonathan Pitre		13/06/2011	Final release of script.
 * --------------------------------------------------------------------------------------------------
 */

// DO NOT MODIFY THESE CONSTANTS
var CONST_TABLE_HEAD = "<table><tr><td>";
var CONST_TABLE_MIDDLE = "</td></tr><tr style=\"text-align: right;\"><td>";
var CONST_TABLE_END = "</td></tr></table>";
var CONST_IMGLOC = "../../img/";

// ==============================================================================
// Modify ONLY this section
// ==============================================================================

// Set the time between two slide changes (milliseconds)
var CONST_SPEED = 7500;

// Duration of crossfade in IE (seconds)
var crossFadeDuration = 3;

/*
 * Specify the images to use here
 */
var Pic = new Array();
Pic[0] = 'eurofighter_banner_merged_2.jpg';
Pic[1] = 'banner2.jpg';
Pic[2] = 'rotatingmenu311.png';
Pic[3] = '2.jpg';

/*
 * Specify the descriptions here
 *
 * Descriptions should have the following format:
 * Descriptions[X] = CONST_TABLE_HEAD + '<div class=shadow>__Your text goes here__</div>' + CONST_TABLE_MIDDLE
 * 					+ '<a href="#">__Link text goes here__<img src=\"img/slide_link.png\" style=\"position: relative; top: 1px; border: 0; width:11px; height:11px;\"></a>' + CONST_TABLE_END;
 * Copy-paste the above two lines for convenience.
 */
var Descriptions = new Array();
Descriptions[0] = CONST_TABLE_HEAD + '<div class=shadow>Reduce Inspection Time and Increase Process Reliability</div>' + CONST_TABLE_MIDDLE
					+ '<a href="en/buildit_features.html">Learn More <img src=\"../../img/slide_link.png\" style=\"position: relative; top: 1px; border: 0; width:11px; height:11px;\"></a>' + CONST_TABLE_END;
Descriptions[1] = CONST_TABLE_HEAD + '<div class=shadow>Multi-CAD Support<br>Multi-Device Capability</div>' + CONST_TABLE_MIDDLE
					+ '<a href="en/buildit_deviceinterfaces.html">Learn More <img src=\"../../img/slide_link.png\" style=\"position: relative; top: 1px; border: 0; width:11px; height:11px;\"></a>' + CONST_TABLE_END;
Descriptions[2] = CONST_TABLE_HEAD + '<div class=shadow style=\"color:#FFFFFF;\">More Than <br/>130 New Features in Build!IT<sup>&reg;</sup> 3.1.1</div>' + CONST_TABLE_MIDDLE
					+ '<a href="en/buildit_latestversion.html">Available Now <img src=\"../../img/slide_link.png\" style=\"position: relative; top: 1px; border: 0; width:11px; height:11px;\">' + CONST_TABLE_END;
Descriptions[3] = CONST_TABLE_HEAD + '<div class=shadow>A Powerful<br>and Versatile Software Platform</div>' + CONST_TABLE_MIDDLE
					+ '<a href="en/industries.html">Learn More <img src=\"../../img/slide_link.png\" style=\"position: relative; top: 1px; border: 0; width:11px; height:11px;\">' + CONST_TABLE_END;

// ==============================================================================
// DO NOT MODIFY ANYTHING BELOW THIS LINE
// ==============================================================================

// Constants for the location of pause buttons
var CONST_PAUSE_LOC = "../../img/slide_pause_trans.png";
var CONST_PAUSE_OVR_LOC = "../../img/slide_pause_ovr_trans.png";
var CONST_PLAY_LOC = "../../img/slide_play.png";
var CONST_PLAY_OVR_LOC = "../../img/slide_play_ovr_trans.png";
var pause_status = 0; /* 0 - paused; 1 - paused_ovr; 2 - play; 3 - play_ovr; */

var slideShowSpeed = CONST_SPEED;

/*
 * Pre-loader & timeout control
 */
var t;
var curr = 0;
var next = 0;
var p = Pic.length;
var q = Descriptions.length;

// DEBUG LINES
if (p != q){
	alert("ERROR: Slides and descriptions do not match.");
}
if (CONST_SPEED < 0){
	alert("ERROR: Speed between slide changes cannot be negative.");
}

var preLoad = new Array();
for (i = 0; i < p; i++){
   preLoad[i] = new Image();
   preLoad[i].src = CONST_IMGLOC + Pic[i];
}

var preLoadDesc = new Array();
for (i = 0; i < q; i++){
   preLoadDesc[i] = Descriptions[i];
}

/*
 * Updates the slide shown and, if necessary, sets up a timeout.
 */
function runSlideShow(){

   if (document.all){
	  document.images.SlideShow.style.filter="blendTrans(duration=2)";
	  document.images.SlideShow.style.filter="blendTrans(duration=crossFadeDuration)";
	  document.images.SlideShow.filters.blendTrans.Apply();
   }
   deactivateButton(curr+1);
   curr = next;
   document.images.SlideShow.src = preLoad[curr].src;
   document.getElementById("slideshowText").innerHTML = preLoadDesc[curr];
   activateButton(curr+1);
   if (document.all){
	  document.images.SlideShow.filters.blendTrans.Play();
   }
   if (slideShowSpeed > 0){
		next = next + 1;
		if (next > (p-1)) next=0;
		t = setTimeout('runSlideShow()', slideShowSpeed);
		if (slideShowSpeed != CONST_SPEED) slideShowSpeed = CONST_SPEED;
	}
}

/*
 * Changes the slide shown on the slideshow to the slide specified by the given ID.
 * 
 * @param slideNum - The number of the slide we wish to show. It is associated with the button ID.
 */
function changeToSlide(slideNum){
	if (curr == slideNum - 1) return;
	deactivateButton(curr+1);
	next = slideNum - 1;
	activateButton(slideNum);
	clearTimeout(t);
	slideShowSpeed = 2*CONST_SPEED;
	runSlideShow();
}

/*
 * Activates a button specified by the given ID.
 *
 * @param buttonNum - The ID number (character) of the button we wish to activate.
 */
function activateButton(buttonNum){
	if (buttonNum == 'X'){
		var button = document.getElementById("slideshow_button_" + buttonNum);
		button.src = CONST_PLAY_OVR_LOC;
		pause_status = 3;
	} else {
		var button = document.getElementById("slideshow_button_" + buttonNum);
		button.className = "active";
	}
}

/*
 * Changes the pause button when the mouse hovers over it.
 */
function pauseHover(){
	var button = document.getElementById("slideshow_button_X");
	if (pause_status == 0) {
		button.src = CONST_PAUSE_OVR_LOC;
		pause_status = 1;
	} else if (pause_status == 1) {
		button.src = CONST_PAUSE_LOC;
		pause_status = 0;
	} else if (pause_status == 2) {
		button.src = CONST_PLAY_OVR_LOC;
		pause_status = 3;
	} else if (pause_status == 3) {
		button.src = CONST_PLAY_LOC;
		pause_status = 2;
	}
}

/*
 * Deactivates a button specified by the given ID.
 * 
 * @param buttonNum - The ID number (character) of the button we wish to deactivate.
 */
function deactivateButton(buttonNum){
	if (buttonNum == 'X'){
		var button = document.getElementById("slideshow_button_" + buttonNum);
		button.src = CONST_PAUSE_OVR_LOC;
		pause_status = 1;
	} else {
		var button = document.getElementById("slideshow_button_" + buttonNum);
		button.className = "inactive";
	}
}

/*
 * Toggles the button for enabling or disabling timeout.
 */
function toggleTimeout(){
	if (slideShowSpeed > 0){
		clearTimeout(t);
		slideShowSpeed = 0;
		activateButton("X");
	}
	else {
		slideShowSpeed = CONST_SPEED;
		next = curr;
		deactivateButton("X");
		runSlideShow();
	}
}
