var lefttime;
function Marquee(sInstanceName)
{
	this.nMarqueeWidth =10;
	this.nMarqueeHeight=50;
	this.nTimerInterval=20;
	this.nMarqueeSpeed=1;
	this.bPauseEffect = true;
	this.sPauseSpeed = 0;
	this.sScrollVerticalHorizontal = "H";
	this.oMarqueeElement;
	this.sClassName ='';
	this.nActualWidth;
	this.nRunTimeSpeed=1;
	this.sScrollComponentId;
	this.nDistanceBetweenEnds = 10;
	this.oClone;
	this.nActualHeight;
	this.nScrollComponentHeight=0;
	this.bCloned = false;	
	this.nNoOfClones=0;
	this.nCloneTimes =0;
	this.fnAddScrollMethods = Marquee_fnAddScrollMethods;
	this.populate = Marquee_populate;
	this.cloneComponent = Marquee_cloneComponent;
	this.MouseOut = Marquee_MouseOut;
	this.MouseOver =	Marquee_MouseOver;
	this.scrollmarquee = Marquee_scrollmarquee;
	this.fnAddScrollMethods(sInstanceName);
	this.sInstanceName = sInstanceName;
}

//this method is used to create method that will be called from setinterval 
//the methods are a copy of the method Marquee_scrollmarquee 
// but here we are adding a statatement for the current handle 
function Marquee_fnAddScrollMethods(sInstanceName)
{
	var sMethod = this.scrollmarquee;
	//alert("befiore "+sMethod);
	//var sM = String(sMethod);
	//alert("index of function "+ sM.indexOf("function Marquee_scrollmarquee() {"));
	//alert(String(sMethod).indexOf("function Marquee_scrollmarquee(){");
	sMethod = String(sMethod).replace("function Marquee_scrollmarquee() {", "function Marquee_scrollmarquee() {  oMarq = "+sInstanceName+";   ");
	//alert("after "+sMethod);
	eval(sInstanceName+"_scrollmarquee="+sMethod);
	//alert(sInstanceName+"_scrollmarquee="+sMethod);
}

function Marquee_populate(sElementId)
{
	this.nCloneTimes =0;
	this.nNoOfClones =0;
	this.bCloned = false;
	this.oClone = null;
	//set the scroll speed
	this.nRunTimeSpeed = this.nMarqueeSpeed;

	//get the container of the scroll content
	//this.oMarqueeElement = eval("window.document.all."+sElementId);

	this.oMarqueeElement = eval("window.document.getElementById('"+sElementId+"')");

	//apply the class
	if(this.sClassName != ""){
		this.oMarqueeElement.className = this.sClassName;
	}

	//get the width of the scroll area
	this.nActualWidth=this.oMarqueeElement.offsetWidth;
	//get the height of the scroll area
	this.nActualHeight = this.oMarqueeElement.offsetHeight;

	//get the handle of the scrollable component
	//var oScrollComponent = eval("window.document.all."+this.sScrollComponentId);
	var oScrollComponent = eval("window.document.getElementById('"+this.sScrollComponentId+"')");

	//get the height of the scrollable component
	this.nScrollComponentHeight = oScrollComponent.offsetHeight;
	//get the width of the scrollable component
	this.nScrollComponentWidth = oScrollComponent.offsetWidth;

	//if the scrolling is horizontal
	//compare the width of the scroll area and the scrollable component 
	// and set the number of times the scrollable component is to be  cloned
	if(this.sScrollVerticalHorizontal == "H"){
		var nWd=0;
		for(var iCtr =0;nWd<this.nActualWidth;iCtr++)
		{
			nWd = nWd + this.nScrollComponentWidth;
			this.nCloneTimes = this.nCloneTimes  + 1;
		}
	}
	else {
		//if the scrolling is vertical
		//compare the heigth of the scroll area and the scrollable component 
		// and set the number of times the scrollable component is to be  cloned
		//get no of clones
		var nHt=0;
		for(var iCtr =0;nHt<this.nActualHeight;iCtr++)
		{
			nHt = nHt + this.nScrollComponentHeight;
			this.nCloneTimes = this.nCloneTimes  + 1;
		}
		
	}

	//clone the scrollable component
	for(var iCl=1;iCl<=this.nCloneTimes; iCl++)
	{
		this.cloneComponent();
	}

	//start scrolling
	//flefttime=setInterval("Marquee_scrollmarquee()",this.nMarqueeSpeed);
	//alert("instanace name "+this.sInstanceName);
	lefttime=setInterval(this.sInstanceName+"_scrollmarquee()",this.nTimerInterval);
	//alert( "function called on intervals " + this.sInstanceName+"_scrollmarquee()");
	//alert("lefttime  "+ lefttime);
	//alert(" method "+ this.sInstanceName+"_scrollmarquee");
	//alert(" method is "+ eval(this.sInstanceName+"_scrollmarquee"));

	//eval(sInstanceName+"_scrollmarquee()");
}

//this method is used for cloning the scrollable component
function Marquee_cloneComponent()
{		
	var oMasterCopy;
	
	//while cloning for the first time clone the scrollable component
	if(this.bCloned == false){
		//oMasterCopy = eval("window.document.all."+this.sScrollComponentId);

		oMasterCopy = eval("window.document.getElementById('"+this.sScrollComponentId+"')");

	}
	else {
		//second time onwards clone the last clone
		oMasterCopy = this.oClone;
	}
	//get the top co-ordiinate
	var nTop = (this.nScrollComponentHeight * ( +1)) + (this.nDistanceBetweenEnds * (this.nNoOfClones +1));
	//get the left  co-ordiinate
	var nLeft = (this.nScrollComponentWidth * (this.nNoOfClones +1)) + (this.nDistanceBetweenEnds * (this.nNoOfClones +1));

	var oNewComponent= oMasterCopy.cloneNode(true);
	oNewComponent.style.position="absolute";
	
	//set the position of the clone
	if(this.sScrollVerticalHorizontal == "H"){
		oNewComponent.style.top="0";
		oNewComponent.style.left= nLeft;
	}
	else {
		oNewComponent.style.left= oMasterCopy.style.left;
		oNewComponent.style.top= nTop;
	}

	//add the clone to the scroll area
	try{
	this.oMarqueeElement.insertBefore(oNewComponent, oMasterCopy);
	}catch(e1){
			//alert("error "+ e1);
	}

	this.oClone = oNewComponent;
	this.bCloned = true;
	this.nNoOfClones++;
}

//this method is used for scrolling the scrollable content
function Marquee_scrollmarquee() {
	//get the marquee object handle
	//oMarq = oMarquee;
	//for horizontal scrolling	
	//alert(String(oMarq.oMarqueeElement) + "  **  "+String(oMarq.oMarqueeElement).indexOf('object' ));
	//alert("stop-----------------");
	if ( String(oMarq.oMarqueeElement).indexOf('object' ) != -1)
	{
	
		if(	oMarq.sScrollVerticalHorizontal == "H"){
			//if the scrollabale component has scrolled out of the visible area
			//then set its left 
			//else scroll it to the left
			if (parseInt(oMarq.oMarqueeElement.style.left)>(oMarq.nScrollComponentWidth*(-1)+9) )		{
				
				oMarq.oMarqueeElement.style.left=parseInt(oMarq.oMarqueeElement.style.left) -  (oMarq.nRunTimeSpeed);
			}
			else
			{
				oMarq.oMarqueeElement.style.left= (oMarq.nMarqueeWidth)+ oMarq.nDistanceBetweenEnds;	
			}
		}
		else 
		{
			//if the scrollabale component has scrolled out of the visible area
			//then set its top 
			//else scroll it to the top
			if (parseInt(oMarq.oMarqueeElement.style.top)>(oMarq.nScrollComponentHeight*(-1)+9) )
			{
				oMarq.oMarqueeElement.style.top=parseInt(oMarq.oMarqueeElement.style.top) -  (oMarq.nRunTimeSpeed);
			}
			else
			{
				oMarq.oMarqueeElement.style.top= (oMarq.nMarqueeWidth)+ oMarq.nDistanceBetweenEnds;	
			}
		}
	}
}

//this method can be used to pause the scrolling on mouseover
function Marquee_MouseOver()
{
	if(this.bPauseEffect == true)
	{
		this.nRunTimeSpeed = this.sPauseSpeed;
	}
}

//this method can be used to resume the scrolling on mouseout
function Marquee_MouseOut()
{
	if(this.bPauseEffect == true)
	{
		this.nRunTimeSpeed = this.nMarqueeSpeed;
	}
}


