// JavaScript Document
var isIE        = false;
var isNS4       = false; 
var isNS6plus   = false;
var isNS7plus   = false;
var agt         = navigator.userAgent.toLowerCase();

//sniff browser
isNS4           = (document.layers) ? true : false;
isIE            = (document.all) ? true : false;
isMac           = (agt.indexOf("mac") != -1); 
isSafari        = (navigator.userAgent.indexOf('Safari')!=-1);
isNS6plus       = (!document.all && document.getElementById) ? true : false;
if (isNS6plus) 
{
  isNS7plus = (navigator.vendorSub.indexOf('7.') != -1) ? true : false;
  isNS6plus = (isNS7plus) ? false : true;
}

function AnchorPosition_getWindowOffsetLeft (el) 
{
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
}	

function AnchorPosition_getWindowOffsetTop (el) 
{
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
}

function AnchorPosition_getPageOffsetLeft (el) 
{
	var ol=el.offsetLeft;
	var count = 0;
	var valArray = new Array();
		while((el=el.offsetParent) != null) { 
		  valArray[count] = el.offsetLeft;
			count += 1;
		}
		if (isMac) {
  	  for (i=0; i < valArray.length-2;i++) {
			  ol += valArray[i];
			}
	  } else {
  	  for (i=0; i < valArray.length;i++) {
			  ol += valArray[i];
			}
    }		
		
	return ol;
}
	
function AnchorPosition_getPageOffsetTop (el) 
{
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}

function align_layers(anchorname, layername, xoffset, yoffset) 
{
    poskey      = anchorname+'pos';
    anchor      = cache.getSet(anchorname,MM_findObj(anchorname));
    layer       = cache.getSet(layername,MM_findObj(layername)); 
    xoffset     = arguments[2] || 0;
    yoffset     = arguments[3] || 0;
    layerPos    = findOnPage(layer);
    position    = findOnPage(anchorname);    
    oldPos      = cache.getSet(poskey,position);

    if(!layer)
        return;
   
    if(layerPos.compare(oldPos) && oldPos.compare(position))
        return;
	
    isNetscape  = cache.getSet('isNetscape',(navigator.appName.indexOf('Netscape')!=-1));
    version     = cache.getSet('browserVer',parseFloat(navigator.appVersion));
    
    position.x = parseInt(position.x) + parseInt(xoffset);
    position.y = parseInt(position.y) + parseInt(yoffset);
    
    if(isNetscape && version < 5)
    {
        layer.x = position.x;
        layer.y = position.y;
    }    
    else
    {
        xKey    = 'left';
        yKey    = 'top';
        
        layer.style[xKey] = position.x;
        layer.style[yKey] = position.y;
    }
    
    cache.set(anchorname,anchor);
    cache.set(layername,layer);
    cache.set(poskey,position);
}

function reAdjust() 
{	
    // Safely obtain (if it exists) the boolean denoting whether or not this
    // page is the theme landing page.
    var themePage = typeof(window.themePage)!='undefined';
    
    // Process 4GRID/6GRID reloading    
	if(((location.href.indexOf('4grid')!=-1) || (location.href.indexOf('6grid')!=-1))
        && (location.href.indexOf('gids')==-1 && location.href.indexOf('sku')==-1))
    {
        // Return if layerCount doesn't exist
        if(typeof(layerCount) == 'undefined')
            return;
        
        // This layer is always present on both 4GRID and 6GRID pages. Oddly
        // enough, resizing this layer in IE actually breaks it?! 
        if(!isIE)
            align_layers('rollsetter', 'off', 0, 67);
        
        // layerCount is generated on the server side and written out as
        // JavaScript in the 4GRID/6GRID cfm pages. (HTM.CAT.*)
        for(i = 1; i <= layerCount; i++)
            align_layers('rollsetter', 'roll'+i, -1, 67);
    }
    // Process room layers resizing
    else if((location.href.indexOf('rms')!=-1 && location.href.indexOf('cat/index.cfm')!=-1)
        && (location.href.indexOf('gids')==-1 && location.href.indexOf('sku')==-1 && !themePage))
    {
        // Return if layerCount doesn't exist
        if(typeof(prodCount) == 'undefined')
            return;
                
        // We've cached the rmlayers array and now we must
        // parse it to move the layers.
        var rmlayers = cache.getSet('rmlayers',new Array());
        for(var i = 0; i < rmlayers.length; i++)
        {
            var vals = layerList[rmlayers[i]].split('|');   
            align_layers('roomAnchor',rmlayers[i],vals[1],vals[2]);
        }
        
        for (i = 1; i < prodCount; i ++) 
        {
            siloName = 'silo' + i;
            align_layers('siloAnchor', siloName, 1, 1);
        }            
        align_layers('siloAnchor', 'defaultSilo', 1, 1);        
    }
}

if (typeof window.addEventListener != 'undefined')
    window.addEventListener('resize',  reAdjust, false);

else if (isIE && !isMac) 
    window.attachEvent("onresize", reAdjust);
	
else if (isIE && isMac)
	window.onresize = function() {location.reload(); return;}

else
    window.onresize = reAdjust;
