﻿
var pageName;

$(document).ready(function () {

    pageName = $('meta[name=pageName]').attr("content");

    GetCartCount();

    //AJAX Forms
    $("form.formwrapper").live("submit", function (e) {
        e.preventDefault();
        var f = $(this);
        var serializedForm = $(f).serialize();
        var action = $(f).attr("action");
        if (serializedForm.length > 0 && action.length > 0) { serializedForm += "&sendto=" + action; }
        $.post("formsprocessor.aspx", serializedForm, function (data) {
            var ra = data.split("||");
            if (ra[0] == "OK") {
                switch (ra[2].toUpperCase()) {
                    case "SUCCESSREDIRECT":
                        if (ra[1].toLowerCase() == "refresh") {
                            window.location.href = window.location.href;
                        } else {
                            window.location.href = ra[1];
                        }
                        break;

                    case "SUCCESSFORM":
                        //set $(f).id to the new forms entity
                        $(f).html(ra[1]);
                        break;

                    default:
                        $(f).html(ra[1]);
                        break;
                }
            } else if (ra[0] == "HASERRORS") {
                $(f).html(ra[1]);

            } else {
                //uhoh :( internal errors or something??
            }
        });
    });

    //START my-jquery File
    $("#canBobDeliver").click(function () {
        $.ajax({
            url: "deliveryZones.ajax",
            data: ({ zip: $(this).siblings(".formTextInput").val()
            }),
            cache: false,
            type: "POST",
            success: function (msg) {

                var y = msg.split("||");
                if (y[0] == "OK") {
                    var result = y[1];
                    if (y.length > 2) {
                        result += "<p>Our nearby locations.<ul>";
                        for (i = 2; i < y.length; i++) {
                            var z = y[i].split("|");
                            result += "<li>" + z[0] + " (" + z[1] + " miles)</li>";
                        }
                        result += "</ul></p>";
                    }
                    $("#canBobDeliverResult").html(result);
                }



            }
        });
        return false;
    });

    // Fancy tool-tip
    $('#swatchThumbnails img').tooltip({
        delay: 0,
        track: true,
        showURL: false
    });

    // Hide all instances of .tabPanel, except for the very first one.
    $('#tabs li:first a').addClass("selected");
    $('.tabPanel:not(:first)').hide();

    // When someone clicks on the More Info link, show the More Info panel
    $('#tabLinkMoreInfo a').click(function () {
        $("#tabs li a").removeClass("selected");
        $(this).addClass("selected");
        $('.tabPanel:visible').hide();
        $('#moreInfo').show();
        return false;
    });

    $('#tabLinkShippingInfo a').click(function () {
        $("#tabs li a").removeClass("selected");
        $(this).addClass("selected");
        $('.tabPanel:visible').hide();
        $('#shippingInfo').addClass("selected").show();
        return false;
    });

    // When someone clicks on the Commercials link, show the Commercials panel
    $('#tabLinkCommercials a').click(function () {
        $("#tabs li a").removeClass("selected");
        $(this).addClass("selected");
        $('.tabPanel:visible').hide();
        $('#productCommercials').addClass("selected").show();
        return false;
    });

    // When someone clicks on the Send to Friend link, show the Send to Friend panel
    $('#tabLinkSendFriend a').click(function () {
        $("#tabs li a").removeClass("selected");
        $(this).addClass("selected");
        $('.tabPanel:visible').hide();
        $('#sendFriend').addClass("selected").show();
        return false;
    });

    //White Gloves Concept Two
    $("#whiteGlovesOver > a").click(function () {
        $("#whiteGlovesOver div:hidden").show();
        return false;
    });

    $("#whiteGlovesOver #closeLink").click(function () {
        $("#whiteGlovesOver div:visible").hide();
        return false;
    });
    //END my-jquery File
});

//Functions
var myTimeout;
var currentbox;
var xmlDoc;

function categorysort(sortdirection, token){
    var y = sortdirection.split("~~");
    $.ajax({
        url: "categorysort.ajax",
        data: ({ sortdirection: y[1],
                 pagecontext: token,
                 sortfield: y[0]
              }),
        cache: false,
        type: "POST",
        success: function(msg) {
            var response_array = msg.split("||");
            if (response_array[0] == "OK") {
                $("#pagecontent").html(response_array[1]);
                $("#sorttopspan").html(response_array[2]);
                $("#sortbottomspan").html(response_array[3]);
                $("#paginationheader").html(response_array[4]);
                $("#paginationfooter").html(response_array[5]);
            } else {
                alert(response_array[1]);
            }
        }
    });
    return false;
}
function GetCartCount(){
    $.ajax({
        url: "cartcount.ajax",
        cache: false,
        type: "POST",
        success: function(msg) {
            var ra = msg.split("||");
            if (ra[0] == "OK") { $("#shoppingCartCount").html(ra[1]); }
            else { $("#shoppingCartCount").html(0); }
        }
    });    
    return false;
}


