var monBlock	= null;
var monImg		= null;
var monIdx		= 0;
var monContainer= null;
var monTimeout	= 7000;
var monEffect	= 2;
var monHref;
var monSrc;
var monCaption;

function montage( href, src, caption, width, height, animate )
{
	if( monBlock )
	{
		return;
	}
		
	monHref		= href;
	monSrc		= src;
	monCaption	= caption;
	
	// this is the case for browsers that don't support filters...
	var cycle	= Math.floor( Math.random() * href.length );

	// switch off animation if we're not on broadband
	var m_montage	= ( typeof(animate) != "undefined" ? animate : hasBroadband() );				

	//if( m_montage == false || src.length <= 1 || !isDHTML )
	//{
	//	alert("entro");
	//	document.writeln( montagePicker( cycle ) );
	//	return;
	//}
	
	// go build all of the nested DIVs out
	monIdx		= cycle;
	
	var nextImage	= (monIdx + 1) % src.length;

	if( height > 0 ) { document.writeln( "<div id=\"container\" style=\"width:" + width +"px;height:" + height + "px\">" ); }
	else { document.writeln( "<div id=\"container\" style=\"width:" + width +"px\">" ); }

	for( i = 0; i < src.length; i++ )
	{
		// set up a placeholder
		document.write( "<div id=\"montage" + i + "\" style=\"display:none\">" );
		
		if( i == nextImage )
		{
			// only prefetch the first image we will display
			document.write( montagePicker( i ) );
		}
		
		document.write( "</div>" );
	}

	document.writeln( "</div>" );

	// pull the images out
	monBlock	= new Array( src.length );
	monImg		= new Array( src.length );

	for( i = 0; i < src.length; i++ )
	{
		monBlock[i]	= document.getElementById( "montage" + i );
		
		if( i == nextImage )
		{
			monImg[i] = document.getElementById( "monimg" + i );
		}
		else
		{
			monImg[i] = null;
		}
	}

	monContainer = document.getElementById("container");
		
	montageEffects();
}

function montageEffects()
{
	var nextImage	= (monIdx + 1) % monImg.length;
	
	// run the transition	
	if( readIEVer() >= 4.0 && monEffect > 0 )
	{
		try
		{
			if( monEffect == 1 )
			{
				monContainer.style.filter = "blendTrans(duration=0.6)";
				monContainer.filters(0).apply();
 				montageSelect( nextImage );
				monContainer.filters(0).play();
			}
			else
			{
				monContainer.style.filter = "blendTrans(duration=1.5) revealTrans(duration=1.0,transition=7)";
				monContainer.filters(0).apply();
				monContainer.filters(1).apply();
 				montageSelect( nextImage );
				monContainer.filters(0).play();
				monContainer.filters(1).play();
			}
		}
		catch( e ) { montageSelect( nextImage ); }
	}
	else
	{
		montageSelect( nextImage );
	}

	// asked to be called again a little later
	setTimeout( "montagePrep()", monTimeout - 1500 );
	setTimeout( "montageSwap()", monTimeout );
}

function montageSelect( nextImage )
{
	monBlock[monIdx].style.display = "none";
	monIdx = nextImage;
	monBlock[monIdx].style.display = "block";
}		

function montagePrep()
{
	// prefetch the next image if we don't already have it
	var nextImage	= (monIdx + 1) % monImg.length;

	if( !monImg[nextImage] )
	{
		monBlock[nextImage].innerHTML = montagePicker( nextImage );
		monImg[nextImage] = document.getElementById( "monimg" + nextImage );
	}
}

function montageSwap()
{
	if( monImg[monIdx].complete )
	{
		// move the image index along
		montageEffects();
	}
	else
	{
		// check again 3 seconds later
		setTimeout( "montageSwap()", 3000 );
	}
}

function montagePicker( cycle )
{
	var divHtml;

	if( monHref[cycle] != null && monHref[cycle] != "" ) 
	{
		divHtml = "<A href=\"" + monHref[cycle] + "\"><IMG src=\"" + monSrc[cycle] + "\" alt=\"" + monCaption[cycle] + "\" BORDER=\"0\" ID=\"monimg" + cycle + "\" alt=\"\"></a>";
	
	}
	else
	{
		divHtml = "<IMG src=\"" + monSrc[cycle] + "\" ID=\"monimg" + cycle + "\" alt=\"\">";
	}
	
	return divHtml;
}

function hasBroadband()
{
	if( readIEVer() < 5.0 )
	{
		return false;
	}
	
	try
	{
		document.body.addBehavior ("#default#clientCaps");
	
		return ( typeof(document.body.connectionType) != "undefined" && document.body.connectionType == "lan" );
	}
	catch( e )
	{
		return false;
	}
}


function initProdMenus ()
{
var divs = document.getElementsByTagName("DIV");
for ( i=0;i < divs.length ; i ++ )
{
if ( divs[i].className == "pmContainer")
{
var pmContainer = divs[i];
var nodes = pmContainer.childNodes;
for ( j =0;j< nodes.length ; j ++)
{
if ( nodes[j].className == "pmShadow")
{
pmContainer.pmShadow = nodes[j];
continue;
}
if ( nodes[j].className == "pmMenu")
{
pmContainer.pmMenu = nodes[j];
var menuItems = pmContainer.pmMenu.getElementsByTagName("DIV");
for ( k=0;k<menuItems.length;k++)
{
var menuItem = menuItems[k];
if ( menuItem.className =="pmMenuItem")
{
menuItem.onclick = function(){ pbRowClick( this );};
menuItem.onmouseout=function(){ this.style.backgroundImage=this.origBG};
menuItem.onmouseover=function(){this.origBG=this.style.backgroundImage ; this.style.backgroundImage="url('" + m_imgPfx + "/images/global/buttons/homepage/drk_menumask_96x28.png')";};
}
}
continue;
}
if ( nodes[j].className == "pmPrimaryLinkContainer")
{
pmContainer.pmPrimaryLinkContainer = nodes[j];
var anchors = nodes[j].getElementsByTagName( "A");
if ( anchors != null && anchors.length > 0 )
{
pmContainer.pmPrimaryLink = anchors[0];
var subDivs =pmContainer.pmPrimaryLink .getElementsByTagName("DIV");
if ( subDivs != null && subDivs.length > 0)
{
pmContainer.pmPrimaryImageHolder= subDivs[0];
var imgs = pmContainer.pmPrimaryImageHolder.getElementsByTagName( "IMG");
if ( imgs != null && imgs.length > 0 )
{
pmContainer.pmImage = imgs[0];
}
}
}
}
}
pmContainer.onmouseover = function (){m_pmIsOverLink = true;pmOver ( this );};
pmContainer.onmouseout = function (){m_pmIsOverLink = false;setTimeout('pmTimer()',2000);};
pmContainer.pmShadow.style.width=pmContainer.style.width;
if ( pmContainer.pmImage != null )
{
if ( isIE6 )
{
pmContainer.pmPrimaryImageHolder.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + pmContainer.pmImage.src + "', sizingMethod='scale')";
pmContainer.pmImage.style.display = "none";
}
}
}
}
}
function pbRowClick ( row )
{
for ( i=0 ; i <row.childNodes.length; i++ )
{
if ( row.childNodes[i].tagName == "A")
{
var href= row.childNodes[i].href;
if( window.event != null && window.event.srcElement != null && window.event.srcElement.tagName == "A")
{
return;
}
else
{
row.childNodes[i].href="javascript:void(0);";
}
document.location.href= href;
}
}
}
m_pmIsOverLink=false;
m_pmOld=false;
function pmTimer ()
{
if (!m_pmIsOverLink && m_pmOld )
{
pmOff(m_pmOld);
}
}
function pmOff ( container )
{
container.style.border = "1px solid #ffffff";
container.style.backgroundImage= "";
container.style.backgroundColor ="#ffffff";
container.pmShadow.style.display="none";
container.pmMenu.style.display = "none";
if(!m_isRtl)
{
container.style.zIndex = 1;
}
}
function pmOn ( container )
{
container.style.border = "1px solid #d7d7d7";
container.style.backgroundColor ="#dddddd";
//container.style.backgroundImage= "url('" + m_imgPfx + "/images/global/buttons/homepage/mask_96x120.png')";
container.pmMenu.style.display = "block";
container.pmShadow.style.display="block";
container.pmShadow.style.top=container.offsetHeight -1;
if(!m_isRtl)
{
container.pmMenu.style.top = -container.pmMenu.offsetHeight+2;
container.style.zIndex = 2;
}
else
{
container.pmMenu.style.top = -container.pmMenu.offsetHeight-1;
}
if( getLeftOffset ( container.pmMenu) < 0 )
{
container.pmMenu.style.left=0;
}
}
function pmOver ( container )
{
if ( m_pmOld && m_pmOld != container )
{
pmOff( m_pmOld );
}
m_pmOld = container;
pmOn ( container );
}
function getLeftOffset(obj) {
var x = obj.offsetLeft
while (obj = obj.offsetParent) x += obj.offsetLeft
return x
}
