var initTabs = function(parent){
    parent = (parent)?parent:document.body;
    for(var i=0, els = getEls(".tabs", parent), l=els.elements.length;i<l;i++){
        (function(el){
            addEvent(el, 'click', function(e){
                e = e || window.event;
                var p = findParent2(e.target, el, null, {'breakElementClass':'tabs'});
                if(p){
                   var p1 = p.last();
                   for(var k=0, l1 = p1.childNodes.length;k<l1;k++) removeClass(p1.childNodes[k],"active");
                    if(p[p.length-2]) addClass(p[p.length-2], 'active');
                    else addClass(e.target, 'active');
                }
            });
        })(els.elements[i]);
    }
};
var initXload = function(){
   var els = getEls(".xload a").elements;
   for(var i = 0, l=els.length; i<l; i++){
        (function(el){
            var t = el.getAttribute("target");
            if(t){
                var c = getEls("#"+t);
                if(c.elements[0])
                    el.onclick = function(){
                        xopen(el.getAttribute("href"), function(x){
                            c.html(x);
                            var s = getEls("script").elements;
                            for(var i=0, l=s.length;i<l;i++) eval(s[i].innerHTML);
                        });
                        return false;
                    };
            }
        })(els[i]);
   }
};
var initXform = function(){
   var els = getEls(".xSend");
   for(var i = 0, l=els.elements.length;i<l;i++){
       if(els.elements[i].nodeName == "FORM"){
           (function(frm){ xForm(frm); })(els.elements[i]);
       }
   }
};
var xForm = function(x){
       x.onsubmit  = function(){return false;};
       (function(f){
           addEvent(f, "submit", function(){
                var inputs = getEls("input,select,textarea", f).elements, res = "";
                for(var i = 0, l=inputs.length;i<l;i++){
                    if(((inputs[i].type=='checkbox' || inputs[i].type=='radio')&&(inputs[i].checked))||(inputs[i].type!='checkbox' && inputs[i].type!='radio'))
                        res +=inputs[i].name+"="+inputs[i].value+"&";
                }
               xsend(f.action, res);
               removeClass(f, 'unsaved');
           });
       })(x);
};
var watchUnsavedForm = function(f){
    addEvent(window, 'keypress', function(e){
        e = e || window.event;
        if(e.target.form === f) addClass(f, 'unsaved');
    });
    addEvent(f, 'click', function(e){
        e = e || window.event;
        if((e.target.type && (e.target.type == 'checkbox' || e.target.type == 'radio')) || e.target.nodeName == 'SELECT') addClass(f, 'unsaved');
    });
};
/* init xLink */
var init_xLink = function(target){
    if (document.body.xLinkMode) return;
    else document.body.xLinkMode = 'true';
    target = (target)?target:document.body;
    addEvent(target, 'click', function(e){
        e = e || window.event;
        if(e.target.nodeName == 'A' && hasClass(e.target, 'xLink')){
            e.preventDefault();
            if(hasClass(e.target, 'confirmed')){
                if(!confirm('are You shure?')) return;
            }
            (function(el){
                xopen(e.target.getAttribute('href'), function(x){
                    var a = el.getAttribute('onSuccess');
                    if(a) eval(a+'(x)');
                });
            })(e.target)
        }
    });
};
/* dropdown hints like google search input */
var listedHintsObject = function(el){
    this.prototype = this;
    this.prototype.srch = el;
    this.prototype.mode = 0;
    this.prototype.srch_val;
    this.prototype.srch_id_var;
    this.prototype.init = function(){
    	this.srch.setAttribute('autocomplete', 'off');
    	this.start_length = (this.srch.getAttribute('start_length'))?this.srch.getAttribute('start_length'):3;
        var var_name = this.srch.getAttribute('var_name');
        if(var_name){
            if(!this.srch.form[var_name])
                this.srch_id_var = _element(this.srch.parentNode, 'input', {'type':'hidden','name':var_name,'value':'', 'class':this.srch.getAttribute('var_class')});
        }
        this.drawHintBlock();
        this.addMouseEvents();
    };
    this.setPos = function(){
        var p = posElement(this.srch);
        this.container.style.top = p.top+this.srch.offsetHeight + 'px';
        this.container.style.left = p.left+'px';
    }
    this.prototype.drawHintBlock = function(){
        this.container = _element(document.body, 'ul', {'class':'listed_hints hidden'});
        this.container.style.height = '10em';
        this.setPos();
    };
    this.prototype.addMouseEvents = function(){
        (function(o){
            addEvent(o.srch, 'focus', function(e){
                if(o.srch.getAttribute('onfocus')) eval(o.srch.getAttribute('onfocus')+'()');
                if(o.srch.value.length < o.start_length){ o.setMode(0); return; }
                else o.setMode(1);
                o.srch_val = o.srch.value;
                xsend(o.srch.getAttribute('src'), 'srch='+o.srch.value, function(x){
                    o.container.innerHTML = x;
                    if(o.container.children.length == 1) addClass(o.container.children[0], 'selected')
                    if(o.container.children.length) o.setMode(1); else o.setMode(0);
                });
            });
        	addEvent(o.srch, 'blur', function(e){setTimeout(function(){o.close();},1000);});
            addEvent(o.srch, 'keyup', function(e){
                e = e || window.event;
                if(o.srch.value.length < o.start_length){ o.setMode(0); return; }
                else o.setMode(1);
                if(e.keyCode == 38 || e.keyCode == 40) o.selectItem(e.keyCode-39);
                if(e.keyCode == 27) { o.setMode(0);return;};
                if(e.keyCode == 13) {o.applyItem(); return; };
                if(o.srch_val == o.srch.value) return;
                o.srch_val = o.srch.value;
                xsend(o.srch.getAttribute('src'), 'srch='+o.srch.value, function(x){
                    o.container.innerHTML = x;
                    if(o.container.children.length == 1) addClass(o.container.children[0], 'selected')
                    if(o.container.children.length) o.setMode(1); else o.setMode(0);
                });
            });
            addEvent(o.container, 'click', function(e){
                e = e || window.event;
                getEls('LI', o.container).removeClass('selected');
                var t  =(e.target.nodeName != 'LI')?findParent(e.target, 'nodeName', 'LI'):e.target;
                addClass(t, 'selected');
                o.applyItem();
            });
        })(this);            
    };
    this.prototype.focusNext = function(){
            var f = this.srch.form;
            var els = f.elements;
            var i = 0;
            while(els[i] && els[i] !==this.srch) i++;
            for(var i=i+1, l=els.length;i<l;i++)
                if(els[i].nodeName=='INPUT' && els[i].type!='hidden'){
                    var tabs = findParent(els[i],'class','tabs');
                    if(tabs){
                        var a = getEls('li.active',tabs);
                        if(a.elements[0] !== findParent(els[i], 'class', 'active')){
                            if(a.elements[0].nextElementSibling){
                                addClass(a.elements[0].nextElementSibling, 'active');
                                a.removeClass('active');
                            }
                        }
                    }
                    els[i].focus();
                    return;
                }
    };
    this.prototype.applyItem = function(){
            this.setMode(0);
            var els = getEls('.selected', this.container);
            if(els.elements.length>0){
                var v_val = (els.elements[0].getAttribute('value'))?els.elements[0].getAttribute('value'):undefined;
                var v_name = els.elements[0].children[0].innerHTML;
                this.srch.value = v_name;
                if(this.srch_id_var) this.srch_id_var.value = v_val;
                if(this.srch.getAttribute('onselect')) eval(this.srch.getAttribute('onselect')+'({"id":v_val,"name":v_name});');
                this.focusNext();
            }else{
                if(this.srch_id_var) this.srch_id_var.value = '';
                if(this.srch.getAttribute('onselect')) eval(this.srch.getAttribute('onselect')+'(0);');
            }
		if(this.srch.onchange) this.srch.onchange(this.srch);
            return false;
    };
    this.prototype.setMode = function(v){
    	if(v && hasClass(this.container, 'hidden')) getEls('.listed_hints').addClass('hidden');
    	this.mode = v;
        (v && this.container.children.length)?removeClass(this.container, 'hidden'):addClass(this.container, 'hidden');
        this.setPos();
    };
    this.prototype.selectItem = function(v){
        var els = getEls('.selected', this.container), s;
        if(els.elements.length) els.removeClass('selected');
        if(v > 0) s = (els.elements.length && els.elements[0].nextSibling)?els.elements[0].nextSibling:this.container.firstChild;
        else s = (els.elements.length && els.elements[0].previousSibling)?els.elements[0].previousSibling:this.container.lastChild;
        addClass(s, 'selected');
        if(s.offsetTop<s.parentNode.scrollTop || s.offsetTop>s.parentNode.offsetHeight + s.parentNode.offsetTop)      
            s.parentNode.scrollTop = s.offsetTop;
    };
    this.prototype.close = function(){ addClass(this.container, 'hidden'); }; 
    if(this.init) this.init();
};
/* initialization dropdown hints  
 *  input element format:
 *  
 *  <input type='text' name='country_name' class='listed' src='/client_nova_travins_old/trunk/web/centrima/tools/wheelsins/test.php?action=srch' onselect='fill' var_name='country_id' />
 *       class = 'listed' : required
 *       src : request result list address
 *       onselect : callback function for selected items. function attribute is object{id, name}
 *       var_name : variable name to save selected value/id
 */
var initListedHints = function(target){
	target = (target)?target:document.body;
    var els = getEls('.listed', target);
    for(var i=0, l=els.elements.length;i<l;i++) new listedHintsObject(els.elements[i]);
};
/* prevalidate function */
var prevalidateInputWatcher = function(address){
    addEvent(document.body, 'click', function(e){
        e = e || window.event;
        if(e.target.nodeName != 'INPUT'  || !hasClass(e.target, 'prevalidate')) return;
        if(!e.target.onchange)
            (function(el){el.onchange = function(){
                setTimeout(function(){goValidate(el)}, 100);
            };})(e.target);
    });
    addEvent(window, 'keyup', function(e){
        e = e || window.event;
        if(e.keyCode != 13 || e.target.nodeName != 'INPUT' || !hasClass(e.target, 'prevalidate')) return;
        if(!e.target.onchange)
            (function(el){el.onchange = function(){
                setTimeout(function(){goValidate(el)}, 100);
            };})(e.target);
    });
}
var goValidate = function(el){
    var t = el.form, v, a, d;
    //var t = findParent(el, 'nodeName', 'FORM'), v, a, d;
    if(!t || !(a=t.getAttribute('action'))) return;
    d = (v = el.getAttribute('var_name'))?v+'='+t[v].value:el.name+'='+el.value;
    d += '&action=prevalidate';
    (function(a,d,el,t){
        xsend(a, d, function(x){
            eval('x='+x);
            var s = '';
            if(x[1] != undefined && x[1] != undefined){
                for(var i in x[1]){
                    for(var i1=0, l=x[1][i].length;i1<l;i1++)
                        s+=x[1][i][i1];
                    if(s!=''){
                        var a = getEls('.error-tip', el.parentNode);
                        if(a.elements.length) a.html(s);
                        else{
                            var er_tip = _element(el.parentNode, 'div', {'class':'error-tip', 'text':s});
                            (function(el, tip){
                                addEvent(el, 'click', function(){delElement(tip);});
                                addEvent(tip, 'click', function(){delElement(tip);el.focus();});
                            }
                            )(el, er_tip);
                        }
                        addClass(el.parentNode, 'error');
                        addClass(el, 'rejected');
                        removeClass(el, 'confirmed');
                    }else{
                        var a = getEls('.error-tip', el.parentNode).del();
                        removeClass(el.parentNode, 'error');
                        removeClass(el, 'rejected');
                        addClass(el, 'confirmed');
                    }
                }

            }
        });
    })(a,d,el,t);
}
var drawFormErrors = function(target, x){
    f = target;
    for(var i in x){
        var el=f[i];
        var et = getEls('.error-tip', f[i].parentNode);
        if(et.elements.length == 0) et = _element(el.parentNode, 'div', {'class':'error-tip'});
        else et = et.elements[0];
        et.innerHTML = '';
        removeClass(el.parentNode, 'error');
        removeClass(el, 'rejected');
        addClass(el, 'confirmed');
        for(var j in x[i]){
            if(x[i][j] != ''){
                et.innerHTML += ' '+x[i][j];
                addClass(el.parentNode, 'error');
                addClass(el, 'rejected');
                removeClass(el, 'confirmed');
            }
        }
    }
};
var xWindow = function(p){
    var a = new xWindowObject(p);
    return a;
}
var xWindowObject = function(p){
    this.prototype = this;
    this.prototype.p = (p)?p:[];
    this.prototype.init = function(){
        this.parentContainer = (this.p.parentContainer)?this.p.parentContainer:document.body;
        this.modal = (this.p.modal)?this.p.modal:true;
        this.draw();
    };
    this.prototype.draw = function(){
        if(this.p.beforeInit) this.p.beforeInit(this);
        if(this.p.modal) this.modalBackground = _element(document.body, 'div', {'class':'modalBackground'});
        this.container = _element(this.parentContainer, 'div', {'class':'xWindow'});
        this.titleBar = _element(this.container, 'div', {'class':'xWindowTitleBar'});
        this.closeBtn = _element(this.titleBar, 'div', {'class':'button', 'text':'x'});
        this.autofitBtn = _element(this.titleBar, 'div', {'class':'button', 'text':'-)', 'title':'autofit window'});
        this.titleText = _element(this.titleBar, 'div', {'class':'titleText', 'text':((this.p.title)?this.p.title:'xWindow')});
        this.contentBlock = _element(this.container, 'div', {'class':'contentContainer'});
        this.contentLayer = _element(this.contentBlock, 'div', {'class':'contentLayer'});
        (function(o){
            addEvent(o.closeBtn, 'click', function(){o.close();})
            addEvent(o.autofitBtn, 'click', function(){o.autofitWindow();})
            addEvent(o.titleText, 'dbkclick', function(){o.maximize()});
        })(this);
        if(this.p.movable !== false) this.setMovable(true);
        if(this.p.resizable !== false) this.setResizable(true);
        if(this.p.contentText) this.content(this.p.contentText);
        if(this.p.autofit) this.autofitWindow();
        if(this.p.magicautofit){
            addClass(this.contentLayer, 'magic');
            this.autofitWindow();
        }
        if(this.p.activateCancel || this.p.activateSubmit){
            var els = getEls('form', this.contentLayer).elements;
            for(var i=0,l=els.length;i<l;i++){
                if(this.p.activateCancel) this.cancelButton(els[i]);
                if(this.p.activateSubmit) this.submitButton(els[i]);
            }
        }
        if(this.p.afterInit) this.p.afterInit(this);
    };
    this.prototype.cancelButton = function(f){
        (function(o, f){
            if(f.cancel) addEvent(f.cancel,'click', function(){o.close()});
        })(this, f);
    };
    this.prototype.submitButton = function(f){
        (function(o, f){
            addEvent(f,'submit', function(){o.close()});
        })(this, f);
    };
    this.prototype.close = function(){
        if(this.p.beforeClose) this.p.beforeClose(this);
        this.setResizable(false);
        this.setMovable(false);
        delElement(this.container);
        delElement(this.modalBackground);
        if(this.p.afterClose) this.p.afterClose(this);
    };
    this.prototype.content = function(x){
        this.contentLayer.innerHTML = '';
        if(typeof x == 'string') this.contentLayer.innerHTML = x;
        else this.contentLayer.appendChild(x);
        evalScripts(this.contentLayer);
    };
    this.prototype.title = function(s){
        this.titleText.innerHTML = 's';
    };
    this.prototype.setMovable = function(p){
        if(this.movable == p) return;
        this.movable = p;
        if(this.movable) setMovable(this.container, this.titleText);
        else unsetMovable(this.container);
    };
    this.prototype.setResizable = function(p){
        if(this.resizable == p) return;
        this.resizable = p;
        if(this.resizable) setResizable(this.container);
        else unsetResizable(this.container);
    };
    this.prototype.autofitWindow = function(){
        this.container.style.width = parseInt(this.contentLayer.offsetWidth)  + 6 + 'px';
        this.container.style.height = parseInt(this.contentLayer.offsetHeight) +  parseInt(this.titleBar.offsetHeight) + 12 + 'px';
    };
    this.maximize = function(){
      if(!this.resizable) return;
      this.container.style.top = '0';
      this.container.style.left = '0';
      this.container.style.right = '0';
      this.container.style.bottom = '0';
    };
    if(this.init) this.init();
};
var selectAll = function(t){getEls('input').each(function(el){el.checked=true;});};
var unselectAll = function(t){getEls('input').each(function(el){el.checked=false;});};
var invertSelection = function(t){getEls('input').each(function(el){el.checked=!el.checked;});};
/** SLIDER
 * 
 *   target 									- place for slider
 * 	  range[min, max] 					- values
 *   rangeAvailable[min, max] 	- available to select values
 *   onChange 							- callback on moving slider, argument is [leftAnchorValue, rightAnchorValue]
 *   leftPos, rightPos 					- start positions for anchors
 *   fixLeft, rightLeft 					- bool, disabling to move anchors 
 * 
 */
var slider = function(p){
		this.prototype = this;
		this.p = p;
		this.prototype.init = function(){
				this.target = (this.p && this.p.target)? this.p.target:document.body;
				this.draw();
		};
		this.prototype.set = function(p){
			if(p !== undefined && p.left !== undefined) this.selectedBlock.style.left = (p.left - 1)  * this.step + 'px', this.leftVal = p.left;
			if(p !== undefined && p.right !== undefined){ 
				if(p.right < this.leftVal) return;
				if(p.right == this.leftVal) this.selectedBlock.style.width = 0;
				else this.selectedBlock.style.width = (p.right-1) * this.step - this.selectedBlock.offsetLeft + 'px', this.rightVal = p.right;
			}
		};
		this.prototype.draw = function(){
			this.container = _element(this.target, 'div', {'class':'sliderContainer'});
			_element(this.container, 'div', {'class':'sliderContainer-center-bg'});
			this.step = this.container.offsetWidth/(this.p.range[1]-this.p.range[0]);			
			this.selectedBlock = _element(this.container, 'div', {'class':'sliderSelectedBlock'});
			this.leftAnchor = _element(this.selectedBlock, 'div', {'class':'sliderLeftAnchor'});
			this.rightAnchor = _element(this.selectedBlock, 'div', {'class':'sliderRightAnchor'});
			this.borderRange = this.rightAnchor.offsetWidth;
			this.leftVal = this.p.leftPos || this.p.range[0];
			this.rightVal = this.p.rightPos || this.p.range[1];
			this.selectedBlock.style.left = (this.p && this.p.leftPos)?(this.p.leftPos - 1)  * this.step + 'px':0+'px';
			this.selectedBlock.style.width = ((this.p && this.p.rightPos)?(this.p.rightPos-1):this.p.range[1] ) * this.step - this.selectedBlock.offsetLeft + 'px';
			(function(sliderObject){
				setResizable(sliderObject.selectedBlock, {'freezeTop':true,
							 'freezeBottom':true, 
							 'freezeLeft': sliderObject.p.fixLeft || false, 
							 'freezeRight': sliderObject.p.fixRight || false,
							 'leftLimit': (sliderObject.p && sliderObject.p.rangeAvailable)?sliderObject.p.rangeAvailable[0]*sliderObject.step:false,
							 'rightLimit': (sliderObject.p && sliderObject.p.rangeAvailable)?sliderObject.p.rangeAvailable[1]*sliderObject.step:false,
							 'borderRange':sliderObject.borderRange, 
							 'step':sliderObject.step, 
							 'minWidth':(sliderObject.p && sliderObject.p.minWidth !== undefined)?sliderObject.p.minWidth:0,
							 'disableCursorStyle':true, 
							 'onMove':function(x){
									if(sliderObject.p.onChange){
										var l = Math.round(x.left/sliderObject.step)+1;
										var r =Math.round( l + x.width/sliderObject.step);
										sliderObject.leftVal = l;
										sliderObject.rightVal = r;
										sliderObject.p.onChange([l, r]);
									}
							}
				});				
			})(this);
		};
		if(this.init) this.init();
}
/** MODERN_SELECT
 * 
 */
var modern_select = function(p){
	this.prototype = this;
	this.p = p;
	this.prototype.init = function(){
		this.draw();
	};
	this.prototype.setZIndex = function(s){
		/* fix for internet explorer - z-index problem */
		if(!IE) return;
		var t = this.options_container;
		if(s){
			addClass(t,'highZIndex');
			while(t = t.parentNode)	addClass(t,'highZIndex');
		}else{
			removeClass(t,'highZIndex');
			while(t = t.parentNode)	removeClass(t,'highZIndex');			
		}

	}
	this.prototype.draw = function(){
		var wd = this.p.source.offsetWidth, hg = this.p.source.offsetHeight, lf = this.p.source.offsetLeft;tp = this.p.source.offsetTop;
		var ie /*@cc_on = ScriptEngineMinorVersion() @*/;
		/* parent */
		var c = window.getComputedStyle(this.p.source.parentNode, '');
		var parentPos = c.getPropertyValue('position');
		if(parentPos == 'static') this.p.source.parentNode.style.position = 'relative';
		this.p.source.parentNode.style.overflow = 'visible';
		//var wd = this.p.source.offsetWidth, hg = this.p.source.offsetHeight, lf = this.p.source.offsetLeft;tp = this.p.source.offsetTop;
		var wd = parseIntX(cmpStyle(this.p.source, 'width')), hg = parseIntX(cmpStyle(this.p.source, 'height')), lf = this.p.source.offsetLeft;tp = this.p.source.offsetTop;		
		if(this.p.source.offsetWidth > wd) wd = parseInt(this.p.source.offsetWidth);
		if(this.p.source.offsetHeight > hg) hg = parseInt(this.p.source.offsetHeight);
		if(wd < 30) wd = 30;
		if(hg < 20) hg = 20;
		this.p.source.style.position = 'relative';
		this.p.source.style.top = '-200000px';
		this.container = _element(this.p.source.parentNode, 'div', {'class':'modern-select'});
		
		/* 29 - костыль для учета паддинга в опициях селекта */
		this.container.style.width = wd +'px';
		/* конец костыля */
		
		this.container.style.height = hg+'px'; 
		this.container.style.left = lf+'px'; this.container.style.top = tp+'px';
		_element(this.container, 'div', {'class':'modern-select-bg-center'});
		_element(this.container, 'div', {'class':'modern-select-arrow-icon'});
		this.selected = _element(this.container,'div',{'class':'modern-select-selected'});
		this.options_container = _element(this.p.source.parentNode, 'div', {'class':'modern-select-options-container'});						
		this.options_container.style.left = lf+'px';
		this.options_container.style.top = tp+hg+'px';
		//this.options_container.style.width = wd+'px';
		
		/* 29 - костыль для учета паддинга в опициях селекта */
		this.options_container.style.minWidth = (wd>2)?wd - 2 + 'px':wd + 'px';

		//this.options_container.style.minWidth = wd - 2 + 29 +'px';
		/* конец костыля */
		
		this.options_container.style.height = '120px';		
		this.options = new Array();
		if(!this.p.source.children.length) return;
		for(var i=0, l=this.p.source.children.length;i<l;i++){
			this.options[i] = _element(this.options_container, 'div', {'class':'modern-select-option', 'text':this.p.source.children[i].innerHTML, 'value':this.p.source.children[i].value, 'parentIndex':i});
			addClass(this.options[i], this.p.source.children[i].className);
		}
		this.setActive(this.options[this.p.source.selectedIndex], true);
		(function(ms_obj){
			addEvent(ms_obj.p.source, 'focus',function(e){ms_obj.activateOptions();});
			addEvent(ms_obj.p.source, 'blur',function(e){
				if(ms_obj.options_container.style.display != 'none'){ 
					removeEvent.call(this, document, 'keydown',  watchKeyboardModernSelect);
					removeEvent.call(this, document, 'mousedown', watchOutsideMouseDownModernSelect);						
					ms_obj.toggleOptions_container();				
				}
				ms_obj.deactivateOptions();
			});
			addEvent(ms_obj.container, 'click', function(e){ms_obj.toggleOptions_container();});			
			addEvent(ms_obj.options_container, 'click', function(e){
				e = e || window.event;
				var t = e.target || e.srcElement;
				ms_obj.setActive(t);
				ms_obj.toggleOptions_container();
			});
			ms_obj.p.source.update = function(){	ms_obj.update.call(ms_obj);};
		})(this);
	};
	this.prototype.update = function(){
		for(var i=0, l=this.options.length;i<l;i++) delElement(this.options[i]);
		for(var i=0, l=this.p.source.children.length;i<l;i++){
			this.options[i] = _element(this.options_container, 'div', {'class':'modern-select-option', 'text':this.p.source.children[i].innerHTML, 'value':this.p.source.children[i].value, 'parentIndex':i});
			addClass(this.options[i], this.p.source.children[i].className);
		}
		this.setActive(this.options[this.p.source.selectedIndex]);
		return 'updated';
	}

		this.prototype.setActive = function(t, skipOnchange){
			if(this.selectedOption) removeClass(this.selectedOption, 'active');
			this.selectedOption = t;
			addClass(t, 'active');
			this.selected.value = t.value;
			this.selected.innerHTML = t.innerHTML;
			this.p.source.value = t.value;
			this.p.source.selectedIndex  = t.getAttribute('parentindex');
			if(this.p.source.onchange && !skipOnchange) this.p.source.onchange(this.p.source);
			
		};
		this.prototype.activateOptions = function(){
			if(hasClass(this.container, 'active')) return;
			addClass(this.container, 'active');
			addEvent.call(this, document, 'keydown', watchKeyboardModernSelect);
		};	
		this.prototype.deactivateOptions = function(){
			removeClass(this.container, 'active');
			removeEvent.call(this, document, 'keydown', watchKeyboardModernSelect);		
		};		
		this.prototype.toggleOptions_container = function(){
			if(!this.options_container.style.display || this.options_container.style.display == 'none'){
				this.activateOptions();
				this.options_container.style.display = 'block';
				addEvent.call(this, this.options_container, 'mousemove', watchMouseMoveModernSelect);
				if(!hasClass(this.container, 'active')) addEvent.call(this, document, 'keydown', watchKeyboardModernSelect);
				addEvent.call(this, document, 'mousedown', watchOutsideMouseDownModernSelect);	
				this.setZIndex(true);
			}else{
				this.deactivateOptions();
				this.options_container.style.display = 'none';
				removeEvent.call(this, this.options_container, 'mousemove', watchMouseMoveModernSelect);
				removeEvent.call(this, document, 'keydown',  watchKeyboardModernSelect);
				removeEvent.call(this, document, 'mousedown', watchOutsideMouseDownModernSelect);	
				this.setZIndex(false);
			}			
		};
		
		function watchKeyboardModernSelect(e){
			e = getEvent(e, arguments.callee.name);
			switch(e.keyCode){
				case 9:
					//e.obj.
					break;
				case 27:
					e.obj.toggleOptions_container();
					e.obj.setActive(e.obj.currentOption);				
					break;
				case 13:
					e.obj.toggleOptions_container();				
					break;
				case 40:
					e.preventDefault();
					var el;
					var n = e.obj.selectedOption.nextElementSibling || e.obj.selectedOption.nextSibling;
					if(n){ 
						if(e.obj.selectedOption) removeClass(e.obj.selectedOption, 'active');																
						e.obj.setActive(n);
						e.obj.options_container.scrollTop = n.offsetTop;
					}
				   break;
				case 38:
					e.preventDefault();
					var el;
					var n = e.obj.selectedOption.previousElementSibling || e.obj.selectedOption.previousSibling;
					if(n){ 
						if(e.obj.selectedOption) removeClass(e.obj.selectedOption, 'active');											
						e.obj.setActive(n);
						e.obj.options_container.scrollTop = n.offsetTop;
					}
				   break;				
				case 39:
				case 37:
					e.obj.toggleOptions_container();
					e.obj.activateOptions();
					break;
			}
		}
			function watchOutsideMouseDownModernSelect(e){
				e = getEvent(e, 'watchOutsideMouseDownModernSelect');
				if(findParent(e.target, 'class', 'modern-select-options-container')){ 
					return;
				}
				if(e.obj && e.obj.toggleOptions_container) e.obj.toggleOptions_container();
			}	
			function watchMouseMoveModernSelect(e){
					e = getEvent(e, arguments.callee.name);
					if(e.obj.selectedOption) removeClass(e.obj.selectedOption, 'active');
			}
			if(this.init) this.init();
}
var modern_checkbox = function(p){
	this.prototype = this;
	this.prototype.p = p;
	this.prototype.init = function(){
		this.p.source.style.position = 'absolute';
		this.p.source.style.left = '-1px';
		this.p.source.style.zIndex = '-5';
		this.container = _element(this.p.source, 'div', {'class':'n_checkbox'}, true);
		this.checkmark = _element(this.container, 'div', {'class':'n_checkmark'});
		if(this.p.source.disabled) addClass(this.container, 'disabled');
		if(this.p.source.readonly) addClass(this.container, 'readonly');
		if(this.p.source.checked)	this.toggle();
		(function a(x){
			addEvent.call(this, x.container, 'click', function(e){
				if(hasClass(x.container, 'disabled') || hasClass(x.container, 'readonly')) return;
				x.p.source.checked = !x.p.source.checked;
				x.toggle();
			});
			addEvent.call(this, x.p.source, 'change', function(e){x.toggle();});
			addEvent.call(this, x.p.source, 'focus', function(e){x.toggle();});
			addEvent.call(this, x.p.source, 'blur', function(e){	x.toggle();});
		})(this);
	};
	this.prototype.toggle = function(){
		(function(x){
			setTimeout(function(){
				if(x.p.source.checked){ addClass(x.container, 'checked'); 	addClass(x.checkmark, 'checked'); }else{ 	removeClass(x.container, 'checked');removeClass(x.checkmark, 'checked');  }
			}, 50);			
		})(this);
	}
	if(this.init) this.init();
}
