$(document).ready (function() {
  var fromcat="";
  var idclicked="";
  var oldvalue="";
  var addthis="";
  $("#divmain").ready(function() {
    if (($("#projtoload")[0]) && (!isNaN($("#projtoload").text()))) {
    $("#divsmallgrid").animate({ "left" : "10px" }, "fast");
    $("#divlefticons").animate({ "left" : "10px" }, "fast");
    getcust($("#projtoload").text());
    return 0;}
    
    var url="_functions.php?action=loaddefault";
    var xmlhttp=ajaxobj("GET", url);
    xmlhttp.onreadystatechange=function() {	
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {	
    $("#divmain").html(xmlhttp.responseText);
    }}
    xmlhttp.send();
  });

  $(".beemenumoz").live("click", function () {
    var url="_functions.php?action=load_bee_menu";
    var xmlhttp=ajaxobj("GET", url);
    xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { $("#divmain").html(xmlhttp.responseText); $("#divbeemenusubmenu").fadeIn("slow");}}
    xmlhttp.send();
    $("#divsmallgrid").animate({ "left" : "10px" }, "fast");
    $("#divlefticons").animate({ "left" : "10px" }, "fast");
  });		

  $("#theframe").live("click", function (){
      $("#divsmallgrid").animate({ "left" : "10px" }, "fast");
      $("#divlefticons").animate({ "left" : "10px" }, "fast");
      $("#divlefticons img").show();
      clientid=idclicked.replace("cust","");
      if ((clientid.length>0) && !isNaN(clientid)) { getcust(clientid, fromcat); }
  });

  $("#theframe").live("mouseleave", function (){ $("#theframe div p").fadeOut("fast"); });
  $("#theframe").live("mouseover", function (){ $("#theframe div p").fadeIn("fast"); });
  
  $("#gridprojects img").live("mouseover", function (){
      var pos=$(this).position();
      var picid=this.id;
      $("#theframe #frametext").html("<p></p>"); 
      filltheframe(picid);
      idclicked=picid;
      $("#theframe").css("left", pos.left-5);
      $("#theframe").css("top", pos.top);
      $("#theframe").fadeIn("fast");
  });

  $("#iconnewsletter").live("click", function() {
    var url="_functions.php?action=newsletter";
    if ($("#divnewsletter").length != 0){
      $("#divnewsletter").fadeOut("slow", function() {$("#divnewsletter").remove();});
    }
    else {
      var xmlhttp=ajaxobj("GET", url);
      xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { $("#divall").append(xmlhttp.responseText); $("#divnewsletter").fadeIn("slow");}}
      xmlhttp.send();
    }
  });

  $("#divkey img").live("click", function() {
    var url="_functions.php?action=privateaccess";
    if ($("#divprivate").length != 0){
      $("#divprivate").fadeOut("slow", function() {$("#divprivate").remove();});
    }
    else {
      var xmlhttp=ajaxobj("GET", url);
      xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { $("#divall").append(xmlhttp.responseText); $("#divprivate").fadeIn("slow");}}
      xmlhttp.send();
    }
  });
  
  $("#divprivate input").live('focusin focusout', function(e){
    if (e.type=='focusin') {
      var regex = /^login$|^password$/
      oldvalue = this.value;
      if (this.value.match(regex)){ this.value = ""; }
    }
    if (e.type=='focusout') { if (this.value == "") { this.value=oldvalue;}}
  });
  
  $("#divprivate form").live('submit', function(){
      var url="_functions.php?action=loginprivate";
      var xmlhttp=ajaxobj("POST", url);
      xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
          $("#divprivate").html(xmlhttp.responseText);
          if (xmlhttp.responseText.match(/not allowed/)){
            setTimeout(function() { $("#divprivate").fadeOut("slow", function() {$("#divprivate").remove();})}, 1000);
          }
          }
        }
      xmlhttp.send($("#formprivate").serialize());
      return false;
  });
  
  $("#logout").live('click', function(){
    var url="_functions.php?action=logoutprivate";
      var xmlhttp=ajaxobj("POST", url);
      xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) { $("#divprivate").html(xmlhttp.responseText); }
        }
      xmlhttp.send();
      return false;
  });
  
  $("#gridprojects").live("mouseleave", function (){ $("#theframe").fadeOut("fast"); });
  
  $("#divnavpictures #left").live ("click", function(){
      var pic = $("#activeprojpic").children(":first").attr("id");
      changeprojectpicture("pref", pic);
  });
  
  $("#divnavpictures #right").live ("click", function(){
      var pic = $("#activeprojpic").children(":first").attr("id");
      changeprojectpicture("next", pic);
  });

  $("#gridfirstline img").live("click", function (){
    var clicked = this;
    var clickedimg = this.src;
    $("#theframe").fadeOut("fast");
    clickedimg = clickedimg.replace(/.+\/([a-z0-9_\.-]+)$/i, "$1");
    this.src="./images/categories/"+clickedimg.replace("_active.",".");
    $("#gridfirstline img").each(function(){
      var image = this.src;
      image = image.replace(/.+\/([a-z0-9_\.-]+)$/i, "$1");
      this.src="./images/categories/"+image.replace("_active.",".");
    });
    
    $("#divlefticons img").each(function(){
      $(this).show();
      var image = this.src;
      image = image.replace(/.+\/([a-z0-9_\.-]+)$/i, "$1");
      if (image == clickedimg.replace("_hover.", ".")) { $(this).hide(); }
      this.src="./images/categories/"+image.replace("_active.",".");
    });
    clicked.src="./images/categories/"+clickedimg.replace("_hover.","_active.");
    $("#divsmallgrid").animate({ "left" : "10px" }, "fast");
    $("#divlefticons").animate({ "left" : "10px" }, "fast");
    var catid = this.id.replace("cat","");
    if ((catid>0) && (catid.length>0)) {
      fromcat = catid;
      fillgrid(catid);
    }
  });

  $("#gridfirstline img").live("mouseover mouseleave", function (e){
    var image = this.src;
    image = image.replace(/.+\/([a-z0-9_\.-]+)$/i, "$1");
    if (!image.match(/_active./, image)){
      if (e.type=="mouseover") { this.src="./images/categories/"+image.replace(".","_hover.");}
      if (e.type=="mouseleave") { this.src="./images/categories/"+image.replace("_hover.","."); }
    }
  });

  $("#divlefticons img").live("mouseover mouseleave", function (e){
    var image = this.src;
    image = image.replace(/.+\/([a-z0-9_\.-]+)$/i, "$1");
    if (e.type=="mouseover") { this.src="./images/categories/"+image.replace(".","_active."); }
    if (e.type=="mouseleave") { this.src="./images/categories/"+image.replace("_active.","."); }
  });

  $("#divlefticons img").live("click", function (){
    var clickedimage = this.src;	
    $("#theframe").fadeOut("fast");
    clickedimage = clickedimage.replace(/.+\/([a-z0-9_\.-]+)$/i, "$1");
    clickedid = this.id.replace("lefticon","");
    if ((clickedid>0) && (clickedid.length>0)) {
      fromcat=clickedid;
      reloadgridwithid(clickedid);}
    $("#divlefticons img").show();
    $(this).hide();
    $("#gridfirstline img").each(function(){
      var image = this.src;
      image = image.replace(/.+\/([a-z0-9_\.-]+)$/i, "$1");
      this.src="./images/categories/"+image.replace("_active.",".");
      if (image == clickedimage.replace("_active.", ".")) { this.src="./images/categories/"+image.replace(".", "_active."); }
    });
  });
  
  $("#imgsmallgrid").click( function (){
    $("#divsmallgrid").animate({ "left" : "93px" }, "fast");
    $("#divlefticons").animate({ "left" : "93px" }, "fast");
    fromcat = "";
    var url="_functions.php?action=loaddefault";
    var xmlhttp=ajaxobj("GET", url);
    xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { $("#divmain").html(xmlhttp.responseText);} }
    xmlhttp.send();
  });
  
  $("#divcontactform input").live('focusin focusout', function(e){
    if (e.type=='focusin') {
      var regex = /^first name$|^last name$|^email address$|^phone number$|^prénom$|^nom$|^adresse e-mail$|^numéro de téléphone$/
      oldvalue = this.value;
      if (this.value.match(regex)){ this.value = ""; }
    }
    if (e.type=='focusout') { if (this.value == "") { this.value=oldvalue;}}
  });
  
  $("#divcontactform textarea").live('focusin focusout', function(e){
      if (e.type=='focusin') { if (this.value.match(/^send me your question$|^envoyez-moi votre question$/)){ oldvalue=this.value; this.value = ""; }}
      if (e.type=='focusout') { if (this.value == "") { this.value=oldvalue; }}
  });
  
  $("#divcontactform form").live('submit', function(){
      var url="_functions.php?action=sendcontactform";
      var xmlhttp=ajaxobj("POST", url);
      xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
          if (xmlhttp.responseText.length > 0) { alert (xmlhttp.responseText); }
          document.getElementById("contactform").reset();
        }
      }
      $.ajaxSetup({'beforeSend' : function(xhr) {xhr.overrideMimeType('text/html; charset=UTF-8');}});
      xmlhttp.send($("#contactform").serialize());
      return false;
  });

  $("#frmEEMSignupForm").live('submit', function(){
    var url="https://app.expressemailmarketing.com/SignupForm.handler?SFAction=ProcessSubmit&SFID=123639&SFCustomerID=123882";
    var xmlhttp=ajaxobj("POST", url);
    xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) {}}
    $.ajaxSetup({'beforeSend' : function(xhr) {xhr.overrideMimeType('text/html; charset=UTF-8');}});
    xmlhttp.send($("#frmEEMSignupForm").serialize());
    alert ("Inscription ok");
    $("#divnewsletter").fadeToggle("fast");
    $("#divnewsletter").html("&nbsp;");
    return false;
  });
  
  $("#clientsortbyproduct").live('click', function(){ beemenuactions("clientslist", "product");});
  $("#clientsortbyindustry").live('click', function(){ beemenuactions("clientslist", "industry");});
});

  $("#frmEEMSignupForm input").live('focusin focusout', function(e){
    if (e.type=='focusin') {
      var regex = /^first name$|^last name$|^e-mail address$|^above code$|^prénom$|^nom$|^adresse e-mail$|^code ci-dessus$/
      oldvalue = this.value;
      if (this.value.match(regex)){ this.value = ""; }
    }
    if (e.type=='focusout') { if (this.value == "") { this.value=oldvalue;}}
  });


function ajaxobj(type, url){
  if (window.XMLHttpRequest) { ajaxobject=new XMLHttpRequest(); }
  else { ajaxobject=new ActiveXObject("Microsoft.XMLHTTP"); }

  if (type=="POST") {
    ajaxobject.open("POST",url,true);
    ajaxobject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajaxobject.setRequestHeader("Connection", "close");
    return(ajaxobject);
  }
  if (type=="GET") {
    ajaxobject.open("GET",url,true);
    return(ajaxobject);
  }
}

function fillgrid(type_id) {
  var url="_functions.php?action=getgrid&cat="+type_id;
  var xmlhttp=ajaxobj("GET", url);
  xmlhttp.onreadystatechange=function() {if (xmlhttp.readyState==4 && xmlhttp.status==200) { $("#gridprojects").html(xmlhttp.responseText); }}
  xmlhttp.send();
}

function reloadgridwithid(id) {
  var url="_functions.php?action=loaddefault&cat="+id;
  var xmlhttp=ajaxobj("GET", url);
  xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { $("#divmain").html(xmlhttp.responseText); }}
  xmlhttp.send();
}

function getcust(cust_id, thecat) {
  if ((thecat>0) && (thecat.length>0) && (!isNaN(thecat))) { addthis = "&fromcat="+thecat; }
  else { addthis=""; }
  var url="_functions.php?action=loadcustomer&cust="+cust_id+addthis;
  var xmlhttp=ajaxobj("GET", url);
  xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { $("#divmain").html(xmlhttp.responseText);  }}
  xmlhttp.send();
}

function filltheframe(id) {
  var url="_functions.php?action=projinfo&cust="+id;
  var xmlhttp=ajaxobj("GET", url);
  xmlhttp.onreadystatechange=function() {if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    $("#theframe #frametext").html(xmlhttp.responseText);
    $("#theframe div p").fadeIn("fast"); 
  }}
  xmlhttp.send();
}

function beemenuactions(category, option) {
  var url="";
  $("#divbeemenusubmenu li").css({ "color" : "#000000" });
  switch (category){
    case "testimonials" :
      url="_functions.php?action=loadtestimonials";
      $("#divbeemenusubmenu #litestimonials").css({ "color" : "#F15A24" });
      break;

    case "aboutbee" :
      url="_functions.php?action=loadaboutbee";
      $("#divbeemenusubmenu #liaboutbee").css({ "color" : "#F15A24" });
      break;

    case "clientslist" :
      if ((option=="product") || (option=="industry"))
        url="_functions.php?action=loadclientlist&bytype="+option;
      else
        url="_functions.php?action=loadclientlist";
      $("#divbeemenusubmenu #liclients").css({ "color" : "#F15A24" });
      break;

    case "contact" :
      url="_functions.php?action=loadcontactform";
      $("#divbeemenusubmenu #licontact").css({ "color" : "#F15A24" });
      break;

    default:
      break;
  }
  if (url==""){ return 1; }
  var xmlhttp=ajaxobj("GET", url);
  xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { $("#divcontentbeemenu").html(xmlhttp.responseText); }}
  xmlhttp.send();
}

function changeprojectpicture (direction, actual) {
  var url="_functions.php?action=load_prev_next_pictures&dir="+direction+"&picture="+actual;
  var xmlhttp=ajaxobj("GET", url);
  xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) {	
    if (xmlhttp.responseText.length>3) {
      var datas = xmlhttp.responseText.split("|");
      $("#activeprojpic").html(datas[0]);
      $("#divnavpictures").html(datas[1]);
      $("#projcategories").html(datas[2]);
    }
  }}
  xmlhttp.send();
}

function showhide(divname, type, speed){
  if (type == "show") {	$('#'+divname).show(speed); }
  if (type == "hide") {	$('#'+divname).hide(speed); }
  if (type == "toggle") { $('#'+divname).toggle(speed); }
}

function trim(mytext) { return mytext.replace(/^\s+/g,'').replace(/\s+$/g,''); }

