/*
 * Site Internet AP CONCEPT
 * Classe de gestion des frames et elements du site Internet
 * Version 0.1
 *
 * (c) 2010 Antoni Paligot / AP CONCEPT
 * All Rights Reserved
 * For further informations : contact us support@apconcept.net
 */
var ApcWebkit = {
	
	version : '0.1',
	
	// Si le menu est deploye
	isOverMenu: false,
	// ID des menus ouvert en cours
	isMenuOpen: [],
	timerMenu: [],
	
	nzLayer: null,
	
	initialize: function()
		{
			this.initNews();
			if($('newbudgLayer'))
				{
					var ba = new BodySlide('newbudgLayer',{autoplay:true});
				}
		},
	
	
	/*
	 * Affiche ou cache le menu déroulé au survol d'un des 4 items
	 */
	switchMenu: function(id)
		{
			if (!this.isMenuOpen[id] && $('smenu_' + id)) {
				//$('smenu_' + id).style.display = "block";
				Effect.Appear('smenu_' + id, {duration:0.5});
				$('menu' + id).className = "over";
			}
			if (typeof this.timerMenu[id] != "undefined") 
				clearTimeout(this.timerMenu[id]);
			this.isOverMenu = true;			

			
		},
	
	// (private)
	hideMenu: function(id)
		{
			if($('smenu_' + id)){
				this.isOverMenu = false;
				this.timerMenu[id] = setTimeout('doHideMenu(' +id + ')', 100);
			}		
		},
	
	doHideMenu: function(id)
		{
			clearTimeout(this.timerMenu[id]);
			//$('smenu_' + id).style.display = "none";
			$('smenu_' + id).fade({duration:0.3});
			$('menu' + id).className = "";	
			this.isMenuOpen[id] = false;
		},
	
	/*
	 * Affiche les news
	 */
	initNews: function()
		{
			
			this.nzLayer = new ulScroller('layer_on_left');
			
		},
	
	/*
	 * Initialise un slider
	 */
	 
	initBigSlide: function(id)
		{
			var ba = new BodySlide(id,{autoplay:false});
		}
	
}
function doHideMenu(id)
{
	window.ApcWebkit.doHideMenu(id);
}

/*
 * BodySlide
 * Gère l'affichage de panneaux dans la première partie du body
 * a la manière d'un slider horizontal
 *
 * Initialisation : 
 * id : 	paramètre obligatoire (string)
 *			id de la layer dontenant les liste a scroller
 * options: optionnel (object)
 *			Partamètre du slider.
 *			Options disponibles : 
 *			width : la largeur du slider (un affichage) en pixels (int)
 *			speed : la vitesse de défilement en secondes (int)
 *			autoplay : si l'aniation est jourée (boolean)
 */
function BodySlide(id,opts)
	{
		id: null,
		this.initialize(id,opts);
	}

BodySlide.prototype = {
	
	
	initialize:function(id,opts)
		{
			
			this.ly= [];
			this.mn= [];
			this.current = 0;
			this.inTransition= false;
			this.tm=null;
			this.speed=10;
			this.slWidth=700;
			this.autoplay=true;
			
			var options = opts || {};
			this.id = id;
			this.slWidth = options.width || 700;
			this.speed = options.speed || 10;
			this.autoplay = (options.autoplay === false) ? false : true;
			$$('#'+this.id+' li.item').each(function(e,i){
					
					var left = i*this.slWidth;
					e.setStyle({'left':left+'px'});
					this.ly.push(e);
					
				}.bind(this));
			$$('#'+this.id+' .menu .itm').each(this.clickhandler.bind(this));	
			$$('#'+this.id+' .menu .prev').each(function(o){ o.onclick = this.prev.bind(this); }.bind(this));
			$$('#'+this.id+' .menu .next').each(function(o){ o.onclick = this.next.bind(this); }.bind(this));
			if(this.mn[0])
				this.mn[0].addClassName('over');
			if(this.autoplay === true)
				this.tm = window.setTimeout(this.next.bind(this), (this.speed*1000));
			
		},
	
	clickhandler:function(o,i)
		{
			o.onmouseover = function(){ this.moveTo(i); }.bind(this);
			this.mn.push(o);	
		},
	
	next: function()
		{
			var next = this.current + 1;
			if(next >= this.ly.length)
				next = 0;
			this.moveTo(next);
		},
	
	prev: function()
		{
			var next = this.current - 1;
			if(next < 0)
				next = (this.ly.length-1);
			this.moveTo(next);
		},
	
	moveTo: function(i)
		{
			
			if(i == this.current || this.inTransition == true)
				return;
				
			
			var decal;
			var diff = this.current - i;
			var _curent = this.current;
			
			decal = this.slWidth*diff;
			
			this.current = i;
			new Effect.Parallel((function(){
					var tab = [];
					this.ly.each(function(obj,i) {
					  tab.push(new Effect.Move(obj, { x: decal, sync: true }));
					});
					return tab;
				  }.bind(this))(), {
					beforeStart : function() { // Fonction de "callBefore"
						this.inTransition = true;
					}.bind(this),
					afterFinish : function() { // Fonction de "callBack"
						this.inTransition = false;
						this.mn[_curent].removeClassName('over');
						this.mn[i].addClassName('over');
					}.bind(this)
				  });
			
			
			if(this.tm)
				window.clearTimeout(this.tm);
			if(this.autoplay === true)
				this.tm = window.setTimeout(this.next.bind(this), (this.speed*1000));
			
		}
	
};


/*
 * ulScroller
 * Fait défiler des élements li comme des news.
 */
function ulScroller(id)
	{
		this.initialize(id);
	}

ulScroller.prototype = {
	
	// Layer dcontenant les UL a scroller
	layerId: null,
	// Liste des elements
	uls: [],
	// Temps de transition
	speed: 5,
	// En lecture ?
	animate: false,
	// L'interval de temps
	int: null,
	tm: null,
	nid:1,
	initialize: function(id)
		{
			this.layerId = id;
			$$('#'+this.layerId+' .newsitem').each(this.addUl.bind(this));
			this.play();
		},
		
	addUl: function(o,i,z)
		{
			o.style.display = 'none';
			this.uls.push(o);
		},
	
	play: function()
		{
			/* Handlers */
			if(this.uls.length > 1)
				{
					this.animate = true;
					$$('#'+this.layerId+' .newsitem').each(this.setItemHandlers.bind(this));
					$$('#'+this.layerId+' .play').each(function(o){ o.className = 'pause'; })
					$$('#'+this.layerId+' .pause').each(this.setPlayHandler.bind(this));
					$$('#'+this.layerId+' .prev').each(this.setPrevHandler.bind(this));
					$$('#'+this.layerId+' .next').each(this.setNextHandler.bind(this));
				}
			this.nextscroll();
		},
	
	setPrevHandler: function(o)
		{
			o.onclick = this.prevscroll.bind(this);
		},
		
	setNextHandler: function(o)
		{
			o.onclick = this.nextscroll.bind(this);
		},
	
	setItemHandlers: function(o)
		{
			o.onmouseover =  this.pause.bind(this);
			o.onmouseout = this.restartHandler.bind(this);
		},
	
	setPlayHandler: function(o)
		{
			o.onclick = this.pause.bind(this);
		},
	
	setPauseHandler: function(o)
		{
			o.onclick = this.play.bind(this);
		},
	
	setScroll: function()	
		{
			/*if(this.int !== null)
				window.clearInterval( this.int );
			this.int = setInterval(this.nextscroll.bind(this),(this.speed*1000));
			$$('#'+this.layerId+' .play').each(function(o){ o.className = 'pause'; })*/
			
		},
	
	scroll: function()
		{
			Effect.Appear(this.uls[0]);
			this.txt(this.nid+'/'+this.uls.length);
			//Effect.SlideDown(this.uls[0], {scaleX:true,scaleY:false});
		},
	
	nextscroll: function()
		{
			
			var _ft = this.uls[0];
			//Effect.BlindUp(_ft);
			_ft.style.display = 'none';
			this.uls.shift();
			this.uls.push(_ft);
			this.nid++;
			if(this.nid > this.uls.length)
				this.nid = 1;
			this.scroll();
			
			if(this.uls.length > 1 && this.animate === true)
				{
					if(this.tm)
						window.clearTimeout(this.tm);
					this.tm = window.setTimeout(this.nextscroll.bind(this), (this.speed*1000));
				}
				
		},
	
	prevscroll: function()
		{
			
			var _ft = this.uls[(this.uls.length - 1)];
			//Effect.BlindUp(_ft);
			this.uls[0].style.display = 'none';
			this.uls.pop();
			this.uls.unshift(_ft);
			this.nid--;
			if(this.nid < 1)
				this.nid = this.uls.length;
			this.scroll();
			if(this.uls.length > 1 && this.animate === true)
				{
					if(this.tm)
						window.clearTimeout(this.tm);
					this.tm = window.setTimeout(this.nextscroll.bind(this), (this.speed*1000));
				}
		},
	
	pause: function()
		{
			//new Effect.Highlight(this.uls[0]);
			this.animate = false;
			if(this.tm)
				{
					window.clearTimeout(this.tm);
					this.tm = null;
				}
			$$('#'+this.layerId+' .pause').each(function(o){ o.className = 'play'; })
			$$('#'+this.layerId+' .play').each(this.setPauseHandler.bind(this));
			/*window.clearInterval( this.int );*/
		},
	
	restartHandler: function()
		{
			this.animate = true;
			if(this.tm)
				window.clearTimeout(this.tm);
			this.tm = window.setTimeout(this.restart.bind(this), 100);
		},
	
	restart: function()
		{
			//alert(this.animate);
			if(this.animate !== true)
				return;
			$$('#'+this.layerId+' .play').each(function(o){ o.className = 'pause'; })
			$$('#'+this.layerId+' .pause').each(this.setPlayHandler.bind(this));
			//this.setScroll();
			this.nextscroll();	
		},
	
	txt: function(txt)
		{
			$$('#'+this.layerId+' .nb').each(function(o){ o.innerHTML = txt; });
		},
		
}

/*
 * Scripts de la page de contact
 */
ApcWebkit.contact = {
	
	errNum:0,
	
	openBar: function()
		{
			Effect.BlindDown('formctl',{
				});
			
		},
	
	sendForm: function()
		{
			this.errNum = 0;
			var vals = xajax.getFormValues('fcontact')
			 
			if(!vals['nom'])
				this.err('Veuillez nous indiquer votre nom','nom');
			if(!vals['email'])
				this.err('Veuillez nous indiquer votre email','email');
			if(!vals['msg'])
				this.err('Veuillez nous indiquer votre message','msg');
			
			if(this.errNum > 0)
				return;
			
			this.wait();
			xajax_send(vals);
		},
	
	err: function(msg, input)
		{
			
			this.ready();
			$('fcpmt').innerHTML = msg;
			this.openPrompt();
			$('input-'+input).addClassName('err');
			this.errNum++;
			
		},
	
	sok: function(msg)
		{
			this.ready();
			$('fcpmt').innerHTML = msg;
			this.openPrompt();
			this.clearForm();
		},
	
	wait: function()
		{
			//Effect.Opacity('fcontact', {from:1.0, to:0.3, duration:0.3});
			Effect.Fade('fcontact', {from:1.0, to:0.3, duration:0.2});
			$('pload').style.display = 'block';
			
		},
	
	ready: function()
		{
			Effect.Appear('fcontact', {from:0.3, to:1, duration:0.2});
			$('pload').style.display = 'none';
		},
	
	clearForm: function()
		{
			$('input-nom').value = '';
			$('input-societe').value = '';
			$('input-email').value = '';
			$('input-msg').value = '';
		},
	
	closePrompt: function()
		{
			
			Effect.Appear('fcontact', {from:0.3, to:1, duration:0.2});
			if($('fcpmtl').style.display !== 'none')
				$('fcpmtl').style.display = 'none';
				//Effect.BlindUp( 'fcpmtl', {duration:0.3} );
			
		},
	
	openPrompt: function()
		{
			Effect.Fade('fcontact', {from:1.0, to:0.3, duration:0.2});
			if($('fcpmtl').style.display === 'none')
				Effect.Appear( 'fcpmtl', {duration:0.3} );
				//Effect.BlindDown( 'fcpmtl', {duration:0.3} );
		}
	
}

/*
 * Page d'accueil
 */
ApcWebkit.home = {
	
	isOver:null,
	
	initSlide: function(id)
		{
			new BodySlide(id,{autoplay:false,width:700});
			$$('.tiers').each(  this.onHomeMouseOver.bind(this) );
		},
	
	onHomeMouseOver: function(o,i)
		{
			o.onmouseover = function(event){ 
				//Effect.Fade(this);
				if(ApcWebkit.home.isOver === this)
					return;
				if(ApcWebkit.home.isOver !== null)
					ApcWebkit.home.isOver.morph( 'background:#fff;');
					
				ApcWebkit.home.isOver = this;
				this.morph( 'background:#D6DAE0;',{duration:0.3});

			}
			
			o.onmouseout = function(event){ 
				//Effect.Fade(this);
				if(ApcWebkit.home.isOver === this)
					return;
					
				ApcWebkit.home.isOver = null;
				alert('fadeout');
				this.morph( 'background:#fff;');
			}
		}

}


/*
 * Pages Internet
 */
ApcWebkit.it = {
	
}

/*
 * Back-office
 */
ApcWebkit.it.bo = {
	
	initSlide: function(id)
		{
			new BodySlide(id,{autoplay:false,width:700});
			$$('.boexp .exp').each(function(o,i){ if(i > 0) o.style.display = 'none'; });
		},
	
	swExp: function(id)
		{
			$$('.boexp a.fll').each(function(o,i){ o.removeClassName('over'); });
			$$('.boexp .exp').each(function(o,i){ o.style.display = 'none'; });
			$('b'+id).addClassName('over');
			Effect.Appear(id);
		}
	
}

document.observe('dom:loaded', function () {
	try{
		ApcWebkit.initialize();
	} catch(e){}
});
