
// declare Global variables
var isCSS 			= false;
var isW3C 			= false;
var isIE4 			= false;

function getBrowserConfig()
{
    if(document && document.images)
    {
        isCSS		= (document.body && document.body.style) ? true : false;
        isW3C		= (isCSS && document.getElementById) ? true : false;
        isIE4		= (isCSS && document.all && getIEVer() >= 4.0) ? true : false;
    }
    //alert("isCSS:"+ isCSS + "isW3C:" + isW3C + "isIE4:" + isIE4)
}

function getIEVer()
{
	var agent	= navigator.userAgent;
	var offset	= agent.indexOf( "MSIE" );
	if( offset < 0 )
	{
		return 0;
	}
	return parseFloat( agent.substring( offset + 5, agent.indexOf( ";", offset ) ) );
}

function slideImage( linkUrls, imgUrls, imgAlt, openNewWindow ,width,height, moduleid , swapTime )
{
	//Radomly pick a image
	var cycle	= Math.floor( Math.random() * imgUrls.length );

	//determine if this is a broadband
	var m_slideImage	= hasBroadband() 
	
	//alert(m_slideImage)

	//just pick a random image if the condition is not met
	if( m_slideImage == false || imgUrls.length <= 1 || !( isIE4 || isW3C ) )
	{
		slideImagePicker( linkUrls, imgUrls, imgAlt, openNewWindow, cycle ,width,height, moduleid);
		return;
	}

	//  build all of the nested DIVs out
	document.getElementById("slideImageIdx" + moduleid ).value	= cycle;

	//set up a container placeholder
	document.writeln( "<div id=\"" + "container" + moduleid + "\"  style=\"width:" + width +"px;height:" + height +"px\">" );

	for( i = 0; i < imgUrls.length; i++ )
	{
		// set up a containeritem placeholder
		document.writeln( "\t<div id=\"" + "containeritem" + moduleid + i + "\" style=\"display:none\">" );
		//set up the actual image
		slideImagePicker( linkUrls, imgUrls, imgAlt, openNewWindow, i ,width,height, moduleid);

		document.writeln( "\t</div>" );
	}
	//container end
	document.writeln( "</div>" );

	for( i = 0; i < imgUrls.length; i++ )
	{		
		if(document.getElementById("containerimg" + moduleid + i ) == null )
		{
			slideImagePicker( linkUrls, imgUrls, imgAlt, openNewWindow,i ,width,height , moduleid);
			return;
		}
	}
	slideImageEffects(moduleid,swapTime);
}

function slideImageEffects(moduleid, swapTime)
{
	//alert(moduleid)
	var imgIdx = parseInt(document.getElementById("slideImageIdx" + moduleid ).value);
	var containerMaxImage = parseInt(eval("containerMaxImage" + moduleid));
	var nextImage	= (imgIdx + 1) % containerMaxImage ;
	var	slideImageContainer = document.getElementById("container" + moduleid);
		
	//alert(imgIdx)
	// run the transition only if its above IE4
	if( getIEVer() >= 4.0 )
	{
		slideImageContainer.style.filter = "blendTrans(duration=" + swapTime + ")";
		slideImageContainer.filters.blendTrans.apply();
 		slideImageSelect( nextImage, moduleid );
		slideImageContainer.filters.blendTrans.play()
	}
	else
	{
		slideImageSelect( nextImage, moduleid );
	}

	// call again a little later
	setTimeout( "slideImageSwap(" + moduleid + ',' + swapTime + ")", (swapTime*1000) );
}

function slideImageSelect( nextImage , moduleid)
{
	var imgIdx = document.getElementById("slideImageIdx" + moduleid ).value;
	
	//alert(imgIdx + 'imgIdx:nextImage'+ nextImage +':' + moduleid)
	
	document.getElementById("containeritem" + moduleid + imgIdx ).style.display = "none";
	document.getElementById("slideImageIdx" + moduleid ).value = nextImage;
	document.getElementById("containeritem" + moduleid + nextImage ).style.display = "block";
}		

function slideImageSwap(moduleid, swapTime)
{
	var imgIdx = document.getElementById("slideImageIdx" + moduleid ).value ;
	if (document.getElementById("containerimg" + moduleid + imgIdx ).complete)
	{
		// move the image index along
		slideImageEffects(moduleid, swapTime);
	}
	else
	{
		// check again 3 seconds later
		setTimeout( "slideImageSwap(" + moduleid + ',' +  swapTime + ")", (swapTime*1000) );
	}
}

function slideImagePicker( linkUrls, imgUrls, imgAlt, openNewWindow, cycle , width,height,moduleid)
{
	if( linkUrls[cycle] != null	&& linkUrls[cycle] !=  '') 
	{
		//alert("\t\t<A HREF=\"" + linkUrls[cycle] + "\" target=\"" + openNewWindow[cycle]  + "\"><IMG SRC=\"" + imgUrls[cycle] + "\" alt=\"" + imgAlt[cycle] + "\" BORDER=\"0\" ID=\"" + "containerimg" + moduleid + cycle + "\"style=\"width:" + width +"px;height:" + height +"px"></a>" )
		document.writeln( "\t\t<A HREF=\"" + linkUrls[cycle] + "\" target=\"" + openNewWindow[cycle]  + "\"><IMG SRC=\"" + imgUrls[cycle] + "\" alt=\"" + imgAlt[cycle] + "\" BORDER=\"0\" ID=\"" + "containerimg" + moduleid + cycle + "\"style=\"width:" + width +"px;height:" + height +"px\"></a>" );
	}
	else
	{
		document.writeln( "\t\t<IMG SRC=\"" + imgUrls[cycle] + "\" alt=\"" + imgAlt[cycle]  + "\" ID=\"" + "containerimg" + moduleid + cycle + "\"style=\"width:" + width +"px;height:" + height +"px\">" );
	}
}

function hasBroadband()
{
	if( getIEVer() < 5.0 )
	{
		return false;
	}
	
	try
	{
		document.body.addBehavior ("#default#clientCaps");
		//alert(document.body.connectionType)
		return ( typeof(document.body.connectionType) != "undefined" && document.body.connectionType == "lan" );
	}
	catch( e )
	{
		return false;
	}
}

