
if($('.socialMedia').length)
{
	FB.Event.subscribe('edge.remove', function(targetUrl) {
	  _gaq.push(['_trackSocial', 'Facebook', 'unlike', targetUrl]);
	});
	
	FB.Event.subscribe('edge.create', function(targetUrl) {
	  _gaq.push(['_trackSocial', 'Facebook', 'like', targetUrl]);
	});
	
	twttr.events.bind('tweet', function(event) {
	  if (event) {
	    var targetUrl;
	    if (event.target && event.target.nodeName == 'IFRAME') {
	      targetUrl = extractParamFromUri(event.target.src, 'url');
	    }
	    _gaq.push(['_trackSocial', 'Twitter', 'tweet', targetUrl]);
	  }
	});
}
function extractParamFromUri(uri, paramName) {
	  if (!uri) {
	    return;
	  }
	  var uri = uri.split('#')[0];  // Remove anchor.
	  var parts = uri.split('?');  // Check for query params.
	  if (parts.length == 1) {
	    return;
	  }
	  var query = decodeURI(parts[1]);

	  // Find url param.
	  paramName += '=';
	  var params = query.split('&');
	  for (var i = 0, param; param = params[i]; ++i) {
	    if (param.indexOf(paramName) === 0) {
	      return unescape(param.split('=')[1]);
	    }
	  }
	}

var Front = new function()
{
  var options;
  
  this.init = function(opt)
  {
    Front.options = opt;
    $.facebox.settings.loadingImage = Util.img_src("../../dmsPlugin/css/facebox/loading.gif");
    $.facebox.settings.closeImage = Util.img_src("../../dmsPlugin/css/facebox/loading.gif");
    $.facebox.settings.classBody = 'ecommerce';
    Front.initDom($("#dms_layout_parts"));
    
  };
  
  this.initDom = function($dom)
  {
	  
	//trackEvent
	  if ($slideHome = $('.content_slide_show_accueil', $dom).orNot()) 
		{
			$('.trackBoutonSlideHome', $slideHome).each(function(){
				$(this).rebind('click', function(){
					var $metadata = $(this).metadata();
					_gaq.push(['_trackEvent', "Slideshow", "Bouton", $metadata.libelle ]);
				});
			});
		}
		
		if ($slidePromo = $('div.content_slide_show_accueil_right', $dom).orNot()) 
		{
			$('.trackHomeSlidePromo', $slidePromo).each(function(){
				$(this).rebind('click', function(){
					var $metadata = $(this).metadata();
					_gaq.push(['_trackEvent', "Promotions", "Home", $metadata.libelle ]);
				});
			});
		}
		
		if ($menuHead = $('ul.content_menu_head', $dom).orNot()) 
		{
			$('.trackMenuHome', $menuHead).each(function(){
				$(this).rebind('click', function(){
					var $metadata = $(this).metadata();
					_gaq.push(['_trackEvent', "Menu", "Home", $metadata.libelle ]);
				});
			});
		}
		
		$('.content_newsletter_in_actu .submit input').rebind('click', function(){
			_gaq.push(['_trackEvent', "Formulaire", "Newsletter", "Pré-inscription" ]);
		});
		
		if ($trackReserveHome = $('a.trackReserveHomeLien', $dom).orNot()) 
		{
			$trackReserveHome.rebind('click', function(){
				_gaq.push(['_trackEvent', "Réservation", "Home", "Je réserve mon séjour (lien)" ]);
			});
		}
		
		if ($trackReserveHome = $('a.trackReserveHome', $dom).orNot()) 
		{
			$trackReserveHome.rebind('click', function(){
				_gaq.push(['_trackEvent', "Réservation", "Home", "Je réserve mon séjour (bouton)" ]);
			});
		}
		
		$('.isTrackinLinkBlob, .trackPubRight, .trackOngletSejour, .trackBoutonReserveOrContact, .trackExternetMenuLeft').each(function(){
			$(this).rebind('click', function(){
				var $metadata = $(this).metadata();
				if($metadata.action)
				{
					_gaq.push(['_trackEvent', $metadata.categorie, $metadata.action, $metadata.libelle ]);
				}
				else
				{
					_gaq.push(['_trackEvent', $metadata.categorie, $metadata.libelle ]);
				}
			});
		});
	  
	//Fin TrackEvent  
	  
	  
	  
	  
	$('.validate_me').each(function(){
		$(this).validate();
	});
		
    if ($tabs = $("div.tabs_me ul.menu_onglet_sejour", $dom).orNot())
    {
      $tabs.tabs();
    }
		
	if ($tabs_choisir_sejour = $("div.tabs_me ul.menu_bien_choisir_sejour", $dom).orNot())
    {
      $tabs_choisir_sejour.tabs();
			 $('li', $tabs_choisir_sejour).each(function(){
		      $(this).hover(
		        function () {
		          $(this).addClass('over_menu');
		        }, 
		        function () {
		          $(this).removeClass('over_menu');
		        }
		      );
		    });
    }
    
    if($slide_show_garantie = $('div.menu_slide_garantie_content', $dom).orNot())
    {
      $slide_show_garantie.cycle({ 
          fx:     'fade', 
          speed:  1000, 
          timeout: 3000,
          pause: 1,
          next:   '.bt_slide_garantie_next', 
          prev:   '.bt_slide_garantie_prev',
          height: 'auto'
      });
      $('.image_slide_garantie img').reflect();
    }
    
    if($slide_show_historique = $('.menu_slide_historique_content', $dom).orNot())
    {
      var nb_slide = $slide_show_historique.metadata().nb_slide;
      $slide_show_historique.cycle({ 
          fx:     'fade', 
          speed:  'fast', 
          timeout: 0,
          next:   '.bt_slide_histoire_next', 
          prev:   '.bt_slide_histoire_prev',
	        after: function(currSlideElement, nextSlideElement)
	        {
						var $height = $(nextSlideElement).height();
						dbg($height);
						if($height > 320)
						{
							$('.menu_slide_historique_left').height($height);
              $('.menu_slide_historique_content').height($height);
              $('.menu_slide_historique_right').height($height);
						}
						else
						{
							$('.menu_slide_historique_left').height(319);
              $('.menu_slide_historique_content').height(319);
              $('.menu_slide_historique_right').height(319);
						}
	        },
          prevNextClick: function(isNext, zeroBasedSlideIndex, slideElement){
            if(zeroBasedSlideIndex == 0)
            {
              $('.bt_slide_histoire_prev').hide();
              $('.bt_slide_histoire_next').show();
            }
            else if(zeroBasedSlideIndex == nb_slide)
            {
              $('.bt_slide_histoire_prev').show();
              $('.bt_slide_histoire_next').hide();
            }
            else
            {
              $('.bt_slide_histoire_prev').show();
              $('.bt_slide_histoire_next').show();
            }
            $('.name_next_slide').html($(slideElement).metadata().name_next);
            $('.name_prev_slide').html($(slideElement).metadata().name_prev);
          }
      });
    }
    
    $('.bt_go_site_groupe', $dom).rebind('click', function(){
      var $parent_li = $(this).parent().parent();
      window.open($('#site_groupe', $parent_li).val());
      return false;
    });
    
    
    $('.content_menu_head li',$dom).each(function(){
      $(this).hover(
        function () {
          $(this).addClass('over_menu');
        }, 
        function () {
          $(this).removeClass('over_menu');
        }
      );
    });
    
		
    if ($('div.slide_show_image_promo', $dom).length)
    {
      $('div.slide_show_image_promo', $dom).cycle({
        speed: 1000,
        timeout: 4000,
        pagerEvent: 'click',
        pause: 1,
        pager: '#nav_focus_promo_home',
        pagerAnchorBuilder: function(idx, element){
          return '<li class="fleft"><a class="link" href="#">' + (idx+1) + '</a></li>';
        }
      });
    }
    // slide show Accueil
    if ($('.content_slide_show_accueil div.slide_show_image', $dom).length) 
    {
      $('div.slide_show_image', $dom).cycle({
        speed: 1000,
        timeout: 4000,
        pagerEvent: 'click',
        pauseOnPagerHover: 1,
        pause: 1,
        pager: '#nav_focus_home_sejour',
        pagerAnchorBuilder: function(idx, element)
        {
          return '<li class="fleft"><a title="'+ $(element).metadata().nom +'" class="trackOngletSlideHome link" href="'+$(element).metadata().lien+'"><span class="beafore"/><span class="beafin">'+ $(element).metadata().nom +'</span><span class="beafter"/></a></li>';
        },
        before: function(currSlideElement, nextSlideElement)
        {
          $('.content_texte_slide_show').hide();
        },
        after: function(currSlideElement, nextSlideElement)
        {
          $($(nextSlideElement).metadata().class_text_slide).show();
        } 
      });
      $('#nav_focus_home_sejour li a', $dom).each(function(){
        $(this).hover(
          function () {
            $(this).click();
            $(this).addClass('isHoverClass');
          },
          function(){
        	  $(this).removeClass('isHoverClass');
          });
      });
      
      if ($menuSlideHome = $('div.content_menu_slide_home_sejour', $dom).orNot()) 
	  {
		$('.trackOngletSlideHome', $menuSlideHome).each(function(){
			$(this).rebind('click', function(){
				if($(this).hasClass("activeSlide") && $(this).hasClass('isHoverClass'))
				{
					_gaq.push(['_trackEvent', "Slideshow", "Onglet", $(this).attr('title') ]);
					window.location.replace($(this).attr('href'));
				}
				
			});
		});
	}
      
      
      $('div.cadre_opacity_slide_show', $dom).hover(
        function () {
          $('div.slide_show_image').cycle('pause');
        }, 
        function () {
          $('div.slide_show_image').cycle('resume');
        }
      );
			$('div.content_texte_slide_show', $dom).hover(
        function () {
          $('div.slide_show_image').cycle('pause');
        }, 
        function () {
          $('div.slide_show_image').cycle('resume');
        }
      );
			$('div.content_slide_show_accueil .bienChoisir', $dom).hover(
        function () {
          $('div.slide_show_image').cycle('pause');
        }, 
        function () {
          $('div.slide_show_image').cycle('resume');
        }
      );
    }
		
		if($gallery_images = $('.gallery_sejour').orNot())
		{
			$('.image_gallery', $gallery_images).each(function(){
				$(this).rebind('click', function(){
					$(this).effect('transfer',{
						to : "#content_big_image_prod",
            className : "ui-effects-transfer"
          }, 1000,function(){
						$.get(Util.light2href("prod/viewBigImage")+'&big_image='+$(this).metadata().big_image, function(data){
	            $('#content_big_image_prod').html(data);
	          });
					});
          
				});
			});
		}
		
		
    if ($envoyer_ami = $(".bt_envoyer_ami", $dom).orNot())
    {
      $envoyer_ami.rebind('click', function(){
        $metadata = $(this).metadata();
        $.facebox.loading();
        $.get(Util.light2href("main/envoyerAmis")+'&url='+ $metadata['url'], function(data){
          $.facebox(data);
          $('#facebox .footer img').hide();
          $("#facebox a.close_facebox").one("click", function(){
            $.facebox.close();
          });
          if ($.browser.msie && $.browser.version < 7)
          {
            $("#facebox").bgiframe();
          }
          $('#facebox form.envoyer_mail_ami').validate();
        });
      });
    }
		
		
    if($reservez_sejour = $("div.reservez_sejour", $dom).orNot())
    {
      Front.ajaxReservezSejour($reservez_sejour);
    }
		
		
    $('.ifixpng').ifixpng();
  };
	
	var time;
  this.ajaxReservezSejour = function($reservez_sejour)
	{
		clearTimeout(time);
    time = setTimeout(function(){
      var $container = $("div.content_formulaire", $reservez_sejour);
      var $form = $("form.dms_cart_form", $reservez_sejour);
			$('.bt_envoie_next_etap input', $form).rebind('click', function(){
        dbg('toto');
        if ($('#cgv').is(':checked')) {}
        else {
          $('.label_cgv').parent().css("border","1px solid red").click(function(){$(this).css('border', 'none')});
          alert("Vous devez accepter les Conditions Générales d'Utilisation pour continuer.");
          return false;
        }
      });
			var url = $form.metadata().url;
      $("select.update_cart", $form).each(function(){
        $(this).rebind("change", function(){
          $form.ajaxSubmit({
		        url: url,
		        beforeSubmit: function(data) {
		          $form.fadeTo(500, 0.3);
		        },
		        success: function(data) {
		          $form.fadeTo(500, 1);
		          $container.html(data);
		          Front.ajaxReservezSejour($reservez_sejour);
		        }
		      });
        });
      });
    }, 500);
		
  };
    
}

Dms.registerController(Front);
