var offset = ""; 
var user_agent,browser, IE6;
var infosOn = false;
// $.fx.speeds._default = 1000;
// alert("TESTING ALERTS")


$(document).ready(function() {
	
	reBindPage();
	
	bindReservation();
	bindZoomPano();
	bindPromo();
	
	$('#tabGlobal, #blockSite').css('height',$(document).height());
		
	$('#mailMarine').html("infos@hotelmarine.com");
	
	init_lien_fancybox_CMS();
});


function reBindPage(){	
	if($(".pirobox").length)
	{		
		 $().piroBox({
			my_speed: 400, //animation speed
			bg_alpha: 0.3, //background opacity
			slideShow : true, // true == slideshow on, false == slideshow off
			slideSpeed : 4, //slideshow duration in seconds(3 to 6 Recommended)
			close_all : '.piro_close,.piro_overlay'// add class .piro_overlay(with comma)if you want overlay click close piroBox
		});
	}
	
	if($("#diapo_page").length){
		$("#diapo_page").carouFredSel({
			width :745,
			height : 336,
			circular :true,
			infinite :true,
			direction : "left",
			items :{
				visible:1,
				width:745,
				height:336
			},
			scroll : {
				effect			: "easeOutBounce",
				duration		: 1000,							
				pauseOnHover	: true
			},
			prev : {	
				button	: "#diapo_prev",
				key		: "left"
			},
			next : { 
				button	: "#diapo_next",
				key		: "right"
			}			
		});
	}
	
	if($(".cadre_img").length){
		$(".cadre_img").each(function(){
			if(!$(this).parent().hasClass('contain_cadre')){
				setCadreOverlays(this);	
			}
		})
	}
}

function getDialoLeft(dialWidth){
	if(dialWidth)
	{
		return (dialWidth/2 - $(window).width());
	}
	else return 'none';
}


function bindZoomPano(){
	if($(".zoom_pano").length)
		{
			$(".zoom_pano").fancybox({
				'overlayOpacity':0.5,
				'overlayColor':	'#000000',
				'hideOnContentClick' : false,
				'zoomSpeedIn'		:	500,
				'zoomSpeedOut'		:	480,
				'transitionIn'		:	'elastic',
				'transitionOut'		:	'elastic',
				'easingIn'			: 	'easeOutBack',
				'easingOut'			: 	'easeInBack',
				'centerOnScroll'	: 	true,
				'padding': 5,
				'autoDimensions' :false
			});	
		}	
}

function bindReservation(){
	if($('.infoFixed').length)
	{
		$('.infoFixed').animate({
			left: '+=40'
			}, 500,"easeOutBounce", function() {					
		}).animate({
			top: '15px'
			}, 500,"easeOutBounce", function() {					
		});	;	
		
		$('.info_opener').click(function(){
			var $this = $(this);
			var $infoFixed = $this.parent();
			if(!infosOn)
			{
				infosOn = true;	
				$infoFixed.animate({
					left: '+=502'
					}, 500,"easeOutBounce", function() {
						$('.closeInfos').fadeIn();
						$this.attr("title","Fermer le panneau");	
				});	
			}
			else{
				infosOn = false;
				$('.closeInfos').fadeOut();
				$infoFixed.animate({
					left: '-=502'
					}, 500,"easeOutBounce", function() {
						$this.attr("title","Ouvrir le panneau");	
				});	
			}
		})
		
		$('.closeInfos').click(function(){
			$('.info_opener').click();
		})
	}	
}

function bindPromo(){
	if($('#promos').length)
	{
		$('#titlePromo').click(function(){
			var toToggle = $(this).parent().find('#contentPromo').find('#contentP');
			if($.browser.msie && $.browser.version.substr(0,1) == '7')
			{
				if(toToggle.is(':hidden'))
					toToggle.css({'padding-top':'10'})
				else toToggle.css({'padding-top':'0'})
			}			
			toToggle.toggle(1000);
		})	
	}
}

function init_lien_fancybox_CMS()
{
	
	$('a.iframe').click(function() {
		var c, w, h,wl, hl, href, partial,rel,px;
		
		rel=$(this).attr('rel');
		
		if(rel!=""){
			var px=rel.indexOf("x");
			if(px>0){
				var wl=rel.substr(0,px);
				var hl=rel.substr(px+1);
				if(hl>0 && wl>0){
					w=parseInt(wl);h=parseInt(hl);
				}
			}
		}
		
		!(w>0) ? w=700 : w=w;
		!(h>0) ? h=400 : h=h;
		
		href = $(this).attr('href');
		partial = $(this).hasClass('partial');
		if(partial)
		{
			$.post('includes/ajax_partial_page.php', {page: href}, function(page) {
				c = '<div style="background-color: #FFFFFF">'+page.content+'</div>';								
				$.fancybox({
					autoDimensions: false,
					content: c,
					width: w,
					height: h,
					transitionIn:'elastic',
					transitionOut:'elastic',
					speedIn:200, speedOut:200,
					title: page.titre
				});
				return false;
			} , 'json');
		}
		else
		{
			$.fancybox({
				width: w,
				height: h,
				transitionIn:'elastic',
				transitionOut:'elastic',
				speedIn:200, speedOut:200,
				type: 'iframe',
				'href': href
			});
		}
		return false;
	});
}

function init_line()
{
	/*var py=$("#baseline").offset().top;
	$("#upline").css("top",(py+15)+"px");*/
}

function format_nombre(valeur,decimal,separateur) {
// formate un chiffre avec 'decimal' chiffres après la virgule et un separateur
	var deci=Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ; 
	var val=Math.floor(Math.abs(valeur));
	if ((decimal==0)||(deci==Math.pow(10,decimal))) {val=Math.floor(Math.abs(valeur)); deci=0;}
	var val_format=val+"";
	var nb=val_format.length;
	for (var i=1;i<4;i++) {
		if (val>=Math.pow(10,(3*i))) {
			val_format=val_format.substring(0,nb-(3*i))+separateur+val_format.substring(nb-(3*i));
		}
	}
	if (decimal>0) {
		var decim=""; 
		for (var j=0;j<(decimal-deci.toString().length);j++) {decim+="0";}
		deci=decim+deci.toString();
		val_format=val_format+"."+deci;
	}
	if (parseFloat(valeur)<0) {val_format="-"+val_format;}
	return val_format;
}

function setCadreOverlays(elem){
	if(!$(elem).is('img')){
		var new_elem = $(elem).find('img');
		elem = new_elem;
	}
	
	var width 			= $(elem).width();
	var height			= $(elem).height();
	var margeTop		= $(elem).css("marginTop");
	var margeBot 		= $(elem).css("marginBottom");
	var margeLeft 		= $(elem).css("marginLeft");
	var margeRight 		= $(elem).css("marginRight");
	var float 			= $(elem).css("float");
	var txtAlign		= $(elem).css('text-align');
	
	//console.log(height);
	
	$(elem).css({
		"position"		:"relative",
		"float"			:"left",
		"z-index"		:"5",
		"marginTop" 	:0,		
		"marginBottom" 	:0,
		"marginLeft" 	:0,			
		"marginRight" 	:0	
	})
	
	var divContain = $("<span></span>")
						.attr("class",'contain_cadre')
						.css({
							"display"		:"inline-block",
							"width"			:width,
							"height"		:height,	
							"marginTop" 	:margeTop,		
							"marginBottom" 	:margeBot,
							"marginLeft" 	:margeLeft,			
							"marginRight" 	:margeRight,
							"float"			:float				
						});
						
				
	var overlay_top = $("<span></span>")
										.attr("class",'overlay_cadre overlay_top')
										.css({
											"width":width,
											"height":'6px',
											"display":"inline-block",
											"position":"relative",
											"margin":0,
											"marginTop":"-"+height+"px",
											"padding":0,
											"overflow":"hidden",
											"float":"left",
											"opacity":.4,
											"z-index":100,
											"background-color":"#FFF"
										});
	var overlay_bot = $("<span></span>")
										.attr("class",'overlay_cadre overlay_bot')
										.css({
											"width":width,
											"height":'6px',
											"display":"inline-block",
											"position":"relative",
											"margin":0,
											"padding":0,
											"marginTop":"-6px",
											"overflow":"hidden",
											"float":"left",
											"opacity":.4,
											"z-index":100,
											"background-color":"#FFF"
										});

	
	$(elem).wrap(divContain).after(overlay_top).after(overlay_bot);
	/*
	
	*/
		
}



