/**********************************************************************
	FILE: milli_newstracker.js
	
News Tracker Usage:
	1. create a div that that must have the width and height set (remember to leave 20 pixels height for the navigation controls.
	2. put the items into an unsorted list <ul>
	3. call the javascript function "var varName = new NewsTicker( param1, param2, param3, param4 )"
		param1 = the name of the variable
		param3 = ID of the div tag
		param2 = timing interval (in seconds) to switch to the next item. Set to -1 you don't want ticker to automatically switch
		param3 = "blue" or "green" for which color scheme to use 

Example:

<div id="milliNewsTicker" style="display:none; position:absolute; top:300px; left:500px; width:300px; height:200px;" >
		<ul>
			<li style="color:red;" >
				<img src="/assets/images/global/icon-antibody_v2.gif" style="float:right;" />
				1. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam interdum pulvinar nibh. Maecenas eget nunc in justo rhoncus aliquam. Phasellus nisl mi, convallis ut, lacinia vel, sodales tempus, ante.
			</li>
			<li style="color:blue;" >2. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam interdum pulvinar nibh. Maecenas eget nunc in justo rhoncus aliquam. Phasellus nisl mi, convallis ut, lacinia vel, sodales tempus, ante.</li>
			<li style="color:green;" >3. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam interdum pulvinar nibh. Maecenas eget nunc in justo rhoncus aliquam. Phasellus nisl mi, convallis ut, lacinia vel, sodales tempus, ante.</li>
			<li style="color:yellow;" >4. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam interdum pulvinar nibh. Maecenas eget nunc in justo rhoncus aliquam. Phasellus nisl mi, convallis ut, lacinia vel, sodales tempus, ante.</li>
			<li style="color:cyan; font-weight:bold;" >5. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam interdum pulvinar nibh. Maecenas eget nunc in justo rhoncus aliquam. Phasellus nisl mi, convallis ut, lacinia vel, sodales tempus, ante.</li>
		</ul>
</div>
<script>
</script>
var	newsTicker1 = new NewsTicker( "newsTicker1", "milliNewsTicker", -3, "blue" );
</script>

***********************************************************************/

var	g_sCurrentTickerVarName = null;
var	g_bNewsTickerCSSWritten = false;
var	g_bNewTickerProcessingScroll = false;
//	TODO: get the processing flag working on a per object basis
function NewsTicker( thisvarname, sID, nAutoScrollInterval, sBlueGreen )
{
	this.sThisVarName = thisvarname;
	this.nCurr = 1;
	this.nMax = 5;
	this.sID = "";
	this.nAutoScrollInterval = -1;
	this.nTimeoutID = null;
	
	this.showNewsItem = function ( nItem )
	{
		try
		{
			this.stopTimer();
			if ( nItem < 1 )
				nItem = 1;
			if ( nItem > this.nMax )
				nItem = this.nMax;
			if (( this.nCurr == nItem )||( g_bNewTickerProcessingScroll == true ))
			{
				restartNewsTickerAutoScrollTimer( this.sThisVarName );
				return;
			}
			
			g_bNewTickerProcessingScroll = true;
			var	divInner = document.getElementById( "divInnerContainer_" + this.sID );
			var	divNewItem = document.getElementById( "divNewsItem_" + this.sID + "_" + nItem );
			var	divOldItem = document.getElementById( "divNewsItem_" + this.sID + "_" + this.nCurr );
			var	nNewPos = divNewItem.offsetLeft;
			var	nOldPos = divOldItem.offsetLeft;
			var	nCurPos = divInner.offsetLeft;
			var	nDist = nOldPos - nNewPos;
			var	nDuration = Math.abs( this.nCurr - nItem );
			g_sCurrentTickerVarName = this.sThisVarName;
			new Effect.Move( divInner, { 
				x: nDist, 
				y: 0, 
				mode: 'relative', 
				transition: Effect.Transitions.linear, 
				duration: nDuration,
				afterFinish: function () {
					g_bNewTickerProcessingScroll = false;
					restartNewsTickerAutoScrollTimer( this.sThisVarName );
				}
			});
			this.nCurr = nItem;
		}
		catch( e )
		{
//			alert( e.message );
		}
	};
	
	this.showPrevNewsItem = function ()
	{
	var	nNew = this.nCurr - 1;
		if ( nNew < 1 )
			nNew = 1;
		this.showNewsItem( nNew );
	};
	
	this.showNextNewsItem = function ()
	{
	var	nNew = this.nCurr + 1;
		if ( nNew > this.nMax )
			nNew = this.nMax;
		this.showNewsItem( nNew );
	};

	this.insertCSS = function ( nWidth, nHeight )
	{
		if( g_bNewsTickerCSSWritten == true )
			return;
		try
		{
		var head = document.getElementsByTagName('head')[0];
		var elStyle = document.createElement('style');
		var	sStyle = "";
			sStyle += ".divNewTickerOuterContainer { overflow:hidden; position:relative; width:200px; height:200px; padding: 0px 0px 0px 0px; }\n";
			sStyle += ".divNewTickerOuterContainer .divNewTickerInnerContainer { position:absolute; top:0px; left:0px; width:1005px; height:200px; overflow:hidden; padding: 0px 0px 0px 0px; }\n";
			sStyle += ".divNewTickerOuterContainer .divNewTickerInnerContainer .divNewTickerNewsItem { float:left; width:200px; height:200px; }\n";
			sStyle += ".tblNewsTickerControls a:visited { text-decoration:none !important; color:black !important; }\n";
			sStyle += ".tblNewsTickerControls a { text-decoration:none !important; color:black !important; }\n";
			sStyle += ".tblNewsTickerControls img { background-color:transparent; border:none; }\n";
			elStyle.setAttribute( 'type', 'text/css' );
			head.appendChild( elStyle );
			 if(!window.ActiveXObject)
			{ // if not Internet Explorer
				elStyle.innerHTML = sStyle;
			}
			else
			{ // if Internet Explorer
				elStyle.styleSheet.cssText = sStyle;
			}
			g_bNewsTickerCSSWritten = true;			
		}
		catch( ex )
		{
//			alert( ex.message );
		}
	};
	
	this.initNewsTicker = function ( sID, sBlueGreen )
	{
	var	container = $(sID);
	
		if (( container == undefined )||( container == null ))
		{
			setTimeout( "processNewsTickerInit( '"+this.sThisVarName+"', '"+sID+"', '"+sBlueGreen+"' );", 1000 );
			return;
		}
		var	items = container.select( "li" );
		var	nWidth = container.offsetWidth;
		var	nHeight = container.offsetHeight;
		this.sID = sID;

		if (( sBlueGreen == undefined )||( sBlueGreen == null ))
			sBlueGreen = "";
		sBlueGreen = sBlueGreen.toLowerCase();
		if ( sBlueGreen != "blue" )
		{
			var	sColorType = "type2";
			sBlueGreen = "green";
		}
		else
		{
			sColorType = "type1";
			sBlueGreen = "blue";
		}
		
		if ( nWidth < 1 )
			nWidth = new Number( container.style.width.split("px")[0] );
		if ( nHeight < 1 )
			nHeight = new Number( container.style.height.split("px")[0] );

		this.insertCSS( nWidth, nHeight );
		var	sHTML = '';
		sHTML += '<div style="width: 100%;margin-left: 0px; margin-right: 0px; overflow:hidden;" class="flexicallout '+sColorType+'">';
		// may need to adjust the "nw" div style='margin-left:-2px;'
		sHTML += '<div class="callout_header titled"><div class="nw"></div><div class="ne"></div></div>';
		sHTML += '<div class="callout_content" style="margin-left:4px; padding-left:10px; padding-right:5px; padding-bottom:0px;">';

		nWidth -= 25;
		nHeight -= 30;
		sHTML += '<div id="divOuterContainer_'+sID+'" class="divNewTickerOuterContainer" style="width:'+nWidth+'px;height:'+nHeight+'px;" >\n';
		sHTML += '<div id="divInnerContainer_'+sID+'" class="divNewTickerInnerContainer" style="width:'+((items.length*nWidth)+5)+'px;height:'+nHeight+'px;" >\n';
		this.nTotalNewsItems = items.length;
		for( n = 0; n < this.nTotalNewsItems; n++ )
		{
		var	itemStyle = items[n].getAttribute( "style" ); 
			sHTML += "<div id='divNewsItem_"+sID+"_"+(n+1)+"' class='divNewTickerNewsItem' style='";
			if (( itemStyle != undefined )&&( itemStyle != null ))
			{
				if( window.ActiveXObject )	// if Internet Explorer
				{
					itemStyle = itemStyle.cssText;
				}
				sHTML += itemStyle + ";";
			}
			sHTML += "width:"+nWidth+"px;height:"+nHeight+"px;' >\n";
			sHTML += items[n].innerHTML;
			sHTML += "\n</div>\n";
		}
		sHTML += "</div>\n</div>\n";
		sHTML += "<table class='tblNewsTickerControls' border='0' cellspacing='0' cellpadding='0' width='95%' ><tr>";
		sHTML += "<td width='20px' ><a href='#' onClick='"+this.sThisVarName+".showNewsItem(1); return(false);' ><img src='/assets/images/global/"+sBlueGreen+"First.gif' /></a></td>";
		sHTML += "<td width='20px' ><a href='#' onClick='"+this.sThisVarName+".showPrevNewsItem(); return(false);' ><img src='/assets/images/global/"+sBlueGreen+"Previous.gif' /></a></td>";
		for( n = 1; n <= this.nTotalNewsItems; n++ ) // the number of items
		{
			sHTML += "<td align='middle' style='text-align: center;' ><a href='#' onClick='"+this.sThisVarName+".showNewsItem("+n+"); return(false);' >"+n+"</a></td>";
			this.nMax = n;
		}
		sHTML += "<td width='20px' ><a href='#' onClick='"+this.sThisVarName+".showNextNewsItem(); return(false);' ><img src='/assets/images/global/"+sBlueGreen+"Next.gif' /></a></td>";
		sHTML += "<td width='20px' ><a href='#' onClick='"+this.sThisVarName+".showNewsItem("+this.nTotalNewsItems+"); return(false);' ><img src='/assets/images/global/"+sBlueGreen+"End.gif' /></a></td>";
		sHTML += "</tr></table>\n";
		sHTML += "\n</div>\n";

		sHTML += '<div class="callout_footer"><div class="sw"></div><div class="se"></div></div>';
		sHTML += '<div class="callout_footer_fade"></div></div>';
		
		$(sID).innerHTML = sHTML;
		$(sID).style.display = "block";
		this.restartTimer();
	};
	
	this.stopTimer = function()
	{
		try
		{
			if ( this.nTimeoutID != null )
			{
				clearTimeout( this.nTimeoutID );
				this.nTimeoutID = null;
			}
		}
		catch( e )
		{
		}
	}
	
	this.restartTimer = function()
	{
		this.stopTimer();
		if ( this.nAutoScrollInterval > 0 ) 
			this.nTimeoutID = setTimeout( "processNewsTickerAutoScrollTimer( '"+this.sThisVarName+"' );", ( this.nAutoScrollInterval * 1000) );
	}
	
	this.processAutoScroll = function()
	{
		this.stopTimer();
		if ( this.nAutoScrollInterval <= 0 )
			return;
		if ( this.nCurr >= this.nMax )
			this.showNewsItem( 1 );
		else
			this.showNewsItem( this.nCurr + 1 );
		
	}
	
	if (( nAutoScrollInterval != undefined )&&( nAutoScrollInterval != null ))
	{
		this.nAutoScrollInterval = nAutoScrollInterval;
	}
	
	if (( sID != undefined )&&( sID != null ))
	{
		this.initNewsTicker( sID, sBlueGreen );
	}
	
}

function processNewsTickerInit( sJSVarName, sID, sBlueGreen )
{
	if (( sJSVarName == undefined )||( sJSVarName == null ))
		return;
	eval( sJSVarName + ".initNewsTicker( '"+sID+"', '"+sBlueGreen+"' );" );
}

function processNewsTickerAutoScrollTimer( sJSVarName )
{
	if (( sJSVarName == undefined )||( sJSVarName == null ))
		sJSVarName = g_sCurrentTickerVarName;
	eval( sJSVarName + ".processAutoScroll();" );
}

function restartNewsTickerAutoScrollTimer( sJSVarName )
{
	if (( sJSVarName == undefined )||( sJSVarName == null ))
		sJSVarName = g_sCurrentTickerVarName;
	eval( sJSVarName + ".restartTimer();" );
}

