﻿$(document).ready(function () {

    $("a.confirmLink, input.confirm").live('click', function (evt) {
        var ok = confirm("Are you sure?");
        $(this).data("cancelled", false);
        if (!ok) {
            $(this).data("cancelled", true);
            evt.preventDefault();
            return false;
        }

    });


    $("a.popupLink").live('click', function (evt) {
        
        evt.preventDefault();

        $.fn.colorbox({
            title: $(this).data("title") || $(this).attr("title") || "HillsdaleFurniture.com"
            , opacity: ".75"
            , width: $(this).data("width") || "480px"
            , height: $(this).data("height") || "480px"
            , inline: $(this).data("inline") == null ? false : $(this).data("inline")
            , iframe: $(this).data("iframe") == null ? true : $(this).data("iframe")
            , href: $(this).data("href") || $(this).attr("href")
            , close: ""
            , onClosed: $(this).data("onClosed") == undefined ? null : $(this).data("onClosed")

        });
        $("#colorbox").appendTo('#wrapper');
        
    });

    $("a.expando").live("click", function (evt) {
        evt.preventDefault();
        var container = $(this).data("container");

        if ($(this).hasClass("expando_open")) {
            $(this).removeClass("expando_open");

            $(container).hide(250);
        }
        else {
            $(this).addClass("expando_open");

            $(container).show(250);
        }

    });

    $("div.formContainer input, div.formContainer select").live("change", function (evt) {
        //show the button bar
        $(this).parentsUntil("div.formContainer").find(".buttons").show();

    });

    $("div.formContainer input").live("keypress", function (evt) {
        //show the button bar if the user has pressed a printable key, meaning they've changed something in the field.
        if (isEditKeyPress(evt)) {
            $(this).parentsUntil("div.formContainer").find(".buttons").show();
        }

    });

    $("div.formContainer input").live("keydown", function (evt) {
        //show the button bar if the user has pressed the delete key, meaining they've changed something in the field.
        //keypress fires after and handles everything else, but won't detect delete.
        if (evt.keyCode == 46) // delete key code per http://www.webonweboff.com/tips/js/event_key_codes.aspx
        {
            $(this).parentsUntil("div.formContainer").find(".buttons").show();
        }

    });


    $("div.formContainer input[type='reset']").live("click", function (evt) {
        //hide the button bar
        var p = $(this).parents("div.buttons");
        p.hide();
    });
    $("div.formContainer .buttons").hide();
});

//http://stackoverflow.com/questions/4194163/detect-printable-keys
//plus mod to accept backspace as a "printable" key... 
//hence function rename from isCharacterKeyPress
function isEditKeyPress(evt) {
    if (typeof evt.which == "undefined") {
        // This is IE, which only fires keypress events for printable keys
        return true;
    } else if (typeof evt.which == "number" && evt.which > 0) {
        // In other browsers except old versions of WebKit, evt.which is
        // only greater than zero if the keypress is a printable key.
        // We need to filter out backspace and ctrl/alt/meta key combinations
        return !evt.ctrlKey && !evt.metaKey && !evt.altKey; 
    }
    
    return false;
}

