// Menu
// Original : http://javascript-array.com/scripts/jquery_simple_drop_down_menu/
// Modification : SGL
var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open() {
  jsddm_canceltimer();
  jsddm_close();
  if($(this).hasClass('simpleLinkMenu')){
    $(this).addClass('simpleLinkMenu_over');
  }else{
    $(this).addClass('over');
  }
  ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}

function jsddm_close() {
  if (ddmenuitem)
    ddmenuitem.css('visibility', 'hidden');
  $('#menu_content > ul > li.over').removeClass('over');
  $('#menu_content > ul > li.simpleLinkMenu_over').removeClass('simpleLinkMenu_over');
}

function jsddm_timer() {
  closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer() {
  if (closetimer) {
    window.clearTimeout(closetimer);
    closetimer = null;
  }
}

/*
 *
 * Service page
 *
 */
var idxLastService = -1;
var imgY = -10;
var BUTTON_OVER = "button_over";
var BUTTON_OUT = "button_out";
var BUTTON_ACTIVE = "button_active";
var BUTTON_SELECTED = "button_selected";

// Return the service's index
function serviceIndex(ele) {
  var idx = ele.index();

  if (ele.attr('class') == 'image') {
  } else {
    idx = idx - 1;
  }

  return idx;
}

// Set the button image state.
function setButtonImage(idx, state) {

  var buttonImg = $('#services > #images > div').eq(idx);

  switch (state) {
  case BUTTON_OVER:
    buttonImg.stop().animate( {
      "top" : "10px"
    }, 300);
    break;
  case BUTTON_OUT:
    buttonImg.stop().animate( {
      "top" : "0px"
    }, 300);
    break;
  case BUTTON_SELECTED:
    buttonImg.stop();
    buttonImg.unbind('click mouseenter mouseleave');
    buttonImg.css("top", "10px");
    break;
  case BUTTON_ACTIVE:
    buttonImg.stop();
    buttonImg.hover(serviceOn, serviceOut);
    buttonImg.click(serviceButtonClick);
    buttonImg.css("top", "0px");
    break;
  default:
    break;
  }
}

// Set the submenu button state.
function setButtonSubMenu(idx, state) {
  var newIdx = idx + 1;
  // Reset the submenu.
  var buttonMenu = $("#submenu > ul > li:eq(" + (newIdx) + ")");

  switch (state) {
  case BUTTON_OVER:
    buttonMenu.addClass("selected");
    break;
  case BUTTON_OUT:
    buttonMenu.removeClass("selected");
    break;
  case BUTTON_SELECTED:
    buttonMenu.addClass("selected");
    buttonMenu.unbind('click mouseenter mouseleave');
    break;
  case BUTTON_ACTIVE:
    buttonMenu.removeClass("selected");
    buttonMenu.hover(serviceOn, serviceOut);
    buttonMenu.click(serviceButtonClick);
    break;
  default:
    break;
  }
}

// Set the title state.
function setTitle(idx, visible) {
  if (visible) {
    $("#titles > div").eq(idx).css( {
      visibility : 'visible'
    });
  } else {
    $("#titles > div").eq(idx).css( {
      visibility : 'hidden'
    });
  }
}

// Set the text state.
function setText(idx, visible) {
  if (visible) {
    $("#texts > div").stop().eq(idx).show(500);
  } else {
    $("#texts > div").stop().eq(idx).hide();
  }
}

// Select a service.
function selectService(idx) {
  var buttonImg = null;
  var buttonMenu = null;
  var title = null;
  var text = null;
  var img = null;

  if (idxLastService >= 0) {
    setButtonImage(idxLastService, BUTTON_ACTIVE);
    setButtonSubMenu(idxLastService, BUTTON_ACTIVE);
    setTitle(idxLastService, false);
    setText(idxLastService, false);
  }

  // Set the elements on select state.
  setButtonImage(idx, BUTTON_SELECTED);
  setButtonSubMenu(idx, BUTTON_SELECTED);

  // Show the current title.
  setTitle(idx, true);

  // Show the current text.
  setText(idx, true);

  // Set the new index.
  idxLastService = idx;
}

// Service's buttons click handler.
function serviceButtonClick() {
  var idx = serviceIndex($(this));

  $(this).blur();

  // Select the service.
  selectService(idx);
}

// Service's buttons hover handler.
function serviceButtonHover(citem, on) {

  var idx = serviceIndex(citem);

  if (idx != idxLastService) {
    // Rollover image
    on ? setButtonImage(idx, BUTTON_OVER) : setButtonImage(idx, BUTTON_OUT);

    // Rollover submenu item
    on ? setButtonSubMenu(idx, BUTTON_OVER) : setButtonSubMenu(idx, BUTTON_OUT);

    if (on) {
      // Hide last title.
      setTitle(idxLastService, false);
      // Show the current title.
      setTitle(idx, true);
    } else {
      // Show last title.
      setTitle(idxLastService, true);
      // Hide current title.
      setTitle(idx, false);
    }
  }
}

// Service's handlers.
function serviceOn() {
  serviceButtonHover($(this), true);
}
function serviceOut() {
  serviceButtonHover($(this), false);
}

// Initialize the service's page.
function init_services() {
  // Determine which service is selected at start.
  idx = serviceIndex($('#submenu > ul > li').siblings('.selected'));

  // Move all titles under their corresponding image.
  var nb = $('#submenu > ul > li').length - 1;

  for ( var i = 1; i < nb; i++) {
    // get the position of the image
    var img = $('#services > #images > div').eq(i);
    var left = img.position().left;
    var width = img.outerWidth(true);

    // Get the padding value.
    var padding = img.css("padding-right");
    padding = padding.replace('px', '');
    paddingInt = parseInt(padding);

    // set the title associated
    var title = $("#services > #titles > div").eq(i);
    var tWidth = title.outerWidth(true) + paddingInt;

    var newLeft = 0;

    if ((1 + i) == nb) {
      newLeft = (left + width) - tWidth;
    } else {
      newLeft = left - Math.abs((width - tWidth) / 2);
    }
    // newLeft -= paddingInt; /* padding */

    title.css('left', newLeft);
  }

  // Set the handlers.
  //
  // hover
  $('#services > #images > div').hover(serviceOn, serviceOut);
  $('#submenu > ul > li').hover(serviceOn, serviceOut);
  // click
  $('#services > #images > div').click(serviceButtonClick);
  //$('#submenu > ul > li').click(serviceButtonClick);

  // Select the current service.
  selectService(idx);
}

// Simple button functions
function simpleButtonOver() {
  this.src = this.src.replace("-out", "-over");
}
function simpleButtonOut() {
  this.src = this.src.replace("-over", "-out");
}

/*
 *
 * Initialization
 *
 */
$(document).ready(function() {

  // Fix the IE z-index problem.
  $(function() {
    var zIndexNumber = 1000;
    $('div').each(function() {
      $(this).css('zIndex', zIndexNumber);
      zIndexNumber -= 10;
    });
  });

  $("a").click(function() {
    $(this).blur();
  });

  // Positionnement des sous-nav du menu
  $('#menu_content > ul > li:eq(1)').children('ul').css('top', '31px');
  $('#menu_content > ul > li:eq(2)').children('ul').css('top', '62px');
  $('#menu_content > ul > li:eq(3)').children('ul').css('top', '93px');
  $('#menu_content > ul > li:eq(4)').children('ul').css('top', '124px');
  $('#menu_content > ul > li:eq(5)').children('ul').css('top', '155px');

  $('#menu_content > ul > li').bind('mouseover', jsddm_open);
  $('#menu_content > ul > li').bind('mouseout', jsddm_timer);
  document.onclick = jsddm_close;

  // PNG hover IE6 : remplacer par un gif
  if ($.browser.msie && parseInt($.browser.version) < 7)
    $('#submenu a').addClass('ie6');

  // Top bar buttons.
  $("#btn-return-group img").hover(simpleButtonOver, simpleButtonOut);

  // Are we inside services page?
  if ($('#services').length) {
    init_services();
  }

  contactf = $("#formcontact");
  if (contactf){
    companysel = $("#id_company");
    activitysel = $("#id_activity");
    productsel = $("#id_product");

    companysel.change(function(){
      activitysel.val('all');
      productsel.val('all');
      contactf.submit();
    });
    activitysel.change(function(){
      contactf.submit();
    });
    productsel.change(function(){
      contactf.submit();
    });
  }
});

/*
 * Image mapping, historique, vissionneuse de photo
 */
$(document).ready(function(){
  if($("#hist_mapping").attr("name")=="hist_mapping"){
    $("#hist_mapping area").click(function(){
      $(this).next("a").trigger("click");
      return false;
    });
    Shadowbox.init({
      onOpen:function(){
        $("#sb-container").css({zIndex:9999});
      }
    });
  }
});

/*
 * Remove "http://" in contact
 */
$(document).ready(function(){
/*
 *   <p class="emailAndSite"><strong>alain.routhier@courchesnelarose.com</strong><br>
 *     <a href="http://courchesnelarose.com">http://courchesnelarose.com</a>
 *   </p>
 */
  $("p.emailAndSite a").each(function(){
    siteText=$(this).text();
    newSiteText=$(this).text().replace("http://","");
    $(this).text(newSiteText);
  });
});
