/***********************************************
* Converted it to use jQuery
***********************************************/

function cCrossFadeScroller( strMasterDiv, strCrossFader0Div, strCrossFader1Div, strOverlayImageDiv,
	intItemDuration, intItemFadeSpeed,
	blnPauseCheck, strDefaultHtml, strXmlData )
{
	// Parameters
	this.m_strMasterDiv = strMasterDiv;
	this.m_strCrossFader0Div = strCrossFader0Div;
	this.m_strCrossFader1Div = strCrossFader1Div;
	this.m_strOverlayImageDiv = strOverlayImageDiv;
	this.m_intItemDuration = intItemDuration;
	this.m_intItemFadeSpeed = intItemFadeSpeed;
	this.m_blnPausecheck = blnPauseCheck;
	this.m_strDefaultHtml = strDefaultHtml;
	this.m_strXmlData = strXmlData;

	// Member Variables
	this.m_aryItem = [];
	this.m_intCounter = 0;
	this.m_idxActiveDiv = 0;
	this.m_idxItemBeingCreated = 0;
	this.m_timeout = null;

	function getInternetExplorerVersion()
	// Returns the version of Internet Explorer or a -1
	// (indicating the use of another browser).
	{
		var rv = -1; // Return value assumes failure.
		if (navigator.appName == 'Microsoft Internet Explorer')
		{
			var ua = navigator.userAgent;
			var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
			if (re.exec(ua) != null)
				rv = parseFloat( RegExp.$1 );
		}
		return rv;
	}

	var htmlEncode = function(value)
	{
		//if IE
//		var IE = getInternetExplorerVersion();
//		if (IE > -1 && IE < 9)
//		{
//			return $('<div/>').html(value).text();
//		}
		return value;
	}

	// Function that runs when the page loads
	//
	this.onLoad = function()
	{	    
		var result = SharedUL.parseXML( this.m_strXmlData );
		var objRoot = $(result.firstChild);
		if( objRoot != null )
		{
			var objNodes = objRoot.find( "item" );
			if( objNodes.length > 0 )
			{
			    var idxItem = 0;
				for( var ii = 0; ii < objNodes.length; ii++ )
				{
					// Now add the item
				    if( objNodes[ii].firstChild != null )
				    {
						this.m_aryItem.push( htmlEncode(SharedUL.serializeNode( objNodes[ii].firstChild )) );
					}

					this.m_idxItemBeingCreated++;
				}
			}
			
			// Set the pause mouseover events (if pause on mouseover is desired)
			if( this.m_blnPausecheck > 0 )
			{
				var selectors = [];
				selectors.push( this.activeObjectName() );
				selectors.push( this.inactiveObjectName() );
				selectors.push( '#' + this.m_strOverlayImageDiv );

				var _this = this;
				$(selectors.join(',')).hover(
					function() {_this.stopScrolling();},
					function() {_this.continueScrolling();}
				);
			}

			// Begin the scroller, now that everything has been loaded
			this.scrollNext();
		}
	};

	this.activeObjectName = function()
	{
		if( this.m_idxActiveDiv == 0 )
		{
			return "#" + this.m_strCrossFader0Div;
		}
		else
		{
			return "#" + this.m_strCrossFader1Div;
		}
	};

	this.inactiveObjectName = function()
	{
		if( this.m_idxActiveDiv == 1 )
		{
			return "#" + this.m_strCrossFader0Div;
		}
		else
		{
			return "#" + this.m_strCrossFader1Div;
		}
	};
	
    this.scrollNext = function()
    {
		// set the content of the object
		var activeObjName = this.activeObjectName();
		var inactiveObjName = this.inactiveObjectName();

		var idx = ((this.m_intCounter) % this.m_aryItem.length);
		$(activeObjName).html(this.m_aryItem[idx]);

		// Note that you DO NOT ANIMATE the zIndex property!  Instead, adjust it with css AFTER the animation
		$(inactiveObjName).animate({ opacity: 0.0 }, {queue:false, duration: this.m_intItemFadeSpeed} ).css("zIndex", "0");
		$(activeObjName).animate(  { opacity: 1.0 }, {queue:false, duration: this.m_intItemFadeSpeed} ).css("zIndex", "1");

		// Only scroll if there are multiple items
		if( this.m_aryItem.length > 1 )
		{
			// Prepare for next one
			this.m_timeout = setTimeout( this.selfName + ".scrollNext()", this.m_intItemDuration);

			this.m_intCounter = ((this.m_intCounter+1) % this.m_aryItem.length);
			this.m_idxActiveDiv = (this.m_idxActiveDiv+1) % 2;
		}
    };
    
    this.continueScrolling = function()
    {
		if( this.m_aryItem.length > 1 )
			this.m_timeout = setTimeout( this.selfName + ".scrollNext()", ( this.m_intItemDuration / 2.0) );
    };
    
    this.stopScrolling = function()
    {
		if( this.m_aryItem.length > 1 )
			clearTimeout( this.m_timeout );
    };

	return this;
}


