// Libraire javscript pour le moteur Week-end & Séjour de Vacances.com
var moteursejour = 
{
	//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
	//Prefix des id des options de theme
	thmPrefixId : "othm",
	// Id de thème spécifique au location de vacances
	thmLocId : "99",

	//pointe sur l'objet (select) en cours des themes.
	thm_online : null, 
	//pointe sur l'objet (select) complet des themes.
	thm_full : null, 

	//stock le select en cours des zones.
	zon_online : null, 
	//stock le select complet des zones.
	zon_full : null, 
	
	//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
	// Initialisation des actions dès que possible (plus rapide que window.onload qui peut-être bloqué par une resource qui tarde)
	asapTimer : null,
	asapObjectId : ["rechsejour", "rechsejour_thm_id", "rechsejour_zon_id"],
	asapObject : [],
	asapLoop : 0,
	asapLoad : function()
	{
		var isis = moteursejour;

		if( null != isis.asapTimer )
		{
			window.clearTimeout(isis.asapTimer);
			isis.asapTimer = null;
		}
		
		var loaded = true;
		isis.asapLoop++;
		
		for( var i in isis.asapObjectId )
		{
			isis.asapObject[isis.asapObjectId[i]] = document.getElementById(isis.asapObjectId[i]);
			loaded = loaded && ( null != isis.asapObject[isis.asapObjectId[i]] );
		}

		if( loaded )
		{
			domtools.event.addListener(isis.asapObject["rechsejour"], "submit", isis.onsubmit);
			
			isis.thm_online = isis.asapObject["rechsejour_thm_id"];
			domtools.className.remove(isis.thm_online,"hiddenbeforeloaded");
			isis.thm_full = isis.thm_online.cloneNode(true);
		
			
			domtools.event.addListener(isis.thm_online, "change", isis.onchangeTheme);

			isis.zon_online = isis.asapObject["rechsejour_zon_id"];
			domtools.className.remove(isis.zon_online,"hiddenbeforeloaded");
			isis.zon_full = isis.zon_online.cloneNode(true);
			
			domtools.event.addListener(isis.zon_online, "change", isis.onchangeZone);

			isis.changeTheme();
			isis.changeZone();
			
			domtools.className.remove(document.getElementById("chercher"),"hiddenbeforeloaded");
						
			//I done my job, I must prevent for no other call, good bye cruel world...
			isis.asapLoad = function(){};
		}
		else
		{
			 isis.asapTimer = window.setTimeout(isis.asapLoad,100);
		}
	},

	//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
	// theme change handle event 
	onchangeTheme : function (e)
	{
		moteursejour.changeTheme();
	},
	
	//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
	// theme change action
	changeTheme : function ()
	{
		var isis = moteursejour;
		var s = isis.thm_online;
		if( -1 == s.selectedIndex)
		{
			s.selectedIndex = 0;
		}
		var o = s.options[s.selectedIndex];
		var thmId = o.id.substring(isis.thmPrefixId.length);
		if( -1 == isis.zon_online.selectedIndex)
		{
			isis.zon_online.selectedIndex = 0;
		}
		var cur_sel_zone_val = isis.zon_online.options[isis.zon_online.selectedIndex].value;
		var themes, opt, new_sel_zones = isis.zon_full.cloneNode(true);
		for( var i = new_sel_zones.options.length-1; i >= 1; i-- )
		{
			opt = new_sel_zones.options[i],
			themes = opt.getAttribute("vac:themes");
			if( 	'1' != thmId  
					&& 
					(
						!themes 
						||
						( 
							( -1 == themes.indexOf('['+thmId+']')) 
							&& 
							( -1 == themes.indexOf('['+thmId+'.')) 
						)
					) 
				)
			{
				opt.parentNode.removeChild(opt);
				delete opt;
			}
			else if (cur_sel_zone_val == opt.value)
			{
				opt.selected = true;
			}
		}
		isis.zon_online.parentNode.replaceChild(new_sel_zones, isis.zon_online);
		domtools.event.removeListener(isis.zon_online, "change", isis.onchangeZone);
		delete isis.zon_online;
		isis.zon_online = new_sel_zones;
		domtools.event.addListener(isis.zon_online, "change", isis.onchangeZone);
	},
	
	//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
	// zone change handle event
	onchangeZone : function (e)
	{
		moteursejour.changeZone();
	},

	//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
	// zone change action 
	changeZone : function ()
	{
		var isis = moteursejour;
		var s = isis.zon_online;
		if( -1 == s.selectedIndex)
		{
			s.selectedIndex = 0;
		}
		var o = s.options[s.selectedIndex];
		if( -1 == isis.thm_online.selectedIndex)
		{
			isis.thm_online.selectedIndex = 0;
		}
		var cur_sel_theme_id = isis.thm_online.options[isis.thm_online.selectedIndex].id;
		var themes = o.getAttribute("vac:themes");
		var thmId, opt, new_sel_themes = isis.thm_full.cloneNode(true);
		for( var i = new_sel_themes.options.length-1; i >= 1; i-- )
		{
			opt = new_sel_themes.options[i],
			thmId = opt.id.substring(isis.thmPrefixId.length);
			if( 	'1' != thmId && o.value.length > 0 
					&& 
					(
						!themes 
						||
						( 
							( -1 == themes.indexOf('['+thmId+']')) 
							&&
							( -1 == themes.indexOf('['+thmId+'.')) 
						)
					) 
				)
			{
				opt.parentNode.removeChild(opt);
				delete opt;
			}
			else if (cur_sel_theme_id == opt.id)
			{
				opt.selected = true;
			}
		}
		isis.thm_online.parentNode.replaceChild(new_sel_themes, isis.thm_online);
		domtools.event.removeListener(isis.thm_online, "change", isis.onchangeTheme);
		delete isis.thm_online;
		isis.thm_online = new_sel_themes;
		domtools.event.addListener(isis.thm_online, "change", isis.onchangeTheme);
	},

	//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
	//
	onsubmit : function (e)
	{
		var isis = moteursejour;
		var loc = "";
		
		//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
		var targetserver = e.target.elements["targetserver"];
		if( "undefined" != typeof targetserver )
		{
			loc = targetserver.value;
			if( loc.length-1 != loc.lastIndexOf("/")  )
			{
				loc += "/";
			}
		}
		else
		{
			loc = "http://www.vacances.com/";
		}

		//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
		var rooturi = "/";
		
		//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
		var o = isis.thm_online.options[isis.thm_online.selectedIndex];
		var thmId = o.id.substring(isis.thmPrefixId.length);
		if( thmId != isis.thmLocId )
		{
			rooturi = e.target.elements["voyagerooturi"];
			if( "undefined" != typeof rooturi )
			{
				rooturi = rooturi.value;
			}
			else
			{
				rooturi = "/";
			}			
			rooturi += o.value + "/" + o.value;
		}
		
		
		var affilie = e.target.elements["affiliate"];
		if( "undefined" != typeof affilie )
		{
			loc += "porte-affilie.php?ref=" + affilie.value + "&url=";
			loc += rooturi;
		}
		else
		{
			loc += ( 0 == rooturi.indexOf("/") ) ? rooturi.substring(1) : rooturi;
		}

		o = isis.zon_online.options[isis.zon_online.selectedIndex];
		if( o.value.length > 0 )
		{
			if( thmId != isis.thmLocId )
			{
				loc += "-" + o.value;
			}
			else
			{
				themes = o.getAttribute("vac:themes");
				if( themes )
				{
					var pattern = '['+thmId+'.';
					var zonId = themes.substring(themes.indexOf(pattern) + pattern.length);
					zonId = zonId.substring(0,zonId.indexOf(']'));
					loc += "location-vacances.php?id_zone=" + zonId;
				}
			}
		}

		if( thmId != isis.thmLocId )
		{
			loc += "/";
		}
		
		e.target.method = "post";
		e.target.action = loc;
	},
	
	//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
	nop : function(){}

}
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
moteursejour.asapLoad();
