﻿/**
*  jquery.spin-button
*  (c) 2008 Semooh (http://semooh.jp/)
*
*  Dual licensed under the MIT (MIT-LICENSE.txt)
*  and GPL (GPL-LICENSE.txt) licenses.
*
**/

/**
* cust_checkbox_plugin.js
* Copyright (c) 2009 myPocket technologies (www.mypocket-technologies.com)
 
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* View the GNU General Public License <http://www.gnu.org/licenses/>.

* @author Darren Mason (djmason9@gmail.com)
* @date 1/22/2009
* @projectDescription	Replaces the standard HTML form checkbox or radio buttons. Allows for disable, and very customizable.
* @version 1.0.2
* 
* @requires jquery.js (tested with 1.3.1)
* 
* @param disable_all:	false,
* @param wrapperclass:	"group"
*/

(function($) {
    function clearallchecks(gridscope) {
        gridscope.siblings(".skulist").find(".defaultskudata").hide("slow");
        gridscope.find(".cust_checkbox_on").removeClass("cust_checkbox_on").addClass("cust_checkbox_off").next("input").removeAttr("checked");
        gridscope.find(".cust_checkbox_backorder_on").removeClass("cust_checkbox_backorder_on").addClass("cust_checkbox_backorder_off").next("input").removeAttr("checked");
        gridscope.siblings(".skulist").find(".spin").attr("value", "0");
    };

    $.fn.quickbuyCustCheckBox = function(options) {

        var defaults = {
            disable_all: true				//disables all the elements
        };
        //override defaults
        var opts = $.extend(defaults, options);

        return this.each(function() {
            $(".checkbox").unbind().click(function() {
                var rangegridscope = $(this).parent().parent().parent().parent().parent().parent(); //hack. ancestor not yet supported. what is better?
                var disabled = $(this).next("input").attr("disabled");
                var showsectionid = $(this).next("input").attr("id").replace('_check', '_skudata');

                if ($(this).hasClass("cust_checkbox_off") && !disabled) {
                    if (opts.disable_all) {
                        clearallchecks(rangegridscope);
                    }

                    $(this).removeClass("cust_checkbox_off").addClass("cust_checkbox_on").next("input").attr("checked", "checked"); //turn on
                    $(".spin", "#" + showsectionid).attr("value", "1");
                    $("#" + showsectionid).show("slow");
                }
                else if ($(this).hasClass("cust_checkbox_backorder_off") && !disabled) {
                    if (opts.disable_all) {
                        clearallchecks(rangegridscope);
                    }
                    $(this).removeClass("cust_checkbox_backorder_off").addClass("cust_checkbox_backorder_on").next("input").attr("checked", "checked"); //turn on

                    $(".spin", "#" + showsectionid).attr("value", "1");
                    $("#" + showsectionid).show("slow");
                }
                else if ($(this).hasClass("cust_checkbox_on") && !disabled) {
                    $(this).removeClass("cust_checkbox_on").addClass("cust_checkbox_off").next("input").removeAttr("checked"); //turn off
                    $("#" + showsectionid).hide("slow", function() { rangegridscope.siblings(".skulist").find(".spin").attr("value", "0"); });

                }
                else if ($(this).hasClass("cust_checkbox_backorder_on") && !disabled) {
                    $(this).removeClass("cust_checkbox_backorder_on").addClass("cust_checkbox_backorder_off").next("input").removeAttr("checked"); //turn off
                    $("#" + showsectionid).hide("slow");
                    $("#" + showsectionid).hide("slow", function() { rangegridscope.siblings(".skulist").find(".spin").attr("value", "0"); });
                }
            });
        });
    };
})(jQuery);
