var m_gPrimaryNavModeOnDemand = false;
var m_gPrimaryNavLoaded = false;
var m_gLoadRequestMade = false;
var m_gSearchLabel = "Search";

function onFailure( request, obj ) {
    window.status = request.url + ": failure - " + obj;
}

function onException( request, error ) {
    window.status = request.url + ": " + error.message;
}

function loadSiteSelectMenu() {

    function onSuccess( request ) {

        if( request.status != 200 ) {
            return;
        }
        var xml = request.responseText;
        var d = document.getElementById("adsk91-countryFlyout");
        if( ! d ) return;
        d.innerHTML = xml;

        initLocationElements();
    }

    new Ajax.Request(
        getCountrySiteSelectionDataUrl(),
        {
            method: 'get',
            onComplete: onSuccess,
            onFailure: onFailure,
            onException: onException
        }
    );
}

function loadPrimaryNavData() {

    if ( m_gLoadRequestMade ) return;
    m_gLoadRequestMade = true;

    function onSuccess( request ) {

        if( request.status != 200 ) {
            return;
        }
        var navText = request.responseText;
        var mainNav = document.getElementById( "adsk91-mainNav" );
        if( ! mainNav ) {
            return;
        }
        mainNav.innerHTML = navText;
        initNavElements();
    }

    var params = "";
    var url = getPrimaryNavDataUrl();
    if ( url != null ) {
        var aUrl = url.split("?");
        if ( aUrl.length == 2 ) {
            url = aUrl[0];
            if ( ! core_isStringBlank(aUrl[1]) )
            params = aUrl[1];
        }

        var req = new Ajax.Request(
            url,
            {
                method: 'get',
                parameters: params,
                onComplete: onSuccess,
                onFailure: onFailure,
                onException: onException
            }
        );
    }
}

function init() {

    if( cda_isPreview() ) {
        m_gPrimaryNavModeOnDemand = true;
    }

    var searchElement = document.getElementById("searchBox");
    if( searchElement ) {
        m_gSearchLabel = searchElement.value;
        searchElement.onfocus = function()	{
             this.className+=" focused";
             if(this.value==m_gSearchLabel)	{
                 this.value="";
             }
         }
         searchElement.onblur = function()	{
             this.className=this.className.replace(/\bactive/g, "");
             this.className=this.className.replace(/\bfocused/g, "");
             if(this.value=="")	{
                 this.value=m_gSearchLabel;
             }
         }
         searchElement.onmouseover = function()	{
             this.className+=" active";
         }
         searchElement.onmouseout = function()	{
             this.className=this.className.replace(/\bactive/g, "");
         }
    }

    function checkSmartPromo() {

        function addCookieParam( params, name ) {
            var value = core_getCookie( name );
            if( value ) {
                params[params.length] = name + "=" + value;
            }
        }

        function hasSmartPromo() {
            var promoDiv = document.getElementById( "adsk91-promos" );
            if( ! promoDiv ) {
                return false;
            }
            return promoDiv.className == "smart";
        }

        function onSuccess( request ) {
            if( request.status != 200 ) {
                return;
            }
            var xml = request.responseXML;

            var nodes = xml.getElementsByTagName( "smart-promos" );
            if( ! nodes || nodes.length == 0 ) {
                return;
            }
            var node = nodes[0];
            if( ! node.childNodes || node.childNodes.length == 0 ) {
                return;
            }
            var html = "";
            // 2008-01-14 (JK): Issue 4753 - Firefox may break the text nodes into multiple chunks            
            for( var i = 0; i < node.childNodes.length; i++ ) {
                html += node.childNodes[i].nodeValue;
            }
            var promoDiv = document.getElementById( "adsk91-promos" );
            if( ! promoDiv ) {
                return;
            }
            var uls = promoDiv.getElementsByTagName( "UL" );
            var ul;
            if( uls.length == 0 ) {
                ul = document.createElement( "ul" );
                promoDiv.appendChild( ul );
            } else {
                ul = uls[0];
            }
            if( ul ) {
                ul.innerHTML = html;
            }

        }

        if( ! hasSmartPromo() ) {
            return;
        }

        var params = new Array();
        addCookieParam( params, "ADSKVISCNTRY" );
        addCookieParam( params, "ADSKVISINDUSSEG" );
        addCookieParam( params, "ADSKVISMKT" );

        if( params.length == 0 ) {
            return;
        }

        var successSection = document.getElementById( "adsk91-oc-typ-section" );
        if( successSection ) {
            params.push( "section=" + successSection.value );
        }

        var siteID = cda_getSiteID();

        if( ! siteID ) {
            return;
        }
        parameters = "siteID=" + siteID;

        var contentID = null;
        var spContentElement = document.getElementById( "adsk91-el-id-sp-cont" );
        if( spContentElement ) {
            contentID = spContentElement.value;
        } else {
            contentID = cda_getContentID();
        }
        if( ! contentID ) {
            parameters += "&catID=" + cda_getCategoryID();
        } else {
            parameters += "&id=" + contentID;
        }

        parameters += "&" + params.join( "&" );
        if( cda_isPreview() ) {
            parameters += "&preview=1";
        }
        var linkID = cda_getLinkCategoryID();
        if( linkID ) {
            parameters += "&linkID=" + linkID;
        }

        var url = core_getContextPath() + "/servlet/sp.xml";

        var req = new Ajax.Request(
            url,
            {
                method: 'get',
                parameters: parameters,
                onComplete: onSuccess,
                onFailure: onFailure,
                onException: onException
            }
        );

    }

    checkSmartPromo();

    if ( ! isMinimalHeader() ) {
        if ( m_gPrimaryNavModeOnDemand ) {
            initOnDemandHandler();
        } else {
            loadPrimaryNavData();
        }
        loadSiteSelectMenu();
    }
}

function openPopup( url, width, height, name, features ) {

    function addFeature( features, name, value ) {
        if( value == null || value == "" ) {
            return features;
        }
        if( features != "" ) {
            features += ",";
        }
        return features + name + "=" + value;
    }

    if( name == null || name == "" ) {
        name = "popupWindow";
    }
    if( features == null || features == "") {
        features = "";
        features = addFeature( features, "scrollbars", "yes" );
        features = addFeature( features, "resizable", "yes" );
        if( name == "popupWindow" ) {
            features = addFeature( features, "location", "yes" );
            features = addFeature( features, "menubar", "yes" );
            features = addFeature( features, "toolbar", "yes" );
            features = addFeature( features, "status", "yes" );
        }
    }
    if( name == "viewlarger" ) {
        if( width == null || width == "" ) {
            width = 800;
        }
        if( height == null || height == "" ) {
            height = 600;
        }
    }
    if( name == "popupWindow" ) {
        if( width == null || width == "" ) {
            width = 800;
        }
        if( height == null || height == "" ) {
            height = 600;
        }
    }

    features = addFeature( features, "width", width );
    features = addFeature( features, "height", height );
    var popupWindow = window.open( url, name, features );
    popupWindow.focus();

    return false;
}

function initNavElements() {
    if (isSaf || document.all&&document.getElementById) {
        var navElement = document.getElementById("adsk91-mainNav");
        if( navElement ) {
            var nodes = navElement.getElementsByTagName("LI");
            for (var i=0; i<nodes.length; i++) {
                node = nodes[i];
                if (node.nodeName=="LI") {
                    nodes[i].onmouseover= function() {
                        if (!isIE7 && !isSaf) {
                            this.className+=" navHover";
                        } else {
                            var uls = this.getElementsByTagName("UL");
                            if (uls[0] != null){
                                uls[0].style.left = 0;
                                uls[0].style.top = 16;
                            }
                        }
                    }
                    nodes[i].onmouseout=function() {
                        if (!isIE7 && !isSaf) {
                            this.className=this.className.replace(" navHover", "");
                        } else {
                            var uls = this.getElementsByTagName("UL");
                            if (uls[0] != null) uls[0].style.left = "-999em";
                        }
                    }
                }
            }
        }
    }
}

function initLocationElements() {

    if( isSaf || document.all && document.getElementById ) {
         var locationElement = document.getElementById("adsk91-location");
         if( locationElement ) {
             var nodes = locationElement.getElementsByTagName("LI");
             for( var i = 1; i < 2; i++ ) {
                 node = nodes[i];
                 if( node.nodeName == "LI" ) {
                     nodes[i].onmouseover = function() {
                         if( !isIE7 && !isSaf ) {
                             this.className += " navHover";
                         } else {
                             var elements = this.getElementsByTagName("DIV");
                             if (elements != null) {
                                 elements[0].style.position = 'fixed';
                                 elements[0].style.left = (document.documentElement.clientWidth/2) - 134;
                                 elements[0].style.top = 106-getScroll();
                             }
                         }
                     }
                     nodes[i].onmouseout = function() {
                         if( !isIE7 && !isSaf ) {
                             this.className = this.className.replace(" navHover", "");
                         } else {
                             var elements = this.getElementsByTagName("DIV");
                             if( elements[0] != null ) {
                                 elements[0].style.left = "-999em";
                             }
                         }
                     }
                 }
             }
        }
    }
}

//returns the top position of the scroll status, and add X pixels to determine X position of the overlay
function getScroll(){
    if (document.documentElement && document.documentElement.scrollTop){
        theTop = document.documentElement.scrollTop;
    } else if (document.body){
        theTop = document.body.scrollTop
    } else{
        theTop = window.pageYOffset;
    }
 return theTop;
}

function adjustScroll(){
   if(isIE7 || navigator.userAgent.toLowerCase().indexOf("msie") == -1){
      document.getElementById("adsk91-countryFlyout").style.top = 106-getScroll();
   }
}

function initOnDemandHandler() {
    if ( m_gPrimaryNavLoaded ) return;
    m_gPrimaryNavLoaded = true;
    if (document.getElementById) {
        var navElement = document.getElementById("adsk91-mainNav");
        if( navElement ) {
            var nodes = navElement.getElementsByTagName("LI");
            for (var i=0; i<nodes.length; i++) {
                node = nodes[i];
                if (node.nodeName=="LI") {
                    nodes[i].onmouseover= function() {
                        loadPrimaryNavData();
                    }
                }
            }
        }
    }
}

function isMinimalHeader() {
    return ( null == document.getElementById( "adsk91-el-id-nav-cont" ) );
}

var isIE7 = (navigator.userAgent.toLowerCase().indexOf("msie 7.0") != -1);
var isSaf = (navigator.userAgent.toLowerCase().indexOf("safari") != -1);
var isMac = (navigator.appVersion.indexOf('Mac') != -1);

// Hooks for overriding data requests
 function getPrimaryNavDataUrl() {
     var params = "";
     var navContentID = document.getElementById( "adsk91-el-id-nav-cont" );
     if ( navContentID != null && !core_isStringBlank(navContentID.value) ) {
         params += "id=" + navContentID.value;
     }
     var navSiteID = cda_getSiteID();
     if ( navSiteID != null && !core_isStringBlank(navSiteID) ) {
         params += "&siteID=" + navSiteID;
     }

     if ( cda_isPreview() ) {
          params += "&preview=1";
      }
     return core_getContextPath() + "/servlet/primarynavdata.xml?" + params;
}

function getCountrySiteSelectionDataUrl() {
    return core_getContextPath() + "/servlet/ss.xml";
}

window.onscroll = adjustScroll;
