$(function() {
    $("#header-nav td").hover(
        function() {$(this).addClass("hover")},
        function() {$(this).removeClass("hover")}
    );

    $(document).keypress(function(event) {
        if (event.keyCode == "13") {
            $("#searchForm").submit();
        }
    })

    $("#btnSelectAllCheck").click(function() {
        if ($(this).attr("checked")) {
            $("#searchSizeBoxes").find("input[type=checkbox]:not('#btnSelectAllCheck')").attr("checked", true).click(function() {
                $("#searchSizeBoxes input[type=checkbox]:not('#btnSelectAllCheck')").unbind("click");
                $("#btnSelectAllCheck").attr("checked", false);
            });
        } else {
            $("#searchSizeBoxes").find("input[type=checkbox]:not('#btnSelectAllCheck')").removeAttr("checked").unbind("click");
        }
    });
});

function setCookie(cname, cval) {
    expire = new Date(((new Date()).getTime() + (60 * 60 * 24 * 3)));
    document.cookie = cname+"="+escape(cval)+";expires="+expire.toGMTString()+";path=/";
}

function displayCDRequest() {
    testwindow = window.open("http://www.gangoeditions.com/cdrequest_form.cfm", "cdrequestform","width=600px,height=530px,resizable=1,scrollbars=1,status=1,location=0");
    testwindow.moveTo(150,150);
}

function customSizeForm(imID, imCode) {
    customSizeWin = window.open("http://www.gangoeditions.com/customsize_form.php?imID="+imID+"&imCode="+imCode, "customsizeform","width=600px,height=500px,resizable=1,scrollbars=1,status=1,location=0");	
    customSizeWin.moveTo(120,150);
}

function showContactPopup()	{
	testwindow = window.open("http://www.gangoeditions.com/contact_form.cfm", "contactform","width=550px,height=500px,resizable=1,scrollbars=1,status=1,location=0");
	testwindow.moveTo(150,150);
}

function newImageDetail(imageURL, width, height) {
    $("body").append(
        $("<div>").attr("id", "imageDetailContainer").html(
            $("<img>").attr("src", imageURL).after("<br />").after(
                $("<a>").attr("href", "javascript:void(0)").html("Close Window").click(function() {
                    $("#imageDetailContainer").remove();
                    $("#mask").remove();
                })
            )
        )
    ).append($("<div>").attr("id", "mask"));

    $(window).resize(function() {
        var newSize = maxResize(
            ($(window).width() - 100),
            ($(window).height() - 100),
            {width: width, height: height}
        );
            
        $("#imageDetailContainer").css({
            left: (($(window).width() - newSize.width) / 2),
            top: (($(window).height() - newSize.height) / 2)
        }).find("img").css(newSize);
    }).resize();
}

function maxResize(maxWidth, maxHeight, size) {
    if ((size.width > maxWidth) || (size.height > maxHeight)) {
        var widthRatio = size.width / maxWidth;
        var heightRatio = size.height / maxHeight;
        var ratio = Math.max(widthRatio, heightRatio);

        size.width = size.width / ratio;
        size.height = size.height / ratio;
    }

    return size;
}
