// JavaScript Document
$.noConflict();
(function($) {
	var g_arrImgs_home,
		g_numSentence,
		g_numSentenceLoaded,
		g_slideIsRunning,
		g_bolSlideIsThere,
		g_sentenceInterval,
		g_countPreloadedImgs,
		g_urlbase,
		g_numImgs_home,
		g_slideInterval,
		g_slideCurrent,
		g_arrImgs_spa,
		g_isiMachine,
		g_section;
	$(document).ready(function() {
		g_section = $('#data-holder').attr('data-section');
		// homepage selector
		$('.homeselector-en').hide();
		
		$('.homeselector-lang-fr').click(function(){
			$('.homeselector-en').hide();
			$('.homeselector-fr').show();
		});
		
		$('.homeselector-lang-en').click(function(){
			$('.homeselector-fr').hide();
			$('.homeselector-en').show();
		});
		
		g_isiMachine = (navigator.userAgent.match(/iPad/i) != null || navigator.userAgent.match(/iPhone/i) != null);
		if (g_isiMachine) {
			var cssObj = {
		  		'position' : 'absolute',
		  		'top' : $(window).height()-30
			}
			positionBottom();
		}else {
			var cssObj = {
		  		'position' : 'fixed',
		  		'bottom' : '0',
				'margin':'0 auto'
			}
		}
		//if (g_isiMachine) $('#bottom').css(cssObj);
		$('#bottom').css(cssObj);
		$('#frm_news').submit(function(e) {
			subscribeNewsletter();
			e.preventDefault();
		});
		$('#frm_news_submit').click(function(e) {
			subscribeNewsletter();
		});
		if ($('#sentence-wrapper').length) {
			if (g_section=="paris") {
				if ($('#data-holder').attr('data-lang')=="fr") {
					g_arrImgs_home = new Array('/images/home_sentences/paris/le-spa-fr.png', '/images/home_sentences/paris/spa-sens-fr.png');
				} else {
					g_arrImgs_home = new Array('/images/home_sentences/paris/le-spa-en.png', '/images/home_sentences/paris/spa-sens-en.png');
				} 
			} else {
				if ($('#data-holder').attr('data-lang')=="fr") {
					g_arrImgs_home = new Array('/images/home_sentences/geneve/le-spa-fr.png', '/images/home_sentences/geneve/spa-sens-fr.png');
				} else {
					g_arrImgs_home = new Array('/images/home_sentences/geneve/le-spa-en.png', '/images/home_sentences/geneve/spa-sens-en.png');
				}
			}
			g_numSentence=0;
			g_numSentenceLoaded=0;
			g_slideIsRunning=false;
			g_bolSlideIsThere = true;
		}
		if ($('#slide-inside-wrapper').length)  {
			$('#slide-inside-img').attr('src','/images/gallery-spa/'+g_section+'/1.png');
			g_slideCurrent = 0;
			if (g_section=="paris") {
				g_arrImgs_spa = new Array('/images/gallery-spa/'+g_section+'/1.png','/images/gallery-spa/'+g_section+'/2.png','/images/gallery-spa/'+g_section+'/3.png','/images/gallery-spa/'+g_section+'/5.png','/images/gallery-spa/'+g_section+'/6.png','/images/gallery-spa/'+g_section+'/7.png','/images/gallery-spa/'+g_section+'/8.png');
			} else {
				g_arrImgs_spa = new Array('/images/gallery-spa/'+g_section+'/1.png','/images/gallery-spa/'+g_section+'/2.png','/images/gallery-spa/'+g_section+'/3.png','/images/gallery-spa/'+g_section+'/4.png','/images/gallery-spa/'+g_section+'/5.png','/images/gallery-spa/'+g_section+'/6.png','/images/gallery-spa/'+g_section+'/7.png');
			}
			var count=0;
			var strJS = '';
			$.each(g_arrImgs_spa, function (key, value) {
				if (count==0) strJS += '<div class="slide-progressor slide-progressor-active" id="sp_'+count+'">&nbsp;</div>';
				else strJS += '<div class="slide-progressor" id="sp_'+count+'">&nbsp;</div>';
				count++;
			});
			$('#slide-progression').append(strJS);
			count=0;
			strJS += '<div class="clear"></div>';
			$('.slide-progressor').each(function() {
				$(this).data('img',g_arrImgs_spa[count]);
				count++;
			});
			$('.slide-progressor').click(function(e) {
				getNewSlide($(this));
			});
			preloadSlide();
			autoSlide();
		}
		if ($('#slide-inside-wrapper-abo').length) {
			$('#slide-inside-img').attr('src','/images/gallery-carteblanche/'+g_section+'/1.png');
			g_slideCurrent = 0;
			if (g_section=="paris") {
				g_arrImgs_spa = new Array('/images/gallery-carteblanche/'+g_section+'/1.png','/images/gallery-carteblanche/'+g_section+'/2.png','/images/gallery-carteblanche/'+g_section+'/3.png','/images/gallery-carteblanche/'+g_section+'/4.png','/images/gallery-carteblanche/'+g_section+'/5.png','/images/gallery-carteblanche/'+g_section+'/6.png');
			} else {
				g_arrImgs_spa = new Array('/images/gallery-carteblanche/'+g_section+'/1.png','/images/gallery-carteblanche/'+g_section+'/2.png','/images/gallery-carteblanche/'+g_section+'/3.png');
			}
			var count=0;
			var strJS = '';
			$.each(g_arrImgs_spa, function (key, value) {
				if (count==0) strJS += '<div class="slide-progressor slide-progressor-active" id="sp_'+count+'">&nbsp;</div>';
				else strJS += '<div class="slide-progressor" id="sp_'+count+'">&nbsp;</div>';
				count++;
			});
			$('#slide-progression').append(strJS);
			count=0;
			strJS += '<div class="clear"></div>';
			$('.slide-progressor').each(function() {
				$(this).data('img',g_arrImgs_spa[count]);
				count++;
			});
			$('.slide-progressor').click(function(e) {
				getNewSlide($(this));
			});
			preloadSlide();
			autoSlide();
		}
		
		/**/
		if ($('#slide-inside-wrapper-marque').length) {
			$('#slide-inside-img').attr('src','/images/gallery-marque/'+g_section+'/1.png');
			g_slideCurrent = 0;
			
			
			g_arrImgs_spa = new Array('/images/gallery-marque/'+g_section+'/1.png','/images/gallery-marque/'+g_section+'/2.png','/images/gallery-marque/'+g_section+'/3.png','/images/gallery-marque/'+g_section+'/4.png','/images/gallery-marque/'+g_section+'/5.png');
			var count=0;
			var strJS = '';
			$.each(g_arrImgs_spa, function (key, value) {
				if (count==0) strJS += '<div class="slide-progressor slide-progressor-active" id="sp_'+count+'">&nbsp;</div>';
				else strJS += '<div class="slide-progressor" id="sp_'+count+'">&nbsp;</div>';
				count++;
			});
			$('#slide-progression').append(strJS);
			count=0;
			strJS += '<div class="clear"></div>';
			$('.slide-progressor').each(function() {
				$(this).data('img',g_arrImgs_spa[count]);
				count++;
			});
			$('.slide-progressor').click(function(e) {
				getNewSlide($(this));
			});	
			preloadSlide();
			autoSlide();
		}
		/**/
		g_countPreloadedImgs=1;
		g_urlbase = "/images/cartesoins/"+g_section+"/"+g_lang+"/ATR_carte-soins_web_page_";
		g_numImgs_home = 27;
		/* supersized */
		if ($('.bg-news').length) url = "/images/bg/bg-news.jpg";
		else if ($('.bg-gift').length) url = "/images/bg/bg-gift.jpg";
		else if ($('.bg-media').length) url = "/images/bg/bg-media.jpg";
		else if ($('.bg-spa').length) url = "/images/bg/bg-spa.jpg";
		else if ($('.bg-children').length) url = "/images/bg/bg-children.jpg";
		else if ($('.bg-homeselector').length) url = "/images/bg/bg-selector.jpg";
		
		else url = "/images/bg/bg-home.jpg";
		$.supersized({
			slides	:  [ { image : url} ]					
		});
		/* fin supersized */
		
		/* detect tables and style them */
		var cssObj = {
			'padding-left' : '20px'
		}
		$('table > tbody > tr > td:eq(1)').css(cssObj);
		$(window).resize(function() {
			setBottom();
		});
		$(window).scroll(function() {
			if (g_isiMachine) positionBottom();
			//positionBottom();
		});
		setBottom();
		if (g_bolSlideIsThere) {
			loadSentence();
		}
		if ($('#carousel').length) {
			
			$('#carousel').jcarousel({
				vertical: true,			
				scroll: 1,
				initCallback: carousel_initCallback,
				buttonNextHTML: null,
				buttonPrevHTML: null,
				wrap: 'circular',
				auto:3
			});
		}
		if ($('.sbox').length) {
			Shadowbox.init({
				handleOversize: "resize",
				modal: true,
				overlayColor:'#fff',
				displayCounter:false,				
				onOpen:function(e) {
					if (!$('#sb-back').length) $('<a id="sb-back" style="background:url(/templates/aftertherain/css/screen/shadowbox/summary_'+g_lang+'.png);"></a>').appendTo($('#sb-nav'));
					$('#sb-back').unbind('click').bind('click',function(e) {
						setIndex();
				 	});
				 	
				},
				onClose:function(e) {
					removeIndex();
				}					
			});
			
			$('.sbox').click(function(e) {
				preloadImage();
			});
		}
		if ($('#gmap').length) {
			initMap();
		}
		if (g_isiMachine && $(document).scrollTop() > 0) setTimeout(positionBottom, 2000);
	});
	/* !carte des soins */
	
	function removeIndex() {
		$('#indexmenu').remove();
	}
	function fadeIndex() {
		$('#indexmenu').fadeOut('slow',function () {
			$(this).remove();
		});
	}
	function setIndex() {
		if ($('#indexmenu').length) {
			fadeIndex();			
		} else {
			if (g_section=="geneve") {
				if (g_lang=="fr") {
					var $menu = $('<div id="indexmenu"><div id="closebtn"></div>'
						+'<div data-goto="4" class="sb-indexmenu"><a href="#">le visage</a></div>'
						+'<div data-goto="7" class="sb-indexmenu"><a href="#">les massages</a></div>'
						+'<div data-goto="9" class="sb-indexmenu"><a href="#">les soins du corps</a></div>'
						+'<div data-goto="11" class="sb-indexmenu"><a href="#">les escales en tandem</a></div>'
						+'<div data-goto="13" class="sb-indexmenu"><a href="#">l\'enveloppe et l\'eau</a></div>'
						+'<div data-goto="15" class="sb-indexmenu"><a href="#">l\'ayurveda</a></div>'
						+'<div data-goto="18" class="sb-indexmenu"><a href="#">le corps au féminin</a></div>'
						+'<div data-goto="20" class="sb-indexmenu"><a href="#">l\'homme</a></div>'
						+'<div data-goto="23" class="sb-indexmenu"><a href="#">les touches finales</a></div>'
						+'<div data-goto="24" class="sb-indexmenu last"><a href="#">les alchimies</a></div>'
						+'</div>');
				} else {
					var $menu = $('<div id="indexmenu"><div id="closebtn"></div>'
						+'<div data-goto="4" class="sb-indexmenu"><a href="#">facials</a></div>'
						+'<div data-goto="7" class="sb-indexmenu"><a href="#">massages</a></div>'
						+'<div data-goto="9" class="sb-indexmenu"><a href="#">body treatments</a></div>'
						+'<div data-goto="11" class="sb-indexmenu"><a href="#">excursions for two</a></div>'
						+'<div data-goto="13" class="sb-indexmenu"><a href="#">you and water</a></div>'
						+'<div data-goto="15" class="sb-indexmenu"><a href="#">ayurveda</a></div>'
						+'<div data-goto="18" class="sb-indexmenu"><a href="#">just for you, madam</a></div>'
						+'<div data-goto="20" class="sb-indexmenu"><a href="#">just for you, sir</a></div>'
						+'<div data-goto="23" class="sb-indexmenu"><a href="#">final touches</a></div>'
						+'<div data-goto="24" class="sb-indexmenu last"><a href="#">alchemies</a></div>'
						+'</div>');
					
				}
			} else {
			
				if (g_lang=="fr") {
					var $menu = $('<div id="indexmenu"><div id="closebtn"></div>'
						+'<div data-goto="3" class="sb-indexmenu"><a href="#">le visage</a></div>'
						+'<div data-goto="6" class="sb-indexmenu"><a href="#">les massages</a></div>'
						+'<div data-goto="9" class="sb-indexmenu"><a href="#">les soins du corps</a></div>'
						+'<div data-goto="11" class="sb-indexmenu"><a href="#">les escales en tandem</a></div>'
						+'<div data-goto="13" class="sb-indexmenu"><a href="#">l\'enveloppe et l\'eau</a></div>'
						+'<div data-goto="15" class="sb-indexmenu"><a href="#">l\'ayurveda</a></div>'
						+'<div data-goto="18" class="sb-indexmenu"><a href="#">le corps au féminin</a></div>'
						+'<div data-goto="20" class="sb-indexmenu"><a href="#">l\'homme</a></div>'
						+'<div data-goto="23" class="sb-indexmenu"><a href="#">les touches finales</a></div>'
						+'<div data-goto="24" class="sb-indexmenu last"><a href="#">les alchimies</a></div>'
						+'</div>');
				} else {
					var $menu = $('<div id="indexmenu"><div id="closebtn"></div>'
						+'<div data-goto="3" class="sb-indexmenu"><a href="#">facials</a></div>'
						+'<div data-goto="6" class="sb-indexmenu"><a href="#">massages</a></div>'
						+'<div data-goto="9" class="sb-indexmenu"><a href="#">body treatments</a></div>'
						+'<div data-goto="11" class="sb-indexmenu"><a href="#">excursions for two</a></div>'
						+'<div data-goto="13" class="sb-indexmenu"><a href="#">you and water</a></div>'
						+'<div data-goto="15" class="sb-indexmenu"><a href="#">ayurveda</a></div>'
						+'<div data-goto="18" class="sb-indexmenu"><a href="#">just for you, madam</a></div>'
						+'<div data-goto="20" class="sb-indexmenu"><a href="#">just for you, sir</a></div>'
						+'<div data-goto="23" class="sb-indexmenu"><a href="#">final touches</a></div>'
						+'<div data-goto="24" class="sb-indexmenu last"><a href="#">alchemies</a></div>'
						+'</div>');
					
				}
			}
			$menu.prependTo('#sb-wrapper').fadeIn('slow');
			$('#closebtn').unbind('click').bind('click',function(e) {
				fadeIndex();
			});
			$('.sb-indexmenu').click(function(e) {
				var num = parseInt($(this).attr('data-goto'));
				Shadowbox.change(num);
				$('#indexmenu').fadeOut('slow',function () {
					$(this).remove();
				});				
			});
		}
	}
	/* !fin carte soins */
	function preloadSlide() {
		$.each (g_arrImgs_spa, function (key, value) {
			var img = $('<img />').attr('src', value).load();
		});
	}
	
	function subscribeNewsletter() {
		if (!checkEmail()) {
			alert("Please enter your email address");
		} else {
		 	$.post("/templates/aftertherain/_services/news.php", {action:'subscribe',email:$('#emailnewsletter').val()}, function (data) {
				alert (data);
			});
		}
	}
	function checkEmail() {
		var s = $('#emailnewsletter').val();
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		return emailPattern.test(s);	
	}
	function getNewSlide($btn) {
		window.clearInterval(g_slideInterval);
		$('.slide-progressor').each(function() {
			$(this).removeClass('slide-progressor-active');	
		});
		$btn.addClass('slide-progressor-active');
		$('#slide-inside-img-wrapper').fadeOut(function () {
			$('#slide-inside-img').attr('src',$btn.data('img'));
			$(this).fadeIn();
		});
	}
	function autoSlide() {
		window.clearInterval(g_slideInterval);
		g_slideInterval = window.setInterval(function() {
			changeSlide();
		}, 3000);	
	}
	function changeSlide() {
		if (g_slideCurrent < g_arrImgs_spa.length-1) {
			g_slideCurrent++;
		} else {
			g_slideCurrent=0;
		}
		$('#slide-inside-img-wrapper').fadeOut(function () {
			$('#slide-inside-img').attr('src',g_arrImgs_spa[g_slideCurrent]);
			$(this).fadeIn();
		});
		$('.slide-progressor').each(function() {
			$(this).removeClass('slide-progressor-active');	
		});
		$('#sp_'+g_slideCurrent).addClass('slide-progressor-active');
	}
	function preloadImage() {
		if (g_countPreloadedImgs<g_numImgs_home) {
			var url = g_urlbase+g_countPreloadedImgs+".jpg";
			var img = $('<img />').attr('src', url).load(function() {
				g_countPreloadedImgs++;
				preloadImage();	
			});
		}
	}
	/* carousel */
	function carousel_initCallback(carousel) {
		//bind click sur prev et next btn
		$('#carousel-next a').bind('click', carousel_next);
		$('#carousel-prev a').bind('click', carousel_prev);
		$('#carousel li').each(function () {
			$(this).addClass('item-not-visible');	
		});
	}
	function carousel_next() {
		var carousel = $('#carousel').data('jcarousel');
		carousel.next();		
		return false;
	}
	function carousel_prev() {
		var carousel = $('#carousel').data('jcarousel');
		carousel.prev();
		return false;		
	}
	
	/* */
	function loadSentence() {
		$('<img />').attr('src', g_arrImgs_home[g_numSentenceLoaded]).load(function() {
			if (!g_slideIsRunning) {
				g_slideIsRunning=true;
				setSentence();	
			}
			if (g_numSentenceLoaded < g_arrImgs_home.length-1) {
				g_numSentenceLoaded++;
				loadSentence();	
			}
		});	
	}
	function setSentence() {
		window.clearInterval(g_sentenceInterval);
		g_sentenceInterval = window.setInterval(function() {
			setSentence();
		}, 3000);
		var imageToShow = $('<img src="'+g_arrImgs_home[g_numSentence]+'" id="image" />').load(function() {
			$('#sentence-wrapper').fadeOut(function () {
				if ($('#image')) $('#image').remove();
				$(this).append(imageToShow);
				$(this).fadeIn();
			});
			g_numSentence= g_numSentence < g_arrImgs_home.length-1 ? g_numSentence+1 : 0;
		});
	}
	function setBottom() {
		var targetW = $(window).width();
		var posBottom = targetW/2 - $('#bottom').innerWidth()/2;
		var posSentence = targetW/2 - $('#sentence-wrapper').innerWidth()/2;
		$('#sentence-wrapper').css('margin-left',posSentence-80);
		$('#bottom').css('margin-left',posBottom);
		if ($('#bottom-msg').length) {
			var posBottomMsg = targetW/2 - $('#bottom-msg').innerWidth()/2;
			$('#bottom-msg').css('margin-left',posBottomMsg);
		}
		
	}
	function positionBottom() {
		var y = $(document).scrollTop()+$(window).height()-30;
		$('#bottom').css('top',(y)+'px');
		
		//console.log($('#bottom').css('top'));
	}
	function initMap() {
		if (g_section=="paris") {
			var latlng = new google.maps.LatLng(48.86421,2.330743);
			var myLatLng = new google.maps.LatLng(48.86421,2.330743);
		} else {
			var latlng = new google.maps.LatLng(46.204073,6.144516);
			var myLatLng = new google.maps.LatLng(46.204073,6.144516);
		}
		var myOptions = {
			zoom: 15,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			scrollwheel:false
		};
		var map = new google.maps.Map(document.getElementById("gmap"),myOptions);
		var image = new google.maps.MarkerImage('/templates/aftertherain/images/picto-google.png',
			new google.maps.Size(29, 42),
			new google.maps.Point(0,0),
			new google.maps.Point(0, 42)
		);
		var shadow = new google.maps.MarkerImage('/templates/aftertherain/images/picto-google-shadow.png',
			// The shadow image is larger in the horizontal dimension
			// while the position and offset are the same as for the main image.
			new google.maps.Size(45, 16),
			new google.maps.Point(0,0),
			new google.maps.Point(-10, 10)
		);
		var beachMarker = new google.maps.Marker({
			position: myLatLng,
			shadow: shadow,
			map: map,
			icon: image
		});
		
	}
})(jQuery);
