var JsSearch = {
	elements : {
		settings : 0,
		everything : 1,
		complaints : 2,
		articles : 3,
		news : 4,
		recalls : 5,
		videos : 6,
		groups : 7,
		questions : 8
	},
	is_opened : false,
	setPosition : function ()	{
		this._drop.style.top  = offsetTop(this._group)+this._group.offsetHeight+'px';
		this._drop.style.left = offsetLeft(this._group)+this._group.offsetWidth-this._drop.offsetWidth+1+'px';
	},
	show : function () {
		this.setPosition();
		this._group.style.borderBottom = 'none';
		this._drop.style.visibility = 'visible';
		this._drop.onmouseout = function (event) {	
			var event = event || window.event;
			var obj = event.currentTarget || event.srcElement;
            var rt = event.relatedTarget || event.toElement;

			while (true) {
				if (rt.id == obj.id) break;
				if(rt.parentElement)	{
					rt = rt.parentElement;
				}else if(rt.parentNode)	{
					rt = rt.parentNode;
				}else{
					JsSearch.hide();
					break;
				}
			}
		}
		this.is_opened = true;
	},
	hide : function () {
		this._group.style.borderBottom = '1px solid #E4E4E4';
		this._drop.style.visibility = 'hidden';
			
		this.is_opened = false;
	},
	setCaption : function () {
		var sh = '';
		var count = 0;
		var e = this._drop.getElementsByTagName('input');
		
		if(e[this.elements.everything].checked)
		{
			for(var i=2;i<e.length;i++)
			{
				e[i].checked = false;
			}
			this._group.innerHTML = 'Everything';
		}else{
			var last = '';
			for(var i=this.elements.complaints;i<e.length;i++)
			{
				if(e[i].checked)
				{
					sh += (count>0 ? ' <b>|</b> ' : '') + e[i].value.substr(0,2);
					last = e[i].value;
					count++;
				}
			}
			if(count==7)
			{
				e[this.elements.everything].checked = true;
				return this.setCaption();
			}
			if(sh.length)
			{
				if(count==1)
				{
					this._group.innerHTML = last;
				}else{
					this._group.innerHTML = sh;
				}
			}else{
				e[this.elements.everything].checked = true;
				this._group.innerHTML = 'Everything';
			}
		}
	},
	init : function ()	{
		JsSearch._drop = $('search-group-dropdown');
		JsSearch._group = $('search-group');
		if(JsSearch._drop)
		{
			var optional = $('search-group-dropdown-settings');
			var inputs = JsSearch._drop.getElementsByTagName('input');
			if(optional && parseInt(optional.value)>0)
			{
				var settings = parseInt(optional.value);
				if(settings & 0x1)	inputs[JsSearch.elements.everything].checked = true;
				if(settings & 0x2)	inputs[JsSearch.elements.complaints].checked = true;
				if(settings & 0x4)	inputs[JsSearch.elements.articles].checked = true;
				if(settings & 0x8)	inputs[JsSearch.elements.news].checked = true;
				if(settings & 0x10)	inputs[JsSearch.elements.recalls].checked = true;
				if(settings & 0x20)	inputs[JsSearch.elements.videos].checked = true;
				if(settings & 0x40)	inputs[JsSearch.elements.questions].checked = true;
				if(settings & 0x80)	inputs[JsSearch.elements.groups].checked = true;
				
				JsSearch.setCaption();
			}else{
				inputs[JsSearch.elements.everything].checked = true;	
			}
		}
	},
	OnChange : function (obj) {
		if(this._drop)
		{
			if(obj.value!='Everything')
			{
				JsSearch._drop.getElementsByTagName('input')[1].checked = false;
			}
			this.setCaption();
			this.setPosition();
		}
		return true;
	}
};

add_load(JsSearch.init);