function RBannerWidgetImg(iUrl)
{
	this.URL = iUrl;
}

function RBannerWidget(guid)
{
	this.GUID = guid;
	this.pointer = -1;
	this.divSwitchOrder=-1;
	this.timerID=-1;
	this.tempInternalTimerID=-1;
	this.transition1;
	this.transition2;
	this.lastPictCookieName = null;
	this.bannerStartUp = "Sequential"; // default, app sets it.
	
	//setting properties
	RBannerConfiguration(this);
	
	//Imgs holds all of the images
	this.Imgs = new Array();
	
	this.AddImg =function (iUrl)
	{
		var img = new RBannerWidgetImg(iUrl);
		img.ID = this.Imgs.length;
		this.Imgs[this.Imgs.length] = img;
	}

	this.ShowNextSlide = function(useEffects)
	{
		/*if(this.bannerStartUp == "Random")
			this.pointer=Math.floor(Math.random()*this.Imgs.length);
		else	*/	
			this.pointer++;
		
		this.divSwitchOrder = (this.divSwitchOrder+1)%2;
		
		if (this.pointer >= this.Imgs.length)
			this.pointer = 0;
		
		if(this.bannerStartUp == "Sequential" && this.lastPictCookieName!=null)
			setCookie(this.lastPictCookieName,this.pointer,1);
		
		var showDuration1= (this.ImgShowTime)/2;
		var showDuration2= this.ImgShowTime;
		
				
		var nextPointer = this.pointer+1;
		/*if(this.bannerStartUp == "Random")
			nextPointer=Math.floor(Math.random()*this.Imgs.length);
		else	*/	
			if (nextPointer >= this.Imgs.length)
				nextPointer = 0;

		var imgUrl = null;
		if(nextPointer>=0 && nextPointer<this.Imgs.length)				
			imgUrl = this.Imgs[nextPointer].URL;
		
		var altImg = null;
		var showImgDiv = null;
		var altImgDiv = null;
		
		if(this.divSwitchOrder==0)
		{
			altImg = this.DisplayImgDupeCell;
			altImgDiv = this.DivImgDupe;
			showImgDiv =this.DivImg;
		}
		else
		{
			altImg = this.DisplayImgCell;			
			altImgDiv = this.DivImg;
			showImgDiv=this.DivImgDupe;
		}
 
		if(useEffects)
		{
			showImgDiv.effect('opacity', {duration: 1000, transition: Fx.Transitions.Quart.easeIn}).start(0,1);
			altImgDiv.effect('opacity', {duration: 1000, transition: Fx.Transitions.Quart.easeIn}).start(1,0).chain(
				function() {
				altImg.src = imgUrl;
				}
			);
		} else {
			showImgDiv.setStyle('opacity', 1);			
			altImgDiv.setStyle('opacity', 0);
			altImg.src = imgUrl;
		}
	}
		
	this.ShowFirst =function()
	{
		if(this.bannerStartUp == "Sequential")
		{
			var lastPict= null;
			if(this.lastPictCookieName!=null) 
				lastPict = getCookie(this.lastPictCookieName);
				
			if (lastPict==null) 			
				this.pointer = -1;
			else 
				this.pointer = lastPict-1;
		}
		/*else if(this.bannerStartUp == "Random")
			this.pointer=Math.floor(Math.random()*this.Imgs.length);*/
		else
			this.pointer = -1;
		
	
		var nextPointer = this.pointer + 1;
		if (nextPointer >= this.Imgs.length)
			nextPointer = 0;
		
		if(nextPointer>=0 && nextPointer<this.Imgs.length)
			this.DisplayImgDupeCell.src = this.Imgs[nextPointer].URL;	//this.Imgs[this.pointer].URL;			
	}
}

function GetWidgetRBannerObject(evt)
{
	if(!evt)
		return null;
	var el =(new Event(evt)).target;
	var parent = $(el).getParent();
	while(parent && !parent.id.test("^RBannerWidget_"))
	{
		parent = parent.getParent();
	}
	try
	{
		return WidgetRBannerObjects[parent.getProperty('WidgetID')];
	}
	catch(ex)
	{
		return null;
	}
}

var WidgetRBannerObjects =new Object();

function RBannerConfiguration(wg)
{
	var divWidget = $("RBannerWidget_"+wg.GUID);
	wg.DisplayDiv = divWidget.getElement("[id=RB_SlideShowBlock]");
	wg.DisplayImgCell = divWidget.getElement("img[id=RB_SlideShowImg]");
	wg.DisplayImgDupeCell = divWidget.getElement("img[id=RB_SlideShowImgDupe]");
	wg.DivImg=divWidget.getElement("div[id=RB_divImg]");
	wg.DivImgDupe=divWidget.getElement("div[id=RB_divImgDupe]");
}

function RBannerInitSingleWidget(wgObj,guid) {
	// need to set the container's width & height to fixed values and set overflow=hidden to avoid positioning problems
	// in IE
	if(wgObj.DisplayDiv) {
		var refEl = wgObj.DisplayDiv.getFirst();
		// if the refEl has a 0 offsetWidth, it isn' displayed yet (e.g. intro flash), delay the initialization until it is.
		if(refEl.offsetWidth<=0) {
			(function() { RBannerInitSingleWidget(wgObj,guid); }).delay(400);	
			return;
		}
		
		wgObj.DisplayDiv.setStyles({
			width: refEl.offsetWidth,
			height: refEl.offsetHeight,
			overflow: 'hidden'
		});
	}
	
	(function() {
		wgObj.ShowFirst();
		wgObj.ShowNextSlide(false);
		wgObj.timerID = window.setInterval(function(){wgObj.ShowNextSlide(true);}, wgObj.ImgShowTime);
	}).delay(100); // start slideshow after all initial resizing had time to settle
}

function RBannerWidgetBlock_OnLoad(evt) {
	$each(WidgetRBannerObjects,function(wgObj,guid) {
		RBannerInitSingleWidget(wgObj,guid);
	});
}




// Cookie Functions
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "0";
}
// Unused at this time
function RBannerWidgetBlock_OnMouseEnter(evt) {
/*	if(!evt)
		evt=window.event;
	var wgObj = GetWidgetRBannerObject(evt);
	if(wgObj)
		ShowRBannerWidgetBlockControls(wgObj, true);  */	
}
function RBannerWidgetBlock_OnMouseLeave(evt) {
/*	if(!evt)
		evt=window.event;
	var wgObj = GetWidgetRBannerObject(evt);
	if(wgObj)
		ShowRBannerWidgetBlockControls(wgObj, false);		*/
}
function getRBannerWidgetListingDetail(evt) {
/*	if(!evt)
		evt=window.event;
	var wgObj = GetWidgetRBannerObject(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!
  window.addEvent('domready',RBannerWidgetBlock_OnLoad);
}
/*
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);
    }
*/

