// remap jQuery to $
(function($){
})(this.jQuery);


// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};

jQuery.validator.addMethod("initialMessage", function (initial_message, element) {
    return this.optional(element) || initial_message != element.defaultValue;
}, " ");  //adiciona metodo para mensagem inicial  

/* newsletterForm clientside Validation START */
function openModal() {
  $('#newsForm').each(function(){this.reset();});
  initNewsForm();  
  $("#modalForm").show();
}
function closeModal() {
  $("#modalForm").hide();
  NewsletterSubscleanForm();
}    
initNewsForm = function() {
  var form = $('#newsForm').get(0); 
  $.removeData(form,'validator');   
  $("#nError").removeClass('ok').addClass('err').html("<span></span>").hide();
  $('#newsForm input').removeAttr('disabled');
  
  verifCampo('nName',$('#nName').val());
  verifCampo('nEmail',$('#nEmail').val());
  
  $('#newsForm').validate({
    errorPlacement: function(error, element) {
      error.hide();
    },  
    invalidHandler: function(form, validator) {  
      if(!($('body').validate().element('#nName'))){
        $('#nName').parent().addClass('error');
        $('#nError').html('<span></span>' + resources['GF_RequiredName']).show();
      } else if(!($('body').validate().element('#nEmail'))){
        $('#nName').parent().removeClass('error');
        $('#nEmail').parent().addClass('error');
        $('#nError').html('<span></span>' + resources['GF_InvalidEmail']).show();
      } else {
        $('#nName, #nEmail').parent().removeClass('error');
        $('#nError').html('').hide();
      }
    },    
    rules: {
      nName: {initialMessage:true,required:true},  
      nEmail: {initialMessage:true,email:true,required:true}
    },
    onsubmit: false
  });
  bindNewsForm();  
}
function bindNewsForm() {
  $('#btSendNewsletter').bind('click', function () {
    if ($("#newsForm").valid()) {
      $('#btSendNewsletter').unbind('click');
      $('#nName, #nEmail').parent().removeClass('error');
      sendNewsletterSubs();
    } else {
      $('#newsForm input').removeAttr('disabled');
      $('#btSendNewsletter').unbind('click');
      bindNewsForm();
    }
  });
}
/* newsletterForm clientside Validation END */
/* servForm clientside Validation START */
initServForm = function() {
  var form = $('#servForm').get(0);
  fInput1 = $('#fsEmail').val();
  fInput2 = $('#fsPhone').val();
  fInput3 = $('#fsMessage').val();
  $.removeData(form,'validator');   
  $("#derror").removeClass('ok').addClass('err').html("<span></span>").hide();
  $('#servForm input, #servForm textarea').removeAttr('disabled');
  
  verifCampo('fsEmail',fInput1);
  verifCampo('fsPhone',fInput2);  
  verifCampo('fsMessage',fInput3);  
  
  $('#servForm').validate({
    errorPlacement: function(error, element) {
      error.hide();
    },  
    invalidHandler: function(form, validator) {  
      if(!($('body').validate().element('#fsEmail'))){
        $('#fsEmail').parent().addClass('error');
        $('#derror').html('<span></span>' + resources['GF_InvalidEmail']).show();
      } else if(!($('body').validate().element('#fsPhone'))){
        $('#fsEmail').parent().removeClass('error');
        $('#fsPhone').parent().addClass('error');
        $('#derror').html('<span></span>' + resources['GF_RequiredPhone']).show();
      } else if(!($('body').validate().element('#fsMessage'))){
        $('#fsEmail, #fsPhone').parent().removeClass('error');
        $('#fsMessage').parent().addClass('error');
        $('#derror').html('<span></span>' + resources['GF_RequiredSubject']).show();        
      } else {
        $('#fsEmail, #fsPhone, #fsMessage').parent().removeClass('error');
        $('#derror').html('').hide();
      }
    },    
    rules: {
      fsEmail: {initialMessage:true,email:true,required:true},
      fsPhone: {initialMessage:true,required:true}, 
      fsMessage: {initialMessage:true,required:true}       
    },
    onsubmit: false
  });
  bindServForm();  
}
function bindServForm() {
  $('#btServForm').bind('click', function () {
    if ($("#servForm").valid()) {
      $('#btServForm').unbind('click');
      $('#fsEmail, #fsPhone, #fsMessage').parent().removeClass('error');
      sendServForm();
    } else {
      $('#servForm input, #servForm textarea').removeAttr('disabled');
      $('#btServForm').unbind('click');
      bindServForm();
    }
  });
}
function resetServForm() {
  $('#servForm input, #servForm textarea').removeAttr('disabled');
  $('#fsEmail').val(fInput1);
  $('#fsPhone').val(fInput2);
  $('#fsMessage').val(fInput3);  
}
/* servForm clientside Validation END */
/* projForm clientside Validation START */
initProjForm = function() {
  var form = $('#projForm').get(0);
  fInput1 = $('#fsName').val();
  fInput2 = $('#fsEmail').val();
  fInput3 = $('#fsPhone').val();
  fInput4 = $('#fsMessage').val(); 
  $.removeData(form,'validator');   
  $("#derror").removeClass('ok').addClass('err').html("<span></span>").hide();
  $('#projForm input,#projForm textarea').removeAttr('disabled');
  
  verifCampo('fsName',fInput1);
  verifCampo('fsEmail',fInput2);  
  verifCampo('fsPhone',fInput3);  
  verifCampo('fsMessage',fInput4);
  
  $('#projForm').validate({
    errorPlacement: function(error, element) {
      error.hide();
    },  
    invalidHandler: function(form, validator) {  
      if(!($('body').validate().element('#fsName'))){
        $('#fsName').parent().addClass('error');
        $('#derror').html('<span></span>' + resources['GF_RequiredName']).show();
      } else if(!($('body').validate().element('#fsEmail'))){
        $('#fsName').parent().removeClass('error');
        $('#fsEmail').parent().addClass('error');
        $('#derror').html('<span></span>' + resources['GF_InvalidEmail']).show();
      } else if(!($('body').validate().element('#fsPhone'))){
        $('#fsName, #fsEmail').parent().removeClass('error');
        $('#fsPhone').parent().addClass('error');
        $('#derror').html('<span></span>' + resources['GF_RequiredPhone']).show();
      } else if(!($('body').validate().element('#fsMessage'))){
        $('#fsName, #fsEmail, #fsPhone').parent().removeClass('error');
        $('#fsMessage').parent().addClass('error');
        $('#derror').html('<span></span>' + resources['GF_RequiredMessage']).show();                
      } else {
        $('#fsName, #fsEmail, #fsPhone, #fsMessage').parent().removeClass('error');
        $('#derror').html('').hide();
      }
    },    
    rules: {
      fsName: {initialMessage:true,required:true},
      fsEmail: {initialMessage:true,email:true,required:true},
      fsPhone: {initialMessage:true,required:true},
      fsMessage: {initialMessage:true,required:true}      
    },
    onsubmit: false
  });
  bindProjForm();  
}
function bindProjForm() {
  $('#btProjForm').bind('click', function () {
    if ($("#projForm").valid()) {  
      $('#btProjForm').unbind('click');
      $('#fsName, #fsEmail, #fsPhone, #fsMessage').parent().removeClass('error');
      sendProjectForm();
    } else {
      $('#btProjForm').unbind('click');
      $('#projForm input, #projForm textarea').removeAttr('disabled');
      bindProjForm();
    }
  });
}
function resetProjForm() {
  $('#projForm input, #projForm textarea').removeAttr('disabled');
  $('#fsName').val(fInput1);
  $('#fsEmail').val(fInput2);
  $('#fsPhone').val(fInput3);
  $('#fsMessage').val(fInput4);    
}
/* projForm clientside Validation END */
/* contForm clientside Validation START */
initContForm = function() {
  var form = $('#contForm').get(0);
  fInput1 = $('#fsEmail').val();
  fInput2 = $('#fsPhone').val();
  fInput3 = $('#fsMessage').val();
  $.removeData(form,'validator');   
  $("#derror").removeClass('ok').addClass('err').html("<span></span>").hide();
  $('#contForm input, #contForm textarea').removeAttr('disabled');
  
  verifCampo('fsEmail',fInput1);
  verifCampo('fsPhone',fInput2);  
  verifCampo('fsMessage',fInput3);  
  
  $('#contForm').validate({
    errorPlacement: function(error, element) {
      error.hide();
    },  
    invalidHandler: function(form, validator) {  
      if(!($('body').validate().element('#fsEmail'))){
        $('#fsEmail').parent().addClass('error');
        $('#derror').html('<span></span>' + resources['GF_InvalidEmail']).show();
      } else if(!($('body').validate().element('#fsPhone'))){
        $('#fsEmail').parent().removeClass('error');
        $('#fsPhone').parent().addClass('error');
        $('#derror').html('<span></span>' + resources['GF_RequiredPhone']).show();
      } else if(!($('body').validate().element('#fsMessage'))){
        $('#fsEmail, #fsPhone').parent().removeClass('error');
        $('#fsMessage').parent().addClass('error');
        $('#derror').html('<span></span>' + resources['GF_RequiredSubject']).show();        
      } else {
        $('#fsEmail, #fsPhone, #fsMessage').parent().removeClass('error');
        $('#derror').html('').hide();
      }
    },    
    rules: {
      fsEmail: {initialMessage:true,email:true,required:true},
      fsPhone: {initialMessage:true,required:true}, 
      fsMessage: {initialMessage:true,required:true}       
    },
    onsubmit: false
  });
  bindContForm();  
}
function bindContForm() {
  $('#btContForm').bind('click', function () {
    if ($("#contForm").valid()) {
      $('#btContForm').unbind('click');
      $('#fsEmail, #fsPhone, #fsMessage').parent().removeClass('error');
      sendContactForm();
    } else {
      $('#btContForm').unbind('click');
      $('#contForm input, #contForm textarea').removeAttr('disabled');
      bindContForm();
    }
  });
}
function resetContForm() {
  $('#contForm input, #contForm textarea').removeAttr('disabled');
  $('#fsEmail').val(fInput1);
  $('#fsPhone').val(fInput2);
  $('#fsMessage').val(fInput3);    
}
/* servForm clientside Validation END */
/* verifCampo */
function verifCampo(oCampo,oValorInic){
  $('#'+oCampo).focus(function () { if ($(this).val() == oValorInic) {$(this).val('');}});
  $('#'+oCampo).blur(function () { if ($(this).val() == '') { $(this).val(oValorInic); } });  
}
/* ha */
function ha(x) {
    if ($(x).length > 0) return true;
}

$(document).ready(function() {
  $(".websites").click(function(){
    $(this).toggleClass("on");
   
    $(this).css("height",  $(".websites > ul").height()+"px");
    $(this).mouseleave(function(){
      $(this).removeClass("on");
       $(this).css("height","26px");
    });
  })  

  $("#check").click(function(){
    $(this).toggleClass("on");
  })  
  $(".aside .news:first-child").addClass('noborder');
  
  $(".rounded-input320.drop").click(function(){
    $(this).toggleClass("on");
    $(this).mouseleave(function(){
      $(this).removeClass("on");
    });
  })  
    
  $(".rounded-input156.drop").click(function(){
    $(this).toggleClass("on");
    $(this).mouseleave(function(){
      $(this).removeClass("on");
    });
  })    

  /* sitemap anim START */
  var footH = Number($("#siteMap").outerHeight()+96),
    foot = $(".footer"), footSMap = $("#siteMap"),
    contnr = $(".container"),  
    showSM = $('#show-sitemap'), hideSM = $('#hide-sitemap');
  function bindSiteMap(){
    showSM.bind('click', function () {
      siteMapStuff();
      footSMap.css({left:8});
      contnr.stop().animate({'margin-bottom':footH},300);
      foot.stop().animate({height:footH},300, function(){
        footSMap.fadeIn(200, bindSiteMap);
      });
    })  
    hideSM.bind('click', function () {
      siteMapStuff();
      footSMap.hide();
      footSMap.css({left:-4000});
      contnr.stop().animate({'margin-bottom':96},300);
      foot.stop().animate({height:96},300, bindSiteMap);
    })  
  }
  function unbindSiteMap(){
    hideSM.unbind('click');
    showSM.unbind('click');    
  }  
  function siteMapStuff(){
    unbindSiteMap();
    var scrollBottom = $(window).scrollTop() + $(window).height();
    $('html, body').animate({scrollTop: scrollBottom },300);
    hideSM.toggleClass('hide');
    showSM.toggleClass('hide');
  }
  bindSiteMap();
  /* sitemap anim END */
  $('#goNewsForm').click(function(){
    openModal();
    return false;
  });
  var fInput1, fInput2, fInput3, fInput4; 
  if (ha('#servForm')) initServForm();
  if (ha('#projForm')) initProjForm();
  if (ha('#contForm')) initContForm();  
  $('#fsearch, .footer *').attr("tabindex", "-1");
});
