// =============================================================
//
// Copyright 2005 Alienware Corporation
// Alienware Image rotation script
//
// =============================================================


// These variables are used by the swap function, since it's not called as part of the object
var nSwapCalls = 0;
var ir = null; // the ImageRotator object that calls swap


/**
 * Image Rotator Class - Used to make a handy image rotator with controls on an HTML page.
 * 
 * ImageRotator Constructor:
 * ImageRotator 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 ImageRotator( targetObj, imgs, time ){

	this.arrImages = new Array();
	this.nLoaded = 0;
	this.nCurrent = 0;
	this.nImages = imgs.length;
	this.bEffects = true;
	this.bAddAnchor = true;
	this.bPaused = true;
	this.tmrSwap = null;
	
	if( time < 5 || time == null) 
		this.nWait = 5;
	else
		this.nWait = time;
	
	this.element = getElementObject( targetObj );
	this.element.style.position = "relative";
	this.elStatus = getElementObject( "ir-status" );
	
	for( var i = 0; i < imgs.length; i ++ ){
		this.imagePreLoad( imgs[i] );
	}
	
	if( this.elStatus != null )
		this.elStatus.innerHTML = "Loading Images...";
	
	ir = this;
	//this.startRotating();
}

ImageRotator.prototype.imagePreLoad = function ( image ){
	var objImage = new Image();
	
	objImage.onload = ImageRotator.prototype.onImageLoad;
	objImage.objLoader = this;
	objImage.bLoaded = false;
	this.arrImages.push( objImage );
	
	objImage.src = image[0];
	objImage.strLink = image[1];
	objImage.strAlt = image[2];
}

ImageRotator.prototype.imagesLoaded = function(){
	return this.nLoaded == this.nImages;
}

ImageRotator.prototype.startRotating = function() {
	this.bPaused = false;
	this.tmrSwap = window.setTimeout( ImageRotator.prototype.swap, this.nWait * 1000 );
}

ImageRotator.prototype.swap = function(){
	nSwapCalls++;
	if( ir.nLoaded == ir.nImages ){
		ir.moveForward();
		ir.rotateImages();
		
		ir.tmrSwap = window.setTimeout( ImageRotator.prototype.swap, ir.nWait * 1000 );
	} else {
		window.setTimeout( ImageRotator.prototype.swap, 1 * 1000 );
	}
}

ImageRotator.prototype.moveForward = function(){
	if( this.nCurrent <= this.nImages - 2 )
		this.nCurrent++;
	else
		this.nCurrent = 0;
}

ImageRotator.prototype.moveBackwards = function(){
	if( this.nCurrent > 0 )
		this.nCurrent--;
	else
		this.nCurrent = this.nImages - 1;
}

ImageRotator.prototype.rotateImages = function(){
	if( document.createElement ){
		//document.getElementById( "status" ).innerHTML = "number of children: " + this.element.childNodes.length;
		
		var div = this.createSwapElement()
		this.element.appendChild( div );
		if( this.element.childNodes.length > 2 )
			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;
	}
}

ImageRotator.prototype.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.alt = elImg.title = elAnc.title = this.arrImages[this.nCurrent].strAlt;
	elAnc.href = this.arrImages[this.nCurrent].strLink;
	
	elDiv.style.position = "absolute";
	elDiv.style.top = "0px";
	elDiv.style.left = "0px";
	elDiv.id = "rotate-img" + this.nCurrent;
	
	if( this.bAddAnchor ){
		elAnc.appendChild( elImg );
		elDiv.appendChild( elAnc );
	} else {
		elDiv.appendChild( elImg );
	}
	
	return elDiv;
}

ImageRotator.prototype.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 + ")";
}

ImageRotator.prototype.next = function(){
	if( this.imagesLoaded() ){
		this.pause();
		this.moveForward();
		this.rotateImages();
	}
}

ImageRotator.prototype.previous = function(){
	if( this.imagesLoaded() ){
		this.pause();
		this.moveBackwards();
		this.rotateImages();
	}
}

ImageRotator.prototype.pause = function(){
	if( !this.bPaused ){
		window.clearTimeout( this.tmrSwap );
		this.bPaused = true;
	}
}

ImageRotator.prototype.play = function(){
	if( this.bPaused ){
		window.setTimeout( ImageRotator.prototype.swap, 100 );
		this.bPaused = false;
	}
}

ImageRotator.prototype.goTo = function( img ){
	if( this.imagesLoaded() && --img < this.nImages ){
		this.pause();
		this.nCurrent = img;
		this.rotateImages();
	}
}


// ==========================================
// General Functions
//
function getElementObject( element ){
	if( document.getElementById )
		return document.getElementById( element );
	else if( document.all )
		return document.all[element];
	else 
		return null;
}

function removeElementChildren( element ){
	if( document.getElementById && document.removeChild ){
		var el = document.getElementById( element );
		while( el.hasChildNodes() )
			el.removeChild( el.lastChild );
		
		return true;
	} else if ( document.getElementById && document.innerHTML ) {
		document.getElementById( element ).innerHTML = "";
		
		return true;
	}
	
	return false;
}

if(Array.prototype.push == null){
	Array.prototype.push = function(item) { this[this.length] = item; return this.length; }
}