function BindPrettyPhoto() {
    $(".ppt").remove();
    $(".pp_overlay").remove();
    $(".pp_pic_holder").remove();
 	$("a[rel^='prettyPhoto']").unbind();
    $("a[rel^='prettyPhoto']").prettyPhoto({animationSpeed: 'fast', theme: 'dark_rounded'});
 }

function BindTooltips() {
    if(!$.browser.msie) {
        $('mark + .tooltip').removeClass('purecss');
        $("mark + .tooltip").css({top: this.offsetTop, left: this.offsetLeft});

        $("mark").mousemove(function(e) {
            $(this).next(".tooltip").css({top: e.pageY + 10 - $("#content > section").position().top, left: e.pageX + 10 - $("#content > section").position().left});
        });

        $('mark').hover(function() {
            $(this).next('.tooltip').fadeIn(250);
        }, function() {
            $(this).next('.tooltip').stop(true, true);
            $(this).next('.tooltip').fadeOut(150);
        });
    }
}

function BindSelects(Container) {

    $(Container + " div.select > div.options + div").live("click", function() {
        var parentID = $(this).parent().attr("id");
        $(".options").each(function() {
            if($(this).parent().attr(("id")) != parentID) {
                $(this).hide();
            }
        });

        $(this).prev(".options").slideToggle(150);
    });

    $(Container + " div.options ul li a").live("click", function() {
        $(this).parent().siblings().children().removeClass("active");
        $(this).addClass("active");
        $(this).parent().parent().parent().next().html($(this).html());
        $(this).parent().parent().parent().next().next().val($(this).attr("rel"));

        if($(this).parent().parent().parent().parent().hasClass("productcolor")) {
            var ColorID = $(this).attr("rel");
            var Chunks = $(this).parent().parent().parent().parent().attr("id").split("\_", 3);
            var ProductID = Chunks[1];
            var Revision = Chunks[2];

            $('#product_'+ProductID+" > h4 + div").html('<p class="acenter mrg pad"><br /><img src="/res/styles/default/images/ajax_loading.gif" class="mrg acenter vmiddle" /></p>');
            $('#product_'+ProductID).load("/products/row/" + ProductID + "/" + ColorID + "/" + Revision, function(responseText, textStatus, XMLHttpRequest) {
                BindTooltips();
             	BindPrettyPhoto();
            });
        }
    });

    $(".qty").live("keyup", function() {
        var tqty = 0;
        $('.qty').each(function() {
            var cdd = $(this).val();
            if(!isNaN(cdd)) {
                tqty = tqty + +cdd;
            }
        });

        if(tqty > 0) {
            $('div[class^=product_row] input[type=submit]').removeAttr('disabled');
        } else {
            $('div[class^=product_row] input[type=submit]').attr('disabled', 'disabled');
        }
    }).keyup();

}

var AjaxMsgTimeout = null;

function UpdateQuantity(Qty, Size, ProductID) {
    var url = "/dealers/cart/update";

    if(!isNaN(AjaxMsgTimeout)) {
        clearTimeout(AjaxMsgTimeout);
    }

    $("input[name='next']").attr({disabled: "disabled"});
    $("#product_ajax_"+ProductID).next().children().stop(false, true);
    $("#product_ajax_"+ProductID).next().children().hide();
    $("#product_ajax_"+ProductID).next().hide();
    $("#product_ajax_"+ProductID).show();
    $.post(url, {Quantity: Qty, Size: Size, ProductID: ProductID}, function(responseText, textStatus, XMLHttpRequest) {
        var Chunks = responseText.split(";", 4);
        var Qty = Chunks[0];
        var TCQty = Chunks[1];
        var TCGT = Chunks[2];

        $("#product_price_"+ProductID).html(Chunks[3]);

        if(Qty == "0") {
            $("#product_ajax_"+ProductID).hide();
            $("#product_ajax_"+ProductID).next().children(".warning").show();
            $("#product_ajax_"+ProductID).next().show();
        } else {
            $("#product_ajax_"+ProductID).hide();
            $("#product_ajax_"+ProductID).next().children(".ok").show();
            $("#product_ajax_"+ProductID).next().show();
        }

        if(TCQty != "0") {
            $("input[name='next']").removeAttr("disabled");
        }

        $('#price_grand_total').html(TCGT);

        AjaxMsgTimeout = setTimeout(function() {
            $("#product_ajax_"+ProductID).next().children(".ok").fadeOut(3000);
        }, 3000);
    });
}

function DealerSearch() {
    var zip = $("#zip").val();
    var cid = $("#cntry .options a.active").attr("rel");
    var pst = $("#dlrsrch_submit").attr("name");
    var url = "/dealers/search";

    $('#ajax_target').html('<p class="acenter mrg pad"><br /><img src="/res/styles/default/images/ajax_loading.gif" class="mrg acenter vmiddle" /></p>');
    $.post(url, {zip: zip, cid: cid, search: pst}, function(responseText, textStatus, XMLHttpRequest) {
        $('#ajax_target').html(responseText);
    });
}
