var formsubmitting =0;
$(document).ready(
	function()
	{
		if($('#doNotObserverFormHere').html() !=26)
		{//if it's login page, we do not observer the changes of form inputs
			$('form').FormObserve();
			$("form").bind("submit", function() {
				formsubmitting=26;
			    });
		}
		$('.drop-down-list').hover(function(){ },function(){$('.drop-down-list').hide()})
		$('.drop-down-list-no-js').hover(function(){ },function(){$('.drop-down-list-no-js').hide()})
		//drop down menu
		$('.drop-down-title').click( function (id) {
			try
			{
				// hide all first
				$('.drop-down-list').hide();

				//current command and its trigger
				var menucmd= this.id;
				if(id.length>0)
				{
					menucmd = id;
				}
				var menutrigger = $('#'+menucmd) ;

				//menu for using
				var menuid = menucmd+"_list";

				//hiddenfieldid for searching
				var hiddenfieldid = menucmd+"_hidden";
				var hiddenbridgeid = menucmd+"_selected";

				$('#'+menuid).toggle();

				//set the selection actions
				$('#'+menuid+' li a').click(function(){

					try
					{
						//remove cur active style
						$('#'+menuid+' li a').removeClass("li-active");

						//update new active item's style
						$('#'+menuid+' li #'+this.id).addClass("li-active");

						//update the selected item's text to user
						menutrigger.html($('#'+menuid+' li #'+this.id).html());

						//set the hidden location value
						$('#'+hiddenfieldid ).val(this.id.substring(menucmd.length,this.id.length));
						$('#'+hiddenbridgeid ).html(this.id.substring(menucmd.length,this.id.length));

						//hide the menu for selection
						$('#'+menuid).hide();
					}
					catch(e)
					{
						alert(e);
					}
				})
			}
			catch(e)
			{
				alert(e);
			}
		});
		//toggle the search hint text.
		var defaultSearchHintText = "enter search keyword here";
		$('#barkeyword').click(function(){
			 if($.trim(this.value )== defaultSearchHintText   )
			 {
				this.value="";
			 }
		})
		$('#barkeyword').blur(function(){
			 if($.trim(this.value )=="")
			 {
				this.value=defaultSearchHintText;
			 }
		})

		$('.barsearching').click(
			function()
			{
				// var keyword = $('#barkeyword').val();
				// if($.trim(keyword) == defaultSearchHintText)
				// {
					// keyword="";
				// }
				var languagecode = $('#menulanguage_selected').html();
				var countryid = $('#menucountry_selected').html();
				var locationid = $('#menulocation_selected').html();
				var typecode = $('#menutype_selected').html();
				var levelid = $('#menulevel_selected').html();
				// var searchurl =$('#thiswebroot').html() +'searches?keyword='+keyword +'&languagecode='+languagecode+'&countryid='+countryid+'&typecode='+typecode;
				var searchurl =$('#thiswebroot').html() +'searches?languagecode='+languagecode+'&countryid='+countryid+'&locationcode='+locationid+'&typecode='+typecode+'&courselevelcode='+levelid;
				location.href=searchurl;
				return false
			}
		);

		try
		{
			//show auth msg in popup
			var authmsg=$('#authMessage');
			if( authmsg .html())
			{
				$.fn.colorbox({
						inline:true,
						href:"#authMessage",
						width:"700px",
						height:"200px",
						transition:"none",
						opacity:0.37,
						scrolling:false,
						//~ onClosed:function()
						onCleanup:function()
							{
								$('#authMessage').html("");
								$('#authMessage').hide();
							}
					})
				$("#cboxWrapper").click(function (){$.fn.colorbox.close()})
				setTimeout('$("#cboxWrapper").click()',5000); //auto close the status massages after 2 seconds
			}

			//show flash msg in popup
			var flashmsg=$('#flashMessage');
			if( flashmsg .html())
			{
				$.fn.colorbox({
						inline:true,
						href:"#flashMessage",
						width:"700px",
						height:"200px",
						transition:"none",
						opacity:0.37,
						scrolling:false,
						onCleanup:function()
							{
								$('#flashMessage').html();
								$('#flashMessage').hide();
							}
					})
				$("#cboxWrapper").click(function (){$.fn.colorbox.close()})
				setTimeout('$("#cboxWrapper").click()',5000);
			}

			//logout from facebook and yago
			 FBlogout = function  () {
				var urllogout = $("#urllogout").html();
				try{
					FB.Connect.ifUserConnected(
						function()
						{
							//~ alert( urllogout )
							FB.Connect.logoutAndRedirect(urllogout);
						}, urllogout);
				}
				catch(e)
				{
					//~ alert(e)
					location.href =urllogout ;
				}
			}

		}
		catch(e)
		{
			//~ alert(e);
		};
	}
)
