﻿var ie7 = false;
var menu_item = 0;
var menu_tab = 0;

function menu_close() {
    if (menu_item) menu_item.css("display", "none");
    if (menu_tab) menu_tab.removeClass("hover");
    if (menu_tab) menu_tab.parent().parent().find("li.separator").removeClass("brownd");
	
    menu_selected();
}

function menu_open() {
    menu_close();

	menu_item = $(this).find("ul").eq(0).css("display", "block");
	menu_tab = $(this).find("a").eq(0).addClass("hover");
	
	$(this).prev("li.separator").addClass("brownd");
	$(this).next("li.separator").addClass("brownd");
}

function menu_selected() {
    if ($("#menuTop li a.selected").length > 0) {
    	$("#menuTop li a.selected").parent().prev("li.separator").addClass("brownd");
	    $("#menuTop li a.selected").parent().next("li.separator").addClass("brownd");
    }
}

function popup(url, height, width) {
    window.open(url, '', 'height=' + height + 'location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0, width=' + width);
}

function productList() {
    $("#product-list tr td.product").click(function() {
        $("#" + $(this).parent().attr("id") + "info div:first").slideToggle("fast");
    });
}

function productModel(dropdown, imgId) {
    img = $("#" + imgId);
    imgBig = $("#modelImgBig" + dropdown.options[dropdown.selectedIndex].value);
    imgHr = $("#modelImgHr" + dropdown.options[dropdown.selectedIndex].value);
    
    if (img.attr("rel") == undefined || img.attr("rel") == "") {
        img.attr("rel", img.attr("src"));
    }
    
    if (img.parent().attr("rel") == undefined || img.parent().attr("rel") == "") {
        img.parent().attr("rel", img.parent().attr("href"));
    }
    
    if (imgBig.text() != "") {
        img.attr("src", imgBig.text());
    } else {
        img.attr("src", img.attr("rel"));
    }
    
    if (imgHr.text() != "") {
        img.parent().attr("href", imgHr.text());
    } else {
        img.parent().attr("href", img.parent().attr("rel"));
    }
}

function validatePayment(sender, args) {
    args.IsValid = ($("#payment input:radio:checked").length > 0);
}

function zIndexWorkaround()
{
    if(ie7) {
        $("#menuTop li ul").parents().each(function() {
            var p = $(this);
            var pos = p.css("position");

            if (pos == "relative" || pos == "absolute" || pos == "fixed") {
                p.hover(function() {
                    $(this).addClass("ontop");
                }, function() {
                    $(this).removeClass("ontop");
                });
            }
        });
    }
}

$(document).ready(function() {
    $("#btnSsnLookup").click(function() {
        if ($("input[id$='tbSsn']").val() != "") {
            $.getJSON("getaddress.ashx", { ssn: $("input[id$='tbSsn']").val() }, function(data) {
                if (data.lname != '404') {
                    $("input[id$='tbLastName']").val(data.lname);
                    $("input[id$='tbAddress']").val(data.street);
                    $("input[id$='tbZipCode']").val(data.zip);
                    $("input[id$='tbCity']").val(data.city);
                } else {
                    alert('Not found!');
                }
            });
        }
    });
    
    $("#catmenu a").hover(function() {
        img = $("#categoryImg");
        if (img.attr("rel") == undefined || img.attr("rel") == "") {
            img.attr("rel", img.attr("src"));
        }
        
        if ($(this).parent().find(".categoryImg").text() != "") {
            img.attr("src", $(this).parent().find(".categoryImg").text());
        } else {
            img.attr("src", img.attr("rel"));
        }
    }, function() {
        img = $("#categoryImg");
        img.attr("src", img.attr("rel"));
    });
    
    if ($("#contentLeft").height() < 450) {
        $("#contentLeft").css("height", "450px");
    }
    
    if ($("#contentMiddle").height() < $("#contentLeft").height()) {
        $("#contentMiddle").css("height", ($("#contentLeft").height() - 90) + "px");
    }

    $(".compare input:checkbox").click(function() {
        if ($(".compare input:checkbox:checked").length > 3) {
            $(this).removeAttr('checked');
            alert($("#compareMsg").text());
        }
    });

    /*$("#faq div.faq").click(function() {
        $('#' + $(this).parent().attr("id") + "answer").slideToggle("fast");
    });*/
    
    $("#filter #filter-trigger img").click(function() {
        var trigger = $(this);
        var content = trigger.parent().parent().find("#filter-content");
        
        if (content.is(":visible")) {
            content.slideToggle('slow', function() {
                var rel = trigger.attr("rel");
                trigger.attr("rel", trigger.attr("src"));
                trigger.attr("src", rel);
            });
        } else {
            content.slideToggle('slow');
            var rel = trigger.attr("rel");
            trigger.attr("rel", trigger.attr("src"));
            trigger.attr("src", rel);
        }
    });
    
    $("#global").bind("mouseover", function() {
        $(this).find("#countries").css("display", "block");
    });
    
    $("#global").bind("mouseout", function() {
        $(this).find("#countries").css("display", "none");
    });
    
    $("#menuTop ul:first > li").hover(menu_open, menu_close);
    
    $("#payment input").click(function() {
        if ($(this).val() == "rbInvoice") {
            $(".creditcard").hide();
            $(".partpay").hide();
            $(".invoice").show();
            $(".salaryvalidate").each(function(i) {
                ValidatorEnable(this, false);
            });
            $(".ssnvalidate").each(function(i) {
                ValidatorEnable(this, true);
            });
        
            if ($("#btnSsnLookup").length > 0) {
                $("input:text.address").attr('readonly', 'true');
            }
        } else if ($(this).val() == "rbPartPay") {
            $(".invoice").hide();
            $(".creditcard").show();
            $(".partpay").show();
            $(".salaryvalidate,.ssnvalidate").each(function(i) {
                ValidatorEnable(this, true);
            });
        
            if ($("#btnSsnLookup").length > 0) {
                $("input:text.address").attr('readonly', 'true');
            }
        } else {
            $(".invoice").hide();
            $(".partpay").hide();
            $(".creditcard").show();
            $(".salaryvalidate,.ssnvalidate").each(function(i) {
                ValidatorEnable(this, false);
            });
        
            if ($("#btnSsnLookup").length > 0) {
                $("input:text.address").removeAttr('readonly'); 
            }
        }
        
        $(".payment:first").val($(this).attr("value"));
        $(".ssnvalidate").each(function(i) {
            ValidatorValidate(this);
        });
    });
    
    $("#payment input:checked").trigger('click');
    
    if ($("#printContent").length > 0) {
        if (window.opener != null) {
            $("#printContent").html(window.opener.$("#contentMiddle").html());
            $("#printContent div[id*='fcFlash']").parent(".fullsize").remove();
            $("#printContent div[id*='fcFlash']").remove();
            print();
        } else {
            self.close();
        }
    }
    
    $("#products-toggle-all").click(function() {
        if ($("#product-list .info div.hand:visible").length < $("#product-list .info div.hand").length) {
            $("#product-list .info div.hand").show();
        } else {
            $("#product-list .info div.hand").hide();
        }
    });
    
    if ($("input[id$='cbDeliveryDetails']").length > 0) {
        if ($("input[id$='cbDeliveryDetails']").is(':checked')) {
            $("div[id$='pnlDeliveryDetails']").hide();
            
            $(".deliverydetailsvalidate").each(function(i) {
                ValidatorEnable(this, false);
            });
        } else {
            $("div[id$='pnlDeliveryDetails']").show();
            
            $(".deliverydetailsvalidate").each(function(i) {
                ValidatorEnable(this, true);
            });
        }
    }
        
    $("input[id$='cbDeliveryDetails']").click(function() {
        if ($(this).is(':checked')) {
             $("div[id$='pnlDeliveryDetails']").hide();
            
            $(".deliverydetailsvalidate").each(function(i) {
                ValidatorEnable(this, false);
            });
        } else {
             $("div[id$='pnlDeliveryDetails']").show();
            
            $(".deliverydetailsvalidate").each(function(i) {
                ValidatorEnable(this, true);
            });
        }
    });
    
    $("input[id$='cbTerms']").click(function() {
        if ($(this).is(':checked')) {
            $("#sTermsRed").hide();
        } else {
            $("#sTermsRed").show();
        }
    });
    
    $("input[id$='cbTerms']").removeAttr('checked');
    
    if ($("input[id$='tbOpenerUrl']").length > 0 && opener != null) {
        $("input[id$='tbOpenerUrl']").attr("value", opener.location.href);
    }
    
    menu_selected();
    productList();
    zIndexWorkaround();
});

document.onclick = menu_close;