// will @ willshown.com

//EYE
function doEyeOpen(dur,msrate) {
  $(window).mousemove(function(e){
    cursor.x=e.pageX;
	cursor.y=e.pageY;
	if(trackon)moveEye(cursor.x, cursor.y);
  });
  eye=document.getElementById("eyehole");
  var totStep = dur/msrate, step=0;
  var start=580, d1=start-175.087, d2=624.913-start;
  window.timer = window.setInterval(
	function() {
	  var prog = easeOut(step,totStep);
	  var y1=start-prog*d1;
	  var y2=start+prog*d2;
	  var d="M1200,800H0V0h1200V800z M1183.589,400C1037.727,400,813.41,"+y1+",600,"+y1+"C386.591,"+y1+",162.274,400,16.412,400C162.274,400,386.591,"+y2+",600,"+y2+"C813.41,"+y2+",1037.727,400,1183.589,400z";
	  eye.setAttribute("d", d);
	  step++;
	  if (step > totStep) {window.clearInterval(window.timer); return}
	} 
  ,msrate)
}

function doEyeReset(dur,msrate){
  var totStep = dur/msrate, step=0;
  var start=580, d1=start-175.087, d2=624.913-start;
             var i1=175.087,       i2=624.913;
  window.timer = window.setInterval(
	function() {
	  var prog = easeOut2(step,totStep/2);
	  var y1=i1+prog*d1;
	  var y2=i2-prog*d2;
	  var d="M1200,800H0V0h1200V800z M1183.589,400C1037.727,400,813.41,"+y1+",600,"+y1+"C386.591,"+y1+",162.274,400,16.412,400C162.274,400,386.591,"+y2+",600,"+y2+"C813.41,"+y2+",1037.727,400,1183.589,400z";
	  eye.setAttribute("d", d);
	  step++;
	  if (step > totStep) {window.clearInterval(window.timer); return}
	},msrate);
  window.setTimeout(function(){
	  playing=false;
	  paused=false;
	  updatePP();
	  needle1.setAttribute("transform","translate(0 -400)");
	  needle2.setAttribute("transform","");
	  vinyl.setAttribute("transform","rotate(-50 986 882)");
	  buttons.setAttribute("transform","translate(0 -400)");
	  iris.setAttribute("transform","");
	  eyec1.setAttribute("cx", 600+"px");
	  eyec1.setAttribute("cy", 400+"px");
	  eyec2.setAttribute("cx", 600+"px");
	  eyec2.setAttribute("cy", 400+"px");
	  trackon=true;
	  pagemode=0;
	  },dur*2);
}

function easeOut(step,totStep){
  return (1 - Math.pow(((step/totStep)-1),8));
}
function easeOut2(step,totStep){
  return (1 - Math.pow(((step/totStep)-1),4));
}

function easeIn(step, totStep){
	return(Math.pow((step/totStep),3));
}

function moveEye(xc, yc) {
	var w=win.x*.5, h=win.y*.5;
	var x=xc-w, y=h-yc;
	var t;
	if(x==0){
	  if(y==0){t=0;}
	  if(y>0){t=Math.PI/2;}
	  if(y<0){t=3*Math.PI/2}
	}else{
	  if(x>0){
	    if(y>=0){t=Math.atan(y/x);
	    }else{t=Math.atan(y/x)+2*Math.PI;}}
	  else{t=Math.atan(y/x)+Math.PI;}}
	var m=.6*(Math.sqrt(x*x+y*y))/radius(t,w,h);
	if(m>1){m=1;}
	var nr=radius(t,220,140);
	ox=m*nr*Math.cos(t)+600, oy=400-m*nr*Math.sin(t);
	eyec1.setAttribute("cx", ox+"px");
	eyec1.setAttribute("cy", oy+"px");
	eyec2.setAttribute("cx", ox+"px");
	eyec2.setAttribute("cy", oy+"px");
}

function radius(t, w, h){
	return Math.sqrt(Math.pow(w*Math.cos(t),2)+Math.pow(h*Math.sin(t),2));
}

function updateMatrix(){
	var m, k, r=(win.x/win.y);
	if(r>1.5)
	{
		m=(2*win.x-3*win.y)/2;
		k=800/win.y;
		matrix.x=cursor.x*k-(m/2);
		matrix.y=cursor.y*k;
		}
	else
	{
		m=(3*win.y-2*win.x)/3;
	    k=1200/win.x;
		matrix.x=cursor.x*k;
		matrix.y=cursor.y*k-(m/2);
		}
	
}

function getSize(){
	if( typeof( window.innerWidth ) == 'number' ) {
		win.x = window.innerWidth;
		win.y = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		win.x = document.documentElement.clientWidth;
		win.y = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		win.x = document.body.clientWidth;
		win.y = document.body.clientHeight;
	}
	return win;
}

function getPosition() {
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    }
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}

//NAV
function hov(n, up){
	  if(up){
		var on='#000000';
		var op1='1';
		document.getElementById('t'+n).setAttribute('fill',on);
		if(n<5)document.getElementById('c'+n).setAttribute('fill',on);
		document.getElementById('r'+n).setAttribute('opacity',op1);
		if(n==1||n==4||(n>5&&n<10)||n=='a'||n=='d')document.getElementById('rr'+n).setAttribute('opacity',op1);
	  }else{if(nav2no!=n){
		var of='#ffffff';
		var op0='0';
		document.getElementById('t'+n).setAttribute('fill',of);
		if(n<5)document.getElementById('c'+n).setAttribute('fill',of);
		document.getElementById('r'+n).setAttribute('opacity',op0);
		if(n==1||n==4||(n>5&&n<10)||n=='a'||n=='d')document.getElementById('rr'+n).setAttribute('opacity',op0);
	  }}
}

function unhov(n){
	if(n!=1)hov(1,false);
	if(n!=2)hov(2,false);
	if(n!=3)hov(3,false);
	if(n!=4)hov(4,false);
}

function toggleNav(){
		var ns=document.getElementById('nav-svg');
		if(!navon){
			if(cursor.y+125>win.y){//UP
			  up=true;
			  ns.style.top='auto';
			  navy=win.y-cursor.y;
			  ns.style.bottom=(win.y-cursor.y)+'px';
			}else{//DOWN
			  up=false;
			  ns.style.bottom='auto';
			  navy=cursor.y+5;
			  ns.style.top=(cursor.y+5)+'px';
			}
			if(cursor.x+250>win.x){//LEFT
			  left=true;
			  ns.style.left='auto';
			  navx=win.x-cursor.x;
			  ns.style.right=(win.x-cursor.x)+'px';
			}else{//RIGHT
			  left=false;
			  ns.style.right='auto';
			  navx=cursor.x+5;
			  ns.style.left=(cursor.x+5)+'px';
			}
		  ns.style.display='block';
		  navon=true;
		}else{
		  ns.style.display='none';
		  navon=false;
		  nav2no=0;
		  unhov(0);
		  clearNav2s(0);
		}
	}

function toggleNav2(n){
	switch(n){
		case 1:
		  clearNav2s(1);
		  var n2=document.getElementById('nav2sci');
		  if(up){n2.style.top='auto';n2.style.bottom=(navy+54)+'px';}else{n2.style.bottom='auto';n2.style.top=(navy+2.5)+'px';}
		  if(left){n2.style.left='auto';n2.style.right=(navx+102)+'px';}else{n2.style.right='auto';n2.style.left=(navx+100)+'px';}
		  n2.style.display='block';
		  break;
		case 2:
		  clearNav2s(2);
		  var n2=document.getElementById('nav2des');
		  if(up){n2.style.top='auto';n2.style.bottom=(navy+36.8)+'px';}else{n2.style.bottom='auto';n2.style.top=(navy+20.3)+'px';}
		  if(left){n2.style.left='auto';n2.style.right=(navx+102)+'px';}else{n2.style.right='auto';n2.style.left=(navx+100)+'px';}
		  n2.style.display='block';
		  break;
		case 3:
		  clearNav2s(3);
		  var n2=document.getElementById('n2mus');
		  if(up){n2.style.top='auto';n2.style.bottom=(navy+20)+'px';}else{n2.style.bottom='auto';n2.style.top=(navy+36.6)+'px';}
		  if(left){n2.style.left='auto';n2.style.right=(navx+102)+'px';}else{n2.style.right='auto';n2.style.left=(navx+100)+'px';}
		  n2.style.display='block';
		  break;
		case 4:
		  clearNav2s(4);
		  var n2=document.getElementById('nav2etc');
		  if(up){n2.style.top='auto';n2.style.bottom=(navy+3)+'px';}else{n2.style.bottom='auto';n2.style.top=(navy+52.9)+'px';}
		  if(left){n2.style.left='auto';n2.style.right=(navx+102)+'px';}else{n2.style.right='auto';n2.style.left=(navx+100)+'px';}
		  n2.style.display='block';
		  break;
		default:
		  clearNav2s(0);
		  break;
	}
}

function clearNav2s(n){
	if(n!=1)document.getElementById('nav2sci').style.display='none';
	if(n!=2)document.getElementById('nav2des').style.display='none';
	if(n!=3)document.getElementById('n2mus').style.display='none';
	if(n!=4)document.getElementById('nav2etc').style.display='none';
}

//VINYL
function doVinylExpose(dur,msrate){
	trackon=false;
	toggleNav();
	var totStep = dur*2/msrate, step=0;
	window.timer = window.setInterval(function(){
		if(step<totStep/2){
		var inangle = -50*easeIn(step,totStep/2);
		iris.setAttribute("transform","rotate("+inangle+" 23 -82)");}else{
		var prog = easeOut2((step-(totStep/2)),totStep/2);
		var outangle = 50*prog;
		var down = 400*prog;
		vinyl.setAttribute("transform","rotate("+(-50+outangle)+" 986 882)");
		needle1.setAttribute("transform","translate(0 "+(-400+down)+")");
		buttons.setAttribute("transform","translate(0 "+(-400+down)+")");}
        step++;
	    if (step > totStep) {window.clearInterval(window.timer); return}
	},msrate);
}

function updatePP(){
	if(playing&&!paused){
		document.getElementById("vb-play").setAttribute("opacity","0");
		document.getElementById("vb-pause").setAttribute("opacity","1");
	}else{
		document.getElementById("vb-play").setAttribute("opacity","1");
		document.getElementById("vb-pause").setAttribute("opacity","0");
	}
}

function rotate(){
	var rTimer=window.setInterval(function(){
		deg=deg+0.06*rpm;
		if(deg>360)deg=deg-360;
		label.setAttribute("transform","rotate("+deg+" 600 400)");
		if(!playing){window.clearInterval(rTimer);}
	},10);
}

function doVinylStart(){
	rotate();
	if(!playing){
		playing=true;
		paused=false;
		updatePP();
		var totStep=50, step=0;
		window.timer = window.setInterval(function(){
		  nedd=(-8*Math.pow((step-50),2))/2500+8;
		  needle2.setAttribute("transform","rotate("+nedd+" 830 160)");
		  rpm=33.3*step/totStep;
		  step++;
		  if (step > totStep) {window.clearInterval(window.timer); return}
		},10);
		window.setTimeout(function(){
			$("#jPlayerBox").jPlayer("play")
			.jPlayer("onProgressChange", function(lp,ppr,ppa,pt,tt) {
 				var pProg = parseInt(pt)/parseInt(tt);
				if(pProg>0)nedd=(22*pProg)+8;
				needle2.setAttribute("transform","rotate("+(nedd)+" 830 160)");
			});
		},1000);
	}
}

function doVinylStop(){
	$("#jPlayerBox").jPlayer("playHeadTime",0)
	.jPlayer("stop");
	paused=false;
	var totStep=50, step=0;
	window.timer = window.setInterval(function(){
		nedd=nedd*((totStep-step)/totStep);
		rpm=rpm*((totStep-step)/totStep);
		needle2.setAttribute("transform","rotate("+(nedd)+" 830 160)");
		step++;
		if(step>totStep){window.clearInterval(window.timer);return}
	},10);
	window.setTimeout(function(){playing=false;updatePP();},500);
}
