var doSelect = function (sender){
	var callback = function(sender){
		var text = sender.text() //new text
		sender.text($("#submit").text());
		$("#submit").text(text);
		var id  = sender.attr("id");
		sender.attr("id", $("#submit_button").val());
		$("#submit_button").val(id);
	}
	hideGroupUI(callback(sender));	
},
timer,
toggle = function() {
	clearTimeout(timer);
	if ($(".groupSubmenu").is(':hidden') ){
		showGroupUI();	
	} else {
		hideGroupUI();
	}
},
autoHide = function(){
	timer=setTimeout(function(){
		hideGroupUI();
	}, 3000);
},
hideGroupUI = function(callback){
	clearTimeout(timer);
	$(".groupSubmenu").hide(1, callback);	
},
showGroupUI = function (){
	clearTimeout(timer);
	var button_width = getJQueryObject('.groupMenu').width();
	var sub_width = getJQueryObject('.groupSubmenu').width();

	if (sub_width <= button_width){
		getJQueryObject(".groupSubmenu").css("width", button_width - 2);
		getJQueryObject(".groupSubmenu").children().css( "width", getJQueryObject(".groupSubmenu").innerWidth() - 8);
	} else {
		getJQueryObject(".groupSubmenu").css("width", 'auto');
		getJQueryObject(".groupSubmenu").children().css( "width", getJQueryObject(".groupSubmenu").innerWidth() - 8);
	}
	$(".groupSubmenu").show();
	autoHide();	
};
$().ready(function() {
	$("#drop").click(toggle);
	$(".groupSubmenu").hover(function(){
		clearTimeout(timer);
	});
	$(".groupSubmenu").mouseleave(function(){hideGroupUI();});
	$(".groupSubmenu div").hover(
		function (){$(this).addClass("highlight");},
		function (){$(this).removeClass("highlight");}
	);
	$(".groupSubmenu div").click(
		function (){doSelect($(this));}
	);
	$('#submit').click(function (){	
		var val = new String (jQuery.trim($('#terms').val()));
		if (val.length > 0){
			$("#submit_button").trigger("click");
		}
	});
});
