//Javascript used for INCO Web and Go, aided by Mootools Library.
//See mootools-release-1.11.js for more information

function slideShowHandler(id,imagesList){

	//Preload the images!
	if(document.images){
		var iList2 = new Object;
		var i = 0;
		for(i=0; i<=imagesList.length; i++) {
			iList2["i"+i] = new Image;
			iList2["i"+i].src = imagesList[i];
		}
	}

	this.id					= id;
	this.imgid				= $(this.id).getElementsBySelector("img")[0];
	this.imagesList			= imagesList;
	this.sliderota_count	= 1;
	this.sliderota_timer	= null;

	$(this.id).setStyles({
		"background-image"	: "url("+this.imgid.getProperty("src")+")",
		"height"			: this.imgid.getProperty("height")+"px",
		"width"				: this.imgid.getProperty("width")+"px"
	});
	this.imgid.setOpacity(0);			

	this.launch = function(milsec) {
		this.tabrota_timer = this.doRotate.periodical(milsec,this);
	}

	this.doRotate = function(){
		this.sliderota_count += 1;		
		var ni		= this.imagesList[this.sliderota_count-1];	
		var fx		= new Fx.Styles(this.imgid, {duration:2000, wait:false});
		var id		= this.id;
		var imgid	= this.imgid;

		this.imgid.setProperty("src",ni);
		fx.start({
			"opacity" : 1
		}).addEvent("onComplete",function(){
				$(id).setStyle("background-image","url("+imgid.getProperty("src")+")");
				imgid.setOpacity(0);
		});
		if(this.sliderota_count == this.imagesList.length){
			this.sliderota_count=0;
		}			
	}
}

//Makes sure that any images with a width of over 340px are surrounded with a DIV tag
//that has "overflow:hidden" property so that the size can shrink in size without
//images overlapping it.
//
// @containerDIV	string	Id of element (start it with a # to identify it as id).
//										   eg  #tpl_page
function fixImageWidths(containerDIV){
	if(containerDIV){
		var list = $$(containerDIV+" img");
		var imgbord = new Element("div",{
			"styles" : {
				"overflow" : "hidden",
				"width" : "auto"
			}
		});
		list.each(function(element) {

			if(element.getProperty("width") > 340){
				imgbord2 = imgbord.clone();

				if(element.hasClass("floatright")){
					imgbord2.addClass("floatright");
					element.removeClass("floatright");
				}

				if(element.hasClass("floatleft")){
					imgbord2.addClass("floatleft");
					element.removeClass("floatleft");
				}

				imgbord2.injectBefore(element);
				element.injectInside(imgbord2);
			}
		});
	}
}