var gnews=
{
	content:""
	,cnt:0
	,me:0
	,currH:0
	,currNews:0
	,meW:0
	,meH:0
	,toolbarW:0
	,innerH:0
	,quote:0
	,quoteCurrent:0
	,quoteIndex:0, div:3,iDiv:0
	,nextNewsDt:1000*5
	,newsTimer:0
	,quotesTimer:0
	,M_init: function()
	{	this.me=$('#HomeAnimation');
		if(this.quotesTimer) {self.clearTimeout(this.quotesTimer); this.quotesTimer=0;}
		if(this.newsTimer) {self.clearTimeout(this.newsTimer); this.newsTimer=0;}
		this.quoteCurrent=0;
		if(this.me.get(0))
		{	this.meW = 500;
			this.meH = 372;
			this.innerH = this.meH-28;
			$('#arrCollapse').click(function(){gnews.M_collapse();}).hide();
			$('#arrExpand').click(function(){gnews.M_expand(gnews.currH)});
			this.toolbarW=this.meW-40;
			this.cnt=0;
			{	this.quote=Array();
				this.me.find('.quote').each(function(i){
				{	gnews.quote[i] = {text:$(this).text(),sign:"",index:i,obj:$(this)};
					if( i==4)
						gnews.quote[i].sign="\n- Manfred Kets de Vries";
				}
				
				});
				this.me.find('.quote p').text("");
			}
			this.me.find('ul li').each(function(i){
				gnews.cnt++;i++;
				$(this).addClass("news"+i);
				$(this).find('.count').html(i).click(function(){
					gnews.M_lauchNewsAnimation();
					gnews.M_selectHeadline( parseInt($(this).text()));
				});
				$(this).find('p,h2,.h').hide();
				$(this).find('.h').click(function(){gnews.M_expand(i)});
			});
			this.me.find('.toolbar').hide();

		} else this.me=0;
	}
	, M_start :function()
	{	if(this.me && this.me.get(0))
		{
			this.M_selectHeadline(1);
			this.M_animate();
			this.M_lauchNewsAnimation();
			this.me.find('.toolbar').fadeIn();
		}
	}
	, M_resize:function(w,h)
	{
		if( this.me && this.me.get(0))
		{	this.meW = w=parseInt(w);
			this.meH = h=parseInt(h);
			this.toolbarW=w-40;
			this.innerH = 1+this.meH-28;
			this.me.css({width:this.meW,height:this.meH});
			this.me.find('.toolbar').css({width:this.toolbarW, top:h-37});
			var fh=""+parseInt(50*h/372)+"px";
			var fh2=""+parseInt(12*h/372)+"px";
			this.me.find('.quotes').css({width:w*this.quote.length,height:h,left:-this.quoteCurrent*this.meW})
			this.me.find('.quote').css({width:w,height:h,fontSize:fh,lineHeight:fh}).each(function(i){$(this).css({left:i*w});  });
			var fh3=""+parseInt(24*h/372)+"px";
			this.me.find('.quote.l3').css({fontSize:fh3,lineHeight:fh3})
			var fh4=""+parseInt(40*h/372)+"px";
			this.me.find('.quote.l4').css({fontSize:fh4,lineHeight:fh4})
			this.M_selectHeadline(this.currH,true);
			this.me.find('.inner').css({width:this.meW-21,height:this.innerH});
			this.me.find('.news').css({height:this.innerH});

		}
		
	}
	, M_selectHeadline : function(iSelect,force)
	{	if( force || (this.currH!=iSelect))
		{	
			this.currH=iSelect;
			//$('.news'+i).
			var x=0;
			var wh = this.toolbarW-this.cnt*20;
			var w;
			this.me.find('ul li').each(function(i){	
				i++;
				w=20;
				if( iSelect==i)
				{	w+=wh;
					$(this).find('.h').css({width:wh}).show();

				}else  $(this).find('.h').animate({width:0},{queue:false,duration:force?0:200});
				$(this).animate({left:x,width:w},{queue:false,duration:force?0:200});
				x+=w;
			});
		}
	}

	, M_expand: function(i)
	{	if( !i) return;
		var me = $('.news'+i)
		if( me.get(0))
		{
			gnews.content = "<div class=\"news\"><h2>"+me.find('h2').text()+"</h2><p>"+me.find('p').html()+"</p></div>";
			gnews.currNews=i;
			$('#arrExpand').fadeOut();
			$('#arrCollapse').fadeIn();
			gnews.me.find('.quotes').animate({opacity:0});
			gnews.me.find('.inner').animate({height:0,top:gnews.innerH},"fast",function()
			{
				$(this).html(gnews.content).animate({height:gnews.innerH,top:0});
			});
		}
	}
	, M_collapse:function()
	{
		$('#HomeAnimation .inner').animate({height:0,top:this.innerH},"fast",function(){ gnews.me.find(".quotes").animate({opacity:1}); });
		gnews.currNews=0;		
		$('#arrExpand').fadeIn();
		$('#arrCollapse').fadeOut();

	}
	, M_animate:function()
	{
		var qt=this.quote[this.quoteCurrent];
		var qtIndex=++this.quoteIndex;
		var i=parseInt(qtIndex/this.div);
		var n=qt.text.length;
		var i1=i<n?i:n;
		var t
		if( isIE)
		{	t=""+(i1>=1? qt.text.substring(0,i1-1):"");
		}
		else
		{
			t=""+(i1>=1? qt.text.substring(0,i1-1):"")+"<span class=\"o1\">"+qt.text.substring(i1-1,i1)+" </span>"+"<span class=\"o\">"+qt.text.substring(i1)+"</span>";
		}
		{	if( qt.sign.length && i>=n)
			{	var reg=new RegExp("\n", "g");
				var tsign = qt.sign.substring(0,i-n)+"<span class=\"o\">"+qt.sign.substring(i-n)+"</span>";
				t+="<a>"+tsign.replace(reg,"<br />")+"</a>";
			}
		}

		var hidn=$("<p>"+t+"</p>");
		hidn.find('.o1').css({opacity:(qtIndex-i*this.div)/this.div})
		hidn.find('.o').css({opacity:0.01})
		qt.obj.find("p").replaceWith(hidn);
		var m = n+qt.sign.length;
		if( i<m)
			this.quotesTimer=self.setTimeout(RQDelegate(this, this.M_animate),20);
		else //  next quote
		{	//this.me.find(".quote").animate({opacity:0});
			this.quoteCurrent++;
			this.quoteIndex=0;
			this.iDiv=0;
			if( this.quoteCurrent<this.quote.length)
			{	// next quote, with a pause
				this.quotesTimer = self.setTimeout(RQDelegate(this, this.M_animateNext),1000);
			}
		}
	}
	,M_animateNext:function()
	{
		this.me.find(".quotes").animate({left:-this.quoteCurrent*this.meW});
		if( this.quoteCurrent< (this.quote.length-1))
			this.M_animate();
		else	// last quote
			this.quotesTimer = self.setTimeout(RQDelegate(this, this.M_animateLastQuote),1000);
	}
	,M_animateLastQuote:function()
	{	var i=this.quoteCurrent++;
		this.me.find(".quotes").css({left:-i*this.meW});
		var qt=this.quote[i];
		qt.obj.find("p").css({opacity:0}).html(qt.text).animate({opacity:1},{duration:1000});
		if( i< (this.quote.length-1))
		{
				gnews.quotesTimer = self.setTimeout(function()
				{	qt.obj.find("p").animate({opacity:0},{duration:1000, complete:function(){gnews.M_animateLastQuote();}});
				},6000);
		}
		else
			this.quotesTimer = 0;
	}

	,M_lauchNewsAnimation:function()
	{
		if( this.newsTimer) self.clearTimeout(this.newsTimer);
		this.newsTimer=self.setTimeout(RQDelegate(this, this.M_animateNews),this.nextNewsDt);
	}
	, M_animateNews:function()
	{	
		if( this.cnt>1)
		{
			var i=this.currH;
			i++;if(i>this.cnt)i=1;
			this.M_selectHeadline( i);			
			this.newsTimer=self.setTimeout(RQDelegate(this, this.M_animateNews),this.nextNewsDt);
		}
	}

};






var gIntro=
{
	fnDone:0
	,o:0,ctx:0
	,rectX:0,rectY:0,rectWidth:72,rectH:72,rectXdest:188
	,tri1X:0,tri1W:36,tri1XDest:0
	,tri2X:0,tri2Y:0,tri2H:36,tri2YDest:0
	,tri3Y:0,tri3YDest:0
	,kdviY:0
	,timer:0,pauseStart:1000*1
	,imgtri2:0
	,k:0.1
	,M_create: function()
	{
		if( this.timer) self.clearTimeout(this.timer);
		var w=$(window).width();
		var h=$(window).height();
		var canvas =$("<canvas id=\"anim\" width=\""+w+"\" height=\""+h+"\"></canvas>");
		canvas.css({position:"absolute",top:0,left:0});
		$('#whiteTop').append(canvas);
		this.o=canvas.get(0);
		if( this.o.getContext)
		{
			this.ctx = this.o.getContext('2d');  
			this.rectWidth = this.rectH= g.logoStartH;
			this.rectY = g.logoStartY
			this.rectX = w-this.rectWidth;
			this.rectXdest	= g.contentx+g.leftSpace+g.logoShift;
			this.tri1XDest	= this.rectXdest+this.rectWidth/2;
			this.tri2X		= this.rectXdest+this.rectWidth/2;
			this.tri1W		= this.rectWidth/2;
			this.tri2H		= this.rectH/2;
			this.tri2YDest	= this.rectY;
			this.tri3Y		= h;
			this.tri3YDest	= this.rectY+this.rectH;
			this.k=0.05
			this.kdviY		= this.rectY+this.rectH+20;
			$('#ketDeVriesInstitute').css({top:this.kdviY})

			this.M_render();

			// get objects
			//this.imgtri2 =this.ctx.getImageData(this.tri2X, this.tri2Y, this.rectWidth, this.tri2H);

			return true;
		}
		else
		{	this.o=0;
			
			showFlashAnim();
			return false;
		}


	}
	,M_start:function(fnDone)
	{
		this.fnDone=fnDone;
		if(this.o)
		{
			this.timer=self.setTimeout(RQDelegate(this, this.M_animate),20);

		}
		else
		{	var delay=1000*4;
			if( $("#IEAnimation span").length)
			{	delay=10; RQDebug("no Flash");
			}
			self.setTimeout(function(){	RQDebug("Intro anim Done"); $('#whiteTop').css({height:40});$("#IEAnimation").remove(); if(gIntro.fnDone)gIntro.fnDone.apply(this); },delay);
			
		}
	}
	,M_animate:function()
	{
		if( !this.pauseStart)
		{
			this.M_clear();
			this.rectX += (this.rectXdest-this.rectX)*this.k;
			this.tri1X += (this.tri1XDest-this.tri1X)*this.k;
			this.tri2Y += (this.tri2YDest-this.tri2Y)*this.k;
			this.tri3Y += (this.tri3YDest-this.tri3Y)*this.k;

			this.k*=1.05;if( this.k>1)this.k=1;
			if( Math.abs(this.tri2YDest-this.tri2Y)<0.01)
			{	$(this.o).remove();
				if(this.fnDone)this.fnDone();
				return;
			}
			this.M_render();
		}
		else
		{	this.pauseStart-=20;
			if( this.pauseStart<=0) this.pauseStart=0;
		}
		$('#ketDeVriesInstitute').css({top:this.kdviY})

		this.timer=self.setTimeout(RQDelegate(this, this.M_animate),20);

	}
	,M_clear:function()
	{
		
		m=4;
		var p=15+2+m;
		this.ctx.clearRect (this.rectX-m, this.rectY-m, this.rectWidth+p, this.rectH+p);  
		this.ctx.clearRect (this.tri1X-m, this.rectY-m, this.tri1W+p, this.rectH+p);  
		this.ctx.clearRect (this.tri2X-m, this.tri2Y-m, this.rectWidth+p, this.tri2H+p);  
		this.ctx.clearRect (this.tri2X-m, this.tri3Y-m-this.tri2H, this.rectWidth+p, this.tri2H+p); 

	}
	,M_render:function()
	{	
		//if( this.imgtri2)	this.ctx.putImageData(this.imgtri2,this.tri2X,this.tri2Y);
		//else

		// triangle jaune
		this.ctx.globalCompositeOperation = "copy";
		this.ctx.fillStyle = "rgba(252,180,62,1)";  	
		this.ctx.beginPath();
		this.ctx.moveTo(this.tri2X,this.tri2Y);
		this.ctx.lineTo(this.tri2X+this.rectWidth, this.tri2Y);
		this.ctx.lineTo(this.tri2X+this.rectWidth/2,this.tri2Y+this.tri2H);
		this.ctx.fill();		

		// red square
		this.ctx.globalCompositeOperation="xor";
		this.ctx.fillStyle = "rgba(242,98,62,1)";  
		this.ctx.fillRect (this.rectX, this.rectY, this.rectWidth, this.rectH);  

		// square no 2
		this.ctx.globalCompositeOperation="destination-over";
		this.ctx.fillStyle = "rgba(228,50,27,1)";  
		this.ctx.fillRect (this.rectX, this.rectY, this.rectWidth, this.rectH);  




		// triangle noir
		this.ctx.globalCompositeOperation = "source-over";
		this.ctx.fillStyle = "rgba(35,31,32,1)";  
		this.ctx.beginPath();
		this.ctx.moveTo(this.tri1X,this.rectY);
		this.ctx.lineTo(this.tri1X+this.tri1W, this.rectY+this.rectH/2);
		this.ctx.lineTo(this.tri1X,this.rectY+this.rectH);
		this.ctx.fill();		

		

		// triangle vert
		this.ctx.globalCompositeOperation="xor";
		this.ctx.fillStyle = "rgba(0,179,109,1.0)";  
		this.ctx.beginPath();
		this.ctx.moveTo(this.tri2X,this.tri3Y);
		this.ctx.lineTo(this.tri2X+this.rectWidth, this.tri3Y);
		this.ctx.lineTo(this.tri2X+this.rectWidth/2,this.tri3Y-this.tri2H);
		this.ctx.fill();		

		// triangle vert no 2
		this.ctx.globalCompositeOperation="destination-over";
		this.ctx.fillStyle = "rgba(25,50,27,1.0)";  
		this.ctx.beginPath();
		this.ctx.moveTo(this.tri2X,this.tri3Y);
		this.ctx.lineTo(this.tri2X+this.rectWidth, this.tri3Y);
		this.ctx.lineTo(this.tri2X+this.rectWidth/2,this.tri3Y-this.tri2H);
		this.ctx.fill();		



	}
};

