/***********************************************
* Ultimate Fade-In Slideshow (v1.5): © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
* NOTE: MAP 2007-Feb-13 - I've modified this, it is now a LOT different from the original code
***********************************************/

function cCrossFadeScroller( strMasterDiv, strCanvas0Div, strCanvas1Div, intDelay, blnPauseCheck, 
	strDefaultHtml, strXmlData )
{
	// Parameters
	this.m_strMasterDiv = strMasterDiv;
	this.m_strCanvas0Div = strCanvas0Div;
	this.m_strCanvas1Div = strCanvas1Div;
	this.m_intDelay = intDelay;
	this.m_blnPausecheck = blnPauseCheck;
	this.m_strDefaultHtml = strDefaultHtml;
	this.m_strXmlData = strXmlData;

	// Member Variables
	this.dom = (document.getElementById); //modern dom browsers
	this.iebrowser = document.all;
	this.mouseovercheck = 0;
	this.degree = 10; //initial opacity degree (10%)
	this.curimageindex = 0;
	this.nextimageindex = 1;
	this.curcanvas = this.m_strCanvas0Div;
	this.displayitems = new Array(); // Array of Html items to rotate
	this.fadeclear = new Array(); //array to cache corresponding clearinterval pointers
	this.objCurrent = null;
	this.objPrevious = null;
	this.m_intCountItems = 0;

	// Functions
	
	// Set the content of the given object
	//
	this.populateslide = function( picobj, picindex )
	{
		picobj.innerHTML = "<div>" + this.displayitems[picindex] + "</div>";
	};

	// Rotate the image to another one, unless user hovering over it
	// 
	this.rotateimage = function()
	{
		var cacheobj;
		var crossobj;
		
		// Hovering to pause is allowed?
		if( this.m_blnPausecheck == 1 )
		{
			cacheobj = this;
		}
		// Hovering, try again later to switch the content
		if( this.mouseovercheck == 1 )
		{
			setTimeout( function(){cacheobj.rotateimage()}, 100 );
		}
		else if( this.dom )
		{
			this.resetit();
			crossobj = this.objCurrent = this.getObject( this.curcanvas );
			crossobj.style.zIndex++;
			this.fadeclear = setInterval( this.selfName + ".fadepic()", 50 );
			this.curcanvas = ( this.curcanvas == this.m_strCanvas0Div ) ? this.m_strCanvas1Div : this.m_strCanvas0Div;
		}
		this.curimageindex = ( this.curimageindex < (this.displayitems.length-1) ) ? this.curimageindex+1 : 0;
	};

	// Reset it
	//
	this.resetit = function()
	{
		var crossobj;
		
		// Initial opacity value
		this.degree = 10;
		crossobj = this.getObject( this.curcanvas );

		// Set it
		this.setOpacity( crossobj, this.degree );
	};

	// Start the cross fade scroller
	//
	this.startit = function()
	{
		var crossobj = this.getObject( this.curcanvas );
		this.populateslide( crossobj, this.curimageindex );

		// Hover to pause the scrolling?
		if( this.m_blnPausecheck == 1 )
		{	
			var cacheobj = this;
			var crossobjcontainer = this.iebrowser ? this.iebrowser[ this.m_strMasterDiv ] : document.getElementById( this.m_strMasterDiv );
			crossobjcontainer.onmouseover = function(){ cacheobj.mouseovercheck = 1; };
			crossobjcontainer.onmouseout = function(){ cacheobj.mouseovercheck = 0; };
		}
		this.rotateimage();
	};

	// Fade in tpicture
	//
	this.fadepic = function()
	{
		// Has it faded all the way in yet?
		if( this.degree < 100 )
		{
			// Fade it in a little more
			this.degree += 10;
			this.setOpacity( this.objCurrent, this.degree );
			this.setOpacity( this.objPrevious, 100-this.degree );

		}
		// It has faded all the way in, rotate in the next picture
		else
		{
			var strNextCanvas;

			clearInterval( this.fadeclear );
			strNextCanvas = ( this.curcanvas == this.m_strCanvas0Div ) ? this.m_strCanvas0Div : this.m_strCanvas1Div;

			// Backup of the "previous" object
			this.objPrevious = this.objCurrent;

			// Make the alternate object not visible
			this.objCurrent = this.getObject( strNextCanvas );
			this.setOpacity( this.objCurrent, 0 );

			this.populateslide( this.objCurrent, this.nextimageindex );
			this.nextimageindex = ( this.nextimageindex < this.displayitems.length-1 ) ? this.nextimageindex+1 : 0;
			if( this.m_intCountItems > 1 )
			{
				setTimeout( this.selfName + ".rotateimage()", this.m_intDelay) ;
			}
		}
	};
	
	// Set the opacity to a given value
	//
	this.setOpacity = function( tmpObj, intOpacity )
	{
		if( tmpObj != null )
		{
			if( tmpObj.filters && tmpObj.filters[0] )
			{
				if( typeof tmpObj.filters[0].opacity == "number" ) //if IE6+
				{
					tmpObj.filters[0].opacity = intOpacity;
				}
				else //else if IE5.5-
				{
					tmpObj.style.filter = "alpha(opacity=" + intOpacity + ")";
				}
			}
			else if (tmpObj.style.MozOpacity)
			{
				tmpObj.style.MozOpacity = intOpacity / 100;	// changed from 101 to 100 because it was not fully opaque;
			}
			else if (tmpObj.style.KhtmlOpacity)
			{
				tmpObj.style.KhtmlOpacity = intOpacity / 100;
			}
		}
	};

	// Get an object by its ID
	// 
	this.getObject = function( strObjectID )
	{
		return this.iebrowser ? this.iebrowser[strObjectID] : document.getElementById(strObjectID);	
	};

	// Function that runs when the page loads
	//
	this.onLoad = function()
	{
		var result = SharedUL.parseXML( this.m_strXmlData );
		var objThis = this;
		var objRoot = result.firstChild;
		if( objRoot != null )
		{
			var objNodes = objRoot.getElementsByTagName( "item" );
			var xmlString;

			if( objNodes.length > 0 )
			{
			    var idxItem = 0;
				for( var ii = 0; ii < objNodes.length; ii++ )
				{
				    if( objNodes[ii].firstChild != null )
				    {
    					objThis.displayitems[ idxItem++ ] = SharedUL.serializeNode( objNodes[ii].firstChild );
					}
				}
				objThis.m_intCountItems = idxItem;
			}
			
			if( objThis.m_intCountItems == 0 )
			{
				objThis.displayitems[0] = objThis.m_strDefaultHtml;
				objThis.m_intCountItems = 1;
			}

			// Start it scrolling
			if( objThis.dom ) //if IE5+ or modern browsers such as Firefox
			{
				objThis.startit();
			}
			else
			{
				objThis.curimageindex++;
				setInterval( objThis.selfName + ".rotateimage()", objThis.m_intDelay );
			}
		}
	}
	
	return this;
}

