$(document).ready(function() {
    
    //bVersion = getInternetExplorerVersion();
    if($('.secondLevel').length) {
        setSecondLevelNav();
    }
    if($('.thirdLevel').length) {
        setThirdLevelNav();
    }
});

function setSecondLevelNav() {

    $('.secondLevel, .primaryNavigation, .secondLevel ul').css({'position': 'relative'});
    firstLevelSelected = $('.primaryNavigation .selected').position();
    firstLevelSelectedWidth = $('.primaryNavigation .selected').width();
    firstLevelFirst = $('.primaryNavigation li:first').position();
    secondLevelPosition = $('.secondLevel ul').position();
    secondLevelWidth = $('.secondLevel ul').width();
    if(secondLevelWidth < 700) {
        secondLevelOffset = (firstLevelSelected.left + (firstLevelSelectedWidth / 2)) - (secondLevelWidth / 2);
        if((secondLevelOffset + secondLevelWidth) > 700) {
            secondLevelTrim = (secondLevelOffset + secondLevelWidth) - 700;
            $('.secondLevel ul').css({'float': 'right'});
        } else {
            $('.secondLevel ul').css({left: secondLevelOffset+'px'});
        }
    } 
} 

function setThirdLevelNav() {
    
    $('.thirdLevel, .thirdLevel ul').css({'position': 'relative'});
    secondLevelSelected = $('.secondLevel .selected').position();
    secondLevelSelectedWidth = $('.secondLevel .selected').width();
    secondLevelPosition = $('.secondLevel ul').position();
    secondLevelSelectedPosition = secondLevelPosition.left+secondLevelSelected.left;
    //thirdLevelPosition = $('.thirdLevel ul').position();
    thirdLevelWidth = $('.thirdLevel ul').width();
    
    if(thirdLevelWidth < 700) {
        thirdLevelOffset = (secondLevelSelectedPosition + (secondLevelSelectedWidth / 2)) - (thirdLevelWidth / 2);
        if((thirdLevelOffset + thirdLevelWidth) > 700) {
            $('.thirdLevel ul').css({'float': 'right'});
        } else {
            $('.thirdLevel ul').css({left: thirdLevelOffset+'px'});
        }
    } 
}

function getInternetExplorerVersion() {
	var rv = -1;
	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;
}

function init() {
    $('a.popupLink').bind('click', function(e) { popupWindow($(this).attr('href'), $(this).attr('title').replace(/[^A-z]/ig, ''),'width=550, height=400, scrollbars'); e.preventDefault(); });
}

function popupWindow(url, name, attributes){
	var newWindow = window.open(url, name, attributes);
	newWindow.focus();
}
 