var pjd = new function(){
	var h=this;
	this.analyse=function(o,d){
		var s='',d=d||'\n';
		for(var i in o)s+=i+" = "+o[i]+", type = "+typeof o[i]+d;
		return s;
	};
	this.isEmpty=function(str){return str.match(/\S+/)==null;};
	this.isString=function(str){return typeof str=='string';};
	this.isNumber=function(num){return typeof num=='number';};
	this.isBoolean=function(obj){return typeof obj=='boolean';};
	this.getById=function(id){try{return document.getElementById(id);}catch(error){return null;}};
	this.getByClassName=function(name,parent,tag_name){
		var parent=parent||document.body,items=[],tag_name=tag_name||'*';
		var nodes=parent.getElementsByTagName(tag_name);
		for(var i=0;i<nodes.length;i++){
			var str=nodes[i].className;
			if(str&&str.match(new RegExp('\\b'+name+'\\b','gi'))!=null)items.push(nodes[i]);
		}
		return items.length>1?items:items[0];
	};
	this.getByName=function(name,parent){return (parent||document.body).getElementsByTagName(name);};
	this.setStyle=function(obj,styles){
		for(var i in styles){
			try{
				if(i=='alpha'){h.setAlpha(obj,styles[i]);}
				else{obj.style[i]=styles[i];}
			}
			catch(error){}
		}
	};
	this.getStyle=function(obj,style){
		if(style=='alpha'){return pjd.getAlpha(obj);}
		else{return obj.style[style];}
	};
	this.show=function(obj){void h.setStyle(obj,{display:'block',visibility:'visible'});};
	this.hide=function(obj){void h.setStyle(obj,{display:'none',visibility:'hidden'});};
	this.setHtml=function(obj,str){try{obj.innerHTML=str;}catch(e){}};
	this.getHtml=function(obj){try{return obj.innerHTML;}catch(e){return null;}};
	this.setZIndex=function(obj,index){void h.setStyle(obj,{zIndex:index});};
	this.setAlpha=function(obj,alpha){
		try{obj.style.opacity=alpha/100;}catch(error){}
		try{obj.style.filter='alpha(opacity='+alpha+')';}catch(error){}
	};
	this.getAlpha=function(obj){
		try{
			return isNaN(parseInt(obj.style.opacity))?100:obj.style.opacity;
		}catch(error){}
		return null;
	};		
	this.setPosition=function(obj,x,y){
		var styles={};
		if(h.isNumber(x))styles.left=x+'px';
		if(h.isNumber(y))styles.top=y+'px';
		void h.setStyle(obj,styles);
	} ;
	this.getOffset=function(obj){
		var x=0,y=0;
		if(obj.offsetParent){
			do{x+=obj.offsetLeft;y+=obj.offsetTop;}
			while(obj=obj.offsetParent);
			return {x:x,y:y};
		}
		return null;
	};
	this.sanitize=function(str){return str.replace(/\s{2,}/gi,' ').replace(/^\s+/,'').replace(/\s+$/,'');};
	this.setClassName=function(obj,name){try{obj.className=name;}catch(e){}};
	this.addClassName=function(obj,name){
		if(!obj)return;
		var classes=obj.className;
		if(classes.match(new RegExp('\\b'+name+'\\b','gi'))!=null)return;
		try{obj.className=this.sanitize(classes+' '+name);}catch(e){}
	} ;
	this.removeClassName=function(obj,name){
		if(!obj)return;
		var classes=obj.className;
		try{obj.className=this.sanitize(classes.replace(new RegExp('\\b'+name+'\\b','gi'),''));}catch(e){}
	};
	this.node=new function(){
		this.create=function(type){try{return document.createElement(type);}catch(e){return null;}};
		this.add=function(node,parent,position){
			try{
				if(h.isNumber(position)){
					var count=0,target;
					for(var i=0;i<parent.childNodes.length;i++){
						var child=parent.childNodes[i],type=child.nodeType==3;
						if(/\w/.test(child.nodeValue)===true)count++;
						if(count==position){
							target=child;
							break;
						}
					}
					try{return parent.insertBefore(node,target);}catch(error){}
				}
				return parent.appendChild(node);
			}catch(error){return null;}
		};
		this.inject=function(type,parent){try{return this.add(this.create(type),parent||document.body);}catch(e){return null;}};
		this.remove=function(node){
			try{node.removeNode(true);}
			catch(error){
				try{node.parentNode.removeChild(node);}
				catch(error){}
			}
		};
		this.clone=function(node,full){
			try{return node.cloneNode(h.isBoolean(full)?full:true);}
			catch(error){return null;}
		};
		this.isChildOf=function(parent,obj){
			if(parent===obj)return false;
			while(obj&&obj!==parent)obj=obj.parentNode;
			return obj===parent;
		};		
	};
	this.getWindowSize=function(){
		if(window.innerWidth){
			return {width:window.innerWidth,height:window.innerHeight};
		}else{
			var doc=document.all&&document.compatMode=='BackCompat'?document.body:document.documentElement;
			return {width:doc.clientWidth,height:doc.clientHeight};
		}
	};	
	this.getBodySize=function(){
		var node=document.all&&document.compatMode=='BackCompat'?document.body:document.documentElement;
		return {width:node.scrollWidth,height:node.scrollHeight};
	};
	this.getScrollOffset=function(){
		var isIE=document.all||false,doc=!isIE?window:document.compatMode!='BackCompat'?document.documentElement:document.body;
		return {x:doc[isIE?'scrollLeft':'pageXOffset'],y:doc[isIE?'scrollTop':'pageYOffset']};
	};
	this.events=new function(){
		var cache={};
		this.addListener=function(obj,event,scope,func,args,strict){
			if(!cache[event])cache[event]=[];
			func=delegate(obj,scope||null,func,args,strict||false);
			cache[event].push([obj,event,scope,func]);
			obj.attachEvent?obj.attachEvent('on'+event,func):obj.addEventListener(event,func,false);
		};
		this.removeListener=function(obj,event,scope,func){
			if(!cache[event])return;
			for(var i=0;i<cache[event].length;i++){
				var item=cache[event][i];
				if(obj==item[0]&&event==item[1]&&scope==item[2]){
					func=item[3];
					cache[event].splice(i,1);
					obj.detachEvent?obj.detachEvent('on'+event,func):obj.removeEventListener(event,func,false);
					break;
				}
			}
		};
		this.stopPropagation=function(event){
			try{event.cancelBubble=true;}catch(error){}
			try{event.stopPropagation();}catch(error){}
		};
		this.getMousePosition=function(event){
			var offset=h.getScrollOffset();
			return {x:event.pageX||(event.clientX+offset.x),y:event.pageY||(event.clientY+offset.y)};
		};
		function delegate(obj,scope,func,args,strict){
			return function(event){
				var event=event||window.event;
				event.args=args||{};
				if(strict){
					var target=event.relatedTarget||event.fromElement;
					if(h.node.isChildOf(obj,target))return;
					//alert('strict:trigger, func:'+func);
				}
				return func.apply(scope,[event]);
			};
		};		
	};
	this.motions=new function(){
		var _collection={};
		var _equations={
			quintOut:function(t,b,c,d){return c*((t=t/d-1)*t*t*t*t+1)+b;},
			elasticOut:function(t,b,c,d,a,p){
				if(t==0)return b;
				if((t/=d)==1)return b+c;
				if(!p)p=d*0.3;
				if(!a||a<Math.abs(c)){
					a=c;
					var s=p/4;
				}else{var s=p/(2*Math.PI)*Math.asin(c/a);}
				return (a*Math.pow(2,-10*t)*Math.sin((t*d-s)*(2*Math.PI)/p)+c+b);
			},				
			bounceOut:function(t,b,c,d){
				var n1=7.5625,n2=2.75;
				if((t/=d)<(1/n2)){return c*(n1*t*t)+b;}
				else if(t<(2/n2)){return c*(n1*(t-=(1.5/n2))*t+0.75)+b;}
				else if(t<(2.5/n2)){return c*(n1*(t-=(2.25/n2))*t+0.9375)+b;}
				else{return c*(n1*(t-=(2.625/n2))*t+0.984375)+b;}
			}
		};
		function Motion(node){
			var _n=node,_p,_s,_e,_d,_f,_u,_c,_i,_x;
			this.start=function(){
				void _clear();
				_c=0;
				_i=setInterval(execute,10);
			};
			this.stop=function(){void _clear();};
			this.set=function(property,start,end,duration,equation,func,unit){
				_p=pjd.isString(property)?property:'top';
				_s=pjd.isNumber(start)?start:parseInt(pjd.getStyle(_n,_p));
				_e=pjd.isNumber(end)?end:0;
				_d=pjd.isNumber(duration)?duration-1:29;
				_x=pjd.isString(equation)?equation:'quintOut';
				_f=typeof func=='function'?func:null;
				_u=pjd.isString(unit)?unit:'px';
			};
			function _clear(){void clearInterval(_i);};
			function execute(){
				var o={};
				o[_p]=_equations[_x](_c,_s,_e-_s,_d)+_u;
				void h.setStyle(_n,o);			
				_c>=_d?complete():_c++;
			};
			function complete(){
				void _clear();
				try{void _f();}catch(error){}
			};
		};
		this.animate=function(id,node,prop,start,end,duration,equation,callback,unit){
			if(!_collection[id])_collection[id]=new Motion(node);
			_collection[id].stop();
			_collection[id].set(prop,start,end,duration,equation,callback,unit);
			_collection[id].start();
		};
	};	
};
var menu=new function(){
	var _timeout=null;
	var _delay=600;
	var _pastElementId=null;
	this.setup=function(){
		$("#mainNavigation a").each(function(index){
			var id=$(this).attr('rel');
			if(id.match(/\S+/)==null)return;
			void setPosition(this,id);
			$('#'+id).mouseover(function(){clearTimeout(_timeout);});
			$('#'+id).mouseleave(function(){
				clearTimeout(_timeout);
				_timeout=setTimeout(function(){$('#'+id).slideUp('fast');},_delay);
			});
			$(this).mouseover(function(){
				clearTimeout(_timeout);
				if(id!=_pastElementId)$('#'+_pastElementId).slideUp('fast');
				_pastElementId=id;
				if($('#'+id).is(':hidden')){
					$('#'+id).css('zIndex',999999);
					$('#'+id).slideDown('fast');
				}
			});
			$(this).mouseleave(function(){
				clearTimeout(_timeout);
				_timeout=setTimeout(function(){$('#'+id).slideUp('fast');},_delay);
			});
		});
	};
	function setPosition(button,id){
		var menuPosition=$("#mainNavigation").position();
		var menuWidth=$("#mainNavigation").width();
		var pos=$(button).position();
		var submenuPosition=Math.floor(pos.left);
		var submenuWidth=$('#'+id).outerWidth();
		if(submenuPosition+submenuWidth>menuPosition.left+menuWidth){
			submenuPosition=menuPosition.left+menuWidth-submenuWidth;
		}
		$('#'+id).css({'left':submenuPosition,'top':pos.top+$(button).height()});
	};
	this.setMenuPosition=function(){
		$("#mainNavigation a").each(function(index){
			var id=$(this).attr('rel');
			if(pjd.isEmpty(id))return;
			void setPosition(this,id);
		});
	};
};
var backgroundRotator=new function(){
	var _imagesNumber=5,_currentId=1,_delay=5000,_timeout;
	var _gatewayUrl=[
		{fr:null,en:null},
		{fr:null,en:null},
		{fr:null,en:null},
		{fr:null,en:null},
		{fr:null,en:null}
	];
	this.setup=function(){
		$('.background-composition .image-header').css('cursor','pointer');
		$('.background-composition .image-header').click(function(){
			var url=_gatewayUrl[_currentId-1][_userSelectedLanguage];
			if(url!=null)window.location=url;
		});
		$('.image-header .control li').each(function(){
			$(this).click(function(event){
				event.stopPropagation();
				clearTimeout(_timeout);
				void changeBackground($('.image-header .control li').index(this)+1);
			});
		});
		void resetTimeout();
	};
	function resetTimeout(){_timeout=setTimeout(forcedNull,_delay);};
	function forcedNull(){void changeBackground(null);};// To avoid FF random num as callback param
	function changeBackground( forcedId ){
		var pastId=_currentId;
		_currentId=typeof forcedId=='number'?forcedId:_currentId==_imagesNumber?1:_currentId+1;
		$('.content .background-composition').css('background-image','url(../images/visuals/header/hivers/img-'+_currentId+'-'+_userSelectedLanguage+'.jpg)');
		$('.image-header .control li').eq(pastId-1).removeClass('current');
		$('.image-header .control li').eq(_currentId-1).addClass('current');
		void resetTimeout();
	}
};
var eventBrowser=new function(){
	var _module,_mask,_previous,_next,_container,_table,_tr,_index=3,_max=3,_visible=3,_active=false,_step=240;
	this.setup=function(){
		_module=pjd.getById('eventsBrowser');
		_mask=pjd.getByClassName('pjd-mask',_module);
		_previous=pjd.getByClassName('events-scroll-previous',_module);
		_next=pjd.getByClassName('events-scroll-next',_module);
		_container=pjd.getByClassName('container',_module);
		_table=pjd.getByName('table',_container)[0];
		_tr=pjd.getByName('tr',_table)[0];
		_max=pjd.getByName('td',_tr).length;
		void pjd.setStyle(_mask,{height:(_container.offsetHeight)+'px'});
		if(_max>3){
			void setPosition();
			void pjd.events.addListener(window,'resize',null,setPosition);
			void pjd.events.addListener(_previous,'click',null,displayPrevious);
			void pjd.events.addListener(_next,'click',null,displayNext);			
		}
	};
	function displayPrevious(){
		if(_active||_index<=3)return;
		_active=true;
		$(_container).animate({left:((_index-_visible)*(0-_step))+_step},'fast',function(){_active=false;});
		_index--;
	};
	function displayNext(){
		if(_active||(_index!=_visible&&_index>=_max))return;
		_active=true;
		$(_container).animate({left:((_index-_visible)*(0-_step))-_step},'fast',function(){_active=false;});
		 _index++;
	};
	function setPosition(){
		var offset=pjd.getOffset(_container);
		void pjd.setStyle(_previous,{left:(offset.x-8)+'px',top:(offset.y+51)+'px',display:'block'});
		void pjd.setStyle(_next,{left:(offset.x+690)+'px',top:(offset.y+51)+'px',display:'block'});		
	};
};
var toggle=new function(){
	this.init=function(){void parse();};
	function parse(){
		var collection=pjd.getByName('*');
		for(var i=0;i<collection.length;i++){
			var node=collection[i];
			var rel=node.getAttribute('rel');
			if(rel!=null&&rel.match(/toggle-/gi)!=null){
				try{node.href="javascript:void(0);";}
				catch(error){}
				void pjd.events.addListener(node,'click',null,toggle,{id:rel.replace(/toggle-/gi,'')});
			}
		}
	};
	function toggle(event){
		var node=pjd.getById(event.args.id);
		void pjd.setStyle(node,{display:node.style.display.toLowerCase()=='block'?'none':'block'});
	};
};
if(typeof $!='undefined'){
	$(document).ready(function(){
		void menu.setup();
		if(typeof _pageId=="undefined")_pageId=null;
		if(_pageId=='01_100'){
			void backgroundRotator.setup();
			void eventBrowser.setup();
		}
		void toggle.init();
	});
	$(window).resize(function(){void menu.setMenuPosition();});
	$(window).unload(function(){try{GUnload();}catch(error){}});
}
