$(document).ready(function(){	
	$('.link-back a.go-back').click(function(event) {
		event.preventDefault();
		history.back();
	});
	
	$('.link-back p.print a').click(function(event) {
		event.preventDefault();
		print();
	});
	
	$('form#search input[type=text]').focus(function(event) {
		event.preventDefault();
		if ($(this).val() == locale_msgs.search)
			$(this).val("");
	});
	$('form#search input[type=text]').blur(function(event) {
		event.preventDefault();
		if ($(this).val() == "")
			$(this).val(locale_msgs.search);
	});
	
	$('form#search').submit(function(event) {
		event.preventDefault();
		
		var s = $(this).find('input[type=text]').val();
		if (s == locale_msgs.search || s == '') {
			$(this).find('input[type=text]').effect("highlight", {}, 1000);
			return;
		}

		$(this).find('input[type=image]').attr("disabled", true);
		$(this).find('input[type=image]').fadeTo(0, 0.5);
		location.assign($(this).attr('action') + "?s=" + s);
		
	});
	
	$('form#newsletter_form input#nwl_name').focus(function(event) {
		event.preventDefault();
		if ($(this).val() == "O seu nome")
			$(this).val("");
	});
	$('form#newsletter_form input#nwl_name').blur(function(event) {
		event.preventDefault();
		if ($(this).val() == "")
			$(this).val("O seu nome");
	});
	$('form#newsletter_form input#nwl_email').focus(function(event) {
		event.preventDefault();
		if ($(this).val() == "O seu email")
			$(this).val("");
	});
	$('form#newsletter_form input#nwl_email').blur(function(event) {
		event.preventDefault();
		if ($(this).val() == "")
			$(this).val("O seu email");
	});
	$('form#newsletter_form').submit(initNewsletter);
	
	$(".msg_body").hide();
	$(".msg_head").click(function(){
		$(this).next(".msg_body").slideToggle();
	})
	
});


function initSearch() {
	$('form#search1 input[type=text]').focus(function(event) {
		event.preventDefault();
		if ($(this).val() == locale_msgs.search)
			$(this).val("");
	});
	$('form#search1 input[type=text]').blur(function(event) {
		event.preventDefault();
		if ($(this).val() == "")
			$(this).val(locale_msgs.search);
	});
	
	$('form#search1').submit(function(event) {
		event.preventDefault();
		
		var s = $(this).find('input[type=text]').val();
		if (s == locale_msgs.search || s == '') {
			$(this).find('input[type=text]').effect("highlight", {}, 1000);
			return;
		}
		
		$(this).find('input[type=image]').attr("disabled", true);
		$(this).find('input[type=image]').fadeTo(0, 0.5);
		location.assign($(this).attr('action') + "?s=" + s);
		
	});
}


function initNewsletter(event) {
	event.preventDefault();
	
	$('form#newsletter_form input[type=image]').hide();

	$.ajax({
		   type:     "POST",
		   dataType: 'json',
		   cache:    false,
		   url:      document.location.pathname,
		   data:     $(this).serialize(),
		   error:    errNewsletter,
		   success:  function(json){
		     if (json.msg) {
		    	 $('form#newsletter_form div.newsletter_hide').hide();
		    	 $('form#newsletter_form div.newsletter_msg').show();
		     } else if (json.error) {			       
		       errNewsletter(json.error);
		     } else {
		       errNewsletter();
		     }
		   }
	});
}

function errNewsletter(error) {
	   if (typeof(error) != 'string')
		   error = locale_msgs.sys_error;
	   
	   $('#newsletter_error strong').html(error);
	   
	   if ($('#newsletter_error').css('display') == 'none')
		   $('#newsletter_error').show();
	   else
		   $('#newsletter_error strong').effect("highlight", {}, 1000);

	   $('form#newsletter_form input[type=image]').show();
}

function initRemoveNewsletter() {
	$('.login1').submit(function(event) {

		event.preventDefault();
		$('.login1 input[type=image]').hide();
		
		$.ajax({
			   type:     "POST",
			   dataType: 'json',
			   cache:    false,
			   url:      document.location.pathname,
			   data:     $(this).serialize(),
			   error:    errRemoveNewsletter,
			   success:  function(json){
			     if (json.msg) {
			       $('form.login1').hide();
			       $('#msg').show();
			     } else if (json.error) {			       
			    	 errRemoveNewsletter(json.error);
			     } else {
			    	 errRemoveNewsletter();
			     }
			   }
		});
	});
}

function errRemoveNewsletter(error) {
	   if (typeof(error) != 'string')
		   error = locale_msgs.sys_error;
	   
	   $('div.indent-line3 p strong').html(error);
	   
	   if ($('div.indent-line3').css('display') == 'none')
		   $('div.indent-line3').show();
	   else
		   $('div.indent-line3 p strong').effect("highlight", {}, 1000);

	   $('.login1 input[type=image]').show();
	}

function initLogin() {
	$('.login1').submit(function(event) {

		event.preventDefault();
		$('.login1 input[type=image]').hide();
		
		$.ajax({
			   type:     "POST",
			   dataType: 'json',
			   cache:    false,
			   url:      document.location.pathname,
			   data:     $(this).serialize(),
			   error:    errLogin,
			   success:  function(json){
			     if (json.msg) {
			       location.reload()
			     } else if (json.error) {			       
			       errLogin(json.error);
			     } else {
			       errLogin();
			     }
			   }
		});
	});
}

function errLogin(error) {
   if (typeof(error) != 'string')
	   error = locale_msgs.sys_error;
   
   $('div.indent-line1 p strong').html(error);
   
   if ($('div.indent-line1').css('display') == 'none')
	   $('div.indent-line1').show();
   else
	   $('div.indent-line1 p strong').effect("highlight", {}, 1000);

   $('.login1 input[type=image]').show();
}



function initRecover() {
	$('.login1').submit(function(event) {

		event.preventDefault();
		$('.login1 input[type=image]').hide();
		
		$.ajax({
			   type:     "POST",
			   dataType: 'json',
			   cache:    false,
			   url:      document.location.pathname,
			   data:     $(this).serialize(),
			   error:    errRecover,
			   success:  function(json){
			     if (json.msg) {
			       $('form.login1').hide();
			       $('#msg').show();
			     } else if (json.error) {			       
			       errRecover(json.error);
			     } else {
			       errRecover();
			     }
			   }
		});
	});
}

function errRecover(error) {
	   if (typeof(error) != 'string')
		   error = pt_locale_msgs['sys_error'];
	   
	   $('div#error p strong').html(error);
	   
	   if ($('div#error').css('display') == 'none')
		   $('div#error').show();
	   else
		   $('div#error p strong').effect("highlight", {}, 1000);

	   $('form.login1 input[type=image]').show();
	}


function initChangeData() {
	$('.form2').submit(function(event) {

		event.preventDefault();
		$('.form2 input[type=image]').hide();
		
		$.ajax({
			   type:     "POST",
			   dataType: 'json',
			   cache:    false,
			   url:      document.location.pathname,
			   data:     $(this).serialize(),
			   error:    errChangeData,
			   success:  function(json){
	    	 	 if (json.login) {
	    	 		location.pathname = locale_msgs.home;
			     } else if (json.msg) {	
			  	   if ($('div#msg').css('display') == 'none')
					   $('div#msg').show();
				   else
					   $('div#msg p strong').effect("highlight", {}, 1000);
			  	   
			  	   $('div.error').hide();
			  	   $('.form2 input[type=image]').show();

			     } else if (json.error) {			       
			    	 errChangeData(json.error);
			     } else {
			    	 errChangeData();
			     }
			   }
		});
	});
}

function errChangeData(error) {
    if (typeof(error) != 'string')
	    error = pt_locale_msgs['sys_error'];
   
    $('div.error p strong').html(error);
   
    if ($('div.error').css('display') == 'none')
	    $('div.error').show();
    else
	    $('div.error p strong').effect("highlight", {}, 1000);

    $('div#msg').hide();
    $('.form2 input[type=image]').show();
}


function initContact() {
	$('form.form2').submit(function(event) {

		event.preventDefault();
		$('form.form2 input[type=image]').hide();
		
		$.ajax({
			   type:     "POST",
			   dataType: 'json',
			   cache:    false,
			   url:      document.location.pathname,
			   data:     $(this).serialize(),
			   error:    errContact,
			   success:  function(json){
			     if (json.msg) {
			       $('form.form2').hide();
			       $('#form_preamble').hide();
			       $('#msg').show();
			     } else if (json.error) {			       
			       errContact(json.error);
			     } else {
			       errContact();
			     }
			   }
		});
	});
}

function errContact(error) {
    if (typeof(error) != 'string')
	    error = locale_msgs.sys_error;
   
    $('div.error p strong').html(error);
   
    if ($('div.indent-line4').css('display') == 'none')
	    $('div.indent-line4').show();
    else
	    $('div.error p strong').effect("highlight", {}, 1000);

    $('div#msg').hide();
    $('.form2 input[type=image]').show();
}

