// and a big up to PLL :)
var theMouseX = 0;
var theMouseY = 0;
var speed = 1;
var popInState = 0;
var legalState = 0;
var footerHiddenBottom = 0;
var initialArticleHeight = 0;
var initialArticleLeft = 0;
var initialArticleTop = 0;
var SI;

function AddEvent(a, b, c) {
    try {
        if (a.addEventListener) {
            a.addEventListener(b, c, false)
        } else if (a.attachEvent) {
            a.attachEvent('on' + b, c)
        }
        return true
    } catch(err) {
        return false
    }
}

$(window).resize(function() {
	if(legalState == 0) {
		$('footer').css("bottom",20-(document.getElementById("footer").offsetHeight));
	}
});


$(document).ready(function () {
	footerHiddenBottom = 20-(document.getElementById("footer").offsetHeight);
	$('footer').css("bottom",footerHiddenBottom);
	
	//if (document.addEventListener){
		//document.addEventListener('mousemove', getPosition, false);
		AddEvent(document,'mousemove',getPosition);
		initialArticleHeight = (document.getElementById("manifesto").offsetHeight)-30;
		initialArticleLeft = document.getElementById('manifesto').offsetLeft-21;
		initialArticleTop = document.getElementById('manifesto').offsetTop-29;
		$("article").hide();
		$("article").css("width","0px");
		$("article").css("height","0px");
	/*} else if (document.attachEvent) {

		document.attachEvent('mousemove', getPosition);
		//popInState = 1;
		initialArticleHeight = (document.getElementById("manifesto").offsetHeight)-30;
		initialArticleLeft = document.getElementById('manifesto').offsetLeft-21;
		initialArticleTop = document.getElementById('manifesto').offsetTop-29;
		$("article").hide();
		$("article").css("width","0px");
		$("article").css("height","0px");
	}*/
	
	initFollowTheMouse();    

	$('#openIcon').click(function () {
		(popInState == 0) ? flyMeToTheMoon() : backToEarth();
	});

	$('#dummy').click(function () {
		(popInState == 0) ? flyMeToTheMoon() : backToEarth();
	});

	$('#socialNetworks a').click(function (event) {
		var theURL = "url-"+$(this).parent().attr("id");
		_gaq.push(['_trackEvent', 'open', theURL]);
		event.preventDefault();
		window.open($(this).attr("href"));
	});

	$('#footer a').click(function (event) {
		_gaq.push(['_trackEvent', 'open', 'photograph-website']);
		event.preventDefault();
		window.open($(this).attr("href"));
	});

	$('#legal').click(function () {
		theFooterHeight = document.getElementById("footer").offsetHeight;
		if(legalState == 0) {
			_gaq.push(['_trackEvent', 'open', 'footer']);
			$('footer').animate({
				bottom: "0px"
				}, 500);
			legalState = 1;
		} else {
			_gaq.push(['_trackEvent', 'close', 'footer']);
			$('footer').animate({
				bottom: 20-(document.getElementById("footer").offsetHeight)+"px"
				}, 500);			
			legalState = 0;
		}
	});

});

function initFollowTheMouse() {
    SI = window.setInterval(
	    function() {
	    	
	    	if(popInState == 0){ 
			    logoX = parseInt($('#openIcon').css("left").replace('px', ''));
			    logoY = parseInt($('#openIcon').css("top").replace('px', ''));
			    //console.log(theMouseX+" "+logoX);
			    if((Math.abs(theMouseX-logoX)-10)>0 || (Math.abs(theMouseY-logoY)-10)>0) {
			    	if(legalState == 0) {
			    		theMouseY < (pageHeight()-58) ?	theRealY = theMouseY : theRealY = (pageHeight()-58);
			    	} else {
			    		theMouseY < (pageHeight()-116) ?	theRealY = theMouseY : theRealY = (pageHeight()-116);
			    	}
			    	$('#openIcon').animate({
						left: (theMouseX-10)+"px",
						top: (theRealY-10)+"px"
					}, 100/speed, 'linear');
			    }
			} else {
				window.clearInterval(SI);
			}
		},100
	);
}

function getPosition(event) {
		var x = new Number();
		var y = new Number();

		if (event.x != undefined && event.y != undefined) {
			x = event.x;
			y = event.y;
		} else { // Firefox method to get the position
			x = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			y = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		
		x -= document.getElementById("container").offsetLeft;
		y -= document.getElementById("container").offsetTop;

		theMouseX = x;
		theMouseY = y;

		//console.log(theMouseX + " " + theMouseY);
}

function flyMeToTheMoon() {
	_gaq.push(['_trackEvent', 'open', 'layer-infos']);
	popInState = 1;

	//var topPosition = (parseInt($('article').css("top").replace('px', '')))-29;
	//var leftPosition = (parseInt($('article').css("left").replace('px', '')))-21;

	$('#openIcon').animate({
		top: initialArticleTop,
		left: initialArticleLeft
		}, 500, function() {
			$('#openIcon').attr("src","assets/images/close_icon.png")
		    $("article").show();
			$("article").animate({
			    width: "600px",
			    //height: "565px",
			    height: initialArticleHeight+"px"
			}, 500 );
		}
	);
}

function backToEarth() {
	_gaq.push(['_trackEvent', 'close', 'layer-infos']);
	$('article').animate({
		width: "0px",
		height: "0px",
		}, 500, function() {
			$("article").hide();
			$('#openIcon').attr("src","assets/images/open_icon.png")
			popInState = 0;
			initFollowTheMouse();
		}
	);
}

function pageWidth() {return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;}
function pageHeight() {return window.innerHeight != null? window.innerHeight: document.body != null? document.body.clientHeight:null;} 

/*

var dx = mouse.x - arrow.x,
            dy = mouse.y - arrow.y,
            angle = Math.atan2(dy, dx),
            vx = Math.cos(angle) * speed,
            vy = Math.sin(angle) * speed;

        arrow.rotation = angle; //radians
        arrow.x += vx;
        arrow.y += vy;

        */
