// Ïñåâäîñåëåêòû
var global_z = 9999;
var Select = function(id, type){
    this.sel_obj = $("#"+id);
    this.type = type == 'select' ? 'select' : 'list';
    this.height = 200;
    this.width = 'auto';
    this.selected = ['def', 'def'];
    this.onchange = null;
    this.options = [];
    this.mouse_but = false;
    this.scroller = {};
    this.scroll_button = {};
    this.list = {};
    this.startScroll = false;
    this.scrollStartPos = 0;
    this.scrollEndPos = 0;
    this.list_realH = 0;
    this.timerID = false;
    
    this.setSelected = function(args) {
        if (args instanceof Array && args[0] && args[1]) {
            that.selected = args;
        }
    };
    
    this.setOptions = function(options) {
        that.options = options;
    }
    
    this.setArgs = function(args) {
        if(args.selected instanceof Array) {
            that.setSelected(args.selected);
        } else if (args.options instanceof Array) {
            that.setSelected(args.options[0]);
        }
        if (args.options instanceof Array) {
            that.setOptions(args.options);
        }
        if (args.onchange) {
            that.onchange = args.onchange;
        }
        
    };
    
    this.create = function(args) {
        that.sel_obj = $("#"+id);
        if (typeof(args) == 'object') {
            that.setArgs(args)
        }
        that.sel_obj.unbind();
        that.bild();
        
    };
    
    this.bild = function() {
        that.show();
        if (that.type == 'select') {
            $(document).click(function(){that.sel_obj.find(".options").removeClass("opt_show");})
            that.sel_obj.click(function(){
                if ($(this).find(".options").hasClass("opt_show")) {
                    $(this).find(".options").removeClass("opt_show");
                } else {
                        $(this).find(".options").addClass("opt_show");
                        that.setAttributes();
                }
                return false;
            });
        }
        return false;
    };
    
    this.show = function() {
        if (that.type == 'select') {
            that.sel_obj.append('<div class="sel_text">'+that.selected[1]+'</div>'+
                                '<div class="sel_val">'+that.selected[0]+'</div>'+
                                '<div class="sel_but"></div>');
            that.sel_obj.css("z-index", global_z--);
        }
        that.sel_obj.css("width", that.width);
        that.sel_obj.append('<div class="options"></div>');
//        that.sel_obj.find(".options").css("z-index", '2');
        that.sel_obj.find(".options").append('<div class="scroll">'+
                '<div class="scroll_up"></div>'+
                '<div class="scroll_down"></div>'+
                '<div class="scroller">'+
                    '<div class="scroll_button">'+
                        '<div class="up_back"></div>'+
                        '<div class="down_back"></div>'+
                    '</div>'+
                '</div>'+
            '</div>'+
            '<ul class="opts"></ul>');
        if (that.type == 'select') {
            for (var i=0; i<that.options.length; i++) {
                that.sel_obj.find(".options ul.opts").append('<li class="itm">'+
                        '<div class="opt_text"><a href="#" class="li">'+that.options[i][1]+'</a></div>'+
                        '<div class="opt_val">'+that.options[i][0]+'</div>'+
                    '</li>')
            }
        } else {
            for (var i=0; i<that.options.length; i++) {
                that.sel_obj.find(".options ul.opts").append('<li>'+that.options[i]+'</li>')
            }
        }
        
        if (that.type == 'list') {
            that.setAttributes();
        }
    }
    
    this.setAttributes = function() {
        that.scroller = that.sel_obj.find(".scroller");
        that.scroll_button = that.sel_obj.find(".scroller .scroll_button");
        that.list = that.sel_obj.find(".options ul.opts");
        that.list_realH = that.list.attr("offsetHeight");
        that.scroller.css("height", that.height-32+'px');
        that.scroll_button.css("height", (that.height/that.list_realH*that.scroller.attr("offsetHeight"))+'px');
        if (that.list_realH <= that.height) {
            that.sel_obj.find(".scroll").css("display", "none");
            that.list.css("margin-right", "0px");
            that.sel_obj.find(".options").css("height", that.list_realH+"px");
        } else {
            that.scrollingBind();
            that.sel_obj.find(".options").css("height", that.height);
            that.sel_obj.find(".options .scroll").css("height", that.height-2);
        }
        if (that.type == 'select') {
            that.list.find("li.itm").click(that.selectItem)
        }
    }
    
    this.scrollingBind = function() {
        that.sel_obj.find(".options").keypress(function(e) {return false;});
        that.sel_obj.find(".options").bind('mousewheel', that.scroll_down);
        that.sel_obj.find(".scroll").click(function(){return false;});
        that.sel_obj.find(".scroll").bind('mousedown',function(e){return false;});
        that.sel_obj.find(".scroll_up").bind('mousedown',{act: 'up'}, that.scroll_down);
        that.sel_obj.find(".scroll_up").bind('mouseup', function(e){clearTimeout(that.timerID)});
        that.sel_obj.find(".scroll_down").bind('mousedown',{act: 'down'}, that.scroll_down);
        that.sel_obj.find(".scroll_down").bind('mouseup', function(e) {clearTimeout(that.timerID)});
        that.scroll_button.bind('mousedown', function(e){that.scrollStart = true;that.scrollStartPos = that.start(e)});
        $(document).bind('mouseup', function(e){that.scrollStart = false;that.end(e)});
        $(document).bind('mousemove', that.scroll);
    };
    
    this.selectItem = function() {
        that.sel_obj.find(".sel_text").html($(this).find('a.li').html());
        that.sel_obj.find(".sel_val").html($(this).find('div.opt_val').html());
        if (that.onchange) {
            if (typeof(that.onchange) == 'string') {
                eval(that.onchange);
            } else if (typeof(that.onchange) == "function"){
                that.onchange.call();
            }
        }
    };
    
    this.getValue = function() {
        return that.sel_obj.find(".sel_val").html();
    }
    
    this.start = function(e) {
	    return e.pageY ? e.pageY : (e.clientY+document.body.scrollTop);
    };
    
    this.end = function(e) {
        var top = that.scroll_button.attr("offsetTop");
        that.scrollEndPos = top;
    };
    
    this.scroll = function(e) {
        if (that.scrollStart) {
            var y = that.start(e);
            var cssTop = y-that.scrollStartPos+that.scrollEndPos;
            cssTop = cssTop < 0 ? 0 : (cssTop > (that.scroller.attr('offsetHeight')-that.scroll_button.attr("offsetHeight")) ? that.scroller.attr('offsetHeight')-that.scroll_button.attr("offsetHeight") : cssTop);
            that.scroll_button.css("top", cssTop+'px');
            var pix2perc = that.height/that.list_realH;
            that.list.css("top", 
                '-'+
                    (that.list_realH*that.scroll_button.attr('offsetTop')/that.scroller.attr("offsetHeight"))+'px');
        }
        return false;
    };
    this.scroll_down = function(e, delta) {
        //alert(delta);
        var act = '';
        var mousewheel = false;
        if (e.data) {
            act = e.data.act;
        } else if (delta) {
            mousewheel = true;
            if (delta == 1) {
                act = 'up';
            }
            if (delta == -1) {
                act = 'down';
            }
        }
        if (mousewheel) {
            that.wheel(act);
        } else {
            that.timerID = setInterval(function(){that.wheel(act)}, 100);
        }
        return false;
    }
    this.wheel = function(act) {
        var listTop = that.list.attr("offsetTop")
        var step = act == 'up' ?  10 : -10;
        step = that.list.attr("offsetTop")+step;
        if (step > 0) {
            step = 0;
        }
        if (Math.abs(step) > (that.list_realH-that.height) ) {
            step = (that.list_realH-that.height)*(-1);
        }
        that.list.css("top", step+"px");
        that.scroll_button.css("top", step*that.scroller.attr("offsetHeight")/that.list_realH*(-1)+"px");
    }
    var that = this;
}

// Î÷èñòêà èíïóòîâ
function input_set(id, phrase) {
    $("#"+id).bind("focus", function() {
        if($(this).val() == phrase) {
            $(this).val('');
        }
    })
    $("#"+id).bind("blur", function() {
        if($(this).val() == '') {
            $(this).val(phrase);
        }
    })
}

// Checkboxes è radiobuttons
var global_id = 1;

function Checkbox(name, value, label) {
    this.name = name ? name : 'checkbox';
    this.value = value ? value : '1';
    this.label = label ? label : '';
    this.checked = false;
    this.obj = {};
    this.input = {};
    this.pre_id = "ch"
    this.id = global_id++;
    
    this.create = function() {
        that.show();
        that.setAttributes();
    }
    this.show = function() {
        document.write('<span class="input_block" id="'+that.pre_id+that.id+'"><input type="hidden" name="'+that.name+'" value="" /><span class="checkbox"></span><span class="label">'+that.label+'</span></span>');
        that.obj = $("#"+that.pre_id+that.id);
    }
    this.setAttributes = function() {
        that.obj.find(".checkbox").bind('click', that.select)
        that.obj.find(".label").bind('click', that.select)
    }
    this.select = function() {
        if (that.checked) {
            that.checked = false;
            that.obj.find(".checkbox").removeClass('checked');
            that.clearVal();
        } else {
            that.checked = true;
            that.obj.find(".checkbox").addClass('checked');
            that.setVal();
        }
    }
    this.setVal = function() {
        //alert(1);
        that.obj.find("input").val(that.value)
    }
    this.clearVal = function() {
        that.obj.find("input").val('')
    }
    
    var that = this;
    that.create()
}
function Radiobutton(name, options, args) {
    this.pre_id = "radio";
    this.id = global_id++;
    this.obj = {};
    this.name = name ? name : 'radio';
    this.options = options instanceof Array ? options : [];
    var args = args ? args : {};
    
    this.create = function() {
//        if (args instanceof Object) {
//            that.setArgs();
//        }
        that.show();
        that.setAttributes();
    }
//    this.setArgs = function() {
//        if (args.inGrid) {
//            alert(1);
//        }
//    }
    this.show = function() {
        document.write('<span id="'+that.pre_id+that.id+'"><input type="hidden" name="'+that.name+'" value="" /></span>');
        that.obj = $("#"+that.pre_id+that.id);
        that.setOptions(that.options);
    }
    this.setOptions = function(options) {
        if (!(options instanceof Array)) {
            alert('not array');
            return false;
        }
        if (args.inGrid) {
            that.obj.append("<table></table>");
            for (var i=0; i<options.length; i++) {
                that.obj.find("table").append('<tr><td style="vertical-align: middle;"><span class="input_block"><span class="value">'+options[i][0]+'</span><span class="radio"></span></span></td><td><span class="label">'+options[i][1]+'</span></td></tr>')
            }
        } else {
            for (var i=0; i<options.length; i++) {
                that.obj.append('<span class="input_block"><span class="value">'+options[i][0]+'</span><span class="radio"></span><span class="label">'+options[i][1]+'</span></span>')
            }
        }
    }
    this.setAttributes = function() {
        that.obj.find(".input_block").bind('click', function() {that.check($(this))})
    }
    this.check = function(option_obj) {
        that.obj.find(".input_block").find(".radio").removeClass("focus");
        option_obj.find(".radio").addClass("focus");
        that.setVal(option_obj.find(".value").html());
    }
    this.setVal = function(val) {
        //$(this).
        //alert(val);
        that.obj.find("input").val(val);
    }
    this.setChecked = function(val) {
        var obj = that.obj.find(".value:contains('"+val+"')").parent();
        that.check(obj);
    }
    var that = this;
    that.create();
}
/* Âõîä */
function show_enter() {
    $("#alpha").show();
    $("#alpha").css("height", $(document).find("body").attr("offsetHeight"));
    $("#enter_block").show();
    $("#enter_block").css("top", ($(document).scrollTop()+200)+'px')
    $("#enter_block").css("left", ($(document).find("body").attr('offsetWidth')/2-150)+'px')
    return false;
}
function hide_enter() {
    $("#alpha").hide();
    $("#enter_block").hide();
    return false;
}
/* //Âõîä */