/*
 * Fonctions pour splash screen
 */
 
function instantHideOvpi(target) {
  jQuery('#ovpi .inside').html('');
  jQuery('html').css('overflow','scroll'); /*pour ie6*/ 
  jQuery('#trame').hide(0);
  enableShowOvpi(target);
}
function hideOvpi(target) {
  target.animate({left: "-330"}, 500, 'linear', function(){
    target.addClass('closed');
    jQuery('#trame').fadeOut(50);
    jQuery('html').css('overflow','scroll'); /*pour ie6*/  
    jQuery('#ovpi .inside').html('');
    enableShowOvpi(target);
  });
}
function showOvpi(target) {
  jQuery('a.toggle').unbind('mouseover');
  jQuery('a.toggle').unbind('mouseout');
  target.animate({left: "50%"}, 500, 'linear', function(){
    target.removeClass('closed');
    enableHideOvpi(target);
    jQuery('#trame').fadeIn(50);
  });
  jQuery('#ovpi .inside').html('<iframe width="720" height="558" src="http://www.youtube.com/embed/VCjFabup6yQ?rel=0&autoplay=1" frameborder="0" allowfullscreen></iframe>');
}
function enableHideOvpi(target) {
    jQuery('a.toggle').unbind('click');
    jQuery('a.toggle, #trame').click(function(){hideOvpi(target);});
}
function enableShowOvpi(target) {
    jQuery('a.toggle, #trame').unbind('click');
    jQuery('a.toggle').mouseover(function () {
      target.animate({left: "-320"}, 100);
    });
    jQuery('a.toggle').mouseout(function () {
      target.animate({left: "-330"}, 100);
    });
    jQuery('a.toggle').click(function(){showOvpi(target);});
}

/**
 * Actions
 */
$(document).ready(function(){
  
  /*------------------------------------------------------------------------------*/
  /* USUAL */
  /*------------------------------------------------------------------------------*/
    $("body").addClass('js-enabled');
    
    /* ajoute un target _blank au lien avec une classe external */
    $("a[rel='external'], a[rel='document'], a[href^='http://'], a[href^='mailto'], a[href$='.pdf'], a[href$='.doc'], a[href$='.xls'], a[href$='.txt']").each(function(){
      if($(this).attr('rel') != 'bookmark') {
        $(this).attr({ title: ($(this).attr("title"))?$(this).attr("title"):$(this).attr("href")+" (nouvelle fenêtre)" });
        $(this).click(function() {window.open($(this).attr('href'));return false;});
      }
    });
  /*----------------------------------------------------------*/
  
  /* gestion splash */
	if(document.getElementById('ovpi')){
		jQuery('#ovpi').addClass('js-enabled');
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
		  jQuery("body","html").css({height: "100%", width: "98%"});
		  if (document.getElementById("HideSelect") === null) {//iframe to hide select elements in ie6
      jQuery("html").css("overflow","hidden");
			jQuery("body").append("<iframe id='HideSelect'></iframe><div id='trame'></div>");
		  }
		}else{//all others
		  if(document.getElementById("TB_overlay") === null){
			jQuery("body").append("<div id='trame'></div>");
		  }
		}
    jQuery('#ovpi').prepend('<p id="js-toggle-ovpi"><a href="#" class="toggle">Fermer</a></p>'); 
    if(jQuery('#ovpi').hasClass('closed')) {
      instantHideOvpi(jQuery('#ovpi'));
    } else {   
      jQuery('#ovpi .inside').html('<iframe width="720" height="558" src="http://www.youtube.com/embed/VCjFabup6yQ?rel=0&autoplay=1" frameborder="0" allowfullscreen></iframe>');
      enableHideOvpi(jQuery('#ovpi'));
    }
	}
	/*--------------------------*/

  /*------------------------------------------------------------------------------*/
  /* PLUGINS */
  /*------------------------------------------------------------------------------*/

  /* fancy zoom (jquery.fancybox)*/
  if(typeof $(this).fancybox == 'function') {   
  	$("a[href$='.JPG'], a[href$='.jpg'], a[href$='.gif'], a[href$='.png']").fancybox({overlayShow: true, overlayOpacity: 0.8, overlayColor: '#000'});
  	$(".gallery a[href$='.JPG'],.gallery  a[href$='.jpg'],.gallery a[href$='.gif'],.gallery a[href$='.png']").each(function(){
      $(this).append($(this).find('img').attr('alt'));
    });
  }
  /*--------------------*/ 
  
  var loadScriptVar = false;
  
  /*----------------page contenu --------------------*/
  $('#home #nav ul li a').click(function()
  {
    var url = $(this).attr('href');

    var contentActual = $('#main #content');
    var backActual = $('.back');

    $.ajax({
      url: url,
      dataType: 'json',
      error: function(XMLHttpRequest, textStatus, errorThrown) {

      },
      success: function(data, textStatus) {
        $('#main').before('<p class="back"><a>retour aux références</a></p>');
        $('#main').prepend('<div id="content"></div>');
        $('#main #content').html(data.result);
        $('#main #content').animate({
          'opacity' : 1
        }, 500, function() {
          $('#main #reference').hide();
          contentActual.remove();
          backActual.remove();
          
          $('.back a').click(function(){ removeContent(); });
          
          if(data.map) {
            loadScript();
            
            $.each($.browser, function(i) {
              if($.browser.msie){
                $('#main #content').css({opacity:''});
              }
            });
          }
        });  
        
      }
    });
    
    return false;
  });  
  
  function loadScript() {
    if(!loadScriptVar) {
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
      document.body.appendChild(script);
      loadScriptVar = true;
    }
    else
    {
      initialize();
    }
  }  
  
  function removeContent() {
    $('#main #reference').show();
    $('#main #content').animate({
      'opacity' : 0
    }, 500, function() {
      $('#main #content').remove();
      $('.back').remove();
    }); 
  }
  /*--------------------------------------------------*/

  /*if(!/ovpi=/.test(document.cookie))
  {
    $('body').append(
      '<div class="ovpi">' +
        '<div class="inner"><div>' +
          '<a href="javascript:void(0)" class="close">Fermer</a>' +
          '<a href="mailto:contact@2s3i.com" class="contact">Contactez-nous</a>' +
          '<object height="267" width="360" class="flash" codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">' +
            '<param value="sameDomain" name="allowScriptAccess">' +
            '<param value="transparent" name="wmode">' +
            '<param value="/flash/ovpi.swf" name="movie">' +
            '<param value="high" name="quality">' +
            '<embed height="267" width="360" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" allowscriptaccess="sameDomain" name="Flash" wmode="transparent" quality="high" src="/flash/ovpi.swf">' +
          '</object>' +
        '</div></div>' +
      '</div>'
    );

    $('.ovpi a.close').click(function(e) {
      e.preventDefault();
      $('.ovpi').fadeOut(560, function() {
        $(this).remove();
      });
    });

    var date = new Date();
    date.setTime(date.getTime() + 7 * 24 * 3600);
    document.cookie = 'ovpi=1; expires=' + date.toGMTString() + '; path=/';
  }*/
});

function initialize() {

  if(typeof(google) != 'undefined') {
    var myLatlng = new google.maps.LatLng(46.661932, 0.358023);
    var myOptions = {
      zoom: 10,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map"), myOptions);

    var p = new google.maps.LatLng(46.661932, 0.358023);
    m = new google.maps.Marker({
      position: p,
      title: '2s3i - Cliquez pour en savoir plus',
      map: map
    });

    map.setCenter(p);

    var description = '<div style="color:#44606d;line-height:16px;"><strong>2S3I<\/strong><br\/>'
                    + 'Bureaux du lac - Téléport 2<br\/>'
                    + 'Av. René CASSIN - 86960 FUTUROSCOPE Chasseneuil'
                    + '<\/div>';

    var infowindow = new google.maps.InfoWindow({
      content: description
    });

    infowindow.open(map, m);
  }
}
