Event.observe(window,'load',init);

var map;
var displayedNext;
var maxNextDisplay=4;

function init(){
	if($('we_details')){
		$$('.tabs a').each(function(elmt){
			Event.observe(elmt,'click',AJAXMotion);
		})
	}
	if($('seeMoreNext')){
		Event.observe($('seeMoreNext'),'click',displayMoreNext);
		Event.observe($('nextDateIn'),'focus',displayAvailability);
		if($('next_we').getElementsByTagName('li').length<=maxNextDisplay){
			$('seeMoreNext').hide();
		}
	}
	if($('map')){
		map=new YMap(document.getElementById('map'));
		homeMap();
	}
	if($('idSsThematic')){
		Event.observe($('idSsThematic'),'change',displayEven);
	}
	if($('we_results')){
		$$('.disponibility').each(function(elmt){
			Event.observe(elmt,'click',displayListCal);
		});
		$$('.close').each(function(elmt){
			Event.observe(elmt,'click',function(e){
				var calWindowId=Event.element(e).up('div',0).id;
				Effect.Fade(calWindowId);
				Event.stop(e);
			});
		});
		Event.observe($('order'),'change',reloadDisplayList);
		Event.observe($('nextDateIn'),'focus',displayListAvailability);
		if($('idSousThematic')){
			Event.observe($('idSousThematic'),'change',reloadDisplayList);
		}
	}
	if($('we_resa')){
		Event.observe($('dateIn'),'focus',displayResaCal);
	}
	/*if($('we_resa')){
		Event.observe($('resaDateIn'),'focus',displayResaCal);
		$$('#we_resa input.submit').each(function(elmt){
			Event.observe(elmt,'click',reDisplayResa);
		});
		Event.observe($('resaPaxAdult'),'change',displayResa);
		if($('resaPaxChild')){
			Event.observe($('resaPaxChild'),'change',displayResa);
		}
		if($('resaPaxInfant')){
			Event.observe($('resaPaxInfant'),'change',displayResa);
		}
	}*/
	
	$$('.popup').each(function(elmt){
		elmt.observe('click',popup);
	});
	$$('.toEmpty').each(function(elmt){
		Event.observe(elmt,'focus',empty);
	});
	if($('alacarte')){
		$$('#alacarte .required').each(function(elmt){
			if(elmt.nodeName.toLowerCase()!='p'){
				if(elmt.value!=''){
					elmt.addClassName('focused');
				}
				Event.observe(elmt,'blur',function(e){
					if(Event.element(e).value==''){
						Event.element(e).removeClassName('focused');
					}
					else{
						Event.element(e).addClassName('focused');
					}
				});
			}
		});
		Event.observe($('paxAdult'),'change',addPax);
		Event.observe($('paxChild'),'change',addPax);
		Event.observe($('paxInfant'),'change',addPax);
		Event.observe($('dateIn'),'click',displayCal);
		Event.observe($('dateOut'),'click',displayCal);
	}
	if($('contact')){
		$$('#contact .required').each(function(elmt){
			
			if(elmt.value!=''){
				elmt.addClassName('focused');
			}
			Event.observe(elmt,'blur',function(e){
				if(Event.element(e).value==''){
					Event.element(e).removeClassName('focused');
				}
				else{
					Event.element(e).addClassName('focused');
				}
			});
			
		});
	}
	
	
}

function popup(e){
	
	window.open('game.html','game','width=680,height=400,status=yes,location=no');
	Event.stop(e);
}

function empty(e){
	Event.element(e).value="";
}
function putEmail(e){
	if(Event.element(e).value.blank()){
		Event.element(e).value='email';
	}
}
function addPax(e){
	new Ajax.Updater('pax','index.php',{method:'get',parameters:{mdl:'we',action:'addPax',paxAdult:$F('paxAdult'),paxChild:$F('paxChild'),paxInfant:$F('paxInfant')},evalScripts:true});
}

function displayResaFields(e){
	var nextElmt=Event.element(e).up('legend',0).next('ul',0);
	if(nextElmt.visible()){
		nextElmt.hide();
		Event.element(e).addClassName('hidden');
	}
	else{
		nextElmt.show();
		Event.element(e).removeClassName('hidden');
	}
	Event.stop(e);
}


function poll(e){
	var params=Event.element(e).href.toQueryParams();
	var note=params.poll;
	var poller=$('poll').getElementsByTagName('a');
	var i=0;
	$$('#poll a').each(function(elmt){
		if(i<note){
			if(elmt.hasClassName('off')){
				elmt.removeClassName('off');
				elmt.addClassName('on');
			}
		}
		else{
			elmt.removeClassName('on');
			elmt.addClassName('off');
		}
		i++;
	})
}
function validPoll(e){
	var params=Event.element(e).href.toQueryParams();
	var note=params.poll;
	new Ajax.Updater('pollUpdated','index.php',{method:'get',parameters:{mdl:'we',action:'poll',idWe:$F('idWe'),note:note},
	onSuccess:function(){
		$('pollUpdated').show();
		setTimeout(function(){Effect.Fade('pollUpdated');},1000);}
		});
	Event.stop(e);
}
function reDisplayResa(e){
	pricePerPax();
	if($('resaCompute')){$('resaCompute').hide();}
	var params=$H($('resaForm').serialize(true));
	params.update({mdl:'we',action:'getResaDetails'});
	new Ajax.Updater('we_details_content','index.php',{method:'post',parameters:params.toObject(),
	evalScripts:true,
	onCreate:function(){
		$('resaProgress').show();
		},
	onSuccess:function(){
		Effect.Fade('resaProgress');
		}
	});
	Event.stop(e);
}

function displayResa(){
	if(!$('resaCompute')){
		return;
	}
	if($('resaCompute')){$('resaCompute').hide();}
	if($('resaPay')){$('resaPay').update('');}
	var params=$H($('resaForm').serialize(true));
	params.update({mdl:'we',action:'getResaDetails'});
	new Ajax.Updater('we_details_content','index.php',{method:'post',parameters:params.toObject(),	evalScripts:true,
	onCreate:function(){
		$('resaProgress').show();
		},
	onSuccess:function(){
		Effect.Fade('resaProgress');
		}
	});
}

function displayResaCal(e) {
	new Ajax.Updater('iCalResaIn','index.php',{method:'get',parameters:{mdl:'we',action:'getResaAvailability',idWe:$F('idWe')},evalScripts:true,onCreate:function(){$('resaProgress').show();},
	onSuccess:function(){
		$('resaProgress').hide();
		$('iCalResaIn').show();
		$('paxAdult').style.visibility='hidden';}
	});
}
function selectResaDate(e){
	var params=Event.element(e).href.toQueryParams();
	var dateIn=params.dateIn.substring(6,8)+'-'+params.dateIn.substring(4,6)+'-'+params.dateIn.substring(0,4);
	$('dateIn').value=dateIn;
	var dateOut=params.dateOut.substring(6,8)+'-'+params.dateOut.substring(4,6)+'-'+params.dateOut.substring(0,4);
	$('dateOut').value=dateOut;
	if(	$('iCalResaIn')){
		$('iCalResaIn').hide();
	}
	$('paxAdult').style.visibility='visible';
	if($('paxChild')){
		$('paxChild').style.visibility='visible';
	}
	if($('paxInfant')){
		$('paxInfant').style.visibility='visible';
	}
	
	pricePerPax();
	
	Event.stop(e);
}
/*function selectResaDate(e){
	var params=Event.element(e).href.toQueryParams();
	var dateIn=params.dateIn.substring(6,8)+'-'+params.dateIn.substring(4,6)+'-'+params.dateIn.substring(0,4);
	$('resaDateIn').value=dateIn;
	var dateOut=params.dateOut.substring(6,8)+'-'+params.dateOut.substring(4,6)+'-'+params.dateOut.substring(0,4);
	$('resaDateOut').value=dateOut;
	if(	$('iCalResaIn')){
		$('iCalResaIn').hide();
	}
	$('resaPaxAdult').style.visibility='visible';
	if($('resaPaxChild')){
		$('resaPaxChild').style.visibility='visible';
	}
	if($('resaPaxInfant')){
		$('resaPaxInfant').style.visibility='visible';
	}
	
	pricePerPax();
	
	Event.stop(e);
}*/
function pricePerPax(){
	new Ajax.Updater('pricePerPax','index.php',{method:'get',parameters:{mdl:'we',action:'getResaPrice',dateIn:$F('dateIn'),idWe:$F('idWe')}});
}
function sendComment(e){
	
	new Ajax.Updater('commentSent','index.php',{method:'post',parameters:{mdl:'we',action:'sendComment',pseudo:$F('pseudo'),idWe:$F('idWe'),commentaire:$F('commentaire'),captcha:$F('captcha')},
		onSuccess:function(){
			$('commentSent').show();
			setTimeout(function(){Effect.Fade('commentSent');},2000);
			}
		});
	$('pseudo').clear();
	$('commentaire').clear();
	$('captcha').clear();
	Event.stop(e);
}
function selectListDate(e){
	var params=Event.element(e).href.toQueryParams();
	var date=params.date.substring(6,8)+'-'+params.date.substring(4,6)+'-'+params.date.substring(0,4);
	$('nextDateIn').value=date;
	$('iCalAvail').hide();
	reloadDisplayList();
	displayListDate();
	Event.stop(e);
}
function reloadDisplayList(e){
	var idSousThematic=$('idSousThematic')?$F('idSousThematic'):'';
	new Ajax.Updater('we_results','index.php',{method:'post',parameters:{mdl:'we',action:'listWe',thematic:$F('thematic'),geoZone:$F('geoZone'),order:$F('order'),nextDateIn:$F('nextDateIn'),idSousThematic:idSousThematic},evalScripts:true,
		onCreate:function(){$('listLoader').show();},
		onSuccess:function(){$('listLoader').hide();}
		});
}
function displayEven(e){
	new Ajax.Updater('evens','index.php',{method:'post',parameters:{mdl:'we',action:'getEven',idSsThematic:$F('idSsThematic')}});
}
function displayListDate(){
	new Ajax.Updater('thematicTeaser','index.php',{method:'post',parameters:{mdl:'we',action:'getNextDateIn',nextDateIn:$F('nextDateIn')}});
}
function displayListAvailability(e){
	new Ajax.Updater('iCalAvail','index.php',{method:'get',parameters:{mdl:'we',action:'getListAvailability',thematic:$F('thematic'),geoZone:$F('geoZone')},evalScripts:true,onCreate:function(){$('listLoader').show();},onSuccess:function(){$('listLoader').hide();$('iCalAvail').show();}});
}
function displayListCal(e){
	var params=Event.element(e).href.toQueryParams();
	var idWe=params.idWe;
	new Ajax.Updater('iCal_'+idWe,'index.php',{method:'get',parameters:params,evalScripts:true,
		onCreate:function(){if(!Event.element(e).hasClassName('calNav')){Event.element(e).previous('span',0).show();}},
		onSuccess:function(){if(!Event.element(e).hasClassName('calNav')){Event.element(e).previous('span',0).hide();Effect.Appear('calWindow_'+idWe);}}
		});
	Event.stop(e);
}
/*calendrier des dispos pour nextWe*/
function displayAvailability(e){
	new Ajax.Updater('iCalAvail','index.php',{method:'get',parameters:{mdl:'we',action:'getNextAvailability'},evalScripts:true,onCreate:function(){$('loaderNext').show();},onSuccess:function(){$('iCalAvail').show();$('loaderNext').hide();}});
}
/*Changement de mois du calendrier des dispos pour nextWe*/
function displayMonth(e){
	var params=Event.element(e).href.toQueryParams();
	new Ajax.Updater('iCalAvail','index.php',{method: 'get',parameters:params,evalScripts:true,
		onCreate:function(){
			if($('listLoader')){
				$('listLoader').show();
			}
		},
		onSuccess:function(){
			$('iCalAvail').show();
			if($('listLoader')){
				$('listLoader').hide();
			}
		}
	});
	Event.stop(e);
}
function displayResaMonth(e){
	var cal=Event.element(e).up('div',1).id.sub('iCalResa', '');
	var params=Event.element(e).href.toQueryParams();
	new Ajax.Updater('iCalResa'+cal,'index.php',{method: 'get',parameters:params,evalScripts:true});
	Event.stop(e);
}
/*récupération des nextWe*/
function getNext(e){
	var params=Event.element(e).href.toQueryParams();
	var date=params.date.substring(6,8)+'-'+params.date.substring(4,6)+'-'+params.date.substring(0,4);
	new Ajax.Updater('nextWe','index.php',{method:'get',parameters:{mdl:'we',action:'nextWe',nextDateIn:date},
		onCreate:function(){$('iCalAvail').hide();$('nextDateIn').value=date;$('loaderNext').show();},
		onSuccess:function(){$('loaderNext').hide();}
		});
	new Ajax.Updater('nbreNext','index.php',{method:'get',parameters:{mdl:'we',action:'countNextWe',nextDateIn:date},
		onSuccess:function(transport){
			if(transport.responseText<=maxNextDisplay){
				$('seeMoreNext').hide();
			}
			else{
				$('seeMoreNext').show();
			}
		}
	});
	Event.stop(e);
}
/*modules de détails d'un produit*/
function AJAXMotion(e){
	var params=Event.element(e).href.toQueryParams();
	params.action='AJAXMotion';
	new Ajax.Updater('we_details_content','index.php',{method: 'get',parameters:params,evalScripts:true,
		onCreate:function(){$('loader').show();},
		onSuccess:function(){$('loader').hide();}
		});
	Event.stop(e);
	$$('.tabs a').each(function(elmt){
		elmt.removeClassName('enable');
		elmt.addClassName('disable');
	})
	Event.element(e).addClassName('enable');
}

function resaGetQuoted(e){
	callGetQuoted();
	if($('resaPay')){
		$('resaPay').update('');
	}
	Event.stop(e);
}
function callGetQuoted(){
	var params=$H($('resaCalculate').serialize(true));
	params.update({mdl:'we',action:'getResaQuoted'});
	new Ajax.Updater('resaBox','index.php',{method:'post',parameters:params.toObject(),evalScripts:true,
	onCreate:function(){
		$('quotedProgress').show();
		$('resaBox').hide();
		},
	onSuccess:function(){
		$('resaBox').show();
		$('getQuoted').value="Recalculer";
		Effect.Fade('quotedProgress');
		}
	});
}
function displayCoord(e){
	var params=$H($('resaCalculate').serialize(true));
	params.update({mdl:'we',action:'resaCoord'});
	if($('resaPaxChild')){
		params.update({resaPaxAdult:$F('resaPaxChild')});
	}
	if($('resaPaxInfant')){
		params.update({resaPaxAdult:$F('resaPaxInfant')});
	}
	params.update({resaPaxAdult:$F('resaPaxAdult')});
	new Ajax.Updater('resaCoord','index.php',{method:'post',parameters:params.toObject(),evalScripts:true,
		onCreate:function(){$('loader').show();},
		onSuccess:function(){
			$('loader').hide();
			$('resaCoord').show();
			}
		});
	if($('resaPay')){
		$('resaPay').update('');
	}
	Event.stop(e);
}



function displayImg(e){
	var link=Event.element(e).up('a',0);
	var img=link.href;
	var legend=link.title;
	if(!$('mainImg')) return;
	$('mainImg').hide();
	var newImg=new Image();
	newImg.src=img;
	$('mainImg').src=newImg.src;
	$('legend').innerHTML=legend;
	Effect.Appear('mainImg');
	Event.stop(e);
}
function startMap(e){
	if(!$F('geoLat')||!$F('geoLon')) return;

	map.addTypeControl();
	map.disableKeyControls();
	map.setMapType(YAHOO_MAP_HYB);
	map.addZoomLong();
	map.addPanControl(); 
	var currentGeoPoint = new YGeoPoint($F('geoLat'),$F('geoLon'));
	placeMarker(currentGeoPoint);
	map.drawZoomAndCenter(currentGeoPoint, 13);
	
	function placeMarker(geoPoint){
		newMarker= new YMarker(geoPoint, createCustomMarkerImage());
		newMarker.addAutoExpand($('title').innerHTML);
		map.addOverlay(newMarker);
	}
	function createCustomMarkerImage(){
		var image='img/marker.png';
		var imageSizeX=39;
		var imageSizeY=52;
		var myImage = new YImage();
		myImage.src = image;
		myImage.size = new YSize(imageSizeX,imageSizeY);
		myImage.offsetSmartWindow = new YCoordPoint(0,0);
		return myImage;	
	}
}
function homeMap(){
	map.addTypeControl();
	map.disableKeyControls();
	map.setMapType(YAHOO_MAP_REG);
	map.addZoomLong();
	map.addPanControl();
	var geoPointLength=geoGlobal.length;
	for(var i=0;i<geoPointLength;i++){
		var point = new YGeoPoint(geoGlobal[i][0] , geoGlobal[i][1] );
		newMarker= new YMarker(point, createCustomMarkerImage());
		newMarker.addAutoExpand('<a href="'+geoGlobal[i][3]+'" style="color:#666;font-size:1.1em;"><img src="'+geoGlobal[i][4]+'" style="border:1px solid #333;"/>'+geoGlobal[i][2]+'</a>');
		map.addOverlay(newMarker);
	} 
	map.drawZoomAndCenter('france', 15);
	
	function createCustomMarkerImage(){
		var image='img/mini_marker.png';
		var imageSizeX=20;
		var imageSizeY=25;
		var myImage = new YImage();
		myImage.src = image;
		myImage.size = new YSize(imageSizeX,imageSizeY);
		myImage.offsetSmartWindow = new YCoordPoint(0,0);
		return myImage;
	}
}
function displayMoreNext(e){
	$$('#next_we li').each(function(elmt){
		if(!elmt.visible()){
			Effect.Appear(elmt.id);
			Event.element(e).innerHTML='Voir moins de week-ends';
		}
		else{
			if(elmt.hasClassName('surNum')){
				Effect.Fade(elmt.id);
			}
			Event.element(e).innerHTML='Voir plus de week-ends';
		}
	})
	Event.stop(e);
}
function displayCal(e){
	var cal = new CalendarPopup('iCal');
	cal.showNavigationDropdowns();
	cal.setWeekStartDay(1);
	cal.setYearSelectStartOffset(4);
	cal.showYearNavigation();
	cal.showNavigationDropdowns();
	var ical=Event.element(e).id;
	var format='dd-MM-yyyy';
	cal.select($(ical),ical,format);
	Event.stop(e);
}

function confirmResa(e){
	var params=$H($('resaCalculate').serialize(true));
	params.update({mdl:'we',action:'recordResa'});
	if(Event.element(e).nodeName.toLowerCase()=='a'){
		params.update({sendSimpleResa:'true'});
	}
	new Ajax.Request('index.php',{method:'post',parameters:params.toObject(),evalScripts:true,
		onCreate:function(){$('resaLoader').show();},
		onSuccess:function(transport){
			$('resaLoader').hide();
			if (transport.responseText.match(/callSips/)){
				 $('sendBooking').hide();
				 if($('error')){
					$('error').hide();
				 }
			     $('resaPay').update(transport.responseText);
			}
			else{
				 $('resaCoord').update(transport.responseText);
			}
		}
		});
	Event.stop(e);
}
function changeFlightInbound(e){
	var params=$H($('resaCalculate').serialize(true));
	params.update({mdl:'we',action:'getResaDetails'});
	new Ajax.Updater('we_details_content','index.php',{method:'post',parameters:params.toObject(),	evalScripts:true,
	onCreate:function(){
		$('resaProgress').show();
		},
	onSuccess:function(){
		Effect.Fade('resaProgress');
		}
	});
}

