jQuery(document).ready(function(){	
	
	jQuery('.pdf-sidebar-download .first').append('<img src="/IFB-theme/images/pub-inforbanca.gif"></img>');
	jQuery('.pdf-sidebar-download a.pdf-download').append('<img src="/IFB-theme/images/pub-download.gif"></img>');
	
	jQuery('form#search input[type=text]').focus(function(event) {
		event.preventDefault();
		if (jQuery(this).val() == locale_msgs.search)
			jQuery(this).val("");
	});
	jQuery('form#search input[type=text]').blur(function(event) {
		event.preventDefault();
		if (jQuery(this).val() == "")
			jQuery(this).val(locale_msgs.search);
	});
	
	jQuery('form#search').submit(function(event) {
		var s = jQuery(this).find('input[type=text]').val();
		if (s == locale_msgs.search || s == '') {
			event.preventDefault();
			jQuery(this).find('input[type=text]').effect("highlight", {color:"#0e6ca7"}, 1500);
			return;
		}

		jQuery(this).find('input[type=image]').attr("disabled", true);
		jQuery(this).find('input[type=image]').fadeTo(0, 0.8);
	});		
	
	jQuery('#inforbancaPubSelect').change(
		//The following function replies to any change in the publication selector.
		function(event) {
		   var indexSelected = this.selectedIndex;
		   var url_add = this.options[indexSelected].value;
		   this.options[indexSelected].selected = true;
		   window.location.href = url_add;
	});
	
	jQuery('.img-tooltip').tipsy({fade: true});
	
	var links = document.getElementsByTagName('a');
	for ( var i = 0; i < links.length; i++ ) {
	    links[i].onmousedown = function () {
	        this.blur();
	        return false;
	    };
	    links[i].onclick = function() {
	        this.blur();
	    };
	    if ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) ) {
	        links[i].onfocus = function() {
	            this.blur();
	        };
	    }
	}
	
	jQuery('.hide-show').click(function() {
		$('#hidden-div').toggle();
		var footerNav = $('.footer-nav');
		  
		if(footerNav !== undefined) {
			footerNav.hide().show();
		}
	});	
});

function initSearch() {
	jQuery('form#search1 input[type=text]').focus(function(event) {
		event.preventDefault();
		if (jQuery(this).val() == locale_msgs.search)
			jQuery(this).val("");
	});
	jQuery('form#search1 input[type=text]').blur(function(event) {
		event.preventDefault();
		if (jQuery(this).val() == "")
			jQuery(this).val(locale_msgs.search);
	});
	
	jQuery('form#search1').submit(function(event) {
		event.preventDefault();
		
		var s = jQuery(this).find('input[type=text]').val();
		if (s == locale_msgs.search || s == '') {
			jQuery(this).find('input[type=text]').effect("highlight", {color:"#172f53"}, 1000);
			return;
		}
		
		jQuery(this).find('input[type=image]').attr("disabled", true);
		jQuery(this).find('input[type=image]').fadeTo(0, 0.5);
		jQuery(this).submit();
		
	});
}




function initLogin() {
	jQuery('.login1').submit(function(event) {

		event.preventDefault();
		jQuery('.login1 input[type=image]').hide();
		
		$.ajax({
			   type:     "POST",
			   dataType: 'json',
			   cache:    false,
			   url:      document.location.pathname,
			   data:     jQuery(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;
   
   jQuery('div.indent-line1 p strong').html(error);
   
   if (jQuery('div.indent-line1').css('display') == 'none')
	   jQuery('div.indent-line1').show();
   else
	   jQuery('div.indent-line1 p strong').effect("highlight", {}, 1000);

   jQuery('.login1 input[type=image]').show();
}



function initRecover() {
	jQuery('.login1').submit(function(event) {

		event.preventDefault();
		jQuery('.login1 input[type=image]').hide();
		
		$.ajax({
			   type:     "POST",
			   dataType: 'json',
			   cache:    false,
			   url:      document.location.pathname,
			   data:     jQuery(this).serialize(),
			   error:    errRecover,
			   success:  function(json){
			     if (json.msg) {
			       jQuery('form.login1').hide();
			       jQuery('#msg').show();
			     } else if (json.error) {			       
			       errRecover(json.error);
			     } else {
			       errRecover();
			     }
			   }
		});
	});
}

function errRecover(error) {
	   if (typeof(error) != 'string')
		   error = pt_locale_msgs['sys_error'];
	   
	   jQuery('div#error p strong').html(error);
	   
	   if (jQuery('div#error').css('display') == 'none')
		   jQuery('div#error').show();
	   else
		   jQuery('div#error p strong').effect("highlight", {}, 1000);

	   jQuery('form.login1 input[type=image]').show();
	}


function initChangeData() {
	jQuery('.form2').submit(function(event) {

		event.preventDefault();
		jQuery('.form2 input[type=image]').hide();
		
		$.ajax({
			   type:     "POST",
			   dataType: 'json',
			   cache:    false,
			   url:      document.location.pathname,
			   data:     jQuery(this).serialize(),
			   error:    errChangeData,
			   success:  function(json){
	    	 	 if (json.login) {
	    	 		location.pathname = locale_msgs.home;
			     } else if (json.msg) {	
			  	   if (jQuery('div#msg').css('display') == 'none')
					   jQuery('div#msg').show();
				   else
					   jQuery('div#msg p strong').effect("highlight", {color:"#172f53"}, 1000);
			  	   
			  	   jQuery('div.error').hide();
			  	   jQuery('.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'];
   
    jQuery('div.error p strong').html(error);
   
    if (jQuery('div.error').css('display') == 'none')
	    jQuery('div.error').show();
    else
	    jQuery('div.error p strong').effect("highlight", {}, 1000);

    jQuery('div#msg').hide();
    jQuery('.form2 input[type=image]').show();
}


function initContact() {
	jQuery('form.form2').submit(function(event) {

		event.preventDefault();
		jQuery('form.form2 input[type=image]').hide();
		
		$.ajax({
			   type:     "POST",
			   dataType: 'json',
			   cache:    false,
			   url:      document.location.pathname,
			   data:     jQuery(this).serialize(),
			   error:    errContact,
			   success:  function(json){
			     if (json.msg) {
			       jQuery('form.form2').hide();
			       jQuery('#form_preamble').hide();
			       jQuery('#msg').show();
			     } else if (json.error) {			       
			       errContact(json.error);
			     } else {
			       errContact();
			     }
			   }
		});
	});
}

function errContact(error) {
    if (typeof(error) != 'string')
	    error = locale_msgs.sys_error;
   
    jQuery('div.error p strong').html(error);
   
    if (jQuery('div.indent-line4').css('display') == 'none')
	    jQuery('div.indent-line4').show();
    else
	    jQuery('div.error p strong').effect("highlight", {}, 1000);

    jQuery('div#msg').hide();
    jQuery('.form2 input[type=image]').show();
}
