var sys_sel_target, sys_sel_mouseY;  
 
document.onmousemove = function setCoor(e) {
	e=e||window.event;
	sys_sel_mouseY=e.clientY;
}
 
/*document.onclick = function select_control(evt) {
	evt = (evt) ? evt : event;
	if (evt) {
		var target = (evt.target) ? evt.target : evt.srcElement;
	}*/
function select_control(target) {
	if (target.className=='sys_button'&&document.all&&!window.XMLHttpRequest) { target.style.display='block'; target.style.display='block'; }
	
	if (target.className&&(target.className=='sys_p'||target.className=='sys_button')) {
		select_open(target.parentNode.childNodes[2], target);
		if (sys_sel_target&&sys_sel_target.parentNode&&sys_sel_target.parentNode.childNodes[2]) {
			if (sys_sel_target!=target) {
				sys_sel_target.parentNode.childNodes[2].style.display='none'; 
				sys_sel_target.parentNode.childNodes[2].parentNode.style.zIndex=10; 
				sys_sel_target=target;
			}
		} else sys_sel_target=target;
	} else {
		if (sys_sel_target&&sys_sel_target.parentNode&&sys_sel_target.parentNode.childNodes[2]) {
			sys_sel_target.parentNode.childNodes[2].style.display='none'; 
			sys_sel_target.parentNode.childNodes[2].parentNode.style.zIndex=10; 
		}
		if (target.className&&target.className=='sys_') {
			target.parentNode.parentNode.childNodes[3].setAttribute('value', target.getAttribute('oValue'));
			target.parentNode.parentNode.childNodes[3].setAttribute('text', target.innerHTML);
			target.parentNode.parentNode.setAttribute('value', target.getAttribute('oValue'));
			target.parentNode.parentNode.setAttribute('value_text', target.innerHTML);
			target.parentNode.parentNode.childNodes[1].innerHTML=target.innerHTML;
		}
	}
}
 
/* Конструктор, подменяющий оригинальный select на js_select */
function select_constructor(obj) {
	if (obj&&obj.tagName=='SELECT'&&obj.className!='zapr') {
		var select = document.createElement('DIV');
		select.setAttribute('id', obj.getAttribute('id'));
		select.setAttribute('defaultText', obj.getAttribute('defaultText'));
		select.setAttribute('defaultValue', obj.getAttribute('defaultValue'));
		select.setAttribute('type', 'select');
		(document.all) ? select.setAttribute('className', 'sys_select') : select.setAttribute('class', 'sys_select');
		select.className='sys_select';
		(obj.style.zIndex) ? select.style.zIndex=obj.style.zIndex : select.style.zIndex=10;
		if (obj.style.margin) select.style.margin = obj.style.margin;
		if (obj.style.padding) select.style.padding = obj.style.padding;
		if (obj.style.position) select.style.position = obj.style.position;
		if (obj.style.top) select.style.top = obj.style.top;
		if (obj.style.left) select.style.left = obj.style.left;
		if (obj.style.right) select.style.right = obj.style.right;
		if (obj.style.bottom) select.style.bottom = obj.style.bottom;
		select.setAttribute('value', '');
		select.setAttribute('text', '');
		var button = document.createElement('DIV');
		(document.all) ? button.setAttribute('className', 'sys_button') : button.setAttribute('class', 'sys_button');
		button.className='sys_button';
		if (document.all) {
			button.attachEvent('onmousedown', function () { event.srcElement.style.backgroundPosition='0 -44px' } );
			button.attachEvent('onmouseup', function () { event.srcElement.style.backgroundPosition='0 -22px' } );
			button.attachEvent('onmouseover', function () { event.srcElement.style.backgroundPosition='0 -22px' } );
			button.attachEvent('onmouseout', function () { event.srcElement.style.backgroundPosition='0 0' } );
		}
		var value_text = document.createElement('P');
		value_text.className='sys_p';
		if (obj.style.width&&obj.style.width!='auto') { 
			select.style.width = obj.style.width;
			value_text.style.width = (obj.style.width.replace(new RegExp("px"),"") - 33) + 'px';
		}
		if (obj.style.backgroundColor) select.style.backgroundColor = value_text.style.backgroundColor = obj.style.backgroundColor;
		var value_inp = document.createElement('INPUT');		
		value_inp.setAttribute('type', 'hidden');
		value_inp.setAttribute('name', obj.getAttribute('name'));
		if (obj.name) value_inp.setAttribute('name', obj.name);
		var options = document.createElement('UL');
		(document.all) ? options.setAttribute('className', 'sys_option') : options.setAttribute('class', 'sys_option');
		options.className='sys_option';
		if (obj.style.border) select.style.border = button.style.borderLeft = options.style.border = obj.style.border;
		options.style.display='none';
		if (obj.style.color) value_text.style.color = options.style.color = obj.style.color;
		if (obj.style.font) value_text.style.font = options.style.font = obj.style.font;	
		var selNum = obj.selectedIndex;
		var maxL = 0;
		for (var i=0; i<obj.length; i++) {
			if (i==0) {
				maxL = obj.options[i].text.length
			} else {
				if (obj.options[i].text.length>maxL) maxL=obj.options[i].text.length;
			}
			var option = document.createElement('LI');
			if (document.all&&!window.XMLHttpRequest) {
				option.attachEvent('onmouseover', function () { event.srcElement.style.backgroundColor='#ccc' } );
				option.attachEvent('onmouseout', function () { event.srcElement.style.backgroundColor='#fff' } );
			}
			if (i==selNum) {
				if (obj.options[i].value) {
					value_inp.setAttribute('value', obj.options[i].value);
					select.setAttribute('value', obj.options[i].value);
				}
				value_text.appendChild(document.createTextNode(obj.options[i].text));
				value_inp.setAttribute('text', obj.options[i].text);
				select.setAttribute('text', obj.options[i].text);
			}
			option.appendChild(document.createTextNode(obj.options[i].text));
			option.setAttribute('oValue', obj.options[i].value);
			option.className='sys_';
			options.appendChild(option);
		}
		if (obj.length>9&&obj.length!=11) {
			options.style.height='230px';
			(window.opera) ? options.style.overflow='scroll' : options.style.overflowY='scroll'; 
		}
		var width = 0.27*maxL*15 + 0.75*maxL*6;
		var widthPx = width + 'px'
		var styleWidth=obj.style.width.replace(new RegExp("px"),"")-0;
		if (obj.style.width) {
			(styleWidth<width) ? options.style.width = widthPx : options.style.width = obj.style.width;
		} else {
			(width>85) ? options.style.width = widthPx : options.style.width = '85px';
		}	
		select.appendChild(button);
		select.appendChild(value_text);
		select.appendChild(options);
		select.appendChild(value_inp);
		obj.parentNode.replaceChild(select, obj);
	}
}

function select_oconstr(text, values, width, type, id, name, dopclass) {
	var select = document.createElement('SELECT');
	if (width) select.style.width=width;
	if (id) select.id=id;
	if (name) select.name=name;
	for (var i=0; i<text.length; i++) {
		var option = document.createElement('OPTION');
		option.appendChild(document.createTextNode(text[i]));
		if (values) option.setAttribute('value', values[i]);
		select.appendChild(option);
	}
	return select;
}

/* Конструктор, строящий js_select на основе массивов */
function select_constr(text, values, width, id, name, dopclass) {
	var select = document.createElement('DIV');
	select.setAttribute('name', name);
	select.setAttribute('type', 'select');
	(document.all) ? select.setAttribute('className', 'sys_select') : select.setAttribute('class', 'sys_select');
	select.className='sys_select';
	select.setAttribute('value', '');
	select.setAttribute('text', '');
	var button = document.createElement('DIV');
	(document.all) ? button.setAttribute('className', 'sys_button') : button.setAttribute('class', 'sys_button');
	button.className='sys_button';
	if (document.all) {
		button.attachEvent('onmousedown', function () { event.srcElement.style.backgroundPosition='0 -44px' } );
		button.attachEvent('onmouseup', function () { event.srcElement.style.backgroundPosition='0 -22px' } );
		button.attachEvent('onmouseover', function () { event.srcElement.style.backgroundPosition='0 -22px' } );
		button.attachEvent('onmouseout', function () { event.srcElement.style.backgroundPosition='0 0' } );
	}
	var value_text = document.createElement('P');
	value_text.className='sys_p';
	var width_sel = '85px';
	select.style.width='85px';
	if (width) select.style.width = width_sel = width; 
	value_text.style.width = (width_sel.replace(new RegExp("px"),"") - 33) + 'px';
	var value_inp = document.createElement('INPUT');		
	value_inp.setAttribute('type', 'hidden');
	value_inp.setAttribute('name', name);
	var options = document.createElement('UL');
	(document.all) ? options.setAttribute('className', 'sys_option') : options.setAttribute('class', 'sys_option');
	options.className='sys_option';
	options.style.display='none';
	var maxL = 0;
	for (var i=0; i<text.length; i++) {
		if (i==0) {
			maxL = text[i].length;
			if (values[i]) {
				//alert('value='+values[i]);
				value_inp.setAttribute('value', values[i]);
				select.setAttribute('value', values[i]);
			} else {
				value_inp.setAttribute('value', '');
				select.setAttribute('value', '');
			}
			//alert(text[i]);
			value_inp.setAttribute('text', text[i]);
			value_text.appendChild(document.createTextNode(text[i]));
			select.setAttribute('text', text[i]);
		} else {
			if (maxL<text[i].length) maxL = text[i].length;
		}
		var option = document.createElement('LI');
		option.setAttribute('oValue', ((values[i]) ? values[i] : ''));
		option.appendChild(document.createTextNode(text[i]));
		option.className='sys_';
		if (dopclass) option.className+=dopclass;
		if (document.all&&!window.XMLHttpRequest) {
			option.attachEvent('onmouseover', function () { event.srcElement.style.backgroundColor='#ccc'; });
			option.attachEvent('onmouseout', function () { event.srcElement.style.backgroundColor='#fff'; });
		}
		options.appendChild(option);
	}
	if (text.length>9&&text.length!=11) {
		options.style.height='230px';
		(window.opera) ? options.style.overflow='scroll' : options.style.overflowY='scroll'; 
	}
	var widthS = 0.27*maxL*15 + 0.75*maxL*6;
	var widthPx = widthS + 'px';
	(widthS>(width_sel.replace(new RegExp("px"),"")-0)) ? options.style.width = widthPx : options.style.width = width_sel;	
	select.appendChild(button);
	select.appendChild(value_text);
	select.appendChild(options);
	select.appendChild(value_inp);
	return select;
}

/* Функция, заменяющая все оригинальные select-ы,присутствующие на страницы, на js_select-ы  */ 
function replace_select() {
//	while (document.getElementsByTagName('SELECT').length!=0) {
	 	var selects = document.getElementsByTagName('SELECT');
		for (var i=0; i<selects.length; i++) { select_constructor(selects[i]); }
//	}
}

function getPosition(o) {
	var y;
	var x=y=0;
    while(o) {
    	x+=o.offsetLeft;
    	y+=o.offsetTop;
    	o=o.offsetParent;
    }
	return[x,y]
}

/* Раскрытие и закрытие js_select-а */
function select_open(obj, but) {
	if (obj&&obj.className&&obj.className=='sys_option') {
		if (obj.style.display=='none') {
			obj.style.display='block';
			obj.parentNode.style.zIndex=1000;
			var y, scroll, h, h1, h2, H;
			y = getPosition(but)[1];
			scroll = (document.all&&!window.XMLHttpRequest) ? document.body.scrollTop : document.documentElement.scrollTop;
			h = obj.offsetHeight;
			H = (document.all&&!window.XMLHttpRequest) ? document.documentElement.offsetHeight : document.documentElement.clientHeight;
			h1 = y - scroll;
			h2 = H - h1 - 22; 
			if (!window.opera) (h2<h&&h1>h) ? obj.style.top = -h + 'px' : obj.style.top = 22 + 'px';
		} else {
			obj.style.display='none';
			obj.parentNode.style.zIndex=10;
		}	
	}
}

function select_set_value(obj, text, value) {
	if (obj&&obj.tagName!='INPUT') {
		var objs = obj.getElementsByTagName('INPUT');
		obj=objs[0];
	}
	if (value) { 
		obj.setAttribute('value', value);
		obj.parentNode.setAttribute('value', value);
	} else {
		obj.setAttribute('value', '');
		obj.parentNode.setAttribute('value', '');			
	}
	if (text) {
		obj.setAttribute('text', text);
		if (obj.parentNode.childNodes[1]&&obj.parentNode.childNodes[1].innerHTML) obj.parentNode.childNodes[1].innerHTML=text;
		obj.parentNode.setAttribute('text', text);			
	} else {
		obj.setAttribute('text', '');
		if (obj.parentNode.childNodes[1]&&obj.parentNode.childNodes[1].innerHTML) obj.parentNode.childNodes[1].innerHTML='';
		obj.parentNode.setAttribute('text', '');			
	}
}

function select_get_value(obj) {
	if (obj&&obj.tagName!='INPUT') {
		var objs = obj.getElementsByTagName('INPUT');
		obj=objs[0];
	}
	var value = obj.getAttribute('value');
	var text = obj.getAttribute('text');
	return [value, text];
}