function CMImageGalleryShuffler(/*prefix,classnames*/){
	// set the prefix
	this.prefix = 'cmig';
	if(arguments.length > 0) this.prefix = arguments[0];
	// set the names of the button classes
	this.classnames  = {
		'before_clicked' : this.prefix+'_clicked',
		'after_clicked' : this.prefix+'_clicked',
		'before_disabled' : this.prefix+'_disabled',
		'after_disabled' : this.prefix+'_disabled'
	}
	if(arguments.length > 1){
		var cn = arguments[1];
		if(cn.before_clicked) this.classnames.before_clicked = cn.before_clicked;
		if(cn.after_clicked) this.classnames.after_clicked = cn.after_clicked;
		if(cn.before_disabled) this.classnames.before_disabled = cn.before_disabled;
		if(cn.after_disabled) this.classnames.after_disabled = cn.after_disabled;
	}
	
	this.preloaded_file ='';
	this.blending = false;
	this.loading_bg=false;
	this.loading_cancel=false;
	this.autorotate = false;
	this.millisecs = 1000;
	this.autorotateinterval = 3000;
	
	this.images = [];
	this.imageindex = 0;
	
	this.beforebutton = document.getElementById(this.prefix+'_beforebutton'); 
	this.afterbutton = document.getElementById(this.prefix+'_afterbutton');
	
	this.obj = "CMImageGalleryShufflerInstance_" + (++ CMImageGalleryShuffler.instance);
	eval (this.obj + "=this");
}
CMImageGalleryShuffler.instance = 0;

CMImageGalleryShuffler.prototype.add = function(path,caption,link){
	this.images[this.images.length] = new CMImageGalleryShufflerImage(path,caption,link);
}

CMImageGalleryShuffler.prototype.start = function(){
	this.autorotate = true;
	setTimeout(this.obj+".rotate();",this.autorotateinterval);
}

CMImageGalleryShuffler.prototype.rotate = function(){
	if(this.autorotate){
		var next = this.imageindex + 1;
		if(next >= this.images.length) next = 0;
		this.show(this.images[next],false);
		this.imageindex = next;
	}
}

CMImageGalleryShuffler.prototype.next = function(){
	var next = this.imageindex + 1;
	if(next >= this.images.length) next = 0;
	this.show(this.images[next],true);
	this.imageindex = next;
	this.autorotate = false;
}

CMImageGalleryShuffler.prototype.previous = function(){
	var previous = this.imageindex - 1;
	if(previous < 0) previous = this.images.length-1;
	this.show(this.images[previous],true);
	this.imageindex = previous;
	this.autorotate = false;
}

CMImageGalleryShuffler.prototype.show = function(newimage,showload){
	var oldimage = document.getElementById(this.prefix+'_fullview');
	var oldimage_src = oldimage.src;
	if(newimage.path.length<=0 || this.blending==true){
		oldimage.src=newimage.path;
		oldimage.title=newimage.caption;
		return;
	 }
	this.blending = true;
	if(showload) this.showLoading();
	preloaded_file = new Image();
	preloaded_file.onload = CMImageGalleryShufflerBlendStart;
	preloaded_file.slider = this;
	preloaded_file.src = newimage.path; 
	preloaded_file.title = newimage.caption;
	var oldlink = document.getElementById(this.prefix+'_fulllink');
	if(oldlink) oldlink.href = '#';
	var caption = document.getElementById(this.prefix+'_caption');
	if(caption) caption.innerHTML = '';
	var counter = document.getElementById(this.prefix+'_counter');
	if(counter) counter.innerHTML = '...';
	return;	
}

CMImageGalleryShuffler.prototype.showLoading = function(){
	if(this.loading_bg==false){
		var div_obj = document.getElementById(this.prefix+'_Image');
		var overlay = document.getElementById(this.prefix+'_overlay');
		this.loading_bg = setTimeout("document.getElementById('"+this.prefix+"_overlay').style.display = 'block';",200)
	}		
}

CMImageGalleryShuffler.prototype.hideLoading = function(){
	clearTimeout(this.loading_bg);
	this.loading_bg = false;
	var overlay = document.getElementById(this.prefix+'_overlay');
	overlay.style.display = "none";
}

CMImageGalleryShuffler.prototype.blendimage =  function(newimage) {
	var timer = 0;
	var div_holder = document.getElementById(this.prefix+'_Image');
	var oldimage = document.getElementById(this.prefix+'_fullview');
	div_holder.style.backgroundImage = "url(" + oldimage.src + ")";
	changeOpac(0, this.prefix);
	oldimage.src = newimage.src;
	oldimage.title = newimage.title;
	this.blend();
}

CMImageGalleryShuffler.prototype.blend = function(){
	var speed = Math.round(this.millisecs / 100);
	var img = document.getElementById(this.prefix+'_fullview'); 
	var opacity = img.style.opacity * 100;
	if(opacity < 100){
		changeOpac(opacity+1,this.prefix);
		setTimeout(this.obj+".blend()",speed);
	}else{
		if(this.autorotate) setTimeout(this.obj+".rotate();",this.autorotateinterval);
		var oldlink = document.getElementById(this.prefix+'_fulllink');
		if(oldlink) oldlink.href = this.images[this.imageindex].link;
		var caption = document.getElementById(this.prefix+'_caption');
		if(caption) caption.innerHTML = this.images[this.imageindex].caption;
		var counter = document.getElementById(this.prefix+'_counter');
		if(counter) counter.innerHTML = (this.imageindex+1) + ' of ' + this.images.length;
		this.preload();
	}
}

CMImageGalleryShuffler.prototype.preload = function(){
	var preload = new Image();
	var index = this.imageindex + 1;
	if(index >= this.images.length) index = 0;
	preload.src = this.images[index].path;
}

function CMImageGalleryShufflerBlendStart(){
	this.slider.hideLoading();
	this.slider.blendimage(this);
}

function CMImageGalleryShufflerImage(path,caption,link){
	this.path = path;
	this.caption = caption;
	this.link = link==''?'#':link;
}
	
//change the opacity for different browsers
function changeOpac(opacity, id) {
	var Imgobject = document.getElementById(id+'_fullview').style; 
	Imgobject.opacity = (opacity / 100);
	Imgobject.MozOpacity = (opacity/100);
	Imgobject.KhtmlOpacity = (opacity / 100);
	Imgobject.filter = "alpha(opacity=" + opacity + ")";
	if(opacity>85) eval('slider_'+id+'.blending=false;');
}


function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft+=obj.offsetLeft;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.x) {
        curleft+=obj.x;
    }
    return curleft;
}
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop+=obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.y) {
        curtop+=obj.y;
    }
    return curtop;
}
