jQuery.noConflict();

jQuery(function($) {
  submit_contact_form('#guest-book', $("#guest-book").hasClass('contact') ? '' : 'guestbook=enviado');

  setTimeout("home_box_animation('#position-1')", 2000);
  setTimeout("home_box_animation('#position-2')", 2250);
  // home_box_animation("#position-2");
  
  animate_menu_element();

  accordeom();

  $('a.language').click(function(event){
    var lang = $('a.language').attr('rel');
    eraseCookie('lang');
    createCookie('lang', lang, 365);
  });
  
  if(!/http:\/\/.*\.(com\.br|cx|dev)\/(pt|en)/.test(document.location.href)) {
    var lang = readCookie('lang') !== null ? readCookie('lang') + '/' : 'pt/';
    var str = document.location.href;
    document.location.href = str.replace(/^(http:\/\/.*\.)(com\.br|cx|dev)\//, '$1$2/' + lang);
  }
  
  $('#extranet-form a').click(function(event) { event.preventDefault; $('#extranet-form').submit(); });
  
});

function slideout(){ setTimeout(function(){ jQuery("#answer").slideUp("slow", function () {});}, 3000);}

function submit_contact_form(form, urlparam) {
  jQuery(form).submit(function(event) {
    event.preventDefault();
    jQuery.post('/wp-content/themes/reconsult/mail_sender.php?' + urlparam, jQuery(form).serialize(), function(theResponse) {
      if(/(not|não)/.test(theResponse)) {
        jQuery('#answer').addClass('fail');
        jQuery("#guest-book").find(':input').not('.no-resized').each(function() {
          jQuery(this).val().length <= 0 ? show_errors(this) : hide_errors(this);
        })
      } else { jQuery('#answer').removeClass('fail'); }

      jQuery('#answer').html('<p>' + theResponse + '</p>');
      slideout();
      jQuery('#answer').slideDown('slow');

      if(/(redirected|redirecionado)/.test(theResponse)) { 
       setTimeout("window.location.href= '/' + jQuery('#guest-book input[name=lang]').attr('value') + '/biblioteca'", 5000);
      }
    })
  });
}

function show_errors(field) {
  var error_element = '#' + jQuery(field).attr('name') + '-error';
  jQuery(field).addClass('red-border');
  jQuery(error_element).addClass('active');
}

function show_email_errors(field) {
    var error_element = '#' + jQuery(field).attr('name') + '-error';
    jQuery(error_element).html("E-mail inválido");
    jQuery(field).addClass('red-border');
    jQuery(error_element).addClass('active');
}

function hide_errors(field) {
  var error_element = '#' + jQuery(field).attr('name') + '-error';
  jQuery(field).removeClass('red-border');
  jQuery(error_element).removeClass('active');
}


function home_box_animation(id) {
  jQuery(id).find('header').fadeTo(0, 0);
  jQuery(id).find('section').fadeTo(0, 0);
  var eheight = jQuery(id).find('section').height() + jQuery(id).find('header').height() + 95 + 70;
  jQuery(id).height(95).css({visibility: 'visible', display: 'none'});
  jQuery(id).fadeIn('fast').delay(400).animate({height: eheight}, 'slow', function() {
    jQuery(id).find('header').fadeTo('fast', 1);
    jQuery(id).find('section').fadeTo('fast', 1);
  });
}

function animate_menu_element() {
  jQuery('header nav div ul li a').stop().animate({ backgroundPosition: '-15px 110px'}, 200);
  jQuery('header nav div ul li a').stop().animate({ backgroundPosition: '-15px 0px'  }, 200);
  jQuery('header nav div ul li.current-menu-item a').stop().animate({ backgroundPosition: '-15px 110px'}, 200);
  jQuery('header nav div ul li').not('.current-menu-item').find('a').hover(
    function () { jQuery(this).stop().animate({ backgroundPosition: '-15px 110px'} ,200);},
    function () { jQuery(this).stop().animate({ backgroundPosition: '-15px 0px'  }, 
          { duration: 200, complete: function() { jQuery(this).css({backgroundPosition: "15px 0px"});          }});
    });
}

function accordeom() {
  jQuery('article.biblioteca section').each(
    function() { 
      jQuery(this).attr('height', jQuery(this).height() + 40).css('display', 'none');
  });
  
  jQuery('article.biblioteca header h2').click(
    function() {
      var height = jQuery(this).parent().parent().find('section').attr('height');
      jQuery(this).parent().parent().find('section').stop().animate({'height':'30px'}, 500);
      jQuery('article.biblioteca section').stop().animate({'height':'0'}, 500);
      jQuery(this).parent().parent().find('section').css('display', 'block');
      jQuery(this).parent().parent().find('section').stop().animate({'height': height + 'px'}, 500);
  });
}

// Cookies 
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) { createCookie(name,"",-1); }

var params = { quality: "high", scale: "noscale", wmode: "transparent", allowscriptaccess: "always", menu: "false", bgcolor: "#ffffff" };
var flashvars = {};
var attributes = { id: "home-image", name: "flashcontent" };
swfobject.embedSWF("/wp-content/themes/reconsult/swf/home_image.swf", "flashcontent", "534", "320", "9", '', flashvars, params, attributes);


