/// <reference path="bI.js" />

// Desktop radar
function openWindow(newURL, width, height)
{
	newWindow = window.open(newURL, 'newWin', 'toolbar=no,location=no,scrollbars=no,width=' + width + ',height=' + height)
};

// Form validation
function validate_tags(source, args)
{
	var theValue;
	theValue = args.Value.toLowerCase();
	
	if ((theValue.match(/<a /)) || (theValue.match(/<img /)) || (theValue.match(/<script /)) || (theValue.match(/<object /)))  {
	     args.IsValid = false;
         return;
     }
     else {
     	args.IsValid = true;
     }
};

function searchSubmit() {
    var re = /[\~\\,\/\*\-\+<>=;'@%\(\)\{\}\[\]\|&\?\$"]/g; 
    $("#searchTerm").val($("#searchTerm").val().replace(re, " "));
};

// MAKE THIS SITE HOME PAGE
function makeStationYourHomePage(stationName,url) {
    if (navigator.userAgent.toLowerCase().indexOf("msie") != -1) {
        document.body.style.behavior='url(#default#homepage)';
        document.body.setHomePage(window.location.href);
    }
};

//Navigation menu scripts
$(document).ready(function ()
{
    var navTopSection = $(".bI-topnav-sc");
    var navTopBox = $("#navTopBox")
    var navTopLowerBar = $("#navTopLowerBar");
    var navTopSubLinks = $("#navTopSubLinks");
    var navTopSubLinksHtml = navTopSubLinks.html();
    var navOnTimeDelay = 100; //controls the hover display speed
    var navOffTimeDelay = 300; //controls the unhover display speed
    var navOffTimeDelayAfterClick = 30000;
    var navTopTimerId; //stores the timer

    //click: section link
    $(".bI-topnav-bc A, .bI-topnav-sc A").click(function (event)
    {
        clearTimeout(navTopTimerId);
        navOffTimeDelay = navOffTimeDelayAfterClick;
        navOnTimeDelay = 0;
        showSelectedSection($(this).parent());
    });

    //hover: over sections EXCEPT the currently selected section
    $(".bI-topnav-bc A").hover(
    //Mouse Over event
        function (event)
        {
            clearTimeout(navTopTimerId);
            var element = $(this).parent();
            navTopTimerId = setTimeout(function ()
            {
                showSelectedSection(element);
            }, navOnTimeDelay);
        },

    //Mouse leave event
        function (event)
        {
            clearTimeout(navTopTimerId);
        }
    );

    //hover: current section
    navTopSection.hover(
    //Mouse Over Default event
        function (event)
        {
            clearTimeout(navTopTimerId);
            navTopTimerId = setTimeout(showCurrentSection, navOnTimeDelay);
        },

    //Mouse leave event
        function (event)
        {
            clearTimeout(navTopTimerId);
        }
    );

    //click: subsection link
    $("#navTopSubLinks A").click(function (event)
    {
        subsectionClick.apply(this);
    });

    //mouseleave: the menu container will show default links after a delay
    navTopBox.bind("mouseleave", function (event)
    {
        clearTimeout(navTopTimerId);
        navTopTimerId = setTimeout(showCurrentSection, navOffTimeDelay);
    });

    //mouseover: resets the timeout if the user accidently leaves the menu
    navTopLowerBar.bind("mouseover", function (event)
    {
        clearTimeout(navTopTimerId);
    });

    var subsectionClick = function ()
    {
        navOffTimeDelay = navOffTimeDelayAfterClick;
        $(".bI-subnav-sc").addClass("bI-subnav-bc").removeClass("bI-subnav-sc").prev().addClass("bI-subnav-bs").removeClass("bI-subnav-ss");
        $(this).parent().addClass("bI-subnav-sc").removeClass("bI-subnav-bc").prev().addClass("bI-subnav-ss").removeClass("bI-subnav-bs");
        $("#navTopSubLinks A").unbind();
    };

    var subsectionMouseEnter = function ()
    {
        $(this).parent().addClass("bI-subnav-sc").removeClass("bI-subnav-bc").prev().addClass("bI-subnav-ss").removeClass("bI-subnav-bs");
    };

    var subsectionMouseLeave = function ()
    {
        $(this).parent().addClass("bI-subnav-bc").removeClass("bI-subnav-sc").prev().addClass("bI-subnav-bs").removeClass("bI-subnav-ss");
    };

    $(".bI-subnav-bc A").hover(subsectionMouseEnter, subsectionMouseLeave);

    var showCurrentSection = function ()
    {
        var selected = $(".bI-topnav-sc");
        selected.addClass("bI-topnav-bc").removeClass("bI-topnav-sc");
        selected.prev().addClass("bI-topnav-bs").removeClass("bI-topnav-sl");
        selected.next().addClass("bI-topnav-bs").removeClass("bI-topnav-sr");
        navTopSection.addClass("bI-topnav-sc").removeClass("bI-topnav-bc");
        navTopSection.prev().addClass("bI-topnav-sl").removeClass("bI-topnav-bs");
        navTopSection.next().addClass("bI-topnav-sr").removeClass("bI-topnav-bs");
        $("#navTopSubLinks A").unbind();
        navTopSubLinks.html(navTopSubLinksHtml);
        $(".bI-subnav-bc A").hover(subsectionMouseEnter, subsectionMouseLeave);
    };

    var showSelectedSection = function (element)
    {
        var selected = $(".bI-topnav-sc");
        selected.addClass("bI-topnav-bc").removeClass("bI-topnav-sc");
        selected.prev().addClass("bI-topnav-bs").removeClass("bI-topnav-sl");
        selected.next().addClass("bI-topnav-bs").removeClass("bI-topnav-sr");
        element.addClass("bI-topnav-sc").removeClass("bI-topnav-bc");
        element.prev().addClass("bI-topnav-sl").removeClass("bI-topnav-bs");
        element.next().addClass("bI-topnav-sr").removeClass("bI-topnav-bs");

        $("#navTopSubLinks A").unbind();

        var section = bINav_sections[element.attr("id")];
        var markup = [];
        for (var i = 0; i < section.subsections.length; i++)
        {
            markup[i] = '<li class="bI-subnav-bc"><a href="' + section.subsections[i].href + '" target="' + (section.subsections[i].target ? section.subsections[i].target : '_top') + '"' + (section.subsections[i].title ? ' title="' + section.subsections[i].title + '"' : '') + '>' + section.subsections[i].text + '</a></li>';
        }
        var linkSeparator = '<li class="bI-subnav-bs"></li>';
        navTopSubLinks.html(linkSeparator + markup.join(linkSeparator) + linkSeparator);

        $("#navTopSubLinks A").click(function (event)
        {
            clearTimeout(navTopTimerId);
            subsectionClick.apply(this);
        });

        $(".bI-subnav-bc A").hover(subsectionMouseEnter, subsectionMouseLeave);
    };
});

function bIShareUploadLoader(anchor, base) {

    // One time only, never return here again once thickbox is loaded;
    $(anchor).removeAttr('onclick');

    // Do setup for thickbox
    $(anchor).addClass("thickbox");

    // Load thickbox, and when loaded, fire the click handler it installed
    bI.html(
        anchor,
        '<link href="' + base + '/css/thickbox.css" type="text/css" rel="stylesheet"><script src="' + base + '/Javascript/jquery.thickbox.js"><\/script>',
        function () {
            $(anchor).click();
        }
    );

    // disable default action (i.e., don't link to the href here, let thickbox do it!)
    return false;
}
