PageFront = Class.create(Page, {

	onload: function()
	{
		this.execute(this.action);
	},


	execute: function(action)
	{
		this.preExecute();
		switch(action)
		{
			case 'accueil':
				this.executeAccueil();
			break;

			case 'ac-etape1':
				this.executeAcEtape1();
			break;

			case 'cd-etape1':
				this.executeAcEtape1();
				this.executeCdEtape1();
			break;

			case 'ac-etape2':
				this.executeAcEtape2();
			break;

			case 'ac-etape4':
				this.executeAcEtape4();
			break;

			default:
			break;
		}
	},

	preExecute: function()
	{

	},

	executeAccueil: function()
	{

	},

	executeAcEtape1: function()
	{
		Event.observe(document.body, 'unload', function(event)
		{
			GUnload();
		});

		if (GBrowserIsCompatible()) {
			this.pMap = new GMap2($('prise_map'));
			this.pMap.setCenter(new GLatLng(48.8566667, 2.3509871), 13);
			this.pMap.addControl(new GLargeMapControl());

			this.rMap = new GMap2(document.getElementById("remise_map"));
			this.rMap.setCenter(new GLatLng(48.8566667, 2.3509871), 13);
			this.rMap.addControl(new GLargeMapControl());
		}

		var pCal = 
		Calendar.setup({
			inputField	: "calendar-field",
			trigger		: "calendar-trigger",
			min			: this.options.minDate,
			weekNumbers	: false,
			showTime		: 24,
			minuteStep	: 15,
			onSelect		: function()
			{
				this.hide();
				$('hour-field').value = Page.instance.fixTime(this.getHours());
				$('min-field').value = Page.instance.fixTime(this.getMinutes());
			},
			onTimeChange  : function()
			{
				$('hour-field').value = Page.instance.fixTime(this.getHours());
				$('min-field').value = Page.instance.fixTime(this.getMinutes());
			}
		});

		var rCal = 
		Calendar.setup({
			inputField	: "rcalendar-field",
			trigger		: "rcalendar-trigger",
			min			: this.options.minDate,
			weekNumbers	: false,
			showTime		: 24,
			minuteStep	: 15,
			onSelect		: function() {
				this.hide()
				$('rhour-field').value = Page.instance.fixTime(this.getHours());
				$('rmin-field').value = Page.instance.fixTime(this.getMinutes());
			},
			onTimeChange  : function() {
				$('rhour-field').value = Page.instance.fixTime(this.getHours());
				$('rmin-field').value = Page.instance.fixTime(this.getMinutes());
			}
		});

		this.fixTime = function(t)
		{
			if (t < 10) t = "0" + t;
			return t
		};

		this.updateLocation = function(location)
		{
			//$(location.tville).update(location.ville);
			map = $H(this).get(location.map);
			map.clearOverlays();
			point = new GLatLng(location.lat,
								location.lng);
			marker = new GMarker(point);
			map.addOverlay(marker);
			map.panTo(point);
		};

		$("prise").observe('change',function(selectEvent)
		{
				new AjaxRequest(this.options.priseCBAjaxUrl, {
					parameters:'ville='+$F('prise')
				});
		}.bind(this));

		$("remise").observe('change',function(selectEvent)
		{
				new AjaxRequest(this.options.remiseCBAjaxUrl, {
					parameters:'ville='+$F('remise')
				});
		}.bind(this));

		if(this.options.priseVille)
		{
			new AjaxRequest(this.options.priseCBAjaxUrl, {parameters:'ville='+this.options.priseVille});
		};

		this.viewAccess = function(url,title)
		{
			jsWin.instance().setOptions({modal:true,width:500,height:400});
			jsWin.instance().setTitle(decodeURIComponent(title.replace(/\+/g,  " ")));
			jsWin.instance().setContent('<img src="'
										+url
										+'" />'
										);
			jsWin.instance().show();
		};
	},

	executeAcEtape2: function()
	{
		$$('input[name="submit"]').each(function (event)
		{
			event.observe('click',function(selectEvent)
			{
				$('categorie').value = event.up('.single').id;
				$('modele').value = event.id;
			});
		});

		$$('.show-more a').each(function (event)
		{
			event.observe('click',function(selectEvent)
			{
				event.toggleClassName('current');
				event.up().next('table').toggle();
				if(event.hasClassName('current'))
				{
					if(!Prototype.Browser.IE)
					{
						Effect.ScrollTo(event.up().previous('h2'));
					}
				}
			});
		});

		/*$$('.car img').each(function (event)
		{
			event.observe('mouseenter',function(selectEvent)
			{
				event.next('a').show();
			});
			event.observe('mouseleave',function(selectEvent)
			{
				event.next('a').hide();
			});
		});*/

		this.currentGallery = 0;

		this.getGallery = function(a)
		{
			if(this.currentGallery != a)
			{
				new AjaxRequest(this.options.galleryAjaxUrl, {parameters:'id='+a});
			}
			else
			{
				this.showGallery(a,true);
			}
		}.bind(this);

		this.showGallery = function(a,loaded)
		{
			if(!loaded)
			{
				this.currentGallery = a;
				this.gallery = new k3Gallery("ulGallery",{jswinOptions:{zIndex:1600,modal:true,useDefaultEffects:true,clearContentOnClose:true},carousel:false});
			}
			this.gallery.showJsWin($('ulGallery').firstDescendant());
		}.bind(this);
	},

	executeAcEtape4: function()
	{
		if($('valTotal'))
		{
			$('valTotal').observe('keyup', function(event)
			{
				if(event.target.value.replace(',','.').gsub(' ','') >= this.options.valMin)
				{
					$('msgFeedback').update(this.options.msgValid).writeAttribute('class' ,'valid');
				}
				else
				{
					$('msgFeedback').update(this.options.msgInvalid).writeAttribute('class' ,'error');
				}
			}.bind(this));
		}
	},

	executeCdEtape1: function()
	{
		$('destination').observe('change',function(selectEvent)
		{
			new AjaxRequest(this.options.destinationCBAjaxUrl, {parameters:'pays='+$F('destination')});
		}.bind(this));

		if(this.options.resetCBAjaxUrl)
		{
			new AjaxRequest(this.options.resetCBAjaxUrl);
		};

		this.getSuccursaleInfo = function(a)
		{
			if(a == 'prise')
			{
				new AjaxRequest(this.options.priseCBAjaxUrl, {parameters:'ville='+$F('prise')});
			}
			else
			{
				new AjaxRequest(this.options.remiseCBAjaxUrl, {parameters:'ville='+$F('remise')});
			}
		}.bind(this);
	}
});

