var useBSNns = true;

// callback pour le formulaire de prise de contact
function callMeBack(v,m){
	if(v){
    error = false;
  	if (m.children('#nom').val() == '') { error = true; m.children('#nom').css("border","solid #ff0000 1px"); }
    if (m.children('#tel').val() == '') { error = true; m.children('#tel').css("border","solid #ff0000 1px"); }
    if (m.children('#email').val() == '') { error = true; m.children('#email').css("border","solid #ff0000 1px"); }
    if (m.children('#commentaire').val() == '') { error = true; m.children('#commentaire').css("border","solid #ff0000 1px"); }

  	if(error)
  	{
  		return false;
  	}
  	else
  	{
  		to = 'postmaster@umiha.fr';
  		from = 'postmaster@umiha.fr';
  		subject = 'Demande de contact';
    	body = m.children('#nom').val()+'\n'+m.children('#tel').val()+'\n'+m.children('#email').val()+'\n'+m.children('#commentaire').val();
    	sendEmail(to,from,subject,body);
      return true;
  	}
  }
  else
  {
  	return true;
  }
}

// callback pour le formulaire de recommandation du site
function recommandThisSite(v,m){
  if(v){
    error = false;
    if (m.children('#to_name').val() == '') { error = true; m.children('#to_name').css("border","solid #ff0000 1px"); }
    if (m.children('#to_email').val() == '') { error = true; m.children('#to_email').css("border","solid #ff0000 1px"); }
    if (m.children('#from_name').val() == '') { error = true; m.children('#from_name').css("border","solid #ff0000 1px"); }
    if (m.children('#from_email').val() == '') { error = true; m.children('#from_email').css("border","solid #ff0000 1px"); }

    if(error)
    {
      return false;
    }
    else
    {
      site = $.ajax({
        url: '/modal/getSiteName',
        dataType: "html",
        async: false
      }).responseText;

      subject = 'Quelqu\'un vous recommande le site '+site;
    	to = m.children('#to_email').val();
    	from = m.children('#from_email').val();
      body = 'Bonjour,\n'
      body += m.children('#from_name').val()+' ('+m.children('#from_email').val()+') vous recommande d\'aller visiter ce site http://'+site;
      sendEmail(to,from,subject,body);
      return true;
    }
  }
  else
  {
    return true;
  }
}

// Envoi du formulaire par email
function sendEmail(to, from, subject, body){
  $.ajax({
   type: "POST",
   url: '/modal/send',
   data: 'to='+to+'&from='+from+'&subject='+subject+'&body='+body,
   dataType: 'html',
   success: function(msg){ $.prompt(msg) },
   error: function(){ $.prompt('Une erreur est survenue lors de l\'envoi du formulaire.') }
  })
}

// Variables pour le scrollup
var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

// Fonction de scrollup
function headline_rotate() {
  current_headline = (old_headline + 1) % headline_count;
  $("div.headline:eq(" + old_headline + ")").animate({top: -105},"slow", function() {
    $(this).css('top', '110px');
  });
  $("div.headline:eq(" + current_headline + ")").animate({top: 5},"slow");
  old_headline = current_headline;
}

$(document).ready(
  function() {
  	// Highslide sur les agenda de la home
     $('.event').each(function() {
        this.onclick = function() {
          //return hs.htmlExpand(this, { contentId: 'highslide-html' } );
        	return hs.htmlExpand(this, {
        	  objectType: 'ajax',
        	  height: 150
          });
        };
     });
     
     // Highslide sur les iPaper
     $('.ipaper').bind('click', function() {
          return hs.htmlExpand(this, {
           objectType: 'iframe',
           objectLoadTime: 'after',
           width: 1024,
           height: 768
          });
     });

    // Initialisation du scrollup
    headline_count = $("div.headline").size();
    $("div.headline:eq("+current_headline+")").css('top', '5px');
    headline_interval = setInterval(headline_rotate,7500);
    $('#scrollup_welcome').hover(function() {
      clearInterval(headline_interval);
    }, function() {
      headline_interval = setInterval(headline_rotate,7500);
      headline_rotate();
    });

  $('#menutop ul > li').hover(function() {
    $(this).find('div').show();
    },
    function() {
      $(this).find('div').hide();
    });
  
    $('.cornerBloc').css('padding', '20px').corner();
    
    settings = {
      tl: { radius: 20 },
      tr: { radius: 20 },
      bl: { radius: 20 },
      br: { radius: 20 },
      antiAlias: true,
      autoPad: true
    };

    settingsMenus = {
      tl: { radius: 0 },
      tr: { radius: 0 },
      bl: { radius: 20 },
      br: { radius: 20 },
      antiAlias: true,
      autoPad: true
    };

    // hides the slickbox as soon as the DOM is ready
    // (a little sooner than page load)
    $('a.hide').hide();
    // toggles the slickbox on clicking the noted link
    $('a.show').click(function() {
      $(this).parents("div:first").height("auto");
      $('a.show').hide();
      $('a.hide').show();
    return false;
    });
    $('a.hide').click(function() {
      $('a.hide').hide();
      $('a.show').show();
      $(this).parents("div:first").animate({
        height: 270
      }, 'slow')
    return false;
    });
    $('a.toggle').click(function(){
      if($(this).parents("div:first").height()==270){
        $(this).text('(r\xE9duire le cadre)');
        $(this).parents("div:first").height("auto");
      }else{
        $(this).text('(voir tout le texte)');
        $(this).parents("div:first").animate({
          height: 270
        }, 'slow')
      }
      return false;
    });
    
    // Change les title
    $('.wait').attr({title:'Le site est en cours de d\xE9veloppement | Cette fonctionnalit\xE9 sera activ\xE9e prochainement.'});
    
    // Tooltips du layout
     $('.wait, .tips').tooltip({ 
         track: true, 
         delay: 0, 
         showURL: false, 
         showBody: " | ", 
         opacity: 0.85 
     });
    
    // Reset les champs input lorsque l'on clique dedans
    $('input').click(function(){
      $(this).val('');
    })
    
    // Fenêtres modal avec le plugin jQuery Impromptu
    $.SetImpromptuDefaults({
      show: 'fadeIn',
      overlayspeed: 'fast',
      opacity: 0.4
    });
    
    // modal de call me back
    $('#callMeBack').click(function(){
      txt = $.ajax({
        url: '/modal/callMeBack',
        dataType: "html",
        async: false
      }).responseText;
      $.prompt(txt, {
        submit: callMeBack,
        buttons: {
          Valider: true,
          Annuler: false
        }
      });
    });
    
    // modal de recommander ce site
    $('#recommandThisSite').click(function(){
      txt = $.ajax({
        url: '/modal/recommandThisSite',
        dataType: "html",
        async: false
      }).responseText;
      $.prompt(txt, {
        submit: recommandThisSite,
        buttons: {
          Valider: true,
          Annuler: false
        }
      });
    });
    
    // Autosuggest en recherche libre colonne de gauche
    var options = {
      script: "/fournisseur/autoSuggestFournisseur?",
      varname: "raison_sociale",
      cache: false,
      timeout: 10000,
      minchars: 3,
      json: true,
      width: 130,
      callback: function(obj) {
        $('#id_fournisseur').val(obj.id);
        document.search_fournisseur.submit();
      },
      json: true
    };
    var as = new bsn.AutoSuggest('raison_sociale', options);
  }
);

