Ext.onReady(function(){
	//timeout needed to allow IE to load everything
	setTimeout(function(){
		//FOOTER REFLECTION
		this.resizeFooter = function() {
			this.windowHeight = 0;
			if (typeof(window.innerHeight) == 'number') {
				this.windowHeight = window.innerHeight;
			} else {
				if (document.documentElement && document.documentElement.clientHeight) {
					this.windowHeight = document.documentElement.clientHeight;
				} else if (document.body && document.body.clientHeight) {
					this.windowHeight = document.body.clientHeight;
				}
			}
			if((this.windowHeight-Ext.fly('main').getHeight()) > 0){
				Ext.get('footer').setHeight((this.windowHeight-Ext.fly('main').getHeight() + 48), false);
			}
		}();
		//Activate flash
		if (Ext.get('flashContent')) {
			var flashContent = {
				el: 	Ext.get('flashContent'),
				name:	'flashContent',
				anim:	'/swf/Anim.swf',
				x:		197,
				y:		546
			}
		} else if (Ext.get('flashContentN1')) {
			var flashContent = {
				el: 	Ext.get('flashContentN1'),
				name:	'flashContentN1',
				anim:	'/swf/Anim2.swf',
				x:		127,
				y:		546
			}
		}
		if (flashContent && swfobject) {
			flashContent.el.addClass('flashContentHidden'); //remove flash background
			swfobject.embedSWF(flashContent.anim, flashContent.name, flashContent.x, flashContent.y, "9.0.0","/swf/expressInstall.swf", false, {wmode: 'transparent'});
			flashContent.el = Ext.get(flashContent.name);
			if (flashContent.el.dom.tagName == 'DIV') { //no flash installed, place flash background
				flashContent.el.removeClass('flashContentHidden');
				flashContent.el.show();
			}
		}
		
		//HOME PAGE SLIDESHOW
		var popSlideShow = function(source, zoomId) {
			// activBtn: all navigation buttons are on slide, they need to reload function
			this.activeBtn=function(){
				this.lastButton = Ext.get('popLast');
				this.nextButton = Ext.get('popNext');
				this.lastButton.dom.style.background = 'url(/img/homePage/flecheGaucheOFF.gif) no-repeat top left;';
				this.lastButton.on({
					'click':this.getLast, 
					'mouseover': function(el) {
						this.lastButton.dom.style.background = 'url(/img/homePage/flecheGaucheON.gif) no-repeat top left;';
					}, 
					'mouseout':function(el){
						this.lastButton.dom.style.background = 'url(/img/homePage/flecheGaucheOFF.gif) no-repeat top left;';
					},
					scope: this
				});
				this.nextButton.dom.style.background = 'url(/img/homePage/flecheDroiteOFF.gif) no-repeat top left;';
				this.nextButton.on({
					'click':this.getNext, 
					'mouseover': function(el) {
						this.nextButton.dom.style.background = 'url(/img/homePage/flecheDroiteON.gif) no-repeat top left;';
					}, 
					'mouseout':function(el){
						this.nextButton.dom.style.background = 'url(/img/homePage/flecheDroiteOFF.gif) no-repeat top left;';
					},
					scope: this
				});
			}
			// getNext: define next slide and animation
			this.getNext = function() {
				this.lastButton.hide();
				this.nextButton.hide();
				if((this.arrayIndex+1) == zoomList.length){
					this.arrayIndex = 0;
				}else{
					this.arrayIndex++;
				}
				this.direction='right';
				this.getContent();
			};
			// getLast: define last slide and animation
			this.getLast = function() {
				this.lastButton.hide();
				this.nextButton.hide();
				if((this.arrayIndex-1) < 0){
					this.arrayIndex=zoomList.length-1;
				}else{
					this.arrayIndex--;
				}
				this.direction='left';
				this.getContent();
			};		
			// getContent: load xml data
			this.getContent = function(){
				this.xmlData=new Array('mmm', 'ppp');
				Ext.Ajax.request({
					url: document.location.href,
					success: this.parseXml,
					failure: function () {
						alert('Connection au serveur interrompue ... Veuillez rééssayer.');
					},
					params: { zoomId: zoomList[this.arrayIndex], out: 'xml' },
					scope:this
				});
			}
			// parseXml: select nodeValue
			this.parseXml = function(response){
				if (response.responseXML.getElementsByTagName('data').length) {
					this.createNextDiv(new Array(response.responseXML.getElementsByTagName('data').item(0).firstChild.nodeValue));
				}
			}
			// createNewDiv: remove all buttons, create new slide, start old slide effect 
			this.createNextDiv=function(xmlData){
				if(this.Wrunning) this.Wrunning.stopFx();
				if (this.lastButton) {
					this.lastButton.remove();
					this.nextButton.remove();
					this.Wrunning.slideOut(this.direction == 'left' ? 'r' : 'l', {callback:this.resizeNextSlide, scope:this, duration:.35, remove:true});
				}
				var nextDiv = new Ext.Element(document.createElement('div'));
				nextDiv.addClass('popWinContent');
				nextDiv.enableDisplayMode();
				nextDiv.update(xmlData);
				nextDiv.appendTo(Ext.get('popBody'));
				//get focus image to handle resize AFTER image is loaded (and size available)
				var img = Ext.query('.popFile>img', nextDiv.dom)[0];
				if (img) {
					Ext.get(img).on('load', function(){
						this.popWinContent = Ext.fly(Ext.query("div.popWinContent")[0]);
						Ext.get('popBody').scale(this.popWinContent.getWidth(), this.popWinContent.getHeight(), {scope:this, easing: 'easeOut',duration: .35});
					}, this);
				}
				this.Wrunning = nextDiv;
				if (!this.lastButton) {
					Ext.fly(Ext.query("div.popWinContent")[0]).remove();
					this.resizeNextSlide();
				}
			};
			// resizeNextSlide: resize win before start new slide effect
			this.resizeNextSlide = function(){
				this.popWinContent = Ext.fly(Ext.query("div.popWinContent")[0]);
				Ext.get('popBody').scale(this.popWinContent.getWidth(), this.popWinContent.getHeight(), {callback:this.animDiv, scope:this, easing: 'easeOut',duration: .35});
			}
			// animDiv: start new slide effect
			this.animDiv=function(){
				//Ext.fly('popBody').stopFx();
				this.Wrunning.slideIn(this.direction == 'left' ? 'l' : 'r', {callback: this.activeBtn, scope:this, duration:.35});
			}
			this.resize = function (){
				//this.popWinContent = Ext.fly(Ext.query("div.popWinContent")[0]);
				Ext.get('popBody').scale(this.popWinContent.getWidth(), this.popWinContent.getHeight(), {callback:this.fadeContent, scope:this, duration:0.35});
			}
			this.fadeContent = function (){
				//this.popWinContent=Ext.fly(Ext.query("div.popWinContent")[0]);
				Ext.get('popBody').setHeight(this.popWinContent.getHeight());
				this.popWinContent.fadeIn();
			}
			//init object if button is founded
			if (source) {
				this.popDiv = Ext.get("pop");
				this.popWin = Ext.get("popWin");
				this.popClose = Ext.get("popClose");
				this.popWinContent = Ext.get(Ext.query("div.popWinContent")[0]);
				this.lastButton = false;
				this.nextButton = false;
				this.arrayIndex = 0;
				this.Wrunning = false;
				this.direction = false;
				this.arrayIndex=0;
				this.Wrunning = Ext.get(Ext.query("div.popWinContent")[0]);
				this.popClose.on('click', function(e){
					e.stopEvent();
					this.popWin.stopFx().ghost('t', {callback:function() {
						this.popDiv.stopFx()
						this.popDiv.hide();
						this.popWinContent = Ext.fly(Ext.query("div.popWinContent")[0]);
						this.popWinContent.stopFx()
						this.popWinContent.setVisible(false);
					}, scope:this});
				}, this);
				//if no zoom id set, then put eent on source
				if (!zoomId) {
					source.on('click', function(e){
						e.stopEvent();
						this.popDiv.show();
						this.popWin.fadeIn({callback:this.resize, duration:0.5, scope:this});
					}, this);
					this.activeBtn();
				} else if(zoomId) { //show slideshow starting with ZoomId
					//set this.arrayIndex accordingly to zoomList position
					this.arrayIndex = zoomList.indexOf(parseInt(zoomId));
					this.popWin.stopFx()
					this.popDiv.show();
					this.getContent();
					this.popWin.fadeIn({duration:0.5, scope:this});
				}
			}
		}
		//auto start focus slideshow for home page
		if (Ext.get("getMoreLink")) {
			//alert('ok getMoreLink');
			new popSlideShow(Ext.get("getMoreLink"));
		}
		//HOME PAGE NEWS
		this.homePageNews = function() { 
			this.initFirstNews=function(){
				var first = Ext.get('sample-1');
				first.addClass('sample-item');
				first.radioClass('sample-item-over');
				var s=this.allnews[0];
				s.el = new Ext.Element(document.createElement('div'));
				s.el.addClass('newsSlide1');
				s.el.enableDisplayMode();
				this.divTpl.overwrite(s.el.dom, s[1]);
				s.el.appendTo(this.box);
				s.el.slideIn('t', {callback: this.clearRunning, duration:.25});
				this.active = s.el;
			};
			this.defineMouseOver=function(e, t){
				if(t = e.getTarget('.sample-item:not(.sample-item-over)')){
					Ext.fly(t).addClass('sample-inactive-over');
				}
			}
			this.defineMouseOut=function(e, t){
				if((t = e.getTarget('.sample-item:not(.sample-item-over)')) && !e.within(t, true)){
					Ext.fly(t).removeClass('sample-inactive-over');
				}
			}
			this.defineMouseDown=function(e, t){
				if(t = e.getTarget('.sample-item:not(.sample-item-over)')){
					if(this.running){
						this.running.stopFx();
					}
					Ext.fly(t).removeClass('sample-inactive-over');
					Ext.fly(t).radioClass('sample-item-over');
					var indexMenu=t.id.split('-')[1]
					var s = this.allnews[indexMenu-1];
					if(this.active){
						this.active.slideOut('b', {duration:.25, remove:true});
					}else{
						this.box.child('div').hide();
					}
					s.el = new Ext.Element(document.createElement('div'));
					s.el.addClass('newsSlide'+indexMenu);
					s.el.enableDisplayMode();
					this.divTpl.overwrite(s.el.dom, s[1]);
			   		s.el.appendTo(this.box);
					s.el.slideIn('t', {callback: this.clearRunning, duration:.25});
					this.running = s.el;
					this.active = s.el;
				}
			}
			this.clearRunning=function(){
				if(this.running){
					this.running.stopFx();
					this.running = null;
				}
			};
			//if no news content on this page, quit ...
			if (!Ext.fly('newsContent')) {
				return;
			}
			//define this vars
			this.allnews = false;
			this.box = false;
			this.running = false
			this.active = false
			this.allnews= new Array();
			this.menuTpl = new Ext.XTemplate(
				'<div id="newsList">',
					'<tpl for=".">',
						'<div class="sample-item" id="sample-{#}"><span>{0}</span></div>',
					'</tpl>',
				 '</div><div id="newsSlide"></div><div class="spacer"></div>'
			);
			this.divTpl = new Ext.XTemplate(
				'<tpl for=".">',
						'<div>{.}</div>',
				'</tpl>'
			);
			//grab all news divs
			var newDiv=Ext.query("li>div");
			var newA=Ext.query("ul.ulNews>li>a");
			for(var i = 0; i < newDiv.length; i++) {
				var divEl = Ext.get(newDiv[i]);
				var aEl = Ext.get(newA[i]);
				this.allnews.push(new Array(aEl.dom.innerHTML, divEl.dom.innerHTML));
			}
			this.menuTpl.overwrite('newsContent', this.allnews);
			var menu = Ext.query('div#newsList>div');
			for(var i = 0; i < menu.length; i++) {
				Ext.get(menu[i]).on({'mouseover': this.defineMouseOver, 'mouseout': this.defineMouseOut, 'click':this.defineMouseDown, scope: this});
			}
			this.box = Ext.get('newsSlide');
			this.running = null;
			this.active = this.box.child('newsSlide');
			this.initFirstNews();
		}();
		//REFERENCES
		if (Ext.get('clientsResults')) {
			this.references = function() {
				this.sequenceDisplayLi = function() {
					if (this.displayedLiOffset < this.lis.getCount()) {
						this.lis.item(this.displayedLiOffset).slideIn('t', {
							duration:	0.2,
							callback:	function(){
								this.displayedLiOffset++;
								this.sequenceDisplayLi()
							},
							scope:		this
						})
					} else {
						this.clientsNext = Ext.get('clientsNext');
						this.clientsPrev = Ext.get('clientsPrev');
						if (this.clientsNext) {
							this.clientsNext.on('click', this.getPage, this);
						}
						if (this.clientsPrev) {
							this.clientsPrev.on('click', this.getPage, this);
						}
					}
				}
				this.sequenceHideLi = function(callback) {
					if (this.maskedLiOffset >= 0) {
						this.lis.item(this.maskedLiOffset).slideOut('b', {
							duration:	0.1,
							callback:	function(){
								this.maskedLiOffset--;
								this.sequenceHideLi(callback)
							},
							scope:		this
						})
					} else {
						callback.createDelegate(this)();
					}
				}
				this.getPage = function(e, el) {
					e.stopEvent();
					var a = Ext.get(el).findParent('a', 3, true);
					if (a) {
						//hide buttons
						if (this.clientsNext) {
							this.clientsNext.hide();
						}
						if (this.clientsPrev) {
							this.clientsPrev.hide();
						}
						//get new page href
						var href = a.dom.href + '&out=xml';
						//hide lis
						this.maskedLiOffset = this.lis.getCount() - 1;
						this.sequenceHideLi(function(){
							//get new page datas
							Ext.Ajax.request({
								url: href,
								success: this.parseXml,
								failure: function () {
									alert('Connection au serveur interrompue ... Veuillez rééssayer.');
								},
								scope:this
							});
						});
					}
					
				}
				this.parseXml = function(response){
					if (response.responseXML.getElementsByTagName('data').length) {
						this.changePage(response.responseXML.getElementsByTagName('data').item(0).firstChild.nodeValue);
					}
				}
				this.changePage = function(datas) {
					//display new page datas
					this.setTitle();
					Ext.get('clientsResults').setWidth(154, {
						duration:	0.5,
						callback:	function(){
							//Ext.get('clientsResults').dom.innerHTML = datas;
							Ext.get('clientsResults').update(datas, true);
							this.start(false);
						}, 
						scope:this}
					);
				}
				this.showH2 = function(e, h2) {
					h2 = Ext.get(h2);
					//get the following ul
					var ul = h2.next('ul');
					if (ul.isDisplayed()) {
						return;
					}
					this.uls.each(function(ul){
						if (ul.isDisplayed()) {
							ul.slideOut('t');
						}
					});
					ul.slideIn('t');
				}
				this.start = function(start){
					this.lis = Ext.select('li.zoom', true, 'clientsResults');
					if (!this.lis.getCount()) {
						//all references mode, get all uls for references
						this.uls = Ext.select('ul.reflist', true, 'clientsResults');
						this.uls.setVisibilityMode(Ext.Element.DISPLAY);
						this.h2s = Ext.select('h2', true, 'clientsResults');
						this.h2s.on('click', this.showH2, this);
						this.showH2('', this.h2s.first());
						return;
					}
					this.displayedLiOffset = 0;
					if (start) {
						Ext.get('clientsResults').hide();
						Ext.get('clientsResults').setWidth(154);
					}
					this.lis.on({
						'mouseover':function(e, el){
							e.stopEvent();
							if (el.tagName.toLowerCase() != 'img') {
								return;
							}
							this.lis.each(function(el) {
								if (el.child('img')) {
									el.child('img').setOpacity(1);
								}
							});
							el = Ext.get(el);
							el.setOpacity(0.6);
							this.setTitle(el.findParent('li', 4, true).child('.refTitle').dom.innerHTML);
						},
						'click':function(e, el){
							e.stopEvent();
							el = Ext.get(el);
							var li = el.findParent('li',5,true);
							if (li && li.id.indexOf('zoom-') === 0) {
								var zoomId = li.id.substr(5);
								new popSlideShow(el, zoomId);
							}
						},
						scope:this
					});
					this.lis.setVisibilityMode(Ext.Element.DISPLAY);
					this.lis.hide();
					var width = this.lis.getCount() * 79 + ((Ext.isIE6) ? 70 : 75);
					Ext.get('clientsResults').fadeIn().pause(0.5).setWidth(width, {
						duration:	1,
						callback:	this.sequenceDisplayLi,
						scope:		this
					});
				}
				this.setTitle = function(title) {
					var h3 = Ext.get('clientFocusH3');
					if (!title && h3.isVisible()) {
						h3.slideOut('t', {callback:function(){
							h3.dom.innerHTML = '';
						}});
					} else if (title) {
						h3.dom.innerHTML = title;
						if (!h3.isVisible()) {
							h3.slideIn('t');
						}
					}
				}
				this.start(true);
			}();
		}
		//FOOTER BUTTONS
		this.fadeButton = function(name, el) {
			this.mouseOutTimeOut = false;
			this.over = false;
			//utility function
			this.capitalize = function(value){
	            return !value ? value : value.charAt(0).toUpperCase() + value.substr(1).toLowerCase();
	        }
			this.overButton = function(button) {
				if (this.over) {
					if (this.mouseOutTimeOut !== false) {
						this.mouseOutTimeOut.cancel();
					}
					return false;
				}
				this.over = true;
				Ext.get('bkg'+ this.capitalize(button)).setOpacity(0, true);
			}
			this.outButton = function(button) {
				this.over = false;
				Ext.get('bkg'+ this.capitalize(button)).setOpacity(1, true);
			}
			this.clickButton = function(e, button) {
				e.stopEvent();
				var href = Ext.DomQuery.selectNode('a', 'footer'+ this.capitalize(button));
				if (href) {
					window.location = href;
				}
			}
			//start timer to out button
			this.outTimer = function(button) {
				if (this.over && this.mouseOutTimeOut === false) {
					this.mouseOutTimeOut = new Ext.util.DelayedTask(function(){
						this.outButton(button);
					}, this);
				}
				this.mouseOutTimeOut.delay(100);
			}
			el.on({
				'mouseover':this.overButton.createDelegate(this,[name]),
				'mouseout':this.outTimer.createDelegate(this,[name]),
				'click':this.clickButton.createDelegate(this,[name], 1),
				scope:this
			});
		}
		//set all fade buttons
		if (!Ext.isIE6) {
			var contact =  new fadeButton('contact', Ext.select('*', true, 'footerContact'));
			var employment =  new fadeButton('employment', Ext.select('*', true, 'footerEmployment'));
			var clients =  new fadeButton('clients', Ext.select('*', true, 'footerClients'));
			var rss =  new fadeButton('rss', Ext.select('*', true, 'footerRss'));
		}
	}, (Ext.isIE ? 100 : 1));
});
