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){
	this.images[this.images.length] = path;
}

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.length<=0 || this.blending==true){
		oldimage.src=newimage;
		return;
	 }
	this.blending = true;
	if(showload) this.showLoading();
	preloaded_file = new Image();
	preloaded_file.onload = CMImageGalleryShufflerBlendStart;
	preloaded_file.slider = this;
	preloaded_file.src = newimage; 
	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;
	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);
	}
}

function CMImageGalleryShufflerBlendStart(){
	this.slider.hideLoading();
	this.slider.blendimage(this);
	if(this.slider.autorotate) setTimeout(this.slider.obj+".rotate();",this.slider.autorotateinterval);
}
	
//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;
}
