/* Run onload scripts */

window.onload = function()
{
    try { adjustLinks(); } catch (err) {}
    try { startList(); } catch (err) {}
    try { linkableDivs(); } catch (err) {}
    try { addTrackClicks(); } catch (err) {}
}

/**
 * Adds css hover ability to LI list items for IE
 *
 * from http://alistapart.com/articles/dropdowns/
 */
startList = function()
{
    if (document.all && document.getElementById)
    {
        if (document.getElementById("topnav"))
        {
            navRoot = document.getElementById("topnav").firstChild;

            for (i=0; i < navRoot.childNodes.length; i++)
            {
                node = navRoot.childNodes[i];
                if (node.nodeName=="LI")
                {
                    node.onmouseover=function()
                    {
                        this.className += " hover";
                    }

                    node.onmouseout=function()
                    {
                        this.className=this.className.replace(" hover", "");
                    }
                }
            }
        }

        if (document.getElementById("leftnav")) {
            listItems = document.getElementById("leftnav").getElementsByTagName("LI");
            for (i=0; i < listItems.length; i++)
            {
                node = listItems[i];
                if (node.nodeName=="LI")
                {
                    node.onmouseover=function()
                    {
                        this.className += " hover";
                    }

                    node.onmouseout=function()
                    {
                        this.className=this.className.replace(" hover", "");
                    }
                }
            }

        }
    }
}

// Adjust links for AgriSuccess and AgNews areas to keep duplicate sections in respective areas
// TODO this needs to be documented better and probably coded better - Ryan
adjustLinks = function()
{
    var content = document.getElementById("main-content");
    var links = content.getElementsByTagName("a");

    for (var i = 0; i < links.length; i++)
    {
        var href, href_arr, href_path, href_id, has_id, query_string;

        href = links[i].getAttribute("href");

        if (href == null || href == "")
            continue;
        if (href.indexOf("/goto.asp") == 0 || href.indexOf("http://") == 0 || href.indexOf("mailto:") == 0 || href.indexOf("ftp://") == 0 || href.indexOf("javascript:") == 0)
            continue;
        if (href.indexOf("?") != -1)
            continue;
        if (href.indexOf("#") != -1)
            continue;

        // Make relative (for IE)
        // TODO should this be before the previous if statements?
        href = href.replace("http://" + location.host + "/", "/");


        href_arr = href.split(/#/);
        if (href_arr.length > 1)
        {
            has_id = true;
            href_path = href_arr[0];
            href_id = href_arr[1];
        }
        else
        {
            has_id = false;
            href_path = href;
        }

        if (reuse == true) {
            query_string = "?main=" + navmain + "&sub1=" + navsub1 + "&sub2=" + navsub2 + "&sub3=" + navsub3;
        }
        else
        {
            query_string = "";
        }

        links[i].setAttribute("href", href_path + query_string);
    }
}

/**
 * Allows any <div> element with the class "clickable" to become a link.
 * The link will be whatever the first <a> element links to.
 */
linkableDivs = function()
{
    var divs = document.getElementsByClassName("clickable", "div");

    for (var i = 0; i < divs.length; i++)
    {
        var anchor, anchor_href, anchor_target;
        anchor = divs[i].getElementsByTagName("a")[0];

        if (typeof anchor == "undefined")
            continue;

        anchor_href = anchor.getAttribute("href") || "";
        anchor_target = anchor.getAttribute("target") || "";

        if (anchor_href == "")
            continue;
        else
        {
            divs[i].onclick = (function() {
                                   var href = anchor_href;
                                   var target = anchor_target;
                                   return function() { link_to(href, target); return false; }
                               })();

        }
    }
}

link_to = function(url, window)
{
    if (window == "")
        location.href = url;
    else if (window == "_blank")
        top.window.open(url);
    else if (window[0] != "_")
        top.window.open(url, window);
    else
        location.href = url;
}

addTrackClicks = function() {
}

track_click = function () {
}