$(document).ready(function() {
   // lang form
   $("a.lang").bind( "click", function() {
      $("#boxLanguage").hide('fast');
      $("#formInterfaceLang").val(this.id);
      $("#formLang").submit();
   });

   $("div#boxLogin a.doBoxClose").bind( "click", function() {
      $("#boxLogin").hide('fast');
      return false;
   });

   $("a#boxLoginOpen").toggle(function(){
      $("#boxLanguage").hide();
      $("#boxLogin").show('fast');
      return false;
	}, function(){
	    $("#boxLogin").hide('fast');
	});

   $("div#boxLanguage a.doBoxClose").bind( "click", function() {
      $("#boxLanguage").hide('fast');
      return false;
   });

   $("div#boxSendMessage a.doBoxClose").bind( "click", function() {
      $("#boxSendMessage").hide('fast');
      return false;
   });
   
   $("div#boxAddHomeCity a.doBoxClose").bind( "click", function() {
      $("#boxAddHomeCity").hide('fast');
      return false;
   });
   
   $("div#boxAddErasmCity a.doBoxClose").bind( "click", function() {
      $("#boxAddErasmCity").hide('fast');
      return false;
   });

   $("a#boxLangOpen").toggle(function() {
      $("#boxLogin").hide();
      var offset = $(this).offset();
      var offsetparent = $('#wrap').parent().offset();
      var width = $(this).width();
      $('#boxLanguage').css('left',offset.left-offsetparent.left+width-125);
      $("#boxLanguage").show('fast');
      return false;
	}, function(){
	  $("#boxLanguage").hide('fast');
	});

   $("div#boxChooseCountry a.doBoxClose").bind( "click", function() {
      $("#boxChooseCountry").hide('fast');
      return false;
   });
   

	   
   $("a#boxChooseCountryOpen").bind( "click", function() {
      $("#boxLanguage").hide();
      $("#boxLogin").hide();
      $("#boxChooseCountry").show('fast');
      $("#boxChooseCategory").hide();
      $("#boxChooseCity").hide();
      $("#boxChooseSchool").hide();
      $("#boxChooseItem").hide();
      $("#boxChooseLanguage").hide();
      return false;
   });

   $("div#boxChooseCategory a.doBoxClose").bind( "click", function() {
      $("#boxChooseCategory").hide('fast');
      return false;
   });

   $("a#boxChooseCategoryOpen").bind( "click", function() {
      $("#boxLanguage").hide();
      $("#boxLogin").hide();
      $("#boxChooseCountry").hide();
      $("#boxChooseCategory").show('fast');
      $("#boxChooseCity").hide();
      $("#boxChooseSchool").hide();
      $("#boxChooseItem").hide();
      $("#boxChooseLanguage").hide();
      return false;
   });

   $("div#boxChooseCity a.doBoxClose").bind( "click", function() {
      $("#boxChooseCity").hide('fast');
      return false;
   });

   $("a#boxChooseCityOpen").bind( "click", function() {
      $("#boxLanguage").hide();
      $("#boxLogin").hide();
      $("#boxChooseCountry").hide();
      $("#boxChooseCategory").hide();
      $("#boxChooseCity").show('fast');
      $("#boxChooseSchool").hide();
      $("#boxChooseItem").hide();
      $("#boxChooseLanguage").hide();
      return false;
   });

   $("div#boxChooseSchool a.doBoxClose").bind( "click", function() {
      $("#boxChooseSchool").hide('fast');
      return false;
   });

   $("a#boxChooseSchoolOpen").bind( "click", function() {
      $("#boxLanguage").hide();
      $("#boxLogin").hide();
      $("#boxChooseCountry").hide();
      $("#boxChooseCategory").hide();
      $("#boxChooseCity").hide();
      $("#boxChooseSchool").show('fast');
      $("#boxChooseItem").hide();
      $("#boxChooseLanguage").hide();
      return false;
   });
	
   $("div#boxChooseItem a.doBoxClose").bind( "click", function() {
      $("#boxChooseItem").hide('fast');
      return false;
   });

   $("a#boxChooseItemOpen").bind( "click", function() {
      $("#boxLanguage").hide();
      $("#boxLogin").hide();
      $("#boxChooseCountry").hide();
      $("#boxChooseCategory").hide();
      $("#boxChooseCity").hide();
      $("#boxChooseSchool").hide();
      $("#boxChooseItem").show('fast');
      $("#boxChooseLanguage").hide();
      return false;
   });

   $("div#boxChooseLanguage a.doBoxClose").bind( "click", function() {
      $("#boxChooseLanguage").hide('fast');
      return false;
   });

   $("a#boxChooseLanguageOpen").bind( "click", function() {
      $("#boxLanguage").hide();
      $("#boxLogin").hide();
      $("#boxChooseCountry").hide();
      $("#boxChooseCategory").hide();
      $("#boxChooseCity").hide();
      $("#boxChooseSchool").hide();
      $("#boxChooseItem").hide();
      $("#boxChooseLanguage").show('fast');
      return false;
   });


   $("div#boxChooseLanguage ul li a").bind( "click", function() {
      $(this).parent().toggleClass('active');
      var query = '';
      $("div#boxChooseLanguage ul li a").each(function (i) {
         if ($(this).parent().hasClass('active')) {
            if (query!='') query += ',';
            query += $(this).attr('title');
         }
      });

      $.ajax({
         type:	 		"POST",
         url:			"/index/ajax-savemylanguages",
         data: 		{
            query:query
         },
         /*dataType:	"json",*/
         success: 	function(msg) {
            //window.alert(msg);
         }
      });

      return false;
   });




   
 
   function clearInput(input) {
		$(input).each(function() {
			var input = $(this);
			var inputValue = $(this).val();
			$(input).focus(function(){
				if ($(this).val() == inputValue) {
					$(input).val("");
				}
			});
			$(input).blur(function(){
				if ($(input).val() == "") {
					$(input).val(inputValue);
				}
			});
		});
	}
	
	clearInput('.publishPost');

});
