﻿/// <reference path="jquery-1.4.1.min.js" />
/// <reference path="jquery-1.4.1-vsdoc.js" />

//The document.ready() method below is a jQuery version of the utilities.js file.
jQuery(function () {

    var timeout = 600;
    var queue = [];

    var queueFlush = function () {
        while (queue.length) {
            clearTimeout(queue[0][0]);
            queueTimeout();
        }
    }

    var queueTimeout = function () {
        if (queue.length) {
            jQuery(queue.shift()[1]).removeClass("sfhover");
        }
    }

    // Check to determine whether the browser is ie7
    var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;

    // Any LI elments in either the international drop down or main navigation will get the "sfHover" class
    // added to it on mouseover
    jQuery("#intl li, #mainnav li").mouseover(
        function () {
            queueFlush();
            jQuery(this).addClass("sfhover");
        }
    );

    // focus and blur handlers for keyboard based navigation.
    jQuery("#intl li, #mainnav li").focus(
        function () {
            queueFlush();
            jQuery(this).addClass("sfhover");
        }
    );

    jQuery("#intl li, #mainnav li").blur(
        function () {
            queue.push([setTimeout(queueTimeout, timeout), jQuery(this)]);
        }
    );

    // For some reason ie7 doesn't like onmouseleave.  It does like onmouseout though.
    if (ie7) {
        // onmouseout, remove the sfhover class
        jQuery("#intl li, #mainnav li").mouseout(
            function () {
                queue.push([setTimeout(queueTimeout, timeout), jQuery(this)]);
            }
        );
    }
    else {
        // All other browsers, even ie6, can deal with onmouseleave
        // onmouseout, remove the sfhover class
        jQuery("#intl li, #mainnav li").mouseleave(
            function () {
                queue.push([setTimeout(queueTimeout, timeout), jQuery(this)]);
            }
        );
    }
});

// BEGIN Chat Agent Supporting Javascript 

// This method will get called if there is an agent available.  It dictates the content of the callout once an agent is available
function agents_available() {
    document.getElementById('smartbutton').innerHTML =
    '<a href="" onClick="window.open(\'http://www.Western-HR.com/Handlers/chat.html\',\'instantservice_advanced_form\',\'width=500,height=650,scrollbars=0\');' +
    'return false;"><strong>Live Chat</strong><span class=\'desc\'>Chat with one of our customer service representatives.</span><span class=\'link\'>Find Out More</span></a>';
    return true;
}

// This method will get called if there is not an agent available.  It dictates the content of the callout if an agent is not available
function agents_not_available() {
    document.getElementById('smartbutton').innerHTML =
            '<strong>Live Chat</strong><span class=\'desc\'>No representatives are currently available. Please try again later.</span><span class=\'link\'>Unavailable</span></a>';
    return true;
}

// END Chat Agent Supporting Javascript 

function addEvent(obj, type, fn) {
    if (obj) {
        if (obj.addEventListener) {
            obj.addEventListener(type, fn, false);
            EventCache.add(obj, type, fn);
        }
        else if (obj.attachEvent) {
            obj["e" + type + fn] = fn;
            obj[type + fn] = function () { obj["e" + type + fn](window.event); }
            obj.attachEvent("on" + type, obj[type + fn]);
            EventCache.add(obj, type, fn);
        }
        else {
            obj["e" + type + fn] = fn;
            obj["on" + type] = obj["e" + type + fn];
        }
    }
}

var EventCache = function () {
    var listEvents = [];
    return {
        listEvents: listEvents,
        add: function (node, sEventName, fHandler) {
            listEvents.push(arguments);
        },
        flush: function () {
            var i, item;
            for (i = listEvents.length - 1; i >= 0; i = i - 1) {
                item = listEvents[i];
                if (item[0].removeEventListener) {
                    item[0].removeEventListener(item[1], item[2], item[3]);
                };
                if (item[1].substring(0, 2) != "on") {
                    item[1] = "on" + item[1];
                };
                if (item[0].detachEvent) {
                    item[0].detachEvent(item[1], item[2]);
                };
                item[0][item[1]] = null;
            };
        }
    };
} ();
addEvent(window, 'unload', EventCache.flush); // fixes IE memory leak issues

// Bing Map Supporting Methods

function toggleUSAmap() {
    jQuery('#viewMap').hide();
    jQuery('#usaMap').show();
    jQuery('#collapseMap').show();
    makeVEMap();
}

function hideUSAmap() {
    jQuery('#usaMap').hide();
    jQuery('#collapseMap').hide();
    jQuery('#viewMap').show();
}

function wheelCallback(e) {
    //returning true to cancel default action.
    window.scrollBy(0, -5 * e.mouseWheelChange);
    return true;
}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

