$(function(){
	Front.initDom();
});
var Front = new function(){
  
  var options;
  this.init = function()
  {
	Front.initDom($(document));
  }
  this.initDom = function($dom)
  {
	  
	  $('.downloadZip', $dom).each(function(){
	      var self = this;
	      var $text = $(this).metadata().text_info_bulle;
	     $(this).qtip({
	       content: $text,
	       position: {
	         corner: {
	            target: 'leftMiddle',
	            tooltip: 'rightMiddle'
	         }
	       },
	       show:
	       {
	           when:
	           { 
	             event: 'mouseover'
	           }
	       },
	       hide:
	       {
	    	   delay:3000,
		       when: 'mouseout'
	       },
	       style:
	         {
	           background: '#4463AD',
	           border:
	           {
	             color: '#4463AD',
	             radius: 4
	           },
	           color: '#fff',
	           tip: 'rightMiddle'
	         }
	     });
	    });
	  
	  
	  $.facebox.settings.opacity = 0.8;
	  $.facebox.settings.closeImage = "/images/facebox/closeFacebox.png";
	  $.facebox.settings.loadingImage = "/images/ajax-loader.gif";
	  
	  
	  $('a.mdpOublie').each(function(){
		  $(this).bind('click', function(){
				Front.forgetPassword();
		  });
		  
	  });
	  
	 if($boutonMenuInfosPerso = $('.boutonMenuInfosPerso', $dom).orNot())
	 {
		 $boutonMenuInfosPerso.hover(
				 function(){
					 $(this).addClass('boutonMenuInfosPersoHover');
				 },
				 function(){
					 if((!$(this).hasClass('boutonMenuInfosPersoCurrentHistoire')) && (!$(this).hasClass('boutonMenuInfosPersoCurrentModif')))
					 {
						 $(this).removeClass('boutonMenuInfosPersoHover');
					 }
				 }
		 );
		 $(".boutonModifCoordonnee",$boutonMenuInfosPerso).hover(
				 function(){
					 $boutonMenuInfosPerso.addClass('boutonMenuInfosPersoHoverModif');
				 },
				 function(){
					 $boutonMenuInfosPerso.removeClass('boutonMenuInfosPersoHoverModif');
				 }
		 );
		 $(".boutonHistoriqueSejour",$boutonMenuInfosPerso).hover(
				 function(){
					 $boutonMenuInfosPerso.addClass('boutonMenuInfosPersoHoverHistoire');
				 },
				 function(){
					 $boutonMenuInfosPerso.removeClass('boutonMenuInfosPersoHoverHistoire');
				 }
		 );

	 }
	 
	  $('.mediaAjax').each(function(){
		  $urlAjax = $(this).metadata().url;
		  Front.refreshMediaAjax($(this), $urlAjax);
	  });
	  $('.contentLastAddMedia').each(function(){
		  Front.mediaFacebox($(this));
	  });
	  if($myGoogleMap = $(".myGoogleMap", $dom).orNot())
	  {
		  MyGoogleMap.init($myGoogleMap);
	  }
	  
	  if($contentGallerySejour = $('.contentGallerySejour', $dom).orNot())
	  {
		  $('li', $contentGallerySejour).each(function(){
			  $(this).hover(
					  function(){
						  $(this).addClass('liHover');
					  },
					  function(){
						  $(this).removeClass('liHover');
					  });
		  });
		  $('li img', $contentGallerySejour).each(function(){
			  $(this).bind('click', function(){
				  $(this).effect('transfer',{
						to : "#contentImageBigSejour",
						className: "ui-effects-transfer"
						},
						500,
						function(){
							$('#contentImageBigSejour').html("<img src='"+$(this).metadata().big_image+"' />");
					});
				  return false;

			  });
		  });
	  }
	  $('a.targetBlank', $dom).each(function(){
	    $(this).attr('target', '_blank');
	  });
	  
	  if($cadreBlancAccueil = $('div.cadreBlancAccueil', $dom).orNot())
	  {
		  $('.cadreBlancContent', $cadreBlancAccueil).tabs({
			  selected:0,
			  fx: { opacity: 'toggle' },
		  	  show: function(event, ui)
		  	  {
				if($(ui.tab).attr('href') == '#tab_visite_visiocolo')
				{
					$('ul.slideImageVisiteGuidee', $cadreBlancAccueil).cycle('resume');
				}
				else
				{
					$('ul.slideImageVisiteGuidee', $cadreBlancAccueil).cycle('pause');
				}
		  	  }
	
		  });
	  }
	  if($slideImageVisiteGuidee = $('ul.slideImageVisiteGuidee').orNot())
	  {
		  $slideImageVisiteGuidee.cycle({
		        speed: 1000,
		        timeout: 0,
		        pagerEvent: 'click',
		        pause: 1,
		        pager: 'div.menuForSlideVisite',
		        before: function(currSlideElement, nextSlideElement)
		        {
			  
			  		$currentMetadata = $(currSlideElement).metadata();
			  		$nextMetadata = $(nextSlideElement).metadata();
			  		
			  		if(!$('.class_text_'+$nextMetadata.classText).hasClass("active"))
			  		{
			  			$('.active').slideToggle();
			  			$('.active').addClass('noActive');
			  			$('.active').removeClass('active');
			  			$('.class_text_'+$nextMetadata.classText).slideToggle();
			  			$('.class_text_'+$nextMetadata.classText).addClass('active');
			  		}
		        },
		        pagerAnchorBuilder: function(idx, element){
			  	  var $metadata = $(element).metadata();
		          return '<a  href="#" class="link fleft clearfix elementNum_'+(idx+1)+'"><span class="clearfix"><img class="fleft" src="'+$metadata.miniature+'" width="'+$metadata.miniatureWidth+'px" height="'+$metadata.miniatureHeight+'px"  /></span></a>';
		     }
		  });
		  
		  $('.slideImageVisiteGuidee li img').each(function(){
			  $(this).bind('click', function(){
				  var $metadata = $(this).parent().parent().metadata();
				  $.facebox.settings.opacity = 0.8;
				  $.facebox.settings.closeImage = "/images/facebox/closeFacebox.png";
				  $.facebox.settings.loadingImage = "/images/facebox/ajax-loader.gif";
				  $.facebox.loading();
				  $.facebox({ image: $metadata.facebox });
			  });
		  });
	  }
	  
  };
  
  
  this.forgetPassword = function()
  {
	  $.facebox.settings.opacity = 0.8;
	  $.facebox.settings.closeImage = "/images/facebox/closeFacebox.png";
	  $.facebox.settings.loadingImage = "/images/facebox/ajax-loader.gif";
	  $.facebox.loading();
	  $.ajax({
		url: "dev.php/main/forgetPassword",  
		success: function(data)
	    {
		  Front.formFaceboxForgetPassword(data);
	    }
	  });
  };
  
  this.formFaceboxForgetPassword = function(data)
  {
    $.facebox(data);
    $('#facebox form.formulairePassword').each(function(){
    	Front.validateFormForgetPassword($(this));
    });
    if($.browser.msie && $.browser.version < 7)
      $("#facebox").bgiframe();
    
    if($('#facebox div.valideForgetPassword').length)
    {
    	clearTimeout(time);
        time = setTimeout(function(){
        	$('#facebox .close').click();
        }, 1500);
    }
  };
  
  this.validateFormForgetPassword  = function($formFacebox)
  {
	  $formFacebox.ajaxForm({
	      success: function(html, statusText)
	      {
	    	  $.facebox.loading();
	    	  Front.formFaceboxForgetPassword(html);
	    	  return false;
		  }
		});
	  return false;
  }
  
  this.faceboxImageProd = function($numeroMedia, $nbElement, $url, $orderBy)
  {
	    $.facebox.settings.opacity = 0.8;
		$.facebox.settings.closeImage = "/images/facebox/closeFacebox.png";
		$.facebox.settings.loadingImage = "/images/facebox/ajax-loader.gif";
	    $.facebox.loading();
		$.ajax({
			url: $url,
			data:
			{
				num: $numeroMedia,
				orderBy: $orderBy
			},
		    success: function(data)
		    {
				$.facebox(data);
			  	$("#facebox .btPrevImagePhototheque").css('top', "155px");
				$("#facebox .btNextImagePhototheque").css('top', "155px");
				$("#facebox .btPrevImagePhototheque").hover(
				  function () {
				    $(this).addClass('isHoverBtFaceboxPrev');
				  }, 
				  function () {
				    $(this).removeClass('isHoverBtFaceboxPrev');
				  }
				);
				$("#facebox .btNextImagePhototheque").hover(
				  function () {
				    $(this).addClass('isHoverBtFaceboxNext');
				  }, 
				  function () {
				    $(this).removeClass('isHoverBtFaceboxNext');
				  }
				);
				$('#facebox .btPrevImagePhototheque').click(function(){
				  $.facebox.loading();
				  var $image_prev = $numeroMedia;
				  $image_prev--;
				  if($image_prev == 0)
				  {
					$image_prev = $nbElement;
				  }
				  Front.faceboxImageProd($image_prev, $nbElement, $url, $orderBy);
				});
				$('#facebox .btNextImagePhototheque').click(function(){
				  $.facebox.loading();
				  var $image_next = $numeroMedia;
				  $image_next++;
				  if($image_next > $nbElement)
				  {
					$image_next = 1;
				  }
				  Front.faceboxImageProd($image_next, $nbElement, $url, $orderBy);
				});
		    }
		});
  };
  
  this.mediaFacebox = function($contentMediaAjax)
  {
	  $('.pictoByType').each(function(){
		  $(this).bind('click', function(){
			  $('img', $(this).parent()).click();
		  });
	  });
	  $nbElement = $('.nbElementForFacebox').metadata().nbElements;
	  $url = $('.nbElementForFacebox').metadata().url;
	  $orderBy = $('.nbElementForFacebox').metadata().orderBy;
	  $('.faceboxImg').each(function(){
		$(this).bind('click', function(){
			var $metadata = $(this).metadata();
			Front.faceboxImageProd($metadata.numMedia ,$nbElement, $url, $orderBy);
		});
	  });
  };
  
  this.refreshMediaAjax = function($contentMediaAjax, $urlAjax)
  {
	  Front.mediaFacebox($contentMediaAjax);
	  $('.isElementAjaxLink', $contentMediaAjax).each(function(){
		  $(this).bind('click', function(){
			  Front.functionAjaxForRefreshMedia($contentMediaAjax, $urlAjax, $(this).metadata().type, $(this).metadata().value)
			  return false;
		  });
	  });
	  $('.isElementAjax', $contentMediaAjax).each(function(){
		  $(this).bind('change', function(){
			  Front.functionAjaxForRefreshMedia($contentMediaAjax, $urlAjax, $(this).metadata().type, $(this).val())
			  return false;
		  });
	  });
	  
  };
  var time;
  this.functionAjaxForRefreshMedia = function($contentMediaAjax, $urlAjax, $key_value, $value)
  {
	  $('div.contentAjaxLoader').show();
	  $contentMediaAjax.fadeTo(500, 0.1);
	  clearTimeout(time);
      time = setTimeout(function(){
		  $.ajax({
		        url: $urlAjax,
		        data:
		        {
			  		key_value: $key_value,
			  		value: $value
		        },
		        success: function(data)
		        {
		        	$contentMediaAjax.html(data);
		        	$('div.contentAjaxLoader').hide();
		        	$contentMediaAjax.fadeTo(500, 1);
		        	$contentMediaAjax.removeAttr('style');
		        	Front.refreshMediaAjax($contentMediaAjax, $urlAjax);
		        }
		  });
      }, 1000);
  };
  
};
function dbg()
{
  try {
    console.debug(arguments);
  }
  catch(e)
  {
    for(var i in arguments)
    {
      if (i<5) alert(arguments[i]);
    }
  }
};

jQuery.fn.extend({
	  orNot: function() {
	    return this.length == 0 ? false : this;
	  }
});

jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
