// Add controls to qty inputs
//
// Code
//

uzCntControl = {
    objectsProp: "name",
    objectsPropVal: "qty",
    showArrows: true,
    arrowsPos: "inside",
    minValue: 1,
    maxValue: 100,
    valueStep: 1,

    //inputWidth: 60, // todo
    //arrowsWidth: 12,// todo
    
    init: function(opt){

        // Set options
        if(typeof(uzCntControlOptions) != 'undefined'){
            opt = uzCntControlOptions;
        }

        // Set options
        if(typeof(opt) != 'undefined'){
            this.setOptions(opt);
        }

        // Attach cnt controls to inputs
        this.attachControls();
    },

    setOptions: function(opt){
        for(var key in opt){
            this[key] = opt[key];
        }
    },

    attachControls: function(opt){
        var selector = ""
        switch(this.objectsProp){
            case "name":
                selector = "input[name='" + this.objectsPropVal + "']";
                break;
            case "class":
                selector = "input." + this.objectsPropVal;
                break;
            default:
                break;
        }
        if(selector != ""){
            $(selector).each(function(index) {
                // Markup input
                $(this).attr("qty_index", index);
                $(this).blur(function(){ uzCntControl.changeVal($(this).attr("qty_index"), 'check') });

                // Add arrows
                if(uzCntControl.showArrows){
                    uzCntControl.attachArrows(this, index);
                }
            });
        }
    },

    attachArrows: function(obj, index){
        obj = $(obj);
        switch(this.arrowsPos){
            case "inside":
                // Get input sizes
                //width = obj.width();
                //height = obj.height();

                // Set class to input
                obj.addClass("uz_cnt_input_inside");

                // Wrap input object into div
                var wrapBoxId = "uz_cnt_arrows_wrap_box_"+index;
                obj.wrap("<span id='"+wrapBoxId+"' class='uz_cnt_arrows_wrap_box_inside'></span>");

                var arrows = "<div><img src='_js/uz_cnt_input_control/icon_cnt_arrows_inside_up.png' class='uz_cnt_arrow' onMouseUp=\"javascript:uzCntControl.changeVal('"+index+"', 'up')\"></div><div><img src='_js/uz_cnt_input_control/icon_cnt_arrows_inside_down.png' class='uz_cnt_arrow' onMouseUp=\"javascript:uzCntControl.changeVal('"+index+"', 'down')\"></div>";

                obj.after(arrows);

                // Set input size

                // Set sizes to wrapper div

                break;
            case "outside":
                // Get input sizes
                //width = obj.width();
                //height = obj.height();


                // Set class to input
                obj.addClass("uz_cnt_input_outside");

                // Wrap input object into div
                var wrapBoxId = "uz_cnt_arrows_wrap_box_"+index;
                obj.wrap("<span id='"+wrapBoxId+"' class='uz_cnt_arrows_wrap_box_outside'></span>");

                var arrows = "<div><img src='_js/uz_cnt_input_control/icon_cnt_arrows_inside_up.png' class='uz_cnt_arrow' onMouseUp=\"javascript:uzCntControl.changeVal('"+index+"', 'up')\"></div><div><img src='_js/uz_cnt_input_control/icon_cnt_arrows_inside_down.png' class='uz_cnt_arrow' onMouseUp=\"javascript:uzCntControl.changeVal('"+index+"', 'down')\"></div>";

                obj.after(arrows);
                
                // Set sizes to wrapper div

                break;
            default:
                break;
        }
    },

    changeVal: function(index, direction){
        //alert(index+", "+direction);
        $("input[qty_index='"+index+"']").each(function(index) {
            var val = $(this).val();

            // Validate current value
            if(isNaN(val) || val == ""){
                val = uzCntControl.minValue -1;
            }
            val = parseInt(val);

            // Calc new value
            if(direction == "up"){
                val += uzCntControl.valueStep;
            }
            if(direction == "down"){
                val -= uzCntControl.valueStep;
            }

            // Validate new value
            if(val < uzCntControl.minValue){
                val = uzCntControl.minValue;
            }
            if(val > uzCntControl.maxValue){
                    val = uzCntControl.maxValue;
                }

            // Apply new value
            $(this).val(val);

            var postChangeCB = $(this).attr("postChangeCB");
            //alert(postChangeCB);
            if(typeof(postChangeCB) != 'undefined'){
                eval(postChangeCB);
            }
            //this.onchange(event);
            //this.onchange();
        });
    },


    endvar: 1
};

//
// Init
//

$(document).ready(function(){
    uzCntControl.init();
});

