/*
 * interfaceEngine.js
 * (c) IPCOMM 2010 - Luc L. (luc.lerot@ipcomm.fr)
 *
 * Class List :
 * ------------
 *		SLIDER : used to handle sliding objects
 *		AJAX-REQUEST : used to handle ajax requests
 */
window.addEvent('domready', function() {


	/* SLIDER CLASS
	 * Handling of sliding events within a CSS class */
	$$('a.slider').each(function(el) {

		//We hide all the slides by default
		if (!el.get('show')) {
			new Fx.Slide(el.get('idDivSlide')).hide ();
		}
		else {
			new Fx.Slide(el.get('idDivSlide')).hide ();
			new Fx.Slide(el.get('idDivSlide')).toggle();
		}

		//onClick, show/hides the slide automatically
		el.addEvent('click', function(event) {

			//Prevent the page from changing
			event.stop ();

			//If we got a value as HREF, we go to the location
			if (el.get('href') != "#") {
				document.location = el.get('href');
			}

			//Start Effects only if we got a slide to toggle
			if (el.get('idDivSlide')) {
				new Fx.Slide(el.get('idDivSlide')).toggle();
			}
		});
	});




	/* AUTO-HIDE CLASS
	 * Handling of auto-hide properties within a CSS class */
	 $$('div.autohideok').each(function(el) {

	 	new Fx.Tween($(el),{
			duration:2000,
			onComplete : function () {
				new Fx.Slide($(el),{
					duration:1000,
					}).slideOut();
			}
		}).start('background-color', '#6CFB80');

	 });


	 $$('div.autohideko').each(function(el) {

	 	new Fx.Tween($(el),{
			duration:2000,
			onComplete : function () {
				new Fx.Slide($(el),{
					duration:1000,
					}).slideOut();
			}
		}).start('background-color', '#fb6c6c');

	 });



	/* AJAX REQUEST CLASS
	 * Handling of ajax request within a CSS class
	 *
	 * Note : a "loading" div can be used (param loading=1 in markup)
	 */

	 $$('a.ajax-request').each(function (el) {
		el.addEvent('click', function(event) {

			//Prevent the page from changing
			event.stop();
			//Make the ajax call + update targeted div with content (only if we got the div id to update)
			if (el.get('idDivUpdate')) {
				var req = new Request.HTML({
					method: 'get',
					url: el.get('href'),
					onRequest: function() {
						//Start fading effect on div to update
						$(el.get('idDivUpdate')).setStyles({'opacity' : 1, 'visibility' : 'hidden'}).fade('out');

						//Start fading effect
						if (el.get('idDivLoading'))
							$(el.get('idDivLoading')).setStyles({'opacity' : 0, 'visibility' : 'visible'}).fade('in');
					},
					update: el.get('idDivUpdate'),
					onComplete: function(response) {
						//Start fading effect on idv to update
						$(el.get('idDivUpdate')).setStyles({'opacity' : 0, 'visibility' : 'visible'}).fade('in');

						//Start fading effect
						if (el.get('idDivLoading'))
							$(el.get('idDivLoading')).setStyles({'opacity' : 1, 'visibility' : 'hidden'}).fade('out');
					}
				}).send();
			}
		});
	 });







});//domready