// JavaScript Document

 /*
 * Blocus - My Jquery plugin
 */
(function($){
  $.fn.extend({
    blocus: function(){
      return this.filter("input:text, textarea, input:password").each(function(){
        var initVal = $(this).val();
        $(this).bind("focus", function(){
          switch($(this).val()){
            case initVal :
            $(this).val('');
            break;
          }
        })
        $(this).bind("blur", function(){
          switch($(this).val()){
            case '' :
            $(this).val(initVal);
            break;
          }
        })
      })
    }
  })
})(jQuery);

function addRoundBorder($element, roundLf, roundRt){
  $element.before(roundLf).after(roundRt);
}

function isIE7(){
  if($.browser.msie&&$.browser.version=="7.0") return true;
    else return false;
}

function browserIsIE6(){
  if($.browser.msie&&$.browser.version=="6.0")
    return true;
    else return false;
}

function checkEmail(emailField) {
  var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
  if (!filter.test(emailField)) {
  return false;
  }else return true;
}

function alertNotification($alertDisplay, message){
  $alertDisplay.html(message);
}

function revertNotification($alertDisplay){
  $alertDisplay.empty();
}

function doEditable(obj) {
	obj.ckeip({
		e_url: base_path+"ajax.ContentStatique.php",
		e_hover_color: "none",
		data : {id_container: obj.attr("id")}
	});
	obj.attr("title", "Double-clic pour editer");
}

function windowSize($divToResize, screenSize){
  if($(window).width() <= 1024){
    $divToResize.css({
      minWidth: screenSize,
      width: screenSize
    })
  }else{
    $divToResize.css({
      minWidth: '',
      width: 'auto'
    });
  }
}

$(document).ready(function(){
  /*
  * Clearfix
  */
  if(browserIsIE6() ==  true) $('body').attr('id', 'bodyIe6');
  $(':floated').not('#menuTopBody :floated', '.round-header-textarea span').addClass('clearfix');
  $(".clearfix").live('addClearFix', function(){
    $(this).after("<span class='clear'></span>");
  }).trigger('addClearFix');

  windowSize($("#mainContentShadow"), "1200px");
  $(window).resize(function(){
    windowSize($("#mainContentShadow"), "1200px");
  })
  
  if($("#candSpontForm").length > 0){
    $("head").append('<script type="text/javascript" src="'+base_path+'design/js/functions-candidature.js" charset="utf-8"></script>');
  }
	if($.browser.msie && $.browser.version == "7.0"){
	 $(".big-menu.contact", "#bodyFooter").next('.clearer').remove();
  }
  $("input:text, input:password", "#meoSpaceForm").blocus();
  $("#mainContentShadow").equalHeights();
	//$(".big-menu:first", "#bodyFooter").css("border-left", "none");
	//$(".big-menu:last", "#bodyFooter").css("border-right", "none");
  // news toggler
		$("a.lire-suite", "#bodyActualites").toggle(function(e){
		  e.preventDefault();
      $(this).parent("li").find("div.detail-actu").slideDown();
      $(this).html('X Fermer');
      $(this).addClass("deplie");
			//alert(currentnews);
		}, function(e){
		  e.preventDefault();
      $(this).parent("li").find("div.detail-actu").slideUp();
      $(this).html('Lire la suite'); 
      $(this).removeClass("deplie");
		});
  
	// Nos metiers
  $("a", ".table-engagement-expertise").click(function(e){
    $("a.current", ".table-engagement-expertise").removeClass("current");
    $(this).addClass("current");
    e.preventDefault();
    var cellIndex = $(this).parent("td").index();
    cellIndex = parseInt(cellIndex + 1);
    $("th", ".table-engagement-expertise").removeClass("current");
    $(this).parent("td").parent("tr").find("th").addClass("current");
    $("th:nth-child("+cellIndex+")", ".table-engagement-expertise").addClass("current");
    //console.log(cellIndex);
    var detailIndex = $("a", ".table-engagement-expertise").index(this);
    //console.log(detailIndex);
    if($(".intro-offres-metiers").show()){
      $(".intro-offres-metiers").hide();
    }
    $(".metiers-select-body:visible").hide("fast");
    $('div[id*="ckeip"]').hide();
    $(".metiers-select-body:eq("+detailIndex+")").show("fast");
	
  });
  //news toggler
  
  // OVERLAY //
    $("#bodyCarriereEquipe li a[rel], #bodyCarrOffreEmpDetail a[rel]").overlay({
        mask: {

    		// you might also consider a "transparent" color for the mask
    		color: '#000',
    
    		// load mask a little faster
    		loadSpeed: 200,
    
    		// very transparent
    		opacity: 0.7
    	},
        top: 'center'
    });
  // OVERLAY //
  
  // Contact //
    
	$("input:text, textarea", "#contactForm").blocus();
  
  $('input[type="button"]', '#contactForm').click(function(){
    if($("#contactNom").val() == "Nom" || $("#contactPrenom").val() == "Prénom" || $("#contactMail").val() == "Adresse email" ||  $("#contactMess").val() == "Message"){
      alertNotification($("h2", "#contactForm"), "Veuillez remplir les champs obligatoires");
      return false;
    }
    if(checkEmail($("#contactMail", "#contactForm").val()) == false){
      alertNotification($("h2", "#contactForm"), "Veuillez insérer une adresse email valide");
      $("#contactMail", "#contactForm").focus();
      return false;
    }else{
    var data = $("#contactForm").serialize();
    $.ajax({
      url: base_path+"mail/sendContact.php?",
      data: data,
      success: function(msg){
        switch(msg){
          case "3":
          alertNotification($("h2", "#contactForm"), "Veuillez remplir tous les champs");
          return false;
          
          case "2":
          alertNotification($("h2", "#contactForm"), "Erreur lors de l'envoie du mail, veuillez recommencer");
          return false;
          
          case "1":
          alertNotification($("h2", "#contactForm"), "Votre message a bien été envoyé, nous vous contacterons dans les plus brefs délais.");
          return true;
        }
      }
    })
  }
})
  
  // end Contact //
  /*
  * Référence
  */
  $(".liste-ref", "#bodySecteurs").click(function(){
    var url = $(this).find("a").attr("href");
    window.location.href = url;
  })
  $(".liste-ref", "#bodySecteurs").not(".liste-ref-hover-persistent").hover(function(){
    $(this).addClass("liste-ref-hover");
  }, function(){
    $(this).removeClass("liste-ref-hover");
  })
  $(".liste-ref", "#bodySecteurs").each(function(){
    var ulChild = $(this).find(" > ul");
    ulChild.filter(function(index){
      return $(this).children().length == 1
    }).addClass("one-row");
    var ulChild = $(this).find(" > ul");
    ulChild.filter(function(index){
      return $(this).children().length == 2
    }).addClass("two-row");
    var ulChild = $(this).find(" > ul");
    ulChild.filter(function(index){
      return $(this).children().length == 3
    }).addClass("three-row");
  })
  /*
  * end référence
  */
  /*
  * Cross browser round border
  */
  if(isIE7()){
    $("input:text, input:password").each(function(){
      $(this).css("line-height", "1.5");
    })
  }
  
  // input text
  $(".beautiful-textbox", $("#contactForm")).live('addRound', function(){
    addRoundBorder($(this), '<img src="'+base_path+'design/images/round-meotec-textbox-lf.png" class="round-beautiful-textbox" />', '<img src="'+base_path+'design/images/round-meotec-textbox-rt.png" class="round-beautiful-textbox" />');
  })
  $(".beautiful-textbox", $("#meoSpace")).live('addRound', function(){
    if($(this).is(":text")){
      addRoundBorder($(this), '<img src="'+base_path+'design/images/round-meospace-textbox-lf.png" class="round-beautiful-textbox" />', '<img src="'+base_path+'design/images/round-meospace-textbox-rt.png" class="round-beautiful-textbox" />');
    }else if($(this).is(":password")){
      addRoundBorder($(this), '<img src="'+base_path+'design/images/round-meospace-textbox-lf.png" class="round-beautiful-textbox" />', '');
      $(this).css("border-right", "solid 1px #008CC0").css("padding-right", "4px");
    }
  })
  $(".beautiful-textbox", $("form#candSpontForm")).live('addRound', function(){
    addRoundBorder($(this), '<img src="'+base_path+'design/images/round-meospace-textbox-lf.png" class="round-beautiful-textbox" />', '<img src="'+base_path+'design/images/round-meospace-textbox-rt.png" class="round-beautiful-textbox" />');
  })
  $(".beautiful-textbox").trigger('addRound');
  //textarea
  //$(".pretty-textarea").wrap('<div class="beautiful-textarea"></div>').before('<div class="round-header-textarea"><span></span></div>').after('<div class="round-footer-textarea"><span></span></div>');
  // -- Tooltip
	$('li', $('#equipeList')).mousemove(function(e){
		var index = $(this).index();
		var xPos = e.pageX;
		var yPos = e.pageY;
		$('.equipe-tip:eq('+index+')', $('#bodyCarriereEquipe')).show().offset({'left':(xPos+15),'top':(yPos-15)});
	}).mouseout(function(){
		var index = $(this).index();
		$('.equipe-tip:eq('+index+')', $('#bodyCarriereEquipe')).hide();
	})
})

$(window).load(function(){
  
  /*
   * Miniature actualité page d'accueil
  */
  $(".actualites-body span a img", "#actualites").vAlign().hAlign();
  $(".intro span img", "#bodyActualites").vAlign().hAlign();
})

