$(document).ready(function(){
    
    //initAccordion();    
    $('div.galery').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: '.holder > ul > li'
	});
	$('div.carusel').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: '.holder > ul > li'
	});
	$('div.carusel').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: '.panel > ul > li'
	});
	/*$('div.slideshow').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: '.holder > ul > li',
		effect:true
	});*/
	$('div.visual-holder').gallery({
		duration: 500,
		listOfSlides: '.wrap > ul > li',
		direction: true,
		nextBtn: 'a.down',
		prevBtn: 'a.up'
	});
	$('div.visual-holder').gallery({
		duration: 500,
		listOfSlides: '.image>img',
		switcher: '.wrap > ul > li',
		effect:true
	});
	
	$('a[rel=diaporama]').fancybox({
		'padding'		: 0,
		'title'			: this.title, //this.title,
		'titlePosition'	: 'over',
		'href'			: this.href
	});

	$('a.diaporama_prod').fancybox({
		'padding'		: 0,
		'title'			: this.title, //this.title,
		'titlePosition'	: 'over',
		'href'			: this.href
	});
	
	$('a.envoyer_par_mail').fancybox({
		'padding'		: 0,
		'title'			: this.title, //this.title,
		'titlePosition'	: 'over',
		'href'			: this.href,
		'type'			: 'iframe',
		'width'			: 300,
		'height'		: 200
	});
	
	$('.proprio, .proprio-blue').fancybox({
		'type'     : 'iframe',
		'width'    : 626,
		'height'   : 560,
		'padding'  : 0
	});	
	
	$('.localiser').fancybox({
		'type'     : 'iframe',
		'width'    : 640,
		'height'   : 480,
		'padding'  : 0
	});
    
	$('.diaporama').fancybox({
		'type'     : 'iframe',
		'width'    : 660,
		'height'   : 335,
		'padding'  : 0
	});
    
    
	
	$('.disponibilites').fancybox({
		'type'     : 'iframe',
		'width'    : 720,
		'height'   : 520,
		'padding'  : 0
	});
	
	$('.reservez').fancybox({
		'type'     : 'iframe',
		'width'    : 720,
		'height'   : 520,
		'padding'  : 0
	});
	
	$('.visite').fancybox({
		'type'     : 'iframe',
		'width'    : 616,
		'height'   : 366,
		'padding'  : 0
	});

	$('.fancybox').fancybox();
	
	$('.research-form input[name="requete[date]"]').datepicker({
		dateFormat: 'dd/mm/yy',
		showOn: "button",
        firstDay: 1,
		buttonImage: chemin_root+"images/btn-calendar.png",
		buttonText : 'Cliquer sur le calendrier pour choisir vos dates de séjour',
        monthNames: ['Janvier','Fevrier','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Decembre'],
        dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
        dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
		buttonImageOnly: true

	});
	
	$('#situation_libellecommune').live('keyup',function(){
		$.ajax({
			type: "POST",
			url: chemin_root+"ajax/liste_communes.php",
			data: "term="+$('#situation_libellecommune').val(),
			success: function(msg){
				$('#situation_libellecommune_autocomplete ul').html(msg);
				if($('#situation_libellecommune_autocomplete').css('display')=='none'){
					$('#situation_libellecommune_autocomplete').fadeIn('slow');
					$('li#li_elargire_ville').css('visibility','hidden');
				}
			}
		});
	});
	$('#situation_libellecommune').live('blur',function(){
		if($('#situation_libellecommune_autocomplete').css('display')!='none'){
			$('#situation_libellecommune_autocomplete').fadeOut('slow',function(){
			$('li#li_elargire_ville').css('visibility','visible');});
		}
	});
	$('#situation_libellecommune_autocomplete a').live('click',function(){
		var lib = $(this).text();
		var cp = $(this).attr('title');
		$('#situation_libellecommune').val(lib);
		$('#situation_insee').val(cp);
		GetResults($('#form_recherche').serialize());
	});
	/*
	var cache = {}, lastXhr;
	$('#situation_libellecommune').autocomplete({
		minLength: 2,
		source: function( request, response ) {
            var term = request.term;
			
            if ( term in cache ) {
				response( cache[ term ] );
				return;
			}
            lastXhr = $.getJSON(chemin_root+"ajax_commune.php", request, function( data, status, xhr ) {
                cache[ term ] = data;
				if ( xhr === lastXhr ) {
					response( data );
				}
			});
            
		},
    	select: function( event, ui ) {	
    		var value_com = '';
			value_com = ui.value;
			var tab_value_com = value_com.split('|');
			$('#situation_insee').val(tab_value_com[0])		
			$('#situation_libellecommune').val(tab_value_com[1]);
    	},
		close: function( event, ui ) {			            
            GetResults($('#form_recherche').serialize());               
		}
        
            
        
	});
	*/
	$('a[name^=basket]').live('click', function(){
		var a = $(this);
		var from_ = a;
		if(from_.parents('.block:first').length) from_ = from_.parents('.block:first');
		else if(from_.parents('.content-panel:first').length) from_ = from_.parents('.content-panel:first');
		else from_ = from_.parents('#content');
		$('body').append('<div id="rectangle" style="position:absolute;z-index:99;width:'+from_.width()+'px;height:'+from_.height()+'px;top:'+from_.offset().top+'px;left:'+from_.offset().left+'px;border:1px solid #B43791;"></div>');
		var to_ = $('.download');
		$('#rectangle').animate({'top':to_.offset().top,'left':to_.offset().left,'width':20,'height':to_.height()},500,function(){
			if (a.hasClass('ajouter-au-panier')){
                $.post(chemin_root+'ajax_panier.php',{lg:lg,'reference':a.attr('name').substr(7),'action':'add'},function(data){to_.html(data)});
			    a.parents('li').next('li').show();
                a.parents('li').hide();
                $(this).remove();
			    
                var img = a.parents('.block:first').find('.img:first img').attr('src');
                var desc = a.parents('.block:first').find('.description:first h3 > a').html();
                var color = a.parents('.block:first').find('.description:first h3 > a').css('color');
                
                var img = a.parents('.block:first').find('.img:first img').attr('src');
                if (img == undefined){
                    img = a.parents('#content').find('.content-panel:first').next('div.visual-holder').find('.image:first img').attr('src');
                }
                var desc = a.parents('.block:first').find('.description:first h3 > a').html();
                if (desc == null){
                    desc = a.parents('#content').find('.content-panel:first').find('h2').html();
                    desc += '<br />'+a.parents('#content').find('.content-panel:first').find('div.title').children('p').html();

                    color = a.parents('#content').find('.content-panel:first').find('h2').css('color');
                }

                var table = '';
                table += '<table width="400">';
                table += '<tr><td colspan="2" align="center"><span style="color: '+color+';font: 1.5em Arial,Helvetica,sans-serif;font-weight: bold;">Votre sélection a été ajoutée à votre panier</span></td></tr>';
                table += '<tr><td><img width="150" src="'+img+'" /></td><td><span style="color: '+color+';font: 1.5em Arial,Helvetica,sans-serif;font-weight: bold;">'+desc+'</span></td></tr>';
                table += '<tr style="height: 48px;"><td colspan="2" align="center"><a style="color:#000000;font-size: 1.2em;font-weight: bold;line-height: 20px;" href="'+$('.download').attr('href')+'" class="download">Accéder à mon panier</a></td></tr>';
                table += '</table>';
                table += '';
                $.fancybox(
                		table,
                		{
                        	'autoDimensions'	: true,
                			'width'         		: 410,
                			'height'        		: 210,
                			'transitionIn'		: 'none',
                			'transitionOut'		: 'none'
                		}
                	);
            }else if(a.hasClass('supprimer-du-panier')){
                $.post(chemin_root+'ajax_panier.php',{lg:lg,'reference':a.attr('name').substr(7),'action':'remove'},function(data){to_.html(data)});
                a.parents('li').prev('li').show();
                a.parents('li').hide();
                $(this).remove();
            }		
		});
		return false;
	});
    
    $('#back_to_basket').live('click', function(e){
        e.preventDefault();
        $.fancybox.close;
    });

	$('#form_recherche a.validate').click(function(){
		$(this).parents('li.active:first').find('a.opener').click();
		return false;
	});
	
	$('a[name=inscrire_newsletter]').live('click',function(){
		$.post(chemin_root+'ajax_newsletter.php',{'lg':lg,'email':$('#newsletter_email').val()},function(data){
			alert(data);
		});
		return false;
	});
	
	$('a[name=desinscrire_newsletter]').live('click',function(){
		$.post(chemin_root+'ajax_newsletter.php',{'desinscription':'oui','lg':lg,'email':$('#newsletter_email').val()},function(data){
			alert(data);
		});
		return false;
	});
	
	var font_size=100;
	$('.resize .descrease').click(function(){
		if(font_size>=80) font_size-=10;
		$('#content').css({'font-size':font_size+'%'});
        $('body').css({'font-size':font_size+'%'});
	});
	$('.resize .increase').click(function(){
		if(font_size<=140) font_size+=10;
		$('#content').css({'font-size':font_size+'%'});
        $('body').css({'font-size':font_size+'%'});
	});
	
});
(function($) {
	$.fn.gallery = function(options) { return new Gallery(this.get(0), options); };
	
	function Gallery(context, options) { this.init(context, options); };
	
	Gallery.prototype = {
		options:{},
		init: function (context, options){
			this.options = $.extend({
				duration: 700,
				slideElement: 1,
				autoRotation: false,
				effect: false,
				listOfSlides: 'ul > li',
				switcher: false,
				disableBtn: false,
				nextBtn: 'a.link-next, a.btn-next, a.next',
				prevBtn: 'a.link-prev, a.btn-prev, a.prev',
				circle: true,
				direction: false,
				event: 'click',
				IE: false
			}, options || {});
			var _el = $(context).find(this.options.listOfSlides);
			if (this.options.effect) this.list = _el;
			else this.list = _el.parent();
			if (this.options.switcher) this.switcher = $(context).find(this.options.switcher);
			this.nextBtn = $(context).find(this.options.nextBtn);
			this.prevBtn = $(context).find(this.options.prevBtn);
			this.count = _el.index(_el.filter(':last'));
			
			if (this.options.switcher) this.active = this.switcher.index(this.switcher.filter('.active:eq(0)'));
			else this.active = _el.index(_el.filter('.active:eq(0)'));
			if (this.active < 0) this.active = 0;
			this.last = this.active;
			
			this.woh = _el.outerWidth(true);
			if (!this.options.direction) this.installDirections(this.list.parent().width());
			else {
				this.woh = _el.outerHeight(true);
				this.installDirections(this.list.parent().height());
			}
			
			if (!this.options.effect) {
				this.rew = this.count - this.wrapHolderW + 1;
				if (!this.options.direction) this.list.css({marginLeft: -(this.woh * this.active)});
				else this.list.css({marginTop: -(this.woh * this.active)});
			}
			else {
				this.rew = this.count;
				this.list.css({opacity: 0}).removeClass('active').eq(this.active).addClass('active').css({opacity: 1}).css('opacity', 'auto');
				if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			}
			
			if (this.options.disableBtn) {
				if (this.count < this.wrapHolderW) this.nextBtn.addClass(this.options.disableBtn);
				if (this.active == 0) this.prevBtn.addClass(this.options.disableBtn);
			}
			
			this.initEvent(this, this.nextBtn, this.prevBtn, true);
			this.initEvent(this, this.prevBtn, this.nextBtn, false);
			
			if (this.options.autoRotation) this.runTimer(this);
			
			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
		},
		installDirections: function(temp){
			this.wrapHolderW = Math.floor(temp / this.woh);
			if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderWwrapHolderW--;
		},
		fadeElement: function(){
			if ($.browser.msie && this.options.IE){
				this.list.eq(this.last).css({opacity:0});
				this.list.removeClass('active').eq(this.active).addClass('active').css({opacity:'auto'});
			}
			else{
				this.list.eq(this.last).animate({opacity:0}, {queue:false, duration: this.options.duration});
				this.list.removeClass('active').eq(this.active).addClass('active').animate({
					opacity:1
				}, {queue:false, duration: this.options.duration, complete: function(){
					$(this).css('opacity','auto');
				}});
			}
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			this.last = this.active;
		},
		scrollElement: function(){
			if (!this.options.direction) this.list.animate({marginLeft: -(this.woh * this.active)}, {queue:false, duration: this.options.duration});
			else this.list.animate({marginTop: -(this.woh * this.active)}, {queue:false, duration: this.options.duration});
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
		},
		runTimer: function($this){
			if($this._t) clearTimeout($this._t);
			$this._t = setInterval(function(){
				$this.toPrepare($this, true);
			}, this.options.autoRotation);
		},
		initEventSwitcher: function($this, el){
			el.bind($this.options.event, function(e){
			    e.preventDefault();
                
				$this.active = $this.switcher.index($(this));
				if ($this.active == 0) {
					$this.prevBtn.addClass($this.options.disableBtn);
					$this.nextBtn.removeClass($this.options.disableBtn);
				}
				else if ($this.active == $this.count) {
					$this.nextBtn.addClass($this.options.disableBtn);
					$this.prevBtn.removeClass($this.options.disableBtn);
				}
				else {
					$this.prevBtn.removeClass($this.options.disableBtn);
					$this.nextBtn.removeClass($this.options.disableBtn);
				}
				if($this._t) clearTimeout($this._t);
				if (!$this.options.effect) $this.scrollElement();
				else $this.fadeElement();
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		initEvent: function($this, addEventEl, addDisClass, dir){
			addEventEl.bind($this.options.event, function(){
				if($this._t) clearTimeout($this._t);
				if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) addDisClass.removeClass($this.options.disableBtn);
				$this.toPrepare($this, dir);
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		toPrepare: function($this, side){
			if (($this.active == $this.rew) && $this.options.circle && side) $this.active = -$this.options.slideElement;
			if (($this.active == 0) && $this.options.circle && !side) $this.active = $this.rew + $this.options.slideElement;
			for (var i = 0; i < $this.options.slideElement; i++){
				if (side) {
					if ($this.active + 1 > $this.rew) {
						if ($this.options.disableBtn && ($this.count > $this.wrapHolderW)) $this.nextBtn.addClass($this.options.disableBtn);
					}
					else $this.active++;
				}
				else{
					if ($this.active - 1 < 0) {
						if ($this.options.disableBtn && ($this.count > $this.wrapHolderW)) $this.prevBtn.addClass($this.options.disableBtn);
					}
					else $this.active--;
				}
			};
			if ($this.active == $this.rew && side) if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) $this.nextBtn.addClass($this.options.disableBtn);
			if ($this.active == 0 && !side) if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) $this.prevBtn.addClass($this.options.disableBtn);
			if (!$this.options.effect) $this.scrollElement();
			else $this.fadeElement();
		},
		stop: function(){
			if (this._t) clearTimeout(this._t);
		},
		play: function(){
			if (this._t) clearTimeout(this._t);
			if (this.options.autoRotation) this.runTimer(this);
		}
	}
}(jQuery));
// accordion function
function initAccordion() {
	var _activeClass = 'active';
	var _activeAccordion = 'active-accordion'

	$('ul.accordion').each(function(){
		var _accordion = $(this);
		var _items = _accordion.find('li');
		_items.each(function(){
			var _holder = $(this);
			var _opener = _holder.find('>a.opener');

			_opener.click(function(){
				if (_holder.hasClass(_activeClass)) {
					_holder.removeClass(_activeClass);
					_accordion.removeClass(_activeAccordion);
				}
				else {
					_holder.addClass(_activeClass);
					_accordion.addClass(_activeAccordion);
				}
				return false;
			});
		});
	});
}

/* APPEL AJAX DU RESULTAT A PARTIR DU FORMULAIRE DE RECHERCHE */
var curentSearch = false;
function GetResults(pData){
	if(!curentSearch){
		var ok = 'non';
		if ($('input[name="requete[type]"]').val()=='produit|type|IN|GE,GS,SE')ok = 'oui';
		curentSearch = true;
		if (id_nav != 23 && 0){
			$('#form_recherche_hide').fadeIn('slow');
			$('#content_hide').fadeIn('slow',function(){
				$.ajax({
					type: "POST",
					async: true,
					url: chemin_root+"inc/centrale_objet.php",
					data: "ajax=oui&lg="+lg+"&id_navigation="+id_nav+"&"+pData+"&m_15="+ok,
					success: function(html){
						if ($('#twocolumns').length >0) $('#twocolumns').attr('id', 'content');
						$('#content').html('<div id="content_hide" style="display:block;"><center><img style="position:fixed;" src="'+chemin_root+'images/search-loader.gif" /></center></div>'+html);
						setTimeout(function(){
							$('#content_hide').fadeOut('slow');
							getCapacite();
							$('#form_recherche_hide').fadeOut('slow');
							curentSearch = false;
							$('html,body').scrollTop($("a[name="+window.location.hash.replace('#','')+"]").offset().top);
						},2000);
					},
					error: function (xhr, ajaxOptions, thrownError){
							//alert(xhr.status + " " + xhr.statusText);
							curentSearch = false;
					}
				});
			});
		}else{
			/*
			$.ajax({
				type: "POST",
				async: true,
				url: chemin_root+"inc/centrale_objet.php",
				data: "ajax=oui&lg="+lg+"&id_navigation="+id_nav+"&"+pData,
				success: function(html){
					$.ajax({
						type: "POST",
						async: true,
						url: chemin_root+"inc/form_recherche.php",
						data: "ajax=oui&id_navigation=22&m_15="+ok+"&lg="+lg+"&"+pData,
						success: function(html){
							//$(location).attr('href', url_panier);
							$('#form_recherche').submit();
							curentSearch = false;
						},
						error: function (xhr, ajaxOptions, thrownError){
								//alert(xhr.status + " " + xhr.statusText);
									curentSearch = false;
						}
					});
				},
				error: function (xhr, ajaxOptions, thrownError){
						//alert(xhr.status + " " + xhr.statusText);
							curentSearch = false;
				}
			});
			*/
			$.ajax({
				type: "POST",
				async: true,
				url: chemin_root+"inc/form_recherche.php",
				data: "ajax=oui&id_navigation=22&m_15="+ok+"&lg="+lg+"&"+pData,
				success: function(html){
					//$(location).attr('href', url_panier);
					//$('#form_recherche').submit();
					curentSearch = false;
				},
				error: function (xhr, ajaxOptions, thrownError){
						//alert(xhr.status + " " + xhr.statusText);
							curentSearch = false;
				}
			});
			return false;
		}
	}
}

/* / APPEL AJAX DU RESULTAT A PARTIR DU FORMULAIRE DE RECHERCHE */

