﻿// =============================================================
//
// Copyright 2006 Alienware Corporation
// Alienware Image Gallery Script V3.5
//
// =============================================================

/**
 * Image Gallery
 * 
 * ImageGallery Constructor:
 * ImageGallery requires three to make: the id of the html element were the rotator will add images,
 * a 2 dimension array with the information for the images, and the time interval for 
 *
 */
 
function ImageGallery( target, imgs, time )
{

	this.arrImages = new Array();
	this.nLoaded = 0;
	this.nCurrent = 0;
	this.nImages = imgs.length;
	this.nWait = ( time < 5 || time == null) ? 5 : time;
	this.bEffects = true;
	this.bAddAnchor = true;
	this.bPaused = true;
	this.tmrSwap = null;	
	this.trmCheckImg = 0;
	
	this.element = $( target );
	//this.element.style.position = "relative";
	
	this.elStatus = $( "ir-status" );
	
	this.imgLoading = new Image();

	this.imgLoading.src = "Themes/Default/Images/ajax-loader.gif";
	
	for( var i = 0; i < imgs.length; i ++ )
	{
		this.imagePreLoad( imgs[i] );
	};
	
	if( this.elStatus != null )
	{
		this.elStatus.innerHTML = "Loading Images...";
	};
	
	this.nSwapCalls = 0;
};

ImageGallery.prototype = 
{
	imagePreLoad : function ( image )
	{
		var objImage = new Image();
		
		objImage.onload = ImageGallery.prototype.onImageLoad;
		objImage.objLoader = this;
		objImage.bLoaded = false;
		this.arrImages.push( objImage );
		
		objImage.src = image[0];
		objImage.strLink = image[1];
		objImage.strAlt = image[2];
	},
	
	imagesLoaded : function ()
	{
		return this.nLoaded == this.nImages;
	},
	
	startRotating : function () {
		this.bPaused = false;
		this.tmrSwap = window.setTimeout( ImageGallery.prototype.swap, this.nWait * 1000, this );
	},
	
	swap : function ( ig ){
		ig.nSwapCalls++;
		if( ig.nLoaded == ig.nImages ){
			ig.moveForward();
			ig.rotateImages();
			
			ig.tmrSwap = window.setTimeout( ImageGallery.prototype.swap, ig.nWait * 1000, ig );
		} else {
			ig.tmrSwap = window.setTimeout( ImageGallery.prototype.swap, 1 * 1000, ig );
		}
	},
	
	moveForward : function (){
		if( this.nCurrent <= this.nImages - 2 )
		{
			this.nCurrent++;
		}
		else
		{
			this.nCurrent = 0;
		}
	},
	
	moveBackwards : function ()
	{
		if( this.nCurrent > 0 )
		{
			this.nCurrent--;
		}
		else
		{
			this.nCurrent = this.nImages - 1;
		}
	},
	
	rotateImages : function ()
	{
		if( document.createElement )
		{
			var div = this.createSwapElement();
			this.element.appendChild( div );
			
			if( this.element.childNodes.length > 1 )
				this.element.removeChild( this.element.firstChild );
			
			if( this.bEffects ) 
			{
				new Effect.Appear( div.id );
			};
		}
		else if ( document.innerHTML )
		{
			var strImg = 	"<div id=\"\">" +
							"<a href=\"" + this.arrImages[this.nCurrent].href + 
							"\" title=\"" + this.arrImages[this.nCurrent].alt + "\">" +
							"<img src=\""  + this.arrImages[this.nCurrent].src +"\"" +
							" style=\"position:absolute;top:0px;left:0px;\" / ></a></div>";
			this.element.innerHTML = strImg;
		}
	},
	
	createSwapElement : function ()
	{
		var elDiv = document.createElement( "DIV" );
		var elAnc = document.createElement( "A" );
		var elImg = document.createElement( "IMG" );
		
		elImg.src = this.arrImages[this.nCurrent].src;
		elImg.border = 0;
		elImg.height = this.arrImages[this.nCurrent].height;
		elImg.width = this.arrImages[this.nCurrent].width;
		elImg.alt = elImg.title = elAnc.title = this.arrImages[this.nCurrent].strAlt;
		elAnc.href = this.arrImages[this.nCurrent].strLink;
		
		if( this.bEffects )
		{
			elDiv.style.display = "none";
		}
		elDiv.style.position = "absolute";
		var p = utilObj.getElementPosition(this.element);
		elDiv.style.top = p.Y + "px";
		elDiv.style.left = p.X + "px";
		elDiv.style.width = this.arrImages[this.nCurrent].width + "px";
		elDiv.style.height = this.arrImages[this.nCurrent].height + "px";
		elDiv.id = "rotate-img" + this.nCurrent;
		
		if( this.bAddAnchor )
		{
			elAnc.appendChild( elImg );
			elDiv.appendChild( elAnc );
		}
		else
		{
			elDiv.appendChild( elImg );
		}
		
		return elDiv;
	},
	
	onImageLoad : function ()
	{
		this.bLoaded = true;
		this.objLoader.nLoaded++;
		
		if( this.objLoader.imagesLoaded() && this.objLoader.elStatus != null )
		{
			this.objLoader.elStatus.innerHTML = "Images are Loaded";
		}
		else if( this.objLoader.elStatus != null )
		{
			this.objLoader.elSatuts.innerHTML = "Loading Images... (" + this.objLoader.nLoaded + " of " + this.objLoader.nImages + ")";
		}
	},
	
	next : function ()
	{
		if( this.imagesLoaded() )
		{
			this.pause();
			this.moveForward();
			this.rotateImages();
		};
	},
	
	previous : function ()
	{
		if( this.imagesLoaded() )
		{
			this.pause();
			this.moveBackwards();
			this.rotateImages();
		}
	},
	
	pause : function ()
	{
		if( !this.bPaused )
		{
			window.clearTimeout( this.tmrSwap );
			this.bPaused = true;
		};
	},
	
	play : function ()
	{
		if( this.bPaused )
		{
			window.setTimeout( ImageGallery.prototype.swap, 100 );
			this.bPaused = false;
		};
	},
	
	goTo : function ( img )
	{
		if( --img < this.nImages && img != this.nCurrent )
		{
			if( this.arrImages[img].bLoaded )
			{
				window.clearTimeout( this.trmCheckImg );
				this.hideLoader();
				this.pause();
				this.nCurrent = img;
				this.rotateImages();
			}
			else
			{
				this.showLoader();
				
				this.nNextimg = img;
				this.trmCheckImg = window.setTimeout( ImageGallery.prototype.checkImg, 500, this );
			};
		};
	},
	
	checkImg : function ( ig )
	{
		try
		{
			if( ig.elStatus != null )
			{
				ig.elStatus.innerHTML = "function checkIMG for: " + ig.nNextimg + " " + calls;
			};
			if( ig.arrImages[ig.nNextimg].bLoaded )
			{
				ig.pause();
				ig.hideLoader();
				ig.nCurrent = ig.nNextimg;
				ig.rotateImages();
				
				ig.tmrCheckImg = -1;
				return;
			};
			
			ig.trmCheckImg = window.setTimeout( ImageGallery.prototype.checkImg, 500, this );
		}
		catch (e) { }
	},
	
	showLoader : function ()
	{
		var loadbar = $( "loading-gif" );
	
		if( loadbar == null )
		{
			if( document.createElement )
			{
				loadbar = document.createElement( "IMG" );		
				loadbar.id = "loading-gif";
				loadbar.style.position = "absolute";
				loadbar.style.top = "15px";
				loadbar.style.left = "15px";
				loadbar.style.zIndex = "1000";
				loadbar.src = this.imgLoading.src;
				this.element.appendChild( loadbar );
			};
		};
		
		loadbar.style.display = "inline";
	},
	
	hideLoader : function ()
	{
		var loadbar = $( "loading-gif" );
		
		if( loadbar != null )
		{
			loadbar.style.display = "none";
		};
	}
};