
var EstateWeb_Objects_GalleryManager_ObjectList = new EstateWeb_Objects_CollectionObject();

function EstateWeb_Objects_GalleryManager(){
	this.StartIndex = -1; // the index of the image to display first
	this.Photos = new Array(); // the array of image urls to display
	this.PhotoDisplayTime = 5; // time in seconds for each photo to display
	this.ImageTagID = ""; // the id of the image tag on the page to update
	this.TimerID = null; // variable to hold interval object when playing
	this.FadeDuration = 3; // time in seconds you want to fade between images (IE only)
	this.Fading = true; // whether to fade the photos
	this.OnTogglePlay = ""; // variable which holds function to execute when gallery is played / paused
	this.OnInit = ""; // variable which holds the function to execute when init is called
}

// Methods

EstateWeb_Objects_GalleryManager.prototype.Next = EstateWeb_Objects_GalleryManager_Next;
EstateWeb_Objects_GalleryManager.prototype.CurrentIndex = EstateWeb_Objects_GalleryManager_CurrentIndex;
EstateWeb_Objects_GalleryManager.prototype.Play = EstateWeb_Objects_GalleryManager_Play;
EstateWeb_Objects_GalleryManager.prototype.Stop = EstateWeb_Objects_GalleryManager_Stop;
EstateWeb_Objects_GalleryManager.prototype.TogglePlay = EstateWeb_Objects_GalleryManager_TogglePlay;
EstateWeb_Objects_GalleryManager.prototype.Previous = EstateWeb_Objects_GalleryManager_Previous;
EstateWeb_Objects_GalleryManager.prototype.isFirstPhotoLoaded = EstateWeb_Objects_GalleryManager_isFirstPhotoLoaded;


// Events
EstateWeb_Objects_GalleryManager.prototype.Init = EstateWeb_Objects_GalleryManager_OnInit;
EstateWeb_Objects_GalleryManager.prototype.BeforePhotoChange = EstateWeb_Objects_GalleryManager_OnBeforePhotoChange;
EstateWeb_Objects_GalleryManager.prototype.AfterPhotoChange = EstateWeb_Objects_GalleryManager_OnAfterPhotoChange;

//
function EstateWeb_Objects_GalleryManager_OnInit(){
	// IE Specific
	if ( HttpManager.Browser.Type() == HttpManager.Browser.Types.InternetExplorer5Plus ){
		if ( this.Fading ){
			HttpManager.Document.GetObject(this.ImageTagID).style.filter = "blendTrans(duration="+this.FadeDuration+")";
		}
	}
	if ( this.OnInit.length ){
		eval(this.OnInit+"()");
	}
	HttpManager.Document.GetObject(this.ImageTagID).removeAttribute("height");
	HttpManager.Document.GetObject(this.ImageTagID).removeAttribute("width");
	//replace all &amp; in the photos
	for ( var i = 0; i < this.Photos.length; i ++ ){
		this.Photos[i] = this.Photos[i].replace(/&amp;/g, "&");
	}
	this.Play();
}

function EstateWeb_Objects_GalleryManager_OnBeforePhotoChange(){
	// IE Specific
	if ( HttpManager.Browser.Type() == HttpManager.Browser.Types.InternetExplorer5Plus ){
		if ( this.Fading ){
			HttpManager.Document.GetObject(this.ImageTagID).filters.blendTrans.apply();
		}
	}
}

function EstateWeb_Objects_GalleryManager_OnAfterPhotoChange(){
	// IE Specific
	if ( HttpManager.Browser.Type() == HttpManager.Browser.Types.InternetExplorer5Plus ){
		if ( this.Fading ){
			HttpManager.Document.GetObject(this.ImageTagID).filters.blendTrans.play();
		}
	}
}

function EstateWeb_Objects_GalleryManager_TogglePlay(){
	// stops the gallery from rotating if playing, otherwise starts
	if ( this.TimerID ){
		this.Stop();
		if ( this.OnTogglePlay.length > 0 && !arguments[0] ){
			eval(this.OnTogglePlay+"(true)");
		}
	}else{
		this.Play();
		if ( this.OnTogglePlay.length > 0 && !arguments[0] ){
			eval(this.OnTogglePlay+"(false)");
		}
	}
}

function EstateWeb_Objects_GalleryManager_Stop(){
	clearTimeout(this.TimerID);
	this.TimerID = null;
}

function EstateWeb_Objects_GalleryManager_Play(){
	if ( arguments[0] ){
		var oGallery = arguments[0];
	}else{
		var oGallery = this;
	}
	EstateWeb_Objects_GalleryManager_ObjectList.Add(oGallery.ImageTagID, oGallery);
	EstateWeb_Objects_GalleryManager_Next(oGallery.ImageTagID);
}

function EstateWeb_Objects_GalleryManager_Next(){
	if (arguments[0]){
		var oGallery = EstateWeb_Objects_GalleryManager_ObjectList.Item(arguments[0]);
	}else{
		var oGallery = this;
	}

	if ( oGallery ){
		clearTimeout(oGallery.TimerID);
		var iNextPhoto = oGallery.CurrentIndex()+1;
		iNextPhoto = ( iNextPhoto > (oGallery.Photos.length - 1) ? iNextPhoto = 0: iNextPhoto );
		var isfirstphoto = oGallery.isFirstPhotoLoaded();
		if ( !isfirstphoto ){
			oGallery.BeforePhotoChange();
		}
		HttpManager.Document.GetObject(oGallery.ImageTagID).src = oGallery.Photos[iNextPhoto];
		if ( !isfirstphoto ){
			oGallery.AfterPhotoChange();
		}
		EstateWeb_Objects_GalleryManager_ObjectList.Add(oGallery.ImageTagID, oGallery);
		oGallery.TimerID = setTimeout("EstateWeb_Objects_GalleryManager_Next('"+oGallery.ImageTagID+"')", oGallery.PhotoDisplayTime*1000);
	}

}

function EstateWeb_Objects_GalleryManager_Previous(){
	if (arguments[0]){
		var oGallery = EstateWeb_Objects_GalleryManager_ObjectList.Item(arguments[0]);
	}else{
		var oGallery = this;
	}

	if ( oGallery ){
		clearTimeout(oGallery.TimerID);
		var iNextPhoto = oGallery.CurrentIndex()-1;
		iNextPhoto = ( iNextPhoto < 0 ? iNextPhoto = oGallery.Photos.length - 1: iNextPhoto );
		oGallery.BeforePhotoChange();
		HttpManager.Document.GetObject(oGallery.ImageTagID).src = oGallery.Photos[iNextPhoto];
		oGallery.AfterPhotoChange();
		EstateWeb_Objects_GalleryManager_ObjectList.Add(oGallery.ImageTagID, oGallery);
		oGallery.TimerID = setTimeout("EstateWeb_Objects_GalleryManager_Next('"+oGallery.ImageTagID+"')", oGallery.PhotoDisplayTime*1000);
	}
}

function EstateWeb_Objects_GalleryManager_CurrentIndex(){
	for ( var i = 0; i < this.Photos.length; i ++ ){
		if ( this.Photos[i].toString() == HttpManager.Document.GetObject(this.ImageTagID).src ){
			return i;
		}
	}
	return this.StartIndex;
}

function EstateWeb_Objects_GalleryManager_isFirstPhotoLoaded(){
	for ( var i = 0; i < this.Photos.length; i ++ ){
		if ( this.Photos[i].toString() == HttpManager.Document.GetObject(this.ImageTagID).src ){
			return false;
		}
	}
	return true;
}
