
    var initUpdatePricingTables = function(options){
        $("body").data("pricingTablelsUrl",options.ajaxUrl);
        $(".filterForm select.screen_printing").change(updatePricingTables);
        $(".filterForm select.embroidery").change(updateEmbPricingTables);
        $("body").data("AC", 0);
        $("#ac").each(function(){$("body").data("AC", 1);});        
    };
    var updatePricingTables = function(){
        if(!$("body").hasClass("updatingPricingTables")){
            $("body").addClass("updatingPricingTables");
            var theSelect=$(this);
            var id = theSelect.attr('id').replace('sp_quantity_','');
            var operation = theSelect.attr('class').replace('quantity-select', '').replace(' ', '');
            var container_id = "#" + operation + "_" + id;
            var product_id = theSelect.siblings("input.product_id").val();
            $(' <img hspace="10" class="loader" src="../images/loading.gif" />').css({verticalAlign:"bottom",width:"20px"}).insertAfter(theSelect);
            $.get($("body").data("pricingTablelsUrl"),{qty:theSelect.val(), "product_id":product_id, ac:$("body").data("AC") },function(data){
                var tdata = $(data);

                var container = $(container_id);
                container.find(".pricingTable tbody").html(tdata.find(".pricingTable").html());
                container.find(".additionalOptions tbody").html(tdata.find(".additionalOptions").html());
                container.find(".filterForm .loader").remove();
                
                $("body").removeClass("updatingPricingTables");
            },"html");
        }
    };
    var updateEmbPricingTables = function(){
        if(!$("body").hasClass("updatingEmbPricingTables")){
            $("body").addClass("updatingEmbPricingTables");
            var theSelect=$(this);
            var id = theSelect.attr('id').replace('emb_quantity_','');
            var operation = theSelect.attr('class').replace('quantity-select', '').replace(' ', '');
            var container_id = "#" + operation + "_" + id;
            var product_id = theSelect.siblings("input.product_id").val();
            $(' <img hspace="10" class="loader" src="../images/loading.gif" />').css({verticalAlign:"bottom",width:"20px"}).insertAfter(theSelect);
            $.get($("body").data("pricingTablelsUrl"),{qty:theSelect.val(), "product_id":product_id, ac:$("body").data("AC"), emb:1 },function(data){
                var tdata = $(data);

                var container = $(container_id);
                container.find(".pricingTable tbody").html(tdata.find(".pricingTable").html());
                container.find(".additionalOptions tbody").html(tdata.find(".additionalOptions").html());
                container.find(".filterForm .loader").remove();
                
                $("body").removeClass("updatingEmbPricingTables");
            },"html");
        }
    };
    $.fn.initFilters = function() {
        return this.each(function() {
            var filterContainer = $(this);
            var filterFS = filterContainer.find("fieldset");
            
            var checkboxes = filterContainer.find(":checkbox");
            var theList = $("#productList > li");
            
            filterContainer.find("h3 a").click(function(){ filterFS.toggleClass("open"); return false; });
            filterContainer.find("a.showAll").click(function(){ checkboxes.attr("checked","checked"); theList.show(); return false; });
            
            checkboxes.change(function(){ 
                var offboxes = filterContainer.find(":checkbox:not(:checked)");
                var offlist = "";
                offboxes.each(function(){ offlist = offlist+", ."+$(this).val(); });
                theList.show().filter(offlist.substr(2)).hide();
            });
        });
    };
    $.fn.initColorHighlightSize = function(){
        return this.each(function(){
            var colorBuild = $(this).find(".colorBuild span");
            var sizes = $(this).find(".sizes");
            $(this).find(".colorChoices > li").each(function(){
                $(this).hover(
                    function(){
                        var cList = $(this).attr("className").split(" ");
                        for(i = 0; i<cList.length; i++){
                            if(cList[i]!==""){
                                sizes.find("."+cList[i]).addClass("on");
                            }
                        }
                        var rgb = $(this).find("span").css("backgroundColor").substring(4);
                        rgb = rgb.substr(0,rgb.length-1);
                        colorBuild.text(rgb);
                    },
                    function(){
                        sizes.find(".on").removeClass("on");
                        colorBuild.text("");
                    }
                );
                $(this).find("a").click(function(){
                    $.displayHelpOverlay({
                        body:'<div class="imgC"><img src="'+$(this).attr("href")+'" /></div>',
                        title:$(this).attr("title")
                    });
                    
                    return false;
                });
            });
        });  
    };
    
    
    $.fn.productOrderAndShipping = function(options){
        
        var productList = $(this).children().find(".productTabs > div").each(function(){

            var container = $(this);
            var orderEstimates = container.find("form.orderEstimates");
            var shippingEstimates = container.find("form.shippingEstimates").submit(function(){ $(this).find("button.get_rates").click(); return false; });
            var getRates = shippingEstimates.find("button.get_rates");
            
            orderEstimates.find("select.select-input-get-rates").change(function(){
                if($(this).is(":select[name=quantity]")){
                    shippingEstimates.find("span.totalQuantity").text($(this).val());
                }
                $.post(options.priceEstimateUrl, $(this).closest("form").serialize(),function(data){
                    orderEstimates.find("ul.orderSummary").html(data);
                    getRates.show();
                    shippingEstimates.find(".shippingResults").empty();
                    
                },"html");
            
            });
            
            shippingEstimates.find("input[name=zipCode]").focus(function(){
                getRates.show();
            });
            getRates.click(function(){
                var params = $(this).closest("form").serializeArray();
                params[params.length] = { "name":"quantity", "value":orderEstimates.find(":select[name=quantity]").val() };
                params[params.length] = {"name":"turnaround","value":orderEstimates.find(":select[name=turnaround]").val()};
                getRates.hide();
                var loader = $('<span class="loading"></span>').insertAfter(getRates);
                $.post(options.shippingUrl,params,function(data){
                    var dData = $("<ul>"+data+"</ul>");
                    var boxes = dData.find(".numBoxes").remove();
                    var shippingResults = shippingEstimates.find(".shippingResults").empty();
                    dData.children().appendTo(shippingResults);
                    if(boxes.length){
                        var totalItems = shippingEstimates.find(".totalItems");
                        var totalQuantity = totalItems.find(".totalQuantity").clone();
                        totalItems.empty().append(totalQuantity).append(" / ").append($(boxes.html()));
                    }
                    loader.remove();
                    
                
                },"html");
            
            });
            
            
        
        
        });
        
    
    
    };
    

    $(function() {
		$(".productTabs").tabs();
        $("#filterForm .filter").initFilters();
        $("#productList > li").initColorHighlightSize();
        
        $(".collapsable .collapser").click(function(){
            $(this).closest(".collapsable").toggleClass("open");
            return false;
        });
        
        $("a.productPhoto").click(function(){
        
            $.displayHelpOverlay({
                body:'<div class="imgC"><img src="'+$(this).attr("href")+'" /></div>',
                title: $(this).attr("title"),
                minHeight: 670
            });
            
            return false;
        });
        
        $("#apProductSearch select").change(function(){
            if($(this).val()!=""){
                window.location = $(this).val();
            }
        });
        
        
	});
