function GalleryWidgetImg(title, url, address, city, state, price, bedsbaths, mls, detailPage)
{
	this.Title = title;
	this.URL = url;
	this.Address = address;
	this.City = city;
	this.State = state;
	this.Price = price;
	this.BedsBaths = bedsbaths;
	this.MLS = mls;
	this.DetailPage = detailPage;
}

function GalleryWidget(guid)
{
	this.GUID = guid;
	this.pointer = -1;
	this.ticker=-1;
	this.timerID=-1;
	this.tempInternalTimerID=-1;
	this.transition1;
	this.transition2;
	
	//setting properties
	GalleryConfiguration(this);
	
	//Imgs holds all of the images
	this.Imgs = new Array();
	
	this.AddImg =function (title, url, address, city, state, price, bedsbaths, mls, detailPage)
	{
		var img = new GalleryWidgetImg(title, url, address, city, state, price, bedsbaths, mls, detailPage);
		img.ID = this.Imgs.length;
		this.Imgs[this.Imgs.length] = img;
	}
	this.ShowPrevious = function ()
	{
		this.pointer--;
		if (this.pointer < 0 )
			this.pointer = this.Imgs.length - 1 ;
		this.ShowImg();
	}
	this.ShowNext = function()
	{
		this.pointer++;
		if (this.pointer >= this.Imgs.length)
			this.pointer = 0;
		this.ShowImg();
	}
	
	this.ShowImg = function()
	{
		window.clearTimeout(this.tempInternalTimerID);
		if(this.transition1)
			this.transition1.stop();
		if(this.transition2)
			this.transition2.stop();
			
		this.DisplayImgCell.src = this.Imgs[this.pointer].URL;
		this.DivImg.setOpacity(1); 
		this.DivImgDupe.setOpacity(0); 
		this.ShowDescription();
	}
	
	this.ShowNextSlide = function()
	{
		this.pointer++;
		this.ticker++;
		
		if (this.pointer >= this.Imgs.length)
			this.pointer = 0;
		
		//window.clearInterval(timerID);

		var showDuration1= (this.ImgShowTime)/2;
		var showDuration2=this.ImgShowTime;
		
				
		var nextPointer = this.pointer+1;
		if (nextPointer >= this.Imgs.length)
			nextPointer = 0;
		var imgUrl = this.Imgs[nextPointer].URL;
		
		var altImg;
		var showImgDiv;
		
		if(this.ticker%2==0)
		{
			altImg = this.DisplayImgDupeCell;
			showImgDiv =this.DivImg;
		}
		else
		{
			altImg = this.DisplayImgCell;			
			showImgDiv=this.DivImgDupe;
		}
		
		this.transition1 = showImgDiv.effect('opacity', {duration: showDuration1, transition: Fx.Transitions.Quart.easeIn});
		this.transition1.start(0,1);
		
		var curObj=this;
		this.tempInternalTimerID = setTimeout(function(){
			curObj.transition2 = showImgDiv.effect('opacity', {duration: showDuration2, transition: Fx.Transitions.Quart.easeIn});
			curObj.transition2.start(1,0);
			curObj.ShowDescription();
			altImg.src = imgUrl;
		}, showDuration1);
		
		
		
	}
	
	this.ShowDescription = function()
	{
		this.DisplayImgTitle.innerHTML = this.Imgs[this.pointer].Title;
		this.DisplayNumber.innerHTML = (this.pointer + 1) + "/" + this.Imgs.length;
		this.ListingAddress.innerHTML = this.Imgs[this.pointer].Address;
		this.ListingCity.innerHTML = this.Imgs[this.pointer].City;
		this.ListingState.innerHTML = this.Imgs[this.pointer].State;
		
		if(this.ListingBedsBaths)
		{
			this.ListingBedsBaths.innerHTML = this.Imgs[this.pointer].BedsBaths;
			this.ListingMLS.innerHTML = "";
			this.ListingPrice.innerHTML = "";
			if(this.Imgs[this.pointer].BedsBaths == "" && this.HideBlanks == "Y")
				this.ListingBedsBaths.innerHTML = this.Imgs[this.pointer].MLS;
			else
				this.ListingMLS.innerHTML = this.Imgs[this.pointer].MLS;
			if((this.Imgs[this.pointer].BedsBaths == "") && (this.Imgs[this.pointer].MLS == "") && this.HideBlanks == "Y")
				this.ListingBedsBaths.innerHTML = this.Imgs[this.pointer].Price;
			else if(this.Imgs[this.pointer].MLS == "" && this.HideBlanks == "Y")
				this.ListingMLS.innerHTML = this.Imgs[this.pointer].Price;
			else
				this.ListingPrice.innerHTML = this.Imgs[this.pointer].Price;
		}
		this.HiddenURL.value = this.Imgs[this.pointer].DetailPage;
	}
	
	this.ShowFirst =function()
	{
		this.pointer = 0;
		this.ShowImg();
	}
	this.ShowLast = function()
	{
		this.pointer = this.Imgs.length - 1;
		this.ShowImg();
	}
	
	
	this.MovePointer=function(step)
	{
		this.pointer=this.pointer+step;
		if (this.pointer >= this.Imgs.length)
			this.pointer = 0;
	}
	
	this.ResetPointer=function()
	{
		this.pointer=-1;
		this.ticker=-1;
	}
	
	this.InitializeImage=function()
	{
		try
		{
		this.ShowFirst();
		//this.DivImgDupe.setOpacity(0);
		this.DisplayImgDupeCell.src = this.Imgs[1].URL;
		}catch(e){}
	}	
}

function GetWidgetGalleryObject(evt)
{
	if(!evt)
		return null;
	var el =(new Event(evt)).target;
	var parent = $(el).getParent();
	while(parent && !parent.id.test("^GalleryWidget_"))
	{
		parent = parent.getParent();
	}
	try
	{
		return WidgetGalleryObjects[parent.getProperty('WidgetID')];
	}
	catch(ex)
	{
		return null;
	}
}

var WidgetGalleryObjects =new Object();

//client use: event handlers
function WidgetGalleryShowImg(action, evt, wgObj)
{
	if(!wgObj)
	{
		if(!evt)
			evt=window.event;
		var wgObj = GetWidgetGalleryObject(evt);
		if(!wgObj)
			return;
	}
	
	try
	{
		window.clearInterval(wgObj.timerID);
	}
	catch(e){}
	
	switch(action)
	{
		case "-1":
			wgObj.ShowPrevious();
			break;
		case "+1":
			wgObj.ShowNext();
			break;
		case "first":
			wgObj.ShowFirst();
			break;
		case "last":
			wgObj.ShowLast();
			break;
		case "play":	
			if(wgObj.Imgs.length>1)
			{
				wgObj.ShowFirst();
				wgObj.ResetPointer();
				wgObj.ShowNextSlide();
				wgObj.timerID = window.setInterval(function(){wgObj.ShowNextSlide();}, wgObj.ImgShowTime);
			}
			break;
		case "stop":
			wgObj.ShowImg();
			break;
	}
}


function GalleryConfiguration(wg)
{
	var divWidget = $("GalleryWidget_"+wg.GUID);
	//wg.DisplayDiv = divWidget.getElement("[id=GW_SlideShowBlock]");
	wg.DisplayNumber = divWidget.getElement("[id=GW_SlideShowTotalCaption]");
	wg.DisplayImgTitle = divWidget.getElement("[id=GW_SlideShowImgTitle]");
	
	wg.DisplayImgCell = divWidget.getElement("img[id=GW_SlideShowImg]");
	wg.DisplayImgDupeCell = divWidget.getElement("img[id=GW_SlideShowImgDupe]");
	wg.DivImg=divWidget.getElement("div[id=GW_divImg]");
	wg.DivImgDupe=divWidget.getElement("div[id=GW_divImgDupe]");
	
	wg.DisplayBlockControls = divWidget.getElement("[id=GW_SlideShowBlockControls]");
	wg.ListingAddress = divWidget.getElement("[id=GW_listingAddress]");
	wg.ListingBedsBaths = divWidget.getElement("[id=GW_listingBedsBaths]");
	wg.ListingCity = divWidget.getElement("[id=GW_listingCity]");
	wg.ListingState = divWidget.getElement("[id=GW_listingState]");
	wg.ListingPrice = divWidget.getElement("[id=GW_listingPrice]");
	wg.ListingMLS = divWidget.getElement("[id=GW_listingMLS]");
	wg.HiddenURL = divWidget.getElement("[id=GW_hiddenURL]");
}

function GalleryWidgetBlock_OnLoad(evt) {
	//debugger;
	for(guid in WidgetGalleryObjects)
	{
		wgObj=WidgetGalleryObjects[guid];
		ShowGalleryWidgetBlockControls(wgObj,false);
		wgObj.InitializeImage();
		WidgetGalleryShowImg('play', evt, wgObj);	
	}
}

function GalleryWidgetBlock_OnMouseEnter(evt) {
	if(!evt)
		evt=window.event;
	var wgObj = GetWidgetGalleryObject(evt);
	if(wgObj)
		ShowGalleryWidgetBlockControls(wgObj, true);	
}
function GalleryWidgetBlock_OnMouseLeave(evt) {
	if(!evt)
		evt=window.event;
	var wgObj = GetWidgetGalleryObject(evt);
	if(wgObj)
		ShowGalleryWidgetBlockControls(wgObj, false);	
}
function ShowGalleryWidgetBlockControls(wgObj, bShow) {
	var hElem = wgObj.DisplayBlockControls;
	if( hElem )
		hElem.style.display = (bShow ? '' : 'none');
	hElem = wgObj.DisplayNumber;
	if( hElem )
		hElem.style.display = (bShow ? '' : 'none');
	hElem = wgObj.DisplayImgTitle;
	if( hElem )
		hElem.style.display = (bShow ? '' : 'none');		
}
function getGalleryWidgetListingDetail(evt) {
	if(!evt)
		evt=window.event;
	var wgObj = GetWidgetGalleryObject(evt);
	//window.location="default.aspx?pp=-1&l=" + wgObj.HiddenURL.value;
	ClientIboxView("default.aspx?c=t&pp=-1&l=" + wgObj.HiddenURL.value, "", {height:600, width:900});	
}
if( window.addEvent ) 
{
  // this function is defined in script/LPCommon.js included from CommonHead.inc
  // if CommonHead is overwritten, make sure script/LPCommon.js is included!
  addEvent(window,'load',GalleryWidgetBlock_OnLoad,false);
}

function ClientIboxView(url,title, param) {
    if(param==null)
    param={height:500, width:900};
        var htm = "<iframe id=\"tFrame\" style=\" width:99%; height:99%\"  src=\""+url+"\"></iframe>"  
        iBox.show(htm, title, param);
    }

