/*
	JUUCE Preloading Gallery With Fading
*/

//eastOutBounce is Copyright © 2001 Robert Penner All rights reserved - http://www.robertpenner.com/easing_terms_of_use.html
function easeOutBounce(t, b, c, d) {
	if ((t/=d) < (1/2.75)) {
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)) {
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)) {
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
}

var idArray = new Array();
var imageArray = new Array();
var titleArray = new Array();
var descArray = new Array();
var loadedArray = new Array();
var currentID;
var currentIndex;
var opacity = 0;
var switching = 0;
var fadeInAuto = 0;
var fadeOutAuto = 0;
var switchAuto = 0;
var autoSwitch = 0;
var currentLoaded = 0;

function leftSwitchOn() {
	document.getElementById('bigImageLeftOff').id = 'bigImageLeftOn';		
	switchesOn();
	return false;
}
function leftSwitchOff() {
	document.getElementById('bigImageLeftOn').id = 'bigImageLeftOff';
	switchesOn();
	return false;
}
function rightSwitchOn() {
	document.getElementById('bigImageRightOff').id = 'bigImageRightOn';
	switchesOn();
	return false;
}
function rightSwitchOff() {
	document.getElementById('bigImageRightOn').id = 'bigImageRightOff';
	switchesOn();
	return false;
}
function switchBack() {
	currentIndex--;
	if (currentIndex < 0) {
		currentIndex = imageArray.length-1;
	}
	switchImage(currentIndex, true);
	return false;
}
function switchForward() {
	currentIndex++;
	if (currentIndex >= imageArray.length) {
		currentIndex = 0;
	}
	switchImage(currentIndex, true);
	return false;
}

function switchesOn() {
	try {
		document.getElementById('bigImageLeftOff').onclick = switchBack;
		document.getElementById('bigImageRightOff').onclick = switchForward;
	} catch (e) {}
	try {
		//delete document.getElementById('bigImageLeftOff').onmouseout; // not work in IE		
		document.getElementById('bigImageLeftOff').onmouseover = leftSwitchOn;
	} catch (e) {}
	try {
		//delete document.getElementById('bigImageLeftOn').onmouseover; // not work in IE
		document.getElementById('bigImageLeftOn').onmouseout = leftSwitchOff;		
	} catch (e) {}
	try {
		//delete document.getElementById('bigImageRightOff').onmouseout; // not work in IE
		document.getElementById('bigImageRightOff').onmouseover = rightSwitchOn;
	} catch (e) {}
	try {
		//delete document.getElementById('bigImageRightOn').onmouseover; // not work in IE
		document.getElementById('bigImageRightOn').onmouseout = rightSwitchOff;
	} catch (e) {}
}

function addImage(id, src, title) {
	idArray.push(id);
	imageArray.push(src);
	titleArray.push(title);
	loadedArray.push(0);
}

function addDescription(description) {
	descArray.push(description);
}

function setText(str) {
	document.getElementById('bigImageLink').href = str;
}

function setTitle(title) {
	document.getElementById('bigImageLink').title = title;
	document.getElementById('bigImage').alt = title;
}

function switchImage(index, force) {
	if ((opacity <= 0 || force==true) && (currentLoaded == 1)) {
		document.getElementById('bigImage').src = imageArray[index];
		clearTimeout(fadeOutAuto);
		fadeOutAuto = 0;
		switchFadeUp();

		try {
			setText(descArray[index]);
			setTitle(titleArray[index]);
		} catch (Exception) {}

	} else if (opacity <= 0) {
		try {
			setText(descArray[index]);
			setTitle(titleArray[index]);
		} catch (Exception) {}
		document.getElementById('bigImage').src = imageArray[index];
		document.getElementById('bigImage').onload = function () {
			currentLoaded = 1;
		}
		switchAuto = setTimeout('switchImage('+index+', false)', 100);
	} else {
		switchAuto = setTimeout('switchImage('+index+', false)', 100);
	}
}

function setFade(id, amount) {
	if (amount > 100) {
		amount = 100;
	}
	if (amount < 0) {
		amount = 0;
	}
	document.getElementById(id).style.opacity = amount/100;
	document.getElementById(id).style.MozOpacity = amount/100;
	document.getElementById(id).style.KhtmlOpacity = amount/100;
	document.getElementById(id).style.filter = 'alpha(opacity='+amount+')';
	try {
		document.getElementById(id+'Txt').style.opacity = amount/100;
		document.getElementById(id+'Txt').style.MozOpacity = amount/100;
		document.getElementById(id+'Txt').style.KhtmlOpacity = amount/100;
		document.getElementById(id+'Txt').style.filter = 'alpha(opacity='+amount+')';
	} catch (Exception) { }
		
}

function switchFadeOut() {
	/*if (debug==1) {
		alert('switchFadeOut');
	}*/
	if (fadeInAuto == 0) {
		fadeOutAuto = 0;
		clearTimeout(fadeInAuto);
		fadeInAuto = 0;
		if (opacity > 0) {
			opacity -= 8;
			if (opacity < 0) {
				opacity = 0;
			}
			setFade('bigImage', opacity);
			fadeOutAuto = setTimeout('switchFadeOut()', 40);
		} else {
			opacity = 0;
		}
	}
}

function switchFadeUp() {
	fadeInAuto = 0;
	clearTimeout(fadeOutAuto);
	fadeOutAuto = 0;
	if (opacity < 100) {
		opacity += 4;
		if (opacity > 100) {
			opacity = 100;
			switching = 0;
		}
		document.getElementById('bigImage').style.marginTop = easeOutBounce(opacity, -196, 196, 100)+"px";
		setFade('bigImage', 100);
		fadeInAuto = setTimeout('switchFadeUp()', 30);
	} else {
		opacity = 100;
		switching = 0;
	}
}

function selectImage(id) {
	clearTimeout(autoSwitch);
	autoSwitch = 0;
	selectImaged(id);
}

function selectImageAuto() {
	if ((switching == 0) || (opacity == 0)) {
		currentIndex++;
		if (currentIndex >= idArray.length) {
			currentIndex = 0;
		}
		selectImaged(idArray[currentIndex]);
	}
/* Change the value 3500 (3.5 seconds) to increase/decrease time between swap   */	
	autoSwitch = setTimeout('selectImageAuto()', 8000);
}

function selectImaged(id) {
	if ((id != currentID)) {
		clearTimeout(fadeInAuto);
		clearTimeout(fadeOutAuto);
		clearTimeout(switchAuto);
		fadeInAuto = 0;
		fadeOutAuto = 0;
		switchAuto = 0;
		switching = 1;
		currentID = id;
		var index;
		var src = '';
		var loaded = 0;
		for (var i=0; i < idArray.length;i++) {
			if (idArray[i] == id) {
				index = i;
				src = imageArray[i];
				loaded = loadedArray[i];
				if (loadedArray[i] == 0) {
					loadedArray[i] = 1;
				}
			}
		}
		currentLoaded = 0;
		currentIndex = index;
		if (loaded == 0) {
			if (src != '') {
				var image = new Image();
				image.src=src;
			}
		}
		switchImage(index, false);
		if (opacity > 0) {
			switchFadeOut();
		} else {
			switchFadeUp();
		}
	}
}

function pickRandom() {
	currentIndex = Math.floor(Math.random()*idArray.length);
	selectImageAuto();
}

function pickFirst() {
	currentIndex = -1;
	selectImageAuto();
}

////// andrey 2009-04-27
function myMouseEvents(){
	document.getElementById("featureSlide").onmouseout  = sethideclass;
	document.getElementById("featureSlide").onmouseover = removehideclass;
}

function sethideclass(){
	document.getElementById("featureSlide").className = "mouseout";
}

function removehideclass(){
	document.getElementById("featureSlide").className = "";

}
