/* Local Helper Functions
 *
 * Requires jQuery javascript library
 * http://jquery.com/
 */

/* Create enclosing namespace */
var BM = new Object;

/*
 * Form Extensions
 */

/* Clear form fields */
BM.clearForm = function() {
    $(":text, :password, textarea", this.form).val("");
    $(":radio, :checkbox, select", this.form).val("");
    $(":file", this.form).val("");
    return false;
};

/* Make enter key behave like a tab in form */
jQuery.fn.fixForm = function() {
    var fields = ":input:not(:hidden, :disabled)";
    var filter = ":text, :checkbox, :radio, select";

    this.keypress(function(evt) {
        if (evt.keyCode == 13) {
            var list = jQuery(fields, this);
            var pos = list.index(evt.target);

            if (pos >= 0 && list.eq(pos).is(filter)) {
                list.get((pos + 1) % list.length).focus();
                return false;
            }
        }
        return true;
    });
    return this;
};

/* Go to first form field */
jQuery.fn.goToFirst = function(pattern) {
    var fields = this.find(":input").not(":hidden, :disabled");
    if (pattern) fields = fields.filter(pattern);
    fields.filter(":first").focus();
    return this;
};

/* Toggle checkboxes
 *      button -> Control that will check/uncheck
 *      pattern -> Checkboxes that will be toggled
 */
BM.FormCheckAll = function(button, pattern) {
    $(button).toggle(
        function() {
            $(pattern, this.form).attr("checked", true);
            this.value = "Uncheck All";
        },
        function() {
            $(pattern, this.form).attr("checked", false);
            this.value = "Check All";
        }
    );
};

/* Toggle select list
 *      button -> Control that will select/unselect
 *      pattern -> Selection lists that will be toggled
 */
BM.FormSelectAll = function(button, pattern) {
    var fields = $(pattern);
    $(button).toggle(
        function() {
            $("option", fields).attr("selected", true);
            this.value = "Unselect All";
        },
        function() {
            $("option", fields).attr("selected", false);
            this.value = "Select All";
        }
    );
};

/*
 * Section Navigation Extensions
 */

/* Load page section when its link is clicked */
jQuery.fn.loadBlock = function(path, target, callback) {
    var block = jQuery(target);
    this.click(function() {
        var url = this.href.replace(/^.*#/, path + "/");
        block.load(url, callback);
        return false;
    }).filter(".default").click();
    return this;
};

/* Show page section only when its link is clicked */
jQuery.fn.showBlock = function(sections) {
    var blocks = jQuery(sections).hide();
    var links = this;
    links.click(function() {
        links.removeClass("active");
        var me = jQuery(this).addClass("active");
        var id = me.is("a") ? me.attr("href") : me.find("a").attr("href");
        blocks.hide().filter(id.replace(/.*#/, "#")).show();
        return false;
    }).filter(".default").click();
    return this;
};

/*
 * Utility Extensions
 */

/* Return maximum value of attribute */
jQuery.fn.max = function(attrib) {
    var maxVal = this.get(0)[attrib];
    this.slice(1).each(function() {
        if (maxVal < this[attrib]) maxVal = this[attrib];
    });
    return maxVal;
};

/* Set link and form targets to new window */
jQuery.fn.setTarget = function(name, opts) {
    var redirect = function() {
        var newWin = BM.openWindow("", name, opts);
        if (newWin) this.target = newWin.name;
    };

    this.filter("a").click(redirect);
    this.filter("form").submit(redirect);
    return this;
};

/* Add zebra striping to table rows */
jQuery.fn.zebra = function(pattern) {
    if (!pattern) pattern = "even";
    this.filter(":nth-child(" + pattern + ")").addClass("high1");
    return this;
};

/*
 * Display Fixes
 */

/* Hide missing product images */
BM.prdImgFix = function() {
    $(this).parents("p").eq(0).hide();
};

/* Replace missing thumbnail images */
BM.thumbImgFix = function() {
    //$(this).unbind("error");
    //this.src = "/prd_images/no_image.gif";
};

/*
 * Content Area Manipulation
 */

/* Resize iframe based on its content */
BM.adjFrame = function(name) {
    var newH = frames[name].document.body.offsetHeight + 30;
    $("iframe[@name=" + name + "]").height(newH);
};

/* Trigger page load from iframe to content div */
BM.triggerLoad = function() {
    if (window != top && top.updateDiv) {
        $(document).ready(top.updateDiv);
    }
};

/*
 *  Window functions
 */

/* Close pop-up window */
BM.closeWindow = function() {
    window.close();
};

/* Open and focus a pop-up window */
BM.openWindow = function(url, name, opts) {
    var newWin = window.open(url, name, opts);
    if (newWin) newWin.focus();
    return newWin;
};

/* Print window */
BM.printWindow = function() {
    window.print();
};

/* Preset window types */
BM.windowType = {
    cart: "width=700,height=450,resizable=1,scrollbars=1",
    img: "width=480,height=480",
    std: "width=640,height=480,resizable=1,scrollbars=1"
};
