$(function()
{
   $("#contactForm div.info img[title]").tooltip({effect:'fade'});

   // ######## Contact Form ############
   var contact=$("#contactForm");
   var contactMessage=$("#contactForm #messageId");
   var nameChange=false;
   var allow=false;
   var errors=new Array();
   var globalError=false;

   var contactInput=$('#contactForm input:text');

   contactInput.focus(function()
   {
      toggleActive($(this));
   });
   contactMessage.focus(function()
   {
      toggleActive($(this));
   });

   contactInput.blur(function()
   {
      toggleInactive($(this));
   });
   contactMessage.blur(function()
   {
      toggleInactive($(this));
   });

   resetForm();

   function toggleActive(selector)
   {

      if (selector.css('font-style')!='normal')
      {
	 selector.attr('value', '');
	 selector.css({'color':'white', 'font-size':'16px', 'font-style':'normal'});
      }
   }

   function toggleInactive(sel)
   {
      selName=sel.attr('name');
      if (trimAll(sel.attr('value')).length==0)
      {
	 sel.css({'color':'#e8ac9e', 'font-size':'12px', 'font-style':'italic'});
	 if (selName=='name')
	 {
	    tekst='Imię i nazwisko';
	 }
	 else if (selName=='email')
	 {
	    tekst='Adres e-mail';
	 }
	 else if (selName=='phone')
	 {
	    tekst='Telefon';
	 }
	 else if (selName=='message')
	 {
	    tekst='Wpisz wiadomość';
	 }
	 sel.attr('value', tekst);
      }

      validForm();
   }

   function resetForm()
   {
      contact.find('input:text, textarea').each(function()
      {
	 selName=$(this).attr('name');

	 $(this).css({'color':'#e8ac9e', 'font-size':'12px', 'font-style':'italic'});
	 if (selName=='name')
	 {
	    tekst='Imię i nazwisko';
	 }
	 else if (selName=='email')
	 {
	    tekst='Adres e-mail';
	 }
	 else if (selName=='phone')
	 {
	    tekst='Telefon';
	 }
	 else if (selName=='message')
	 {
	    tekst='Wpisz wiadomość';
	 }
	 $(this).attr('value', tekst);
      });
   }

   contact.submit(function(event)
   {
      event.preventDefault();
      contact.find('div.forminfo').remove();
      contact.find('div.formerror').remove();

      var name=$("#nameId");
      var message=$("#messageId");
      var phone=$("#phoneId");
      var email=$("#emailId");

      var nameValue=name.attr('value');
      var messageValue='';
      if (message.css('font-style')=='italic') messageValue='';
      else messageValue=message.attr('value');
      var phoneValue=phone.attr('value');
      var emailValue=email.attr('value');
      var tokenValue=$("#contactToken").attr('value');

      var submitButton=contact.find('input:submit');

      //we deactivate submit button while sending
      submitButton.attr({ disabled:true}).addClass('blur');
      submitButton.blur();
      //send the post to sendMessage.php
      $.ajax(
      {
	 type: "POST", url: "ajax/sendMessage.php",
	 data: "name="+nameValue+"&message="+messageValue+"&phone="+phoneValue+"&email="+emailValue+"&token="+tokenValue,

	 complete: function(data)
	 {
	    var result='';

	    if (data.responseText==1)
	    {
	       resetForm();

	       altInfo='Wiadomość została wysłana';
	       result='<div class="forminfo">'+altInfo+'</div>';
	    }
	    else
	    {
	       result=data.responseText;
	    }

	    contact.after(result);
	    //reactivate the send button
// 	    submitButton.attr({ disabled:false, src:"gfx/wyslij.gif" });
	 }
      });
      //we prevent the refresh of the page after submitting the form
   });

   function validForm()
   {
      var tmpError=false;

      contact.find('input:text').each(function()
      {
	 if (!$(this).val() || $(this).css('font-style')!='normal')
	 {
	    tmpError=true;
	    errors[$(this).attr('name')]=true;
	 }

	 if (tmpError) globalError=true;
	 else globalError=false;
      });

      if (!globalError)
      {
	 contact.find('input:submit').removeClass('blur').attr('disabled', false);
      }
      else
      {
	 console.log('error');
	 contact.find('input:submit').addClass('blur').attr('disabled', true);
      }
   }
});
