/**
* Image SlideShow in JavaScript v1.0
* This code is freelicence.
* Do not remove this commentari.
* 
* Original code version garants IE, FireFox and Opera compatibility
* 
* By Oriol Tendero - oriol@kasualstudios.com
* 05-04-2009
*/

/*** CONFIG ***/
var _waitTime = 5000; //Timewait between images
var _fadeInterval = 5; //How many increase or decrease the opacity of fade in and fade out
var _width = 220;
var _height = 220;
var _sizeUnit = "px";
var _path = "lib_ss/";

/*** GLOBALS ***/
var _act = 0;
var _pre = 0;
var MUTEX_pre = true;
var _alpha = 0;
var _images = new Array();
var _none = _path+"c-none.gif";



function _get(id){
	return document.getElementById(id);
}

function _preLoaded(){
	MUTEX_pre = false;
}

function _preLoad(img){
	_preLoader = new Image();
	_preLoader.onLoad = _preLoaded();
	_preLoader.src = img;
}

/*** Construct carrousel ***/
function _init(images){
	
	_images = images;
	
	
	
	/*** SETTING UP configuration ***/
	_get("c-img").src = _none;
	_get("c-img-back").src = _none;
	
	_get("c-box").style.width = _width+_sizeUnit;
	_get("c-border").style.width = _width+_sizeUnit;
	_get("c-box").style.height = _height+_sizeUnit;
	//_get("c-border").style.height = _height+_sizeUnit;
	
	_get("c-img").style.width = _width+_sizeUnit;
	_get("c-img-back").style.width = _width+_sizeUnit;
	
	
	
	if(a.length >= 2){
		_act = (a.length-1);
		_pre = 0;
		_preLoad(_images[_pre]);
		
		_slideShow(); //Starting slide show
		
	}else if(a.length == 1){
		_get("c-img").src = _images[_act];
		
		//We have only one image, we do NOT slide show
	}else{
		//Load into c-img a default image or non images available or error image
		_get("c-img").src = _none;
			
		//There are no images, and obviously no slide show
		return false;
	}
	return true;
}

function _slideShow(){
	
	if(!MUTEX_pre){ //Preloading DONE, ready to show next image
		_act = _pre;
		_pre++;
		
		if( _pre > (_images.length-1) ){ //If we was on last one
			_pre = 0;
		}
		
		_show(_act);
		MUTEX_pre = true; _preLoad(_images[_pre]);
		
		setTimeout("_slideShow()",_waitTime);
	}else{
		setTimeout("_slideShow()",100); //Wait time spent, waiting for img loading
	}
	
}

function _show(img){
	
	if(_alpha >= 100){ return false; }
	_alpha += _fadeInterval;
	if(_alpha >= 100){ _alpha = 100; }
	
	var elem = _get("c-img");
	if(elem.src != _images[img]){
		elem.src = _images[img]; //Setting image in box
		
		//Center the image
		_centerV(elem);
	}
	
	_setAlpha(elem,_alpha);
	
	if(_alpha < 100){ setTimeout("_show("+img+")",33); }
	else{
		//Setting img to back
		_get("c-img-back").src = _get("c-img").src;
		_get("c-img").src = _none;
		
		//RESET _alpha
		_alpha = 0;
		
		//Center the image, Vertical
		_centerV(_get("c-img-back"));
	}
}

function _centerV(elem){
	if(elem.height < _height){
		elem.style.top = Math.round(((_height-elem.height)/2))+_sizeUnit;
	}else if(elem.height >= _height){
		elem.style.top = "0px";
	}
}

function _setAlpha(elem,alpha){
	var floatA = alpha;
	if(alpha > 0){
		floatA = (alpha/100);
	}
	
	if( elem.style.opacity != undefined ) {
		//FF
		elem.style.opacity = floatA;
	}else{
		//IE
		elem.style.filter = "alpha(opacity="+alpha+")";
	}
}
