/**
 * 
 *
 * @return void
 * @author Nicolas Binet
 **/
$(document).ready(function()
{
  switch ($('body').attr('class'))
  {
    case 'contact':
      initContact();
    case 'home':
      initHome();
    case 'reference':
      initReference();
    case 'referenceToutes':
      initReferenceToutes();
    default:
      initLe6();
  }
});

/**
 * 
 *
 * @return void
 * @author Nicolas Binet
 **/
function initHome()
{
  currentNewsItemNumber = 1;
  showHomeNewsItem(currentNewsItemNumber);
  
  itemNumber=1;
  jQuery.each($("#newslist a.news_item"), function()
  {
    $(this).attr('itemNumber', itemNumber);
    $(this).bind("click", function (e)
    {
      e.stopPropagation();
      showHomeNewsItem($(this).attr('itemNumber'));
    })
    itemNumber++;
  })
}

/**
 * 
 *
 * @return void
 * @author Nicolas Binet
 **/
function showHomeNewsItem(newsItemNumber)
{
  $("#newslist a#news_num_"+newsItemNumber).addClass('current');
  $("#newslist div#news_item_"+newsItemNumber).css('display', 'block');
  
  if (currentNewsItemNumber!=newsItemNumber)
  {
    $("#newslist a#news_num_"+currentNewsItemNumber).removeClass('current');
    $("#newslist div#news_item_"+currentNewsItemNumber).css('display', 'none');
    
    leftTarget = -761*(newsItemNumber-1);
    $("#content-image-list").animate({ 
        left: leftTarget
      }, 1000);
  }
  currentNewsItemNumber = newsItemNumber;
}

/**
 * 
 *
 * @return void
 * @author Nicolas Binet
 **/
function initLe6()
{
  $("#footer a").bind("mouseover", function(e)
  {
    $("#le6-legend").stop();
    $("#le6-legend").animate({ 
      width: "160px"
    }, 250 );
  });
  $("#footer a").bind("mouseout", function(e)
  {
    $("#le6-legend").stop();
    $("#le6-legend").animate({ 
      width: "1px"
    }, 250 );
  });
}

/**
 * 
 *
 * @return void
 * @author Nicolas Binet
 **/
function initReference()
{
  currentReferencePrimaryMediaNumber = 1;
  
  itemNumber=0;
  jQuery.each($("#content-image ul li.media_item_nav"), function()
  {
    itemNumber++;
    $(this).attr('itemNumber', itemNumber);
    $(this).bind("click", function (e)
    {
      e.preventDefault();
      showReferencePrimaryMediaItem($(this).attr('itemNumber'));
    });
  });
  
  $("#image-previous").addClass("disabled");
  $("#image-previous a").bind("click", showPreviousReference);
  $("#image-next a").bind("click", showNextReference);
}

function showPreviousReference(e)
{
  e.preventDefault();
  if (currentReferencePrimaryMediaNumber > 1)
  {
    showReferencePrimaryMediaItem(parseInt(currentReferencePrimaryMediaNumber)-1)
  }
}

function showNextReference(e)
{
  e.preventDefault();
  if (currentReferencePrimaryMediaNumber < itemNumber)
  {
    showReferencePrimaryMediaItem(parseInt(currentReferencePrimaryMediaNumber)+1)
  }
}

/**
 * 
 *
 * @return void
 * @author Nicolas Binet
 **/
function showReferencePrimaryMediaItem(referencePrimaryMediaNumber)
{
  $('#media_'+referencePrimaryMediaNumber).addClass('current');
  if (currentReferencePrimaryMediaNumber!=referencePrimaryMediaNumber)
  {
    $('#media_'+currentReferencePrimaryMediaNumber).removeClass('current');
    $("#image-legend").html($("#media_img_"+referencePrimaryMediaNumber).attr('title'));
    leftTarget = -761*(referencePrimaryMediaNumber-1);
    $("#content-image-list").animate({
      left: leftTarget
    }, 500 );
  }
  currentReferencePrimaryMediaNumber = referencePrimaryMediaNumber;
  
  if (currentReferencePrimaryMediaNumber==1)
  {
    $("#image-previous").addClass("disabled");
    $("#image-next").removeClass("disabled");
  }
  else if (currentReferencePrimaryMediaNumber==itemNumber)
  {
    $("#image-previous").removeClass("disabled");
    $("#image-next").addClass("disabled");
  }
  else
  {
    $("#image-previous").removeClass("disabled");
    $("#image-next").removeClass("disabled");
  }
}

/**
 * 
 *
 * @return void
 * @author Nicolas Binet
 **/
function initReferenceToutes()
{
  currentReferenceSecondary = "";
  jQuery.each($("#content-detail div"), function()
  {
    idRef = $(this).attr('id').substr(-2, 2);
    
    refImage = $("#reference-toutes-image-"+idRef);
    refImage.attr('idRef', idRef);

    refImage.bind("mouseover", function (e)
    {
      refDetail = $("#reference-toutes-detail-"+$(this).attr('idRef'));
      refDetail.css("top", $(this).offset().top-270);
      refDetail.css("left", 12);
      refDetail.css("display", "block");
      e.preventDefault();
    })
    refImage.bind("mouseout", function (e)
    {
      refDetail = $("#reference-toutes-detail-"+$(this).attr('idRef'));
      refDetail.css("display", "none");
      e.preventDefault();
    })
    refImage.bind("click", function (e)
    {
      e.preventDefault();
    })
  })
  $("#footer").css("top", 260+parseInt($("#content").height()));
}

/**
 * 
 *
 * @return void
 * @author Nicolas Binet
 **/
function initLeoff()
{
  currentLeOffItem = 0;
  
  leOffList = $("#leoff_list li a");
  leOffCount = leOffList.length;
  leOffAlphaDelta =  (Math.ceil(70/leOffCount));
  
  itemNumber = 1;
  jQuery.each(leOffList, function()
  {
    $(this).attr('targetAlpha', (90-(itemNumber*leOffAlphaDelta))/100);

    if($(this).parent().hasClass('current'))
    {
      currentLeOffItem = itemNumber;
      $(this).css('opacity', $(this).attr('targetAlpha'));
    }
    
    if (currentLeOffItem != itemNumber)
    {
      $(this).bind("mouseover", function (e)
      {
        e.stopPropagation();
        $(this).fadeTo("fast", $(this).attr('targetAlpha'));
      });
      $(this).bind("mouseout", function (e)
      {
        e.stopPropagation();
        $(this).fadeTo("fast", 1);
      });
    }
    itemNumber++;
  })
}

/**
 * 
 *
 * @return void
 * @author Nicolas Binet
 **/
function initContact()
{
  if (GBrowserIsCompatible())
  {
    var map = new GMap2(document.getElementById("contact-map"));
    map.setCenter(new GLatLng(48.856598,2.405663), 16);

    var baseIcon = new GIcon();
    baseIcon.shadow = "/assets/images/six_shadow.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    baseIcon.infoShadowAnchor = new GPoint(18, 25);
    baseIcon.image = "/assets/images/six.png";
    
    // Set up our GMarkerOptions object
    markerOptions = { icon:baseIcon };
    map.addOverlay(new GMarker(new GLatLng(48.856638,2.405763), markerOptions));
  }
}
