/*========================================================================================
* Image sequence slideshow with fade and preloader for unknown number of images with
* given suffix and extension
*
* @copyright	www.monogroup.ro
* @author		Cosmin Cimpoi <cosmin dot cimpoi at monogroup dot ro>
*=========================================================================================*/
if (!Mc_Objects) {
	var Mc_Objects = [];
};

function SlideShow(theFieldnamePrefix)
{//--- class declaration start
	this._objectId;
	this.bwCheckObj;
	this.containerObj;
	this.containerElm;
	this.imageElement;
	this.width = 468;
	this.height = 60;
	this.moveX = 0;
	this.moveY = 0
	this.preloaderUrl;
	this.preloaderWidth;
	this.preloaderHeight;
	this._preloaderDepth = 1;
	this.imagesPath = 'img/';
	this.blankImageUrl;
	this.imagesPrefix = '';
	this.imagesFirstNo = 0;
	this.imageNameIndent = false;
	this.imageNameIndentLength = 0;
	this.imagesSuffix;
	this.delayTime = 2;	
	this.fadeTime = 2;
	this.fadeDuration;
	this.fadeResolution;
	this.delayIntervalId;
	this.fadeIntervalId;
	this._fadeCount = 1;
	this._display = 2;
	this.imageObjectsArray;
	this._currentImageId;
	this._newImageSrc;
	this._firstImageSrc;
	this._visibleImageId = 0;
	this._maxImageId = null;
	this.depth_images = 100;
	this.stopFetching;
	this._imagesNumber = null;

	this.init = function(containerDivId)
	{
		this.startShow();
	}
	
	this.render = function(tagId)
	{
		this._containerId = 'co' + tagId;
		this._preloaderContainerId = 'preco' + tagId;
		this._preloaderImageId = 'preimg' + tagId;
		this._imageContainerId = 'imgco' + tagId;		
		this._imageId = 'img' + tagId;

		var divWidth = this.width;
		var divHeight = this.height;
		var preloaderOffsetX = Math.round((this.width - this.preloaderWidth)/2);
		var preloaderOffsetY = Math.round((this.height - this.preloaderHeight)/2);
		var outHtmlCode = '';
		var out = new Array();
		var outI = 0;
		
		out[outI++] = '\n<div id="' + this._containerId + '"';
		if (this.styleContainerClass) {
			out[outI++] = ' class="' + this.styleContainerClass + '"';
		}
		out[outI++] = ' style="position:relative';
		out[outI++] = '">';
		out[outI++] = '\n\t<div id="' + this._preloaderContainerId + '"';
		out[outI++] = ' style="position:absolute; left:' + preloaderOffsetX + 'px; top:' + preloaderOffsetY + 'px; z-index:' + this._preloaderDepth + '">';
		out[outI++] = '\n\t\t<img id="' + this._preloaderImageId + '" src="'+ this.preloaderUrl +'" width="' + this.preloaderWidth + '" height="' + this.preloaderHeight + '" border="0">'		
		out[outI++] = '\n\t</div>';
		out[outI++] = '\n\t<div id="' + this._imageContainerId + '"';
		out[outI++] = ' style="position:absolute; left:' + this.moveX + '; top:' + this.moveY + ';';
		out[outI++] = '">';
		out[outI++] = '\n\t</div>';
		out[outI++] = '\n</div>';
		outHtmlCode = out.join('');
		document.getElementById(tagId).innerHTML = outHtmlCode;

		this._containerObj = new makeObj(this.bwCheckObj, this._containerId);
		this.__preloaderContainerObj = getDivFromName(this._preloaderContainerId);
		this._preloaderImageObj = getDivFromName(this._preloaderImageId);
		this._imageContainerObj =  getDivFromName(this._imageContainerId);
		
		this.imageObjectsArray = new Array();
		for(var i=0; i<2; i++){
			newImage = createImage('img_'+i, this.depth_images + i, 0, 0, divWidth, divHeight, this._imageContainerObj);
			newImage.src = this.blankImageUrl;
			newImage.onload = this.onLoadingSuccess;
			newImage.onabort = this.onLoadingError;
			newImage.onerror = this.onLoadingError;	
			newImage.__parent = this;
			this.imageObjectsArray.push(newImage);
		}
		this._opacityType = getOpacityType(newImage);
	}
	
	this.drawInto = function(tagId)
	{
		this.render(tagId);
//		ShowSource();
	}
	
	this._constructor = function(theFieldnamePrefix)
	{
		this.bwCheckObj = lib_bwcheck();
		initCrossBrowserLib();
		this._id = Mc_Objects.length;
		Mc_Objects[this._id] = this;
		this._objectId = "Mc_SlideShow_"+this._id;
		this.objectName = this._objectId;
		if (typeof(theFieldnamePrefix) == 'string'){
			this.fieldName = theFieldnamePrefix + '_value';
			this.objectName = theFieldnamePrefix;
		}		
	}
	
	this.setImage = function(newImagePath)
	{
		this._imageObj.src = newImagePath;
	}

	this.startShow = function()
	{
		this._currentImageId = 0;
		this.loadNextImage();
	}
	
	this.setTimeoutForLoadNextImage = function()
	{
		var loadNextImageRef = 'Mc_Objects[' + this._id +'].loadNextImage()';
		window.setTimeout(loadNextImageRef,this.delayTime *1000);
	}
	
	this.loadNextImage = function()
	{	
//		alert(this._currentImageId);
		if(this._currentImageId == 1){setDivVisibility(this.__preloaderContainerObj, false)}
		if(this.imageNameIndent){
			imageName = stringIndentZeros((this.imagesFirstNo + this._currentImageId), this.imageNameIndentLength);
		}else{
			imageName = this.imagesFirstNo + this._currentImageId;
		}		
		this._newImageSrc = this.imagesPath + this.imagesPrefix + imageName + this.imagesSuffix;		
		if(this._currentImageId == 0){this._firstImageSrc = this._newImageSrc}		
		this._visibleImageId = 1 - this._visibleImageId;
		this.swapVisibleImage(this._visibleImageId);
		this._currentImageId ++;
		if((this._maxImageId != null) && (this._currentImageId > this._maxImageId -1)){
			this._currentImageId = 0;
		}
	}
	
	this.resetSrc = function()
	{
		this._currentImageId = 0;		
		this.imageObjectsArray[1 - this._visibleImageId].src = this._firstImageSrc;
	}
	
	this.setMaxImageId = function()
	{
		this._maxImageId = this._currentImageId - 1;
	}
	
	this.onLoadingSuccess = function()
	{
		if(this.src.indexOf(this.__parent.blankImageUrl) == -1){
			this.__parent.setTimeoutForLoadNextImage();
		}
	}
	
	this.onLoadingError = function()
	{
		if(this.src.indexOf(this.__parent.blankImageUrl) == -1){
			if(this.__parent._maxImageId == null){
				this.__parent.setMaxImageId();
				this.__parent.resetSrc();
			}
		}
	}
	
	this.swapVisibleImage = function()
	{
		this._fadeCount = 1;
		this.fadeDuration = parseInt(this.fadeTime, 10) * 1000;
		this.fadeResolution = parseInt(this.fadeTime, 10) * 50;
		var fadeImgRef = 'Mc_Objects[' + this._id +'].fadeImages()';
		this.fadeIntervalId = window.setInterval(fadeImgRef, this.fadeDuration/this.fadeResolution);
	}
	
	this.fadeImages = function()
	{
		var image_in = this.imageObjectsArray[this._visibleImageId];
		var image_out = this.imageObjectsArray[1- this._visibleImageId];
		setDivZIndex(image_in, this.depth_images+1);
		setDivZIndex(image_out, this.depth_images);
	
		this._fadeCount -= (1 / this.fadeResolution);
		if(this._fadeCount  < (1 / this.fadeResolution))
		{
//			alert(this._fadeCount +'\n'+ (1 / this.fadeResolution));
			image_out.src = this._newImageSrc;
			this.setImageObjDisplay(image_out, 1);
			clearInterval(this.fadeIntervalId);
		}
		
		if(this._fadeCount == (1 - (1 / this.fadeResolution)))
		{
			this.setImageObjDisplay(image_in, 2);
		}
		
		switch(this._opacityType)
		{
		case 'ie' :
			image_in.filters.alpha.opacity = (1 - this._fadeCount) * 100;
			break;
			
		case 'khtml' :
			image_in.style.KhtmlOpacity = (1 - this._fadeCount);
			break;
			
		case 'moz' : 
			image_in.style.MozOpacity = (1 - this._fadeCount);
			break;
			
		default : 
			image_in.style.opacity = (1 - this._fadeCount);
		}		
	}
	
	this.setImageObjDisplay = function(imageObj, display)
	{
		if (imageObj) {
			switch (display) {
				case 0:
					imageObj.style.display = 'none';
					break;
				case 1:
					imageObj.style.visibility = 'hidden';
					break;
				case 2:
					imageObj.style.visibility = 'visible';
					imageObj.style.display = 'block';
					break;
				default:
			}
		}
	}
	
	this.setDisplay = function(display)
	{
		this._display = display;
		if (this._imageContainerObj) {
			switch (display) {
				case 0:
					this._imageContainerObj.style.display = 'none';
					break;
				case 1:
					this._imageContainerObj.style.visibility = 'hidden';
					break;
				case 2:
					this._imageContainerObj.style.visibility = 'visible';
					this._imageContainerObj.style.display = 'block';
					break;
				default:
			}
		}
	}
	
	this.moveElement = function(newX, newY)
	{
		moveDivTo()
		if(newX != null){
			this.__preloaderContainerObj.style.left = newX;
		}
		if(newY != null){
			this.__preloaderContainerObj.style.top = newY;
		}
	}	
	this._constructor(theFieldnamePrefix);
}//--- class declaration end

function lib_bwcheck()
{
	this.ver=navigator.appVersion;
	this.agent=navigator.userAgent;
	this.dom=(document.getElementById) ? 1 : 0;
	this.opera5=(navigator.userAgent.indexOf("Opera")>-1 && document.getElementById)?1:0;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6;
	this.mac=this.agent.indexOf("Mac")>-1;
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5);
	bs_px_char = (this.ns4 || window.opera) ? "" : "px";
	return this;
}

function makeObj(bwCheckObj, obj, nest){
	nest=(!nest) ? "":'document.'+nest+'.';
	this.el=bwCheckObj.dom?document.getElementById(obj):bwCheckObj.ie4?document.all[obj]:bwCheckObj.ns4?eval(nest+'document.'+obj):0;
	this.css= (bwCheckObj.dom) ? document.getElementById(obj).style : (bwCheckObj.ie4) ? document.all[obj].style : (bwCheckObj.ns4) ? eval(nest+'document.'+obj) : 0;
	this.scrollHeight=bwCheckObj.ns4?this.css.document.height:this.el.offsetHeight;
	this.clipHeight=bwCheckObj.ns4?this.css.clip.height:this.el.offsetHeight;
	this.x=0;
	this.y=0;
	this.obj = obj + "Object";
	eval(this.obj + "=this");
	return this;		
}
