var j$ = jQuery;

j$(function(){

	function setBackground() {
		var $last = j$(".category > li:last > a");
		if($last.hasClass("close"))
			$last.css("background-position", "left bottom");
		else
			$last.css("background-position", "left -30px");
	}

	j$(".category").each(function(){
		j$("li > ul"            , this).wrap("<div></div>");
		j$("li > div:not(:last)", this).append("<div class='notlast'>&nbsp;</div>");
		j$("li > div:last"      , this).append("<div class='last'>&nbsp;</div>");

		j$("li > a", this).each(function(index){
			var $this = j$(this);

			if(index > 0) 
				$this.addClass("close").next().hide();
			else
				$this.css("background-position", "left top");

			setBackground();

			var prms = {height:"toggle", opacity:"toggle"};
			$this.click(function(){
				j$(this).toggleClass("close").next().animate(prms, {duration:"fast"})
					.parent().siblings().children("div:visible").animate(prms, {duration:"fast"}).prev().addClass("close");
				setBackground();

				if ($this.attr("href") == "#")
					return false;
				else
					return true;
			});
		});
	});

	j$('#Search').submit(function() {
		if (document.getElementById('Search').target[0].checked) {
			j$.cookie('target', 'company');
		} else if (document.getElementById('Search').target[1].checked) {
			j$.cookie('target', 'commodity');
		}
	});

	if (j$.cookie('target') == 'company') {
		document.getElementById('Search').target[0].checked = true;
	} else if (j$.cookie('target') == 'commodity') {
		document.getElementById('Search').target[1].checked = true;
	}
});

function execSearch(parent_field, child_field) {
	if (document.getElementById('Search').target[0].checked) {
		j$.cookie('target', 'company');
	} else if (document.getElementById('Search').target[1].checked) {
		j$.cookie('target', 'commodity');
	}

	document.getElementById('Search').word.value         = '';
	document.getElementById('Search').word.value         = '';
	document.getElementById('Search').parent_field.value = parent_field;
	document.getElementById('Search').child_field.value  = child_field;
	document.getElementById('Search').submit();

	return true;
}

