// 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 $selectedRegionState = $("#dealer-states");
    var $noDealers = $("#no-dealers");
    var $dealerRegions = $(".dealer-region");
    var priceBooks = "";
    var state = "";
    var region = "";

    // Display Select Region 
    $("#load-dealers").click(function(e) {
        e.preventDefault();
        $dealerRegions.fadeIn(400);
    });

    $selectedRegionState.change(function() {
        $dealerRegions.fadeOut(400);
        var $this = $(this);
        if ($this.val().length > 0) {
            var regionState = $selectedRegionState.val();
            if (regionState.indexOf("|") != -1) {
                var sRegionState = regionState.split("|");
                region = sRegionState[0];
                state = sRegionState[1];
                if (region == "E") {
                    priceBooks = $this.data("pbe");
                }
                else if (region == "W") {
                    priceBooks = $this.data("pbw");
                }
            }
            $dealers.empty();
            $noDealers.hide();
            $.ajax({
                type: "POST",
                contentType: 'application/json',
                url: "/umbraco/webservices/Dealers.asmx/GetDealersForPricebook",
                data: '{ country: "AU", priceBooks: "' + priceBooks + '", region: "' + region + '", state: "' + state + '" }',
                dataType: "json",
                success: function(msg) {
                    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();
        $('.share').fadeIn(600);
    });

    // 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);
}
