$(function() {
    // <IE7 Navigation
    if ($.browser.msie && $.browser.version < 7) {
        // Emulate menu hover
        $("#navigation li").mouseover(function() {
            $(this).addClass("hover");
        });
        $("#navigation").mouseout(function() {
            $("li", this).removeClass("hover");
        });

        // Fix SELECT bleedthrough
        $("#navigation li").append('<iframe src="javascript:\'\'"></iframe>');

        // Style required labels
        $("span.required + label").addClass("required");
    }
    $("div.marketplace a.view").click(function() {
        var height  = 10;
        var width   = 10;
        var img     = $(this).attr("href");
        var oimg    = new Image();
        oimg.src    = img;
        oimg.onload = function() {
            height      = oimg.height + 20;
            width       = oimg.width + 20;
            var top     = (screen.height - (height + 110)) / 2;
            var left    = (screen.width - width) / 2;
            window.open(img, 'popup', 'width=' + width + ',height=' + height + ',top='+ top + ',left='+ left +',toolbar=no,scrollbars=no,menubar=no');
        }
        return false;
    });
});

function bookmark() {
    var url     = window.location;
    var title   = document.title;

    // Firefox, Safari, etc.
    if (window.home || $.browser.safari) {
        var ua      = navigator.userAgent.toLowerCase();
        var isMac   = (ua.indexOf('mac') != -1);
        var button  = isMac ? 'Command' : 'Ctrl';
        alert('Please press '+ button +' + D to bookmark our site.');
        // window.sidebar.addPanel(title, url, "");
    // Internet Explorer
    } else if (window.external) {
		window.external.AddFavorite(url, title);
    }
}
function popUp(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=700,height=600');");
}
function resetAndHide(ele) {
    $("dd.mandatory").remove();
    $(ele).parents("form").hide();
}
function toggleSearchOptions() {
    var arrow = "right";
    $("#search-options").slideToggle();
    if ($("#search-options.hidden").length > 0) {
        $("#search-options").removeClass("hidden");
        arrow = "down";
    } else {
        $("#search-options").addClass("hidden");
    }
    $("img.search-options-twisty").attr("src", "/images/twisty_" + arrow + ".gif");
}
function validate(form) {
    var valid = true;
    $("dd.mandatory").remove();
    $("#" + form + " :input.required").each(function() {
        if ($(this).val() === "") {
            if (valid) {
                // Give focus to the first invalid item
                $(this).focus();
            }
            $(this).parents("dd").after("<dd class='mandatory'>This is a required field</dd>");
            valid = false;
        }
    });
    $("#" + form + " :input.email").each(function() {
        if ($(this).val() !== "") {
            var isEmail = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$/i.test($(this).val());
            if (!isEmail) {
                if (valid) {
                    // Give focus to the first invalid item
                    $(this).focus();
                }
                $(this).parents("dd").after("<dd class='mandatory'>Please input a valid e-mail address</dd>");
                valid = false;
            }
        }
    });
    $("#" + form + " :input.pdf").each(function() {
        if ($(this).val() !== "") {
            var isPdf = /\.pdf$/i.test($(this).val());
            if (!isPdf) {
                if (valid) {
                    // Give focus to the first invalid item
                    $(this).focus();
                }
                $(this).parents("dd").after("<dd class='mandatory'>Please select a .PDF file</dd>");
                valid = false;
            }
        }
    });
    $("dd.mandatory+dd.mandatory").remove();
    return valid;
}

// Legacy dependencies
function MM_openBrWindow(theURL, winName, features) {
    window.open(theURL, winName, features);
}