//NOTE: only used on homepage where title always begins with GoPasta
(function()
{
	if(document.title.substr(0,7)!='GoPasta') return;
	var interval = 10000;
	var charSpeed = 200;
	var timer
	var nbsp = '\u00A0';
	var oldTitle = document.title;
	var moveleft = 7; //amount of chars that have to be removed
	document.title = oldTitle + nbsp;
	var rotate = function()
	{
		if(moveleft>0 && document.title.length>nbsp.length){
			//remove first char
			document.title = document.title.substr(1);
			moveleft--;
			timer = setTimeout(rotate, charSpeed);
		} else if (moveleft==0) {
			//GoPasta was removed (keep empty for a moment)
			moveleft = -1;
			timer = setTimeout(rotate, charSpeed);
		} else if (moveleft==-1) {
		    //first add "Go"
			document.title = oldTitle.replace("GoPasta", "Go");
			moveleft = -2;
			timer = setTimeout(rotate, charSpeed*3);
		} else {
			//reset entire title
			document.title = oldTitle + nbsp;
			moveleft = 7;
			timer = setTimeout(rotate, interval);
		}
	}
	timer = setTimeout(rotate, interval);
})();


//frame buster
(function()
{
	if( window!=window.top )
		window.top.location.replace(document.location);
})();

//facebook
(function()
{
	if( /MSIE 6/i.test(navigator.userAgent) )
		return;
	if( window.screen.width<800 )
		return;
	
	var tld = location.host.substr(-2);
	var path = {
		be:'GoPasta.be'
		,nl:'pages%2FGoPasta-Nederland%2F173337552738948'
		,de:'pages%2FGoPasta-Deutschland%2F210542355659647'
		,at:'pages%2FGoPasta-Österreich%2F124142947597450'
		,fr:'GoPasta.fr'
		,ch:'pages%2FGoPasta-Schweiz%2F256826011020490'}[tld];
	if (!path)
		path = 'gopastainternational';

	var box = document.createElement('div');
	box.setAttribute('style','position:absolute; bottom:20%; right:-238px; width:235px; height:383px; background: url(/images/fb-likebox.png) 0 0 no-repeat;');
	setTimeout(function(){
		box.innerHTML='<iframe src="//www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2F'+path+'&amp;width=190&amp;colorscheme=light&amp;show_faces=true&amp;border_color=%23fff&amp;stream=false&amp;header=false&amp;height=375" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:190px; height:352px; background:#fff;position:absolute;top:23px;right:5px;border-radius:6px;" allowTransparency="true"></iframe>';
		animate(-201);
		}, 4000);
	
	var shown = false;
	box.onmouseover = function(){
		if( !shown )
			animate(0);
		shown = true;
		};
	box.onmouseout=function(e){
		//detect internal mouseout
		if (!e) var e = window.event;
		var target = (window.event) ? e.srcElement : e.target;
		if (target!=box) return;
		var reltarget = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		while (reltarget!=box && reltarget.nodeName != 'BODY')
			reltarget=reltarget.parentNode;
		if (reltarget==box) return;
		
		if( shown )
			animate(-201);
		shown = false;
		};
	document.body.appendChild(box);
	
	var animate = (function() 
	{
		var fps = 25;
		var speed = 5/fps;
		var maxSpeed = speed*30000/fps;
		var spring = 0;//.5;
		
		var from, to, current, vel;
		var scrollTimoutId;
		var ani = function()
		{
			var diff = to - current;
			var oldVel=vel;
			vel *= spring;
			vel += (speed*diff);
			vel = (vel>0?1:-1)*Math.min(maxSpeed, Math.abs(vel));//miximize
			
			var next = current+vel;
			if( Math.round(Math.abs(vel))==0 )
			{
				next = to;//jump to end
			}
			else
			{
				scrollTimoutId = setTimeout(ani, 1000/fps);
			}
			box.style.right = Math.round(next)+'px';
			current = next;
		}
		return function(aTo)
			{
				from=current=parseInt(box.style.right);
				to=aTo;
				vel=0;
				ani()
			};
	
	})();
	
})();
