
var SAS = {};

SAS.bookmarkedSection = YAHOO.util.History.getBookmarkedState("section");
SAS.querySection = YAHOO.util.History.getQueryStringParameter("section");
SAS.initialSection = SAS.bookmarkedSection || SAS.querySection || "1";

SAS.Nav = {};
SAS.Util = {};
SAS.Local = {};

SAS.Nav.loadSection = function (section) {
    var ar, i;
    this.currentSection = section;
    if (section < 6) {
        //tab or Overview link has been selected
        YAHOO.util.Dom.addClass('middleCol', 'displayOff'); //hide ajax section
        YAHOO.util.Dom.addClass('rn1', 'active');
        YAHOO.util.Dom.setStyle('inlinecontent', 'display', 'block');
        YAHOO.util.Dom.setStyle('content' + section, 'display', 'block');
        if (this.contentType === 'solution.productDetail') {
            YAHOO.util.Dom.addClass('tab' + section + 'a', 'active');
            YAHOO.util.Dom.setStyle('tabs', 'visibility', 'visible');
        }
        //handle previous stuff
        ar = YAHOO.util.Dom.getElementsByClassName('panel', 'div', 'inlinecontent');
        for (i = 0, len = ar.length; i < len; i++) {
            if (ar[i].getAttribute("id") !== 'content' + section) {
                YAHOO.util.Dom.setStyle(ar[i], 'display', 'none');
            } 
        }
        if (this.previousSection) {
            if (this.previousSection < 6) {
                YAHOO.util.Dom.removeClass('tab' + this.previousSection + 'a', 'active');
                
            } else {
                YAHOO.util.Dom.removeClass('rn' + this.previousSection, 'active');
            }
        }
    } else {
        //right nav link has been selected
        window.scrollTo(0,top);
        this.loadAJAX(SAS.Nav.middleServletPath + section, this.previousSection);
    }
    this.previousSection = section;
};

SAS.Nav.loadAJAX = function(url, previousSection) {
    function successHandler(responseObj) {
        SAS.Nav.fillMiddleColumn(responseObj, previousSection);
    }
    function failureHandler(responseObj) {
        // Fallback
        YAHOO.util.Dom.removeClass("rn" + previousSection, "active");
    }
    YAHOO.util.Connect.asyncRequest("GET", url,
            {
                success:successHandler,
                failure:failureHandler
            }
        );
};

SAS.Nav.fillMiddleColumn = function(responseObj, previousSection) {
    var d, ar, i, len, newScript;
    YAHOO.util.Dom.setStyle('inlinecontent', 'display', 'none');
    YAHOO.util.Dom.get("middleAjax").innerHTML = responseObj.responseText;
    YAHOO.util.Dom.removeClass('middleCol', 'displayOff');
    YAHOO.util.Dom.addClass("rn" + SAS.Nav.currentSection, 'active');
    YAHOO.util.Dom.removeClass('rn1', 'active');
    if (SAS.Nav.currentSection == 6) {
        SAS.Local.hideDescription = SAS.Local.whitepaperHideDescription;
        SAS.Local.showDescription = SAS.Local.whitepaperShowDescription;
    } else if (SAS.Nav.currentSection == 7) {
        SAS.Local.hideDescription = SAS.Local.webcastHideDescription;
        SAS.Local.showDescription = SAS.Local.webcastShowDescription;
        d = new Date();
        ar = YAHOO.util.Dom.getElementsByClassName(d.getFullYear().toString() + (d.getMonth() + 1) + d.getDate().toString(), 'span', 'middleCol');
        for (i = 0, len = ar.length; i < len; i++) {YAHOO.util.Dom.removeClass(ar[i], 'displayOff');}
    } else if (SAS.Nav.currentSection == 11) {
        ar = YAHOO.util.Dom.getElementsByClassName('blogScript', 'div', 'middleCol');
        for (i = 0, len = ar.length; i < len; i++) {
            newScript = document.createElement("script");
            newScript.type = "text/javascript";
            newScript.src = ar[i].innerHTML.replace(/&amp;/gi, "&");
            ar[i].appendChild(newScript);
        }
    }
    if (previousSection) {
        YAHOO.util.Dom.setStyle('content' + previousSection, 'display', 'none');
        YAHOO.util.Dom.removeClass('tab' + previousSection + 'a', 'active');
        YAHOO.util.Dom.removeClass('rn' + previousSection, 'active');
    }
};

SAS.Nav.initializeNavigationBars = function () {
    var anchors, i, len, anchor, href, section, currentSection;

    //add listeners to links
    anchors = YAHOO.util.Dom.getElementsByClassName('rn', 'a', 'inner_container');
    for (i = 0, len = anchors.length; i < len; i++) {
        anchor = anchors[i];
        YAHOO.util.Event.addListener(anchor, "click", function (evt) {
            href = this.getAttribute("href");
            section = href.substr(href.indexOf('#') + 9) || "1";
            try {
                YAHOO.util.History.navigate("section", section);
            } catch (e) {
                this.loadSection(section);
            }
            SAS.Util.callGA(section,'');
            YAHOO.util.Event.preventDefault(evt);
        });
    }
    // This is the tricky part... The window's onload handler is called when the
    // user comes back to your page using the back button. In this case, the
    // actual section that needs to be loaded corresponds to the last section
    // visited before leaving the page, and not the initial section. This can
    // be retrieved using getCurrentState:
    currentSection = YAHOO.util.History.getCurrentState("section");
    this.loadSection(currentSection);
};

SAS.Util = {
    
    // icons for abstracts 
    toggleShow:null,
    toggleHide:null,
    hideDescription:null,

    // array to keep track of which abstracts are in an expanded state 
    expandedArray: [],

    // preload images 
    preloadImages: function() {
        // toggle icons for abstracts
        this.toggleShow = new Image();
        this.toggleHide = new Image();
        this.toggleShow.src = "http://www.sas.com/images/whitepapers/toggle_show.gif";
        this.toggleHide.src = "http://www.sas.com/images/whitepapers/toggle_hide.gif";  
    },

    // toggle controls which abstracts are hidden & displayed 
    toggle: function(node) {
        id = node.getAttribute("id");
        if (this.expandedArray[id]) {
            //this id was expanded so collapse it
            this.hideAbstract(node);
            //remove this id from array
            this.expandedArray[id] = "";    
        } else {
            //not yet in the array yet so just expand it & add to array
            this.showAbstract(node);
            this.expandedArray[id] = id;
        }
    },

    // make an abstract visible 
    showAbstract: function(node) {
        node.getElementsByTagName("img")[0].setAttribute("src", this.toggleHide.src);
        node.getElementsByTagName("span")[0].firstChild.nodeValue = SAS.Local.hideDescription;
        // for IE:
        node.setAttribute("className", "toggleTabOn");
        node.parentNode.nextSibling.setAttribute("className", "abstractOn");
        // for FF:
        node.setAttribute("style", "background:#C8E2F9;");
        node.parentNode.nextSibling.setAttribute("style", "visibility:visible;display:block;background-color:#C8E2F9;padding:5px;");
    },

    // conceal an abstract 
    hideAbstract: function(node) {
        node.getElementsByTagName("img")[0].setAttribute("src", this.toggleShow.src);
        node.getElementsByTagName("span")[0].firstChild.nodeValue = SAS.Local.showDescription;
        // for IE:
        node.setAttribute("className", "toggleTabOff");
        node.parentNode.nextSibling.setAttribute("className", "abstractOff");
        // for FF:
        node.setAttribute("style", "");
        node.parentNode.nextSibling.setAttribute("style", "");
    },
    
    playFile: function(mp3) {
        window.open((SAS.Nav.podcastDomain+'/resources/podcast/sascast_player.html?my_mp3='+mp3),'player','width=480,height=180,left=0,top=0,resizable=yes,scrollbars=no,toolbar=no,location=no,directories=no,menubar=no,copyhistory=no');
    },

    ssDisplay: function(ssid) {
        window.open("http://www.sas.com/resources/screenshots/screenshot_display.html?ssid="+ssid+"","ss_display","width=820,height=650,left=0,top=0,resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,menubar=no,copyhistory=no");
    },

    callGA: function(i,file) {
        var path, intTrim;
        path = document.location.pathname;
        if (path.indexOf(".htm")>-1) {
            //filename and extension are in the path
            intTrim = path.indexOf(".htm");
            path = path.substring(0,intTrim);
        } else {
            //filename and extension aren't in the path
            path = path + "index";
        }
        if (i == 1) {path+=".html";}
        else if (i == 2) {path+="_benefits";}
        else if (i == 3) {path+="_features";}
        else if (i == 4) {path+="_screenshots";}
        else if (i == 5) {path+="_system-requirements";}
        else if (i == 6) {path+="_whitepapers";}
        else if (i == 7) {path+="_webcasts";}
        else if (i == 8) {path+="_news";}
        else if (i == 9) {path+="_events";}
        else if (i == 10) {path+="_podcasts";}
        else if (i == 11) {path+="_blogs";}
        else if (i == 12) {path+="_customer-stories";}
        else if (i == 13) {path+="_awards";}
        else if (i == 14) {path+="_books";}
        else if (i == 15) {path+="_market-research";}
        else if (i == 16) {path+="_analyst-viewpoints";}
        else if (i == 17) {path+="_interactive-tour";}
        else if (i == 18) {path="/pdf"+path+"_"+file;}
        else if (i == 19) {path+="_training";}
        else if (i == 20) {path+="_customer-support";}
        try{pageTracker._trackPageview(path);}catch(e){}
    },

    openTour: function(url,h,w) {
        window.open(''+url+'','tour','width='+w+',height='+h+',left=0,top=0,resizable=yes,scrollbars=no,toolbar=no,location=no,directories=no,menubar=no,copyhistory=no');
        this.callGA(17,'');
    }
};

/* Load the function to fire when back-forward buttons are clicked */
YAHOO.util.History.register("section", SAS.initialSection, function (state) {SAS.Nav.loadSection(state);});

/* Fire when document is fully loaded */
YAHOO.util.History.onReady(function () {
    SAS.Nav.initializeNavigationBars();
});

/* Initialize the browser history management library */
try {
    YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
} catch (e) {
    // The only exception that gets thrown here is when the browser is
    // not supported (Opera, or not A-grade) Degrade gracefully.
    SAS.Nav.loadSection(SAS.initialSection);
}

SAS.Util.preloadImages();

if (YAHOO.env.ua.ie == 6) {
    try {document.execCommand("BackgroundImageCache", false, true);} catch(e) {}
}

/* instruct GA to skip the usual call to trackPageview */
var SAStrack = 1;