// Clear textbox and return to default (focus/blur)
$(document).ready(function() {
    $(".textbox-auto-clear").each(function() {
        var origVal = $(this).val(); // Store the original value
        $(this).focus(function() {
            if ($(this).val() == origVal) {
                $(this).val('');
            }
        });
        $(this).blur(function() {
            if ($(this).val() == '') {
                $(this).val(origVal);
            }
        });
    });

    var $dealerTemplate = $(".dealer-template");
    var $dealers = $("#dealers");
    var $noDealers = $("#no-dealers");
    $("#load-dealers").click(function(e) {
        e.preventDefault();
        var priceBooks = $(this).data("pb");
        $.ajax({
            type: "POST",
            contentType: 'application/json',
            url: "/umbraco/webservices/Dealers.asmx/GetDealersForPricebook",
            data: '{ country: "NZ", priceBooks: "' + priceBooks + '", region: "", state: "" }',
            dataType: "json",
            success: function(msg) {
                $dealers.empty();
                $noDealers.hide();
                var data = $.parseJSON(msg.d);
                if (data.length > 0) {
                    $.each(data, function() {
                        var $dealer = $dealerTemplate.clone();
                        var $dealerContent = $dealer.children("div");

                        $dealerContent.children("h4").text(this.name);
                        $dealerContent.children(".address").html(this.address);
                        $dealerContent.children(".phone").text(this.phone);
                        if (this.email.length > 0) {
                            $dealerContent.children(".email").text(this.email).attr("href", "mailto:" + this.email);
                        }
                        else {
                            $dealerContent.children(".email").remove();
                        }
                        $dealers.append($dealerContent);
                    });
                }
                else {
                    $noDealers.text("Sorry, there are no dealers currently supplying this product.").show();
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                $noDealers.text("Sorry, we are unable to load dealers for this product at this time. Please try again later.").show();
            }
        });
    });

    // Prodcut List Hover
    $("#product-list").children("li").click(function() {
        window.location = $("a", this).attr("href");
    }).hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });

    $("#search-cont .submit").click(function(e) {
        $(this).closest("form").submit();
        e.preventDefault();
    });

    // Category List Hover
    $("#category-list").children("li").click(function() {
        window.location = $("a", this).attr("href");
    }).hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });


    // Product Image Popup
    $("a[rel=imagegroup]", "#prodDetailCont").fancybox({
        'width': 600,
        'padding': 20,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic'
    });

    // Product info tabs
    $(".infoTabs .tabs").tabify();

    $(".shareProd").click(function(e) {
        e.preventDefault();
        $('#divShareModal').toggle();
    });

    // Last: Child
    $("#nav li:last-child").addClass("last-child");
    $(".rightColContent_B .alt-views li:last-child").addClass("last-child");

    // Top Navigation
    $(function() {
        $("#nav li").hover(function() {
            $(this).children("a").addClass("hover");
            $(this).parent().children("a").addClass("hover");
            $("ul:first", this).css('visibility', 'visible');
        }, function() {
            $(this).children("a").removeClass("hover");
            $(this).parent().children("a").removeClass("hover");
            $("ul:first", this).css('visibility', 'hidden');
        });

        $("#right-nav li").hover(function() {
            $(".sub-nav", this).slideDown(300);
        }, function() {
            $(".sub-nav", this).slideUp(300);
        });
    });
});

function ValidatePrice(obj, args) {
    var value = args.Value.replace(/^\$|,/g, '') - 0;
    console.log(value);
    args.IsValid = isNumber(value);
}

function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}
