	var offsetY=0,influence=0,ffzoom={},dotCount=25,dotSize=50,dotsOnCanvas=false,SCREEN_WIDTH=window.innerWidth,SCREEN_HEIGHT=window.innerHeight,stats,particle,camera,scene,renderer,mouseX=0,mouseY=0,velX=0,velY=0,windowHalfX=window.innerWidth/2,windowHalfY=window.innerHeight/2,zoomedIn=false,juggernaut=Math.max(SCREEN_WIDTH,SCREEN_HEIGHT)/192*(SCREEN_WIDTH/SCREEN_HEIGHT)+1,offY=0,offX=0,zoom={v:1},lastTouchRender=0;
	var framerate = 1000/85;
	var colors = [0x0c8cb7, 0xc9c92d, 0x924011, 0xdd6328, 0x008641],	
	isIpad = navigator.appVersion.match(/ipad/gi) ? true : false
	isIphone = navigator.appVersion.match(/iphone/gi) ? true : false
	isTouch = isIphone || isIpad;
	triggerOn = isTouch ? 'touchstart' : 'click';
	viewmore = 0;
	category = 0;
	showing = 5;
	blogFirst = false;
	
	function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
	function toHex(N) {
	 if (N==null) return "00";
	 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
	 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
	 return "0123456789ABCDEF".charAt((N-N%16)/16)
			+ "0123456789ABCDEF".charAt(N%16);
	}

	tween_smallt = {
		loop:8000,
		delayBefore:3000,
		delayAfter:2000,
		frames:[
			{wait:0,selector:"#smallt .arrowTrack .arrow",property:"left",value:10,duration:400},
			{wait:500,selector:"#smallt .bubble",property:"backgroundPosition",value:140,duration:800},
			{wait:500,selector:"#smallt .arrowTrack .arrow",property:"left",value:35,duration:400},
			{wait:4000,selector:"#smallt .bubble",property:"backgroundPosition",value:-20,duration:0},			
			{wait:4000,selector:"#smallt .bubble",property:"backgroundPosition",value:23,duration:500},						
			{wait:4200,selector:"#smallt .arrowTrack .arrow",property:"left",value:85,duration:400},			
			{wait:5000,selector:"#smallt .arrowTrack .arrow",property:"left",value:-20,duration:0},			
		]		
	}
	tween_smallt = tween.add(tween_smallt); 
	tween_who = {
		loop:8000,
		delayBefore:3000,
		delayAfter:2000,
		frames:[
			{wait:0,selector:"#who .arrowTrack .arrow",property:"left",value:30-44,duration:500},
			{wait:500,selector:"#who .bubble",property:"backgroundPosition",value:200,duration:1000},
			{wait:500,selector:"#who .arrowTrack .arrow",property:"left",value:95-15,duration:450},
			{wait:2000,selector:"#who .bubble",property:"backgroundPosition",value:-120,duration:0},
			{wait:4000,selector:"#who .bubble",property:"backgroundPosition",value:26,duration:1000},
			{wait:4500,selector:"#who .arrowTrack .arrow",property:"left",value:200-15,duration:600},
			{wait:5300,selector:"#who .arrowTrack .arrow",property:"left",value:30-44,duration:0},				
		]		
	}
	//tween_who = tween.add(tween_who); 	

	$(function() {
		
		if(!isTouch) {
			VideoJS.setupAllWhenReady();			  
		}
		$(".view a:last").fadeOut(0);				
		//var myManyPlayers = VideoJS.setup("All");

		$(".mask").each(function() {
			c = $(this).text().split(",");
			v="";
			for(l in c) {
				v+=(String.fromCharCode(c[l]));
			}
			$(this).html("<a href='mailto:" + v + "'>" + v + "</a>");
		});	
		$("*").hover(function() {
			$(this).data("hovering",true);
		},function() {
			$(this).data("hovering",false);			
		});
		
		// SORT
		$('.sort ul').live(triggerOn, function() {
			if($(this).is(".open")) {
				$(this).find('ul').hide();				
				$(this).removeClass("open");
		 	} else {
				$(this).find('ul').show();
				$(this).addClass("open");				
			}
		});
		$(".sort ul").live("mouseout",function() {
			setTimeout(function() {
				$(".sort *").each(function() {
					if(!$(this).data("hovering")) {
						$(this).find('ul').hide();				
						$(this).removeClass("open");											
					}
				});
			},500);
		});
		$(".sort ul ul li").live(triggerOn,function() {
			viewmore = 0;
			category = $(this).find("a:first").attr("title");
			$(".view a:last").fadeIn();
			
			$(".sort ul").removeClass("open");	
			$(".sort ul > li a:first").html($(this).find("a").text());
			$("#blogDark .blogs").fadeTo(1000,.01,function() {
				$("#blogDark .blogs").load("do.loadPosts.php?category=" + category + "&more=" + viewmore,function() {
					viewmore=5;
					if($("#blogDark .blogs").html().indexOf("end-of-blog")>-1 || $("#blogDark .blogs").html().indexOf("<!--nothingtoshow-->")>-1) {
						$(".view a:last").fadeOut();														 
					}																																				  
					renderTitles();
					$("#blogDark .blogs").fadeTo(1000,1);
				});
			});	
			$(".sort ul").find('ul').hide();				
			$(".sort ul").removeClass("open");
			
		});
		$(".view a:last").live("click",function() {
			// load 5 more
			//viewmore+= showing;
			$("#blogDark .blogs").append("<div class='more'></div>");
			$("#blogDark .blogs .more").fadeOut(0);
			$("#blogDark .blogs .more").load("do.loadPosts.php?category=" + category + "&more=" + viewmore,function() {
				if($("#blogDark .blogs .more").html().indexOf("end-of-blog")>-1) {
					$(".view a:last").fadeOut();														 
				}
				$("#blogDark .blogs .more").fadeIn(function() {	
					renderTitles();
					offsetY = window.pageYOffset + 0;	
					destY = $("#blogDark .blogs .more").offset().top-30;
					if($.browser.mozilla) {
						$("html, body").animate({scrollTop:offsetY+700});	
					} else {
						if(isTouch) {
							destY = offsetY + 550;	
						}
						(new Tween(document.body,'scrollTop', Tween.strongEaseInOut,offsetY,destY,2)).start();	
					}
					$("#blogDark .blogs .more").removeClass("more");
				});
			});
		});
		$(".archives dd a").live("click",function() {
			viewmore = 0;															
			category = 0;
			archive = $(this).attr("title").replace(" ","-");
			$("#blogDark .blogs").fadeTo(1000,.01,function() {
				$("#blogDark .blogs").load("do.loadPosts.php?archive=" + archive,function() {
					if($("#blogDark .blogs").html().indexOf("end-of-blog")>-1) {
						$(".view a:last").fadeOut();														 
					}																																				  
					renderTitles();
					$("#blogDark .blogs").fadeTo(1000,1);
				});
			});																	
		});
		$(".top").live("click",function() {
			if($.browser.mozilla) {
				$("html,body").animate({scrollTop:1110});										 
			} else {
				offsetY = window.pageYOffset + 0;					
				(new Tween(document.body,'scrollTop', Tween.strongEaseInOut,offsetY,1110,2)).start();		
			}
		});
		
		centerScreen1();
		
		$("#blog").css({left:0});
		$("#cycleLogo").css({left:720,top:450});
		

		init();  
		//cycleLogo();		
		setInterval(loop,framerate);	
		//setInterval(cycleLogo,18000);

		if(window.location.hash=="") {
			hash.set("home");
		} else {
			if(window.location.hash.indexOf("#news/")>-1) {
				jumpToPost(window.location.hash.split("/")[1].split("/")[0]);
			}
		}
		
		hash.rule("home",	function() {},	function() {});
		hash.rule("zoomed",
			function() {
				//if(hash.prev!="blog") {
					enter_zoom();			
				//}
			},function() {
				if(hash.now=="home") {
					exit_zoom();	
				}
		});
		hash.rule("news",
			function() {
				if(hash.prev=="") {
					// come right into blog
					blogFirst = true;
					$("#who .bubble").css({background:"#77787B"})
					$("#contact img").css({cursor:"pointer"});
					$("#contact .grey").stop().fadeOut(100);
					setTimeout(function() {
						$("#contact .grey").fadeOut(0);					  
					},500)
					//$("#contact .grey").remove();
//					console.log("hide grey");
					$("#contact .white").stop().fadeIn();
					$("#smallt .arrow").fadeOut(100);
					$("#smallt .bubble").animate({backgroundPosition:20});				
					$("#fictional, #smallt").removeClass("influence").fadeOut(0);
					$("#contact").removeClass("influence").animate({left:40,top:428});
					$("#contact").css({color:"#fff"}).fadeIn();
					$("#contact .bubble").fadeOut();
				} 
				enter_blog();	
			},function() {
				exit_blog();
		});
		
		// handle posts
		
		
		$("#who, #cycleLogo, #fictional, #contact, #blog").fadeOut(0);

		if((window.location+"").indexOf("#news")<0) {
			$("#fictional, #contact").fadeIn();
		}
			
		$("#fictional").live(triggerOn,function() {
			hash.set("zoomed");
		});
		$("#smallt").live(triggerOn,function() {
			hash.set("zoomed");												 
		});
		$(".close").live(triggerOn,function() {
			setTimeout(centerScreen1,100);
			hash.set("zoomed");										
		});
		$("#who").hover(function() {
			if(!$("#who").is(".open")) {		
	 			(new ColorTween($("#who .bubble")[0].style,"backgroundColor",Tween.regularEaseIn,"505050","77787B",.25)).start();	
			}
		},function() {
			if(!$("#who").is(".open")) {
	 			(new ColorTween($("#who .bubble")[0].style,"backgroundColor",Tween.regularEaseIn,"77787B","505050",.25)).start();				
			}
		});
		$("#who").live(triggerOn,function(e) {
			e.preventDefault();
			if(!$(this).is(".open")) {		
				hash.set("news");
			} else {
				$(this).addClass("open");
			}			
		});
		$(".blog h2 a").live(triggerOn,function(e) {
			post = $(this).attr("href").split("/")[1];
			jumpToPost(post);
		});
		
		$("#contact img").live(triggerOn,function() {
			if(window.location.hash != "#home") {
				window.location = window.location.toString().split("#")[0];					 
			}
		});
		
		$(window).resize(function() {
			setTimeout(function() {
				SCREEN_WIDTH = $(window).width();
				SCREEN_HEIGHT = $(window).height();			
				renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
				camera.projectionMatrix = THREE.Matrix4.makePerspective(75, $(window).width() / $(window).height(), 1, 10000);	
				if(!isTouch) {
					centerScreen1();
					}
				});	
			},1500);
		$("body").removeClass("hide");
		
	});
	
	function centerScreen1() {
	$("#fictional").css({
			left:$(window).width()/2-$("#fictional").width()/2,
			top:$(window).height()/2-$("#fictional").height()/2
		});	
		$("#contact").css({
			left:40,
			top:428
		});	
		$("#who").css({
			left:$(window).width()/2-175/2-200,
			top:$(window).height()/2-175+250
		});
		$("#smallt").css({
			left:$(window).width()/2-$("#smallt").width()/2+230,
			top:$(window).height()/2-$("#smallt").height()
		});	
		$("#fictional, #contact, #who, #smallt").each(function(i) {
			$(this).data("oLeft",$(this).position().left)
			$(this).data("oTop",$(this).position().top)
			$(this).data("oIndex",i);
		});		
	}
	
	function init() {
		
		camera = new THREE.Camera( 75, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );

		camera.position.z = 15000;
		
		scene = new THREE.Scene();
		renderer = new THREE.CanvasRenderer();
		renderer.setSize($(window).width(), $(window).height());			
		document.addEventListener('mousemove', onDocumentMouseMove, false);
		document.addEventListener('touchmove', onDocumentTouchMove, false);		
		$("#canvas").prepend(renderer.domElement);	
		for (var i = 0; i < dotCount; i++) {
				particle = new THREE.Particle( new THREE.ParticleCircleMaterial(colors[Math.floor(Math.random()*colors.length)], 1 ) );			
				particle.position = {x:Math.random() * 2000 - 1000,y:Math.random() * 2000 - 1000,z:Math.random() * 2000 - 1000}
				particle.scale.x = particle.scale.y = Math.random() * dotSize + 5;
				scene.add(particle);
			}	
			tlogo = new Image()
			tlogo.src = "images/tbig.png";
			tlogo = new THREE.Particle( new THREE.ParticleBitmapMaterial(tlogo));
			tlogo.position ={x:50,y:50,z:0}
			tlogo.scale.x = tlogo.scale.y = 1;		
			scene.add(tlogo);
	}
	
	// states

	function enter_zoom(duration) {
		
		//console.log("enter_zoom()");		

		$("#contact img").css({cursor:"pointer"});
		
		$("#smallt .arrow").fadeOut(100);
		$("#smallt .bubble").animate({backgroundPosition:20});
		
		$("#fictional, #smallt").removeClass("influence");
		$("#contact").removeClass("influence").animate({left:40,top:428});		
																												  
		setTimeout(function() { $("#who").fadeIn(300); } ,300);																													  
		$("#fictional,#smallt").fadeOut(300);														
		$("#contact .bubble").fadeOut();
		(new Tween(camera.position,'z', Tween.strongEaseOut,15000,1200,2/3)).start();

	}
	function exit_zoom() {	
		//console.log("exit_zoom()");
		(new Tween(camera.position,'z', Tween.strongEaseOut,1200,15000,2)).start();
		
		$("#contact img").css({cursor:"default"});	
		
		$("#who").fadeOut();
		
		$("#smallt .arrow").fadeIn();
		$("#contact .bubble").fadeIn();
		$("#fictional").fadeIn();				
		$("#smallt").fadeIn();
		
		$("#fictional").addClass("influence");			
		$("#smallt").addClass("influence");
		$("#contact").addClass("influence");
		centerScreen1()
	}
	
function enter_blog() {
		$("#contact .grey").fadeOut(0);	
		//console.log("enter_blog()")
		$("#blogDark .blogs").load("do.loadPosts.php",function() {
			
			totalPosts = $("#blogDark .blogs").html().split("<!-- total=[")[1];
			totalPosts = totalPosts.replace("]-->","");

			//console.log(totalPosts + "," + showing);
			if(totalPosts<=showing) {
				$(".view a:last").fadeOut();				
			} else {
				$(".view a:last").fadeIn();				
				viewmore = showing;			
			}
			
			renderTitles();
		});

					
		$("#who").addClass("open");
		rgb = $("#who .bubble").css("background-color").replace("rgb(","").replace(")","").split(",");
		rgb = RGBtoHex(rgb[0],rgb[1],rgb[2]);
		(new ColorTween($("#who .bubble")[0].style,"backgroundColor",Tween.regularEaseIn,rgb,"77787B",.25)).start();				
		$("#who .bubble").css({backgroundImage:"none"});
		$("#who .arrow").hide();

		// BUBBLE SCALE /////////////////////////////////////////////////////
		// webkit
		$("#who .bubble").transition({"-webkit-transform":"scale(50)"},{speed:"1s"});
		if($.browser.mozilla) {
			// firefox
			ffzoom.z = 1;
			(new Tween(ffzoom,'z', Tween.strongEaseOut,1,50,2)).start();
			ffzooming = setInterval(function() {
				$("#who .bubble").css({"-moz-transform":"scale(" + ffzoom.z + ")"});
			},20);
		}
		
		if(!blogFirst) { 
		setTimeout(function() { 
			//clearInterval(ffzooming); 
			$("#who").hide();
			$("canvas").hide();
			$("body").css({backgroundImage:"url(images/bg_blog.jpg)", backgroundRepeat:"repeat-x", backgroundColor:"#505050"});			
		},2000);
			
		
		// END OF BUBBLE SCALE //////////////////////////////////////////////
		
		
			setTimeout(function() {
				(new ColorTween($("#contact")[0].style,"color",Tween.regularEaseIn,"77787b","ffffff",.25)).start();			
				$("#contact .grey").fadeOut();						  			
			},100);
			setTimeout(function() {
				$("#blog").fadeIn();		
				$("#cycleLogo").fadeIn();
			},1500);
		} else {
			$("#contact .grey").fadeOut(0);
			$("#contact .bubble").hide();
			$("#contact").css({color:"#fff"});
			$("#blog").fadeIn(0);
			$("#cycleLogo").fadeIn(0);
			$("#who").hide();
			$("canvas").hide();
			$("body").css({backgroundImage:"url(images/bg_blog.jpg)", backgroundRepeat:"repeat-x", backgroundColor:"#505050"});
			blogFirst = false;
		}
		
	}
	function exit_blog() {	
	
		$("video")[0].pause();
		// BUBBLE SCALE /////////////////////////////////////////////////////
		//webkit
		scrollTo(0,0);
		$("#who .bubble").transition({"-webkit-transform":"scale(1)"},{speed:"1s"});				
		//firefox
		if($.browser.mozilla) {
			ffzoom.z = 50;
			(new Tween(ffzoom,'z', Tween.strongEaseOut,50,1,2)).start();
			ffzooming = setInterval(function() {
				$("#who .bubble").css({"-moz-transform":"scale(" + ffzoom.z + ")"});
			},20);
		}
		$("body").css({background:"#f8f8ec"});
		$("canvas").show();		
		$("#who").show();
		
		setTimeout(function() { 
			//clearInterval(ffzooming);
			$("#who").removeClass("open");	
			$("#who .bubble").css({background:"#505050 url(images/whoWeAre.png) no-repeat",backgroundPosition:"26px 76px"});
			$("#who .arrow").show();			
		},2000);
		
		// END OF BUBBLE SCALE //////////////////////////////////////////////
				
		
		$("#blog").fadeOut();
		$("#cycleLogo").fadeOut();
			
		setTimeout(function() {
			$("#contact .grey").fadeIn();				
			(new ColorTween($("#contact")[0].style,"color",Tween.regularEaseIn,"ffffff","77787b",.25)).start();							
		},500);			
	}
	
	
	// listeners
	function onDocumentMouseMove(event) {
		mouseX = event.clientX - windowHalfX;
		mouseY = event.clientY - windowHalfY;
		
		velX = event.clientX - mouseX - windowHalfX > 20 ? 20 : event.clientX - mouseX - windowHalfX;
		velY = event.clientY - mouseY - windowHalfY > 20 ? 20 : event.clientY - mouseY - windowHalfY;
	}
	function onDocumentTouchStart(event) {if(event.touches.length == 1) {if(!zoomedIn) {event.preventDefault();	}mouseX = event.touches[0].pageX - windowHalfX;	mouseY = event.touches[0].pageY - windowHalfY;}}
	function onDocumentTouchMove(event) {
		if(!$("#who").is(".open")) {
			event.preventDefault();			
		}
		if(event.touches.length == 1) {
			velX = event.touches[0].pageX - mouseX - windowHalfX;
			velY = event.touches[0].pageY - mouseY - windowHalfY;			
			mouseX = event.touches[0].pageX - windowHalfX;	
			mouseY = event.touches[0].pageY - windowHalfY;	
		}
	}
	
	function jumpToPost(post) {
		$("#contact .grey").stop().fadeOut();		
		setTimeout(function() {
			$("#blogDark .blogs").load("do.loadPosts.php?single=" + post,function() {
				
				//(new Tween(document.body,'scrollTop', Tween.strongEaseInOut,offsetY,1110,2)).start();	
				renderTitles();
				offsetY = $(window).scrollTop() + 0;	
				//if(offsetY==0) {
					if($.browser.mozilla) {
						$("html,body").animate({scrollTop:1110},2000);	
					} else {
						(new Tween(document.body,'scrollTop', Tween.strongEaseInOut,offsetY,1100,2)).start();	
					}
				//}
			});
		},500);	
		$(".view a:last").fadeOut();
	}
	// loops
	function loop() {
		camera.position.x += (mouseX - camera.position.x) * .1 + (velX*5);
		camera.position.y += (-mouseY - camera.position.y) * .1 - (velY*5);
		renderer.render(scene, camera);
		$(".influence").each(function() {
			influence = ($(this).data("oIndex")+1)/30;
			l = (camera.position.x*(influence*-1))+$(this).data("oLeft");
			t = (camera.position.y*(influence*1))+$(this).data("oTop");
			if(l<35) { l=35; }
			if($(this).is("#who")) {
				if(l<260) { l=260; }							  
		   }
			$(this).css({
				left:l,
				top:t
			});
		});
	}
	function renderTitles() {
		$("h1, h2, h3").each(function() {
			if($(this).find("img").length<1) {
				if($(this).find("a").length>0) { 
					$(this).find("a").html('<img border="0" src="resources/do.renderFont.php?size=' + parseInt($(this).css("font-size")) + '&str=' + $(this).find("a").html().replace("+","%2B") + '" alt="' + $(this).find("a").html() + '">');							 
				} else {
					$(this).html('<img border="0" src="resources/do.renderFont.php?size=' + parseInt($(this).css("font-size")) + '&str=' + $(this).html().replace("+","%2B") + '" alt="' + $(this).html() + '">');							 					
				}
			}
		});	
	}
	function cycleLogo() {
		setTimeout(function() {		
			(new ColorTween($("#cycleLogo")[0].style,"background",Tween.regularEaseIn,"FFFFFF","eb7b21",1)).start();	
			$("#cycleLogo .white").fadeIn(1000);						
		},3000);
		setTimeout(function() {
			(new ColorTween($("#cycleLogo")[0].style,"background",Tween.regularEaseIn,"eb7b21","29aedb",1)).start();	
		},6000);
		setTimeout(function() {
			(new ColorTween($("#cycleLogo")[0].style,"background",Tween.regularEaseIn,"29aedb","4ec010",1)).start();	
		},9000);		
		setTimeout(function() {		
			(new ColorTween($("#cycleLogo")[0].style,"background",Tween.regularEaseIn,"4ec010","e5cb1b",1)).start();			
		},12000);		
		setTimeout(function() {		
			(new ColorTween($("#cycleLogo")[0].style,"background",Tween.regularEaseIn,"e5cb1b","FFFFFF",1)).start();					
			$("#cycleLogo .white").fadeOut(1000);					
		},15000);		

	}
	
