var CMBlender = function(windowid,mainimageid,contentid,displayid,webroot){
	this.items = [];
	this.container = document.getElementById(windowid);
	this.mainimage = document.getElementById(mainimageid);
	this.maincontent = document.getElementById(contentid);
	this.display = document.getElementById(displayid);
	this.mainlink = this.mainimage.parentNode;
	this.webroot = webroot;
	this.limit = 0;
	this.speed = 1000;
	this.iteration = 0;
	this.timer = null;
	this.loadtimer = null;
	this.blendingtimers = null;
	this.objid = 'Blender_' + CMBlender.Instance++;
	eval(this.objid + '=this;');
}
CMBlender.Instance = 1;
CMBlender.prototype.startAutoAnimation = function(){
	this.blendimage();
}
CMBlender.prototype.startAnimation = function(index){
	var preloaded_file = new Image();
	this.mainimage.onload = ''; // removing onload event handler
	if(this.timer) clearTimeout(this.timer);
	if(this.loadtimer) clearTimeout(this.loadtimer);
	this.cancelBlend();
	
	preloaded_file.onload = eval("new function(){ "+this.objid+".blendimage("+index+"); }");
	preloaded_file.src = this.items[index].image;
	return;
}
CMBlender.prototype.blendimage = function(/*index*/) {
	this.limit = this.items.length - 1;
	var speed = Math.round(this.speed / 100),
	timer = 0,
	previous = this.iteration;
	
	//set the current image as background and change opacity on image to 0
	if(this.back && this.back.parentNode == this.container){
		this.container.removeChild(this.back);
		this.back = null;
	}
	this.back = document.createElement('div');
	var backcontent = this.display.innerHTML.replace(/id=\"[^\"]+\"/gi,'');
	backcontent = backcontent.replace(/onload=.*?6000\);[\'\"]/gi,'');
	this.back.innerHTML = backcontent;
	this.back.className = 'CMExhibitionFeature_display';
	this.back.style.zIndex = 0;
	this.container.insertBefore(this.back,this.display);
	this.changeOpac(0,this.display.id);
	
	
	//calculating current image index
	if(arguments.length){
		this.iteration = arguments[0];
		if(this.iteration > this.limit) this.iteration = this.limit;
	}
	else this.iteration = (++this.iteration)>this.limit? 0:this.iteration;
	
	//setting active thumbnail
	var spans = this.container.getElementsByTagName('span');
	spans[previous].firstChild.style.display = 'none';
	spans[this.iteration].firstChild.style.display = 'block';

	//loading the current image
	this.mainimage.src = this.webroot+this.items[this.iteration].image;
	this.mainlink.href = this.items[this.iteration].url;
	var maincontent = '';
	if(this.items[this.iteration].title!='') maincontent = '<h2>'+this.items[this.iteration].title+'</h2>';
	if(this.items[this.iteration].description!='') maincontent += '<p>'+this.items[this.iteration].description+'</p>';
	if(this.items[this.iteration].url!='') maincontent += '<a class="arrowlink" href="'+this.items[this.iteration].url+'">Read More</a>';
	this.maincontent.innerHTML = maincontent;
	
	//applying the fade-in
	this.blendingtimers = [];
	for(i = 1; i <= 100; i++) {
		this.blendingtimers[i] = setTimeout(this.objid+".changeOpac(" + i + ",'"+this.display.id+"')",(timer*speed));
		timer++;
	}
}	
	//change the opacity for different browsers
CMBlender.prototype.changeOpac = function(opacity, id) {
	var Imgobject = document.getElementById(id).style; 
	Imgobject.opacity = (opacity / 100);
	Imgobject.MozOpacity = (opacity/100);
	Imgobject.KhtmlOpacity = (opacity / 100);
	Imgobject.filter = "alpha(opacity=" + opacity + ")";
	if(opacity==100){
		Imgobject.filter = "";
	}
}	

CMBlender.prototype.cancelBlend = function() {
	if(this.blendingtimers){
		for(i = 1; i <= 100; i++) {
			if(this.blendingtimers[i]) clearTimeout(this.blendingtimers[i]);
		}
	}
	this.blendingtimers = null;
}

CMBlenderItem = function(image,thumb,url,title,description){
	this.image = image;
	this.thumb = thumb;
	this.url = url==''?'#':url;
	this.title = title;
	this.description = description;
}
	
