function addToFavorites(url, name) { 
    if (window.sidebar) { // Mozilla Firefox 
        window.sidebar.addPanel(name, url, ""); 
    } else if (window.external) { // IE 
        window.external.AddFavorite(url, name); 
    } else if (window.opera && window.print) { 
        window.external.AddFavorite(url, name); 
    } else { 
        alert("Sorry! Your browser doesn't appear to support this function."); 
    } 
} 
function ValidateAndSubmit (evt) {
    var $group = $(evt.currentTarget).closest('fieldset');
    var isValid = true;
    var firstInvalidItem = null;
    var j = 0;
    $group.find(':input').each(function (i, item) {
        if (!$(item).valid()) {
            isValid = false;
            if (++j == 1) firstInvalidItem = item;
        }
    });
    if(!isValid) {
        evt.preventDefault();
        firstInvalidItem.focus();
    }
}
$(document).ready(function(){
    $("#main-nav ul.level_2").children("li [class!='header']").prepend("<span>&gt;</span>");
    $("#main-nav ul.level_3").children("li [class!='header']").prepend("<span>&gt;</span>");
    $("#main-nav ul.level_1")
        .supersubs({
            minWidth: 8,
            maxWidth: 20,
            extraWidth: 1
        })
        .superfish({
            autoArrows: false,
            pathLevels: 1
        });
    $("ul.level_2 a.selected")
        .parent().parent().prev()
        .addClass('selected');
    $("a.colorbox")
        .colorbox({
            photo: true
        });
    $('ul.meta-links').prepend('<li class="print"><a href="#">Print page</a></li><li class="bookmark"><a href="#">Bookmark page</a></li><li class="email"><a href="#">Email page</a></li>');
    $('ul.meta-links li.print a').click(function() {
        window.print();
        return false;
    });
    // chrome does not permit addToFavorites() function by design 
    if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) { 
        $('ul.meta-links li.bookmark a').attr({ 
            title: 'This function is not available in Google Chrome. Click the star symbol at the end of the address-bar or hit Ctrl-D to create a bookmark.', 
            href: 'javascript:return false' 
        }) 
        .css({opacity: .25}) // dim the button/link
        .click(function() {
            alert('This function is not available in Google Chrome. Click the star symbol at the end of the address-bar or hit Ctrl-D to create a bookmark.');
        });
    }
    else {
        $('ul.meta-links li.bookmark a').attr({ 
            title: document.title, 
            href: 'javascript:addToFavorites("'+encodeURIComponent(window.location.href)+'", "'+document.title+'")'
        }) 
    }
    $("ul.meta-links li.email a").attr('href', 'mailto:?subject='+encodeURIComponent(document.title)+'&body='+encodeURIComponent(window.location.href));
    $('#marvalMasterForm').validate({
        onsubmit: false,
        errorPlacement: function(error,element) { return true; }
    });
    $('fieldset .causesValidation').click(ValidateAndSubmit);
    $('fieldset :text').keydown(function (evt) {
        if (evt.keyCode == 13) {
            evt.preventDefault();
            var $nextInput = $(this).nextAll(':input:first');
            if ($nextInput.is(':submit')) $nextInput.click();
            else $nextInput.focus();
        }
    });
});
