jQuery(document).ready(function()
{
  /* init: hauteur noir du bloc droit "misc" 
  var misc = document.getElementById('misc');
  var content = document.getElementById('content');
  if (misc && content) {
    var content_h = parseInt(content.offsetHeight);
    var misc_h = parseInt(misc.offsetHeight);
    if (content_h > misc_h) {
      misc.style.height = content_h + 'px';
    }
  }
  */

  cookie_size = getCookie('dyn_txt');
  if(cookie_size != null) {
      jQuery('.dyn_txt').css('font-size', cookie_size);
  }

  jQuery('#search .search').autocomplete("/recherche/autocomplete");
  jQuery('#textup').click(function() {
      size = parseInt(jQuery('.dyn_txt').css('font-size')) + 1;
      jQuery('.dyn_txt').css('font-size', size + 'px');
      document.cookie = "dyn_txt=" + size + 'px; path=/; domain=.mon-sondage.com';
  });
  jQuery('#textdown').click(function() {
      size = parseInt(jQuery('.dyn_txt').css('font-size')) - 1;
      jQuery('.dyn_txt').css('font-size', size + 'px');
      document.cookie = "dyn_txt=" + size + 'px; path=/; domain=.mon-sondage.com';
  });
  jQuery('#launchmail').click(function() {
    jQuery('#mailer').slideToggle("slow");
  });
  jQuery('#mailer').submit(function() {
    nom = jQuery("input[@name=nom]").val();
    my_mail = jQuery("input[@name=my_mail]").val();
    emails = jQuery("input[@name=emails]").val();
    bcc = jQuery("input[@name=bcc]").attr("checked");
    msg = jQuery("textarea[@name=msg]").val();

    if(nom == '') {
      alert('Le champs nom est obligatoire');
      return false;
    }
    if(my_mail == '') {
      alert('Le champs de votre email est obligatoire');
      return false;
    }
    if(emails == '') {
      alert('Le champs des emails à envoyer est obligatoire');
      return false;
    }

    jQuery.ajax({
      type: "POST",
      dataType: "html",
      data: "nom="+escape(nom)+"&my_mail="+escape(my_mail)+"&emails="+escape(emails)+"&bcc="+escape(bcc)+"&msg="+ escape(msg),
      url: "/mailer",
      success: function(msg) {
        jQuery('#mailer').before("<p id='successmsg' style='display: none;'>Votre message a bien &eacute;t&eacute; envoy&eacute;.</p>");
        jQuery('#mailer').slideToggle("slow");
        jQuery('#successmsg').slideDown("slow");
      }
    });
    return false;
  });

  //Set Greybox
  jQuery("a.greybox").click(function()
  {
    var t = this.title || this.innerHTML || this.href;
    GB_show(t,this.href,600,800);

    return false;
  });


  jQuery('a[@rel=external]').click(function(){
    window.open(this.getAttribute('href'));
    return false;
  });


  jQuery('a[@rel^=popup]').click(function(){
    var dims = this.getAttribute('rel').match(/.*\[([0-9]+)-([0-9]+)\].*/);
    window.open(this.getAttribute('href'),'Info','width=' + dims[1] + ',height=' + dims[2] + ',resizable,scrollbars');
    return false;
  });

  jQuery('input.autoclear:text, input.autoclear:password').focus(function(){
	  if (this._defaultValue == this.value){
	    this.value="";
	  }
  }).each(function(){
    this._defaultValue = this.getAttribute('value');
  });
});


function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';   
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)              
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)                                           
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

