var adSapiensAppDelegate = Class.create({
	initialize: function(){
	
	
	var faceslide = new Element('div',{id:'faceslide'}).update("");
			$("hero2").insert({'top': faceslide});
	
	this.faceCanvas = faceslide;
	this.faceCount = 0;
	this.animParts = [];
	this.currentIndex=0;
	this.maxIndex = 1;
	
	this.realPos = 0;
	
		//init Default Values
		this.initFaces();
		
		this.buildFace();
		
		this.resetLefts();
		
		this.startShowTimer();
	},
	initFaces: function(){
		this.faces = [
			'/global/images/faceslide/face1/face1_',
			'/global/images/faceslide/face2/face2_',
			'/global/images/faceslide/face1/face1_'
		];
	},
	buildFace: function(){
		
		this.parts = ["part1", "part2", "part3", "part4"];
		
		for(var i = 0; i < this.parts.length; i++)
			this.buildRail(this.parts[i]);
			
			
		/*var facerail = new Element('div',{'class':'facerail'});
		
		
		
		var facepart = new Element('div',{'class':'facepart'});*/
		
		
	},
	startDelay: function(E,C)
	{
		var B=this;
		var D=new Object();
		D.method=E;
		D.duration=C;
		D.currentTime=new Date();
		D.paused=false;
		D.timeout=setTimeout(function(){B[E]()}.bind(B),C);
		return D
	},
	runShow: function(){
		
		if(this.showTimer)
		{
			this.showLoop(1,true);
		}
		
		this.startShowTimer();
	},
	showLoop: function(A,B)
	{
		this.nextAnim();
		this.startAnim();
	},
	nextAnim: function(){
		
		
			
			
		this.currentIndex = this.roundToIndex(this.currentIndex+1);
		
		
		
	},
	roundToIndex: function(A)
	{
		var B=A%(this.maxIndex+1);
		if(B<0)
		{
			B=this.maxIndex+B+1;
			
			
		}
		return B;
	},
	startShowTimer: function()
	{
		
		this.showTimer=this.startDelay("runShow",3000);
	},
	buildRail: function(partName){
	
		var facerail = new Element('div',{'class':'facerail'}).setStyle({'top':this.faceCount + "px", "left": "0px"});
		this.faceCount += 83;
		
		for(var i = 0; i < this.faces.length; i++)
		{
			var facepart = new Element('div',{'class':'facepart'}).update("<img src=" + this.faces[i] + partName + ".jpg>");
			facerail.insert({'bottom': facepart});
		}
		
		
		
		this.faceCanvas.insert({"bottom":facerail});
		this.animParts.push(facerail);
		
	},
	slideOutThumbnail: function(theid, direction){
		var animp = this.animParts[theid];
		
		
		var val = 870;
		
		
		
		
		
		if(direction<0)
		{
			//val = val * 2;
			//animp.setStyle({'left':-val+"px"});
			val = -val;
		}
		
		
		
		
		
		
		new Effect.Move(animp, { 
				x: -val, 
				y: 0,
				duration: 0.9,
				transition: Effect.Transitions.EaseFromTo,
				afterFinish: function()
				{
					//animp.setStyle({'left':'0px'});
				}
		});
	
	},
	resetLefts: function(){
		
		var val = 870 * 2;
			this.animParts[0].setStyle({'left':'0px'});
			this.animParts[1].setStyle({'left':'0px'});
			this.animParts[2].setStyle({'left': -val + 'px'});
			this.animParts[3].setStyle({'left': -val + 'px'});
		
	
	},
	startAnim: function(){
		
		/*this.animPart(0);
		this.animPart(2);*/
		this.realPos = this.realPos+1;
		
		if(this.realPos>this.faces.length)
		{
			this.resetLefts();
			this.realPos = 0;
		}
		
		
		this.slideOutThumbnail(this.currentIndex, 1);
		this.slideOutThumbnail(this.currentIndex+2, -1);
				
	},
	animPart: function(theID, direction){
	
	
		new Effect.Morph(this.animParts[theID],{
					style:'left:-870px;',
					duration:1.75,
					transition: Effect.Transitions.EaseFromTo
				});
	}
});















document.observe('dom:loaded', function() { 

if($("hero2"))
	{

		var theadSapiensapp = new adSapiensAppDelegate();
	}

if($("contact") || $("about") || $("utbildningar"))
{

var lista = $("content").select(".mail");
	
	lista.each(function(elm){
		
		elm.observe('click',function(e){
        	
        	Event.stop(e);
        	
        	var info = "person=" + elm.href.split("#")[1];
			Modalbox.show('/global/specialpages/test.php', {params: info,title: 'ADSAPIENS', afterLoad: function(){
				FLIR.replace($('popinforub'), new FLIRStyle({ cFont:'olympian' }) );
				
			}});        
		
		});		
						
	});   
	
	};   
});
