var delayDetachTimer;
var delayFadeInTimer;
var rotateInitTimer;
var targetObj;

// client-side fix for IE's inability to use dynamically inserted images from it's cache
try { document.execCommand("BackgroundImageCache",false,true); } catch(e) {};


function getTracker()
{
    if( window.urchinTracker )
    {
       _uacct = "UA-188336-2";
       urchinTracker();
    }
}

function attachExpando()
{
    var expandoArray = $S( ".expando" );
    
    expandoArray.each
    (
        function( expandoElem )
        {
            expandoElem.onclick = setStyle;
            expandoElem.onmouseover = showMouseTip;
        }
    );
}

function setStyle()
{
    try
    {
        var targetParent = this.parentNode;
        var siblingArray = targetParent.childNodes;
        
        siblingArray.each
        (
            function( siblingElem )
            {
                if( siblingElem.className == "hidden" )
                {
                    var targetElem = siblingElem;
                    var targetHeight = targetElem.offsetHeight;
                    var heightValue;
                    
                    if( targetHeight == 0 )
                    {
                        heightValue = "auto";
                    }
                    else
                    {
                        heightValue = 0;
                    }
                    targetElem.style.height = heightValue;
                }
            }
        )
    }
    catch( e )
    {
        window.status = "Trapping JS error: " + e;
    }
}

function showMouseTip()
{
    this.title = "Click to show or hide more information"
}

function cloneMenu()
{
    var parentElem = document.getElementById( "jsDisabled" );
    var childArray = parentElem.getElementsByTagName( "ul" );
            
    if( childArray.length > 0 )
    {
        var sourceNode = childArray[0];
        sourceNode.id = "udm";
        sourceNode.className = "udm";
        
        var htmlSource = sourceNode.cloneNode( true );
        var htmlTarget = document.getElementById( "navigationCenter" );
        
        htmlTarget.appendChild( htmlSource );
        parentElem.removeChild( sourceNode );
    }
}

function getWindowHeight()
{
    var windowHeight = 0;
    if ( typeof(window.innerHeight) == "number" )
    {
        windowHeight = window.innerHeight;
    }
    else
    {
        if (document.documentElement && document.documentElement.clientHeight)
        {
            windowHeight = document.documentElement.clientHeight;
        }
        else
        {
            if (document.body && document.body.clientHeight)
            {
                windowHeight = document.body.clientHeight;
            }
        }
    }
    return windowHeight;
}

var imageHash =
[
    {
        src: "imgs/truck2.jpg",
        href: "/site/index/solutions.html",
        title: "Plexus Scientific Corporation"
    },
    {
        src: "/site/imgs/mansci.jpg",
        href: "/site/index/solutions/information-sciences.html",
        title: "Management Sciences"
    },
    {
        src: "/site/imgs/infosci.jpg",
        href: "/site/index/solutions/information-sciences.html",
        title: "Business Solutions"
    },
    {
        src: "/site/imgs/inframan.jpg",
        href: "/site/index/solutions/built-infrastructure.html",
        title: "Built Infrastructure"
    },
	{
        src: "/site/imgs/logistics.jpg",
        href: "/site/index/solutions/logistics-intl.html",
        title: "Logistics Support & Management"
    },
    {
        src: "/site/imgs/envisci.jpg",
        href: "/site/index/solutions/esoh.html",
        title: "Environmental Services and Remediation"
    },
    {
        src: "/site/imgs/safety.jpg",
        href: "/site/index/solutions/esoh.html",
        title: "Safety and Occupational Health"
    },
    {
        src: "/site/imgs/legal.jpg",
        href: "/site/index/solutions/rla.html",
        title: "Regulatory and Legal Affairs"
    }
];

function rotateInit()
{
    if( delayDetachTimer )
    {
        $clear( delayDetachTimer );
    }
    
    if( rotateInitTimer )
    {
        $clear( rotateInitTimer );
    }
    
    var callout = document.getElementById( "callout" );
    if( callout )
    {
        if( !document.all )
        {
            callout.style.left = "9px";
        }
        
        rotater = new RotatingImage();
        rotater.attachImage();
        rotater.fadeIn();
        
        delayDetachTimer = ( function() { rotater.detachImage(); } ).delay( 4000 );
        rotateInitTimer = ( function() { rotateInit(); } ).delay( 6000 );
    }
}

var RotatingImage = function(){}
RotatingImage.prototype =
{
    hashId     : null,
    hashIndex  : null,
    childAnchor: null,
    childImage : null,
    container  : null,
    
    attachImage: function()
    {
        if( imageHash.length > 0 )
        {
            var imageHashLength = imageHash.length - 1;
            this.hashIndex = Math.round( Math.random() * imageHashLength );
            
            while( this.hashId == this.hashIndex )
            {
                this.hashIndex = Math.round( Math.random() * imageHashLength );
            }
            this.hashId = this.hashIndex;
            
            this.container = document.getElementById( "callout" );
            if( this.container )
            {
                var cA = this.childAnchor = document.createElement( "a" );
                    cA.href = imageHash[ this.hashIndex ].href;
                    cA.title = imageHash[ this.hashIndex ].title;
                    cA.id = "anchorHash" + this.hashIndex;
                    cA.zIndex += 1;
                    cA.style.position = "absolute";
                    cA.style.top = "0";
                
                var cI = this.childImage = document.createElement( "img" );
                    cI.src = imageHash[ this.hashIndex ].src;
                    cI.id = "imageHash" + this.hashIndex;
                    cI.height = this.container.offsetHeight;
                    cI.style.visibility = "hidden";
                    cI.width = this.container.offsetWidth;
                    cI.border = 0;
                
                cA.appendChild( cI );
                this.container.appendChild( cA );
            }
        }
    },

    fadeIn: function()
    {
        var newTransition = new Fx.Style( this.childImage, "opacity" ).custom( 0, 1 );
    },
    
    detachImage: function()
    {
        while( this.container.firstChild && this.container.firstChild.id != "anchorHash" + this.hashIndex )
        {
            this.container.removeChild( this.container.firstChild );
        }
    }
}

function getHTML()
{
    if( document.getElementById("ajaxTarget") )
    {
        targetObj = document.getElementById("ajaxTarget");
        var url = targetObj.firstChild.nodeValue;
        var params = null;
        var bAsyncRequest = true;
        var oTarget = null;
        var bExecScript = false;
        
        doAjaxRequest( url, params, bAsyncRequest, oTarget, bExecScript );
    }
}

function doAjaxRequest( url, params, bAsyncRequest, oTarget, bExecScript )
{
    var myAjax = new Ajax
    (
        url, 
        {
            method: "get",
            postBody: params,
            async: bAsyncRequest || true,
            onComplete: doAjaxResponse,
            onStateChange: doAjaxError,
            update : oTarget,
            evalScripts: bExecScript || false
        }
    ).request();
}

function doAjaxResponse( originalRequest )
{
    var tempContainer = document.createElement( "div" );
    var responseHtml = originalRequest.responseText;
    
    tempContainer.innerHTML = responseHtml;
    targetObj.replaceChild( tempContainer, targetObj.firstChild );
    targetObj.replaceChild( document.getElementById("ajaxSource"), tempContainer );

    styleObject( targetObj, "display", "block" );

    var links = targetObj.getElementsByTagName( "a" );
    
    for( var n in links )
    {
        links[n].onclick = ajaxHref;
    }
}

function doAjaxError()
{
    targetObj.innerHTML = "Unable to load project data";
    styleObject( targetObj, "display", "block" );
    
}

function ajaxHref()
{
    var fileExt = this.href.substring( this.href.length, this.href.length - 4 );

    if( fileExt == "html" )
    {
        this.href = doAjaxRequest( this.href, null );
        return false;
    }
}

function styleObject( objectToStyle, styleAttribute, styleValue )
{
    objectToStyle.style[styleAttribute] = styleValue;
}
