﻿$(document).ready(function()
{
    //select top menu link based on page name loaded
    var isEmptyUrl = document.location.href.length > 0 && document.location.href.charAt(document.location.href.length - 1) == '/';
    var links = $('#navigation > ul > li > a');
    for (var i = 0; i < links.length; i++)
    {
        var link = file_name_only(links[i].href);
        if (link.indexOf(file_name_only(document.location.href)) > -1 || (isEmptyUrl && link == "default"))
        {
			var ver = getInternetExplorerVersion();
			if (ver > -1) {
				if (ver > 7) {
					links[i].setAttribute('class', 'active');
				}
				else {
					links[i].setAttribute('className', 'active');
				}
			}
			else 
				links[i].setAttribute('class', 'active');
            break;
        }
    }

    links = $('#navigation > ul > li');
    for (var i = 0; i < links.length; i++)
    {
        links[i].setAttribute("onmouseover", "this.className==''?this.className='nav_hover':this.className+=' nav_hover';");
        links[i].setAttribute("onmouseout", "this.className.indexOf('sperator')>=0?this.className='sperator':this.className='';");
    }

    //mark external links
    $('a.external').attr('title', 'External Link (open in a new window)');
    $('a.external').attr('target', '_blank');

    $('a.external-white').attr('title', 'External Link (open in a new window)');
    $('a.external-white').attr('target', '_blank');

    $('div.back2top > a').click(function() { $.scrollTo({ top: 0, left: 0 }, 1500) });
});

function file_name_only(str)
{
    try
    {
        return str.substring(str.lastIndexOf('/') + 1, str.lastIndexOf('.'))
    }
    catch (e)
    {
        return "";
    }
}

function getInternetExplorerVersion() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}
