

//document.observe('dom:loaded', function() { window.setTimeout('init_jv_dropdown()', 50); });
var jv_dropdown_menus = new Array();

function init_jv_dropdown() {
	var divs = document.getElementsByTagName('div');
	
//	alert('dropdown_bla'.match(/^dropdown/));
	
	for(var x=0; x < divs.length; x++) {
		if (divs[x].className == 'dd_container' 
			|| divs[x].className == 'dd_container_selected'
			|| divs[x].className == 'bdd_container'
			|| divs[x].className == 'rdd_container'
			|| divs[x].className.match(/^dropdown/)
			|| divs[x].className == 'dropdown_search'
			|| divs[x].className == 'dropdown_book'
			|| divs[x].className == 'dropdown_bookstep2'
			|| divs[x].className == 'dropdown_bookstep2_phone') {

			if (divs[x].handler)		// already initialized?
				continue;

			var id = divs[x].id.replace('_container', '');
//			jv_dropdown_menus[id] = new JvDropdown(id, divs[x]);

			if (!divs[x].handler)
				divs[x].handler = new JvDropdown(id, divs[x]);
		}
	}
}

function JvDropdown(id, obj) {
	// init
	this.input_id = id;
	this.divContainer = obj;
	this.dropdownContainer = getByClassname(this.divContainer, 'dd_menulist');
	this.valueContainer = getByClassname(this.divContainer, 'dd_value');
	this.menu_timer = null;
	
	// set events
	this.divContainer.jv_dropdown_id = this.input_id;
	$(this.divContainer).observe('mouseout',  function() { this.handler.menuMouseOut(); } );
	$(this.divContainer).observe('mouseover', function() { this.handler.menuMouseOver(); } );

	this.dropdownContainer.jv_dropdown_id = this.input_id;
	$(this.dropdownContainer).observe('mouseout',  function() { this.parentNode.handler.menuMouseOut(); } );
	$(this.dropdownContainer).observe('mouseover', function() { this.parentNode.handler.menuMouseOver(); } );
	
	this.menuMouseOut = function() {
		if (this.menu_timer != null)
			clearTimeout(this.menu_timer);
		
		this.menu_timer = setTimeout('$(\''+this.divContainer.id+'\').handler.closeMenu();', 200);
	}
	this.menuMouseOver = function() {
		if (this.menu_timer == null)
			return;
		clearTimeout(this.menu_timer);
		this.menu_timer = null;
	}
	
	this.openMenu = function() {
		this.dropdownContainer.style.display = 'block';
	}
	this.closeMenu = function() {
		this.dropdownContainer.style.display = 'none';
	}
	this.toggleMenu = function() {
		if (this.divContainer.toggleMenu_disabled)
			return;
		
		if (this.dropdownContainer.style.display == 'block')
			this.closeMenu();
		else
			this.openMenu();
	}

	this.removeAll = function() {
		this.divContainer.className = 'dropdown_theme1';
		this.valueContainer.innerHTML = $(this.input_id + '_default_text').value;
		var ml = getByClassname(this.divContainer, 'dd_menulist');

		var anchors = ml.getElementsByTagName('A');
		for(var x=anchors.length-1; x >= 0; x--) {
			ml.removeChild(anchors[x]);
		}
	}
	this.addItem = function(name, value) {
		var ml = getByClassname(this.divContainer, 'dd_menulist');
		var ml_footer = getByClassname(this.divContainer, 'footer_right');
		
		//<a href="javascript:void(0);" onclick="$(\''.$aName.'_container\').handler.select(\''.$k.'\', this.innerHTML);" class="dd_menuitem">'.$v.'</a>
		
		var anch = document.createElement('A');
		anch.href      = 'javascript:void(0);';
		//$(anch).observe('click',  function() { $(this.divContainer.id).handler.select(name, this.innerHTML) });
		anch.name = name;
		anch.onclick = function() { this.parentNode.parentNode.handler.select(this.name, this.innerHTML); };
		anch.className ='dd_menuitem';
		anch.innerHTML = value;

		ml.insertBefore(anch, ml_footer);
	}

	this.select = function(value, txt) {
		$(this.input_id).value = value;

		if ($(this.input_id).onchange) {
			c = $(this.input_id).onchange;
			c($(this.input_id));
		}

		this.valueContainer.innerHTML = txt;

		if (this.divContainer.className == 'dd_container' || this.divContainer.className == 'dd_container_selected') {
			if (value == '')
				this.divContainer.className = 'dd_container';
			else
				this.divContainer.className = 'dd_container_selected';
		}
		if (this.divContainer.className == 'dropdown_theme1' || this.divContainer.className == 'dropdown_theme1_selected') {
			if (value == '')
				this.divContainer.className = 'dropdown_theme1';
			else
				this.divContainer.className = 'dropdown_theme1_selected';
		}
	}
}



function dd_chose(name, value, txt) {
	var p = document.getElementById(name);
	
	getByClassname(p, 'dd_value').innerHTML = txt;
	
	
	var m = getByClassname(p, 'dd_menulist');
	hideObj(m);
}



function getByClassname(parent_obj, name) {
	var objs = parent_obj.getElementsByTagName('*');
	
	for(var x=0; x < objs.length; x++) {
		if (objs[x].className == name)
			return objs[x];
	}
	
	return null;
}

