
var pixels = new Array();
var drawingCanvas;
var context;
var decay = 0.99;
var counter = 0;
var countTarget = 100;
var wind = 0;
var keyisup = true;

var disco_w;
var disco_h;
var disco_n;
var disco_tilesize = 80;

var thisIsARandomNumber;
var numberOfBackgrounds;
var urlVars = {};

jQuery(document).ready(function(e) {
	urlVars = getUrlVars();
	
	thisIsARandomNumber = urlVars["background"];
	
	if (thisIsARandomNumber == null)
	{
		thisIsARandomNumber = "1";
	}
	//thisIsARandomNumber = Math.ceil(Math.random()*numberOfBackgrounds);
	
	var w = jQuery(document).width() - 500;
	
	jQuery("#content").css("width",w+"px");
	//jQuery("#footer").css("width",jQuery(document).width()+"px");
	
	jQuery('.folio-item').click(event,function() {
		event.preventDefault();
		var ss = jQuery(event.currentTarget).parent().attr('href');
		flipOff(ss);
	});
	
	 slideOn();
	// alert(thisIsARandomNumber);
	 switch (thisIsARandomNumber)
	 {
		 case "1" :
		  	// DRIPPY BACKGROUND
			
			jQuery('#backgroundHolder').append('<canvas id="canvas" width="500" height="500"></canvas>'); 
			
			drawingCanvas = document.getElementById('canvas');
			drawingCanvas.width = jQuery(document).width();
			drawingCanvas.height = jQuery(document).height();
			
			if(drawingCanvas.getContext)
			{
				context = drawingCanvas.getContext('2d');
				
				document.onkeydown = keydown;
				document.onkeyup = keyup;
				
				var int = setInterval("render()",1000/60);
			}
			break;
		 case "2" :
		 	// DISCO BACKGROUND
		 	disco_tilesize = 80;
			disco_w = jQuery('body').width();
			disco_h = jQuery('body').height();
			
			for (j=0;j<disco_h / disco_tilesize;j++)
			{
				for (var i=0;i<disco_w / disco_tilesize+1;i++)
				{
					disco_n = Math.ceil(Math.random()*5);
					jQuery("#backgroundHolder").append("<div class='transition discobox"+disco_n+"'></div>");
					//jQuery("body").add("<div class='discobox"+n+"'></div>");
					
					var int = setInterval("dothedisco()",1000/60);
				}
			}
		 break;
	 }
	
});

function getUrlVars() {
	var vars = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		vars[key] = value;
	});
	return vars;
}


jQuery(window).resize(function(e) {
	var w = document.width - 500;
	
	jQuery("#content").css("width",w+"px");
});

function animateOff() {
	
	var i2 = 0;
	jQuery("#folio-nav li").each(function(index, element) {
		jQuery(element).delay(index*100).animate({rotateY: -1.6,opacity: 0 },{ 
        duration: 200,
		easing:"sineEaseOut"} );
		
		i2++;
	});
	jQuery("#other-nav li").each(function(index, element) {
		jQuery(element).delay((i2*100)+(index*100)).animate({rotateY: -1.6,opacity: 0 },{ 
        duration: 200,
		easing:"sineEaseOut"} );
	});
	
}
function slideOff() {
	
	
	
}

function slideOn() {
	
	var i2 = 0;
	
	jQuery("#folio-nav li").each(function(index, element) {
		jQuery(element).delay(index*100).animate({opacity:1 },{ 
        duration: 200,
		easing:"sineEaseOut"} );
		
		i2++;
	});
	jQuery("#other-nav li").each(function(index, element) {
		jQuery(element).delay((i2*100)+(index*100)).animate({opacity:1 },{ 
        duration: 200,
		easing:"sineEaseOut"} );
	});
	
	
	jQuery(".folio-item").each(function(index, element) {
		jQuery(element).delay(Math.random()*200).animate({rotateY: 0,opacity: 1 },{ 
        duration: 250, 
		easing:"quartEaseInOut",
        queue: true} );
	});
	
	jQuery("#content").animate({left:"450px",opacity:1},{ 
        duration: 800, 
        queue: true,
		easing:"sineEaseInOut"
	} );
	
}

function flipOff(targ) {
	jQuery(".folio-item").each(function(index, element) {
		jQuery(element).delay(Math.random()*200).animate({rotateY: 1.6,opacity: 0 },{ 
        duration: 250, 
        queue: true, 
		easing:"quartEaseInOut",
        complete: function() {window.location = targ } });
	});
	
}
//removeElement("theyear");

function disableSelection(target){
if (typeof target.onselectstart!="undefined") //IE route
	target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
	target.style.MozUserSelect="none"
else //All other route (ie: Opera)
	target.onmousedown=function(){return false}
	target.style.cursor = "default"
}

$(window).resize(function() {
	var cnt = document.getElementById('content');
	cnt.style.width = ($(window).width() - 360) + "px";
});


disableSelection(document.getElementById("nav"));
disableSelection(document.getElementById("backbtn"));


//37 left
//39 right
function keydown(e)
{
	var unicode=e.keyCode? e.keyCode : e.charCode;
	switch (unicode)
	{
		case 37 :
			wind -= 0.05;
			keyisup = false;
			break;
		case 39 :
			wind += 0.05;
			keyisup = false;
			break;
	}
}
function keyup(e)
{
	keyisup = true;
}

function render()
{
	if (keyisup && wind != 0)
	{
		if (wind > 0){
			wind -= 0.07;
		}else if (wind < 0){
			wind += 0.07;
		}
		
		wind = Math.abs(wind) <= 0.08 ? 0 : wind;
	}
	
	counter++;
	
	if (counter == countTarget)
	{
		counter = 0;
		countTarget = Math.floor(Math.random() * 1500);
		
		var p = new Object();
		var x = Math.round(Math.random()*document.width);
		p.x = x;
		p.ox = x;
		p.y = 0;
		p.vy = 1;
		p.influence = Math.random() * 0.7 + 0.3;
		p.a = Math.round(Math.random()*150)+50;
		
		if (x <= 44)
		{
			p.r = 45;
			p.g = 45;
			p.b = 45;
		}
		else if (x > 44 && x <= 85 )
		{
			p.r = 214;
			p.g = 221;
			p.b = 33;
		}
		else if (x > 85 && x <= 182 )
		{
			p.r = 137;
			p.g = 195;
			p.b = 63;
		}
		else if (x > 182 && x <= 312 )
		{
			p.r = 34;
			p.g = 178;
			p.b = 115;
		}
		else if (x > 312 && x <= 413 )
		{
			p.r = 0;
			p.g = 166;
			p.b = 154;
		}
		else if (x > 413 && x <= 484 )
		{
			p.r = 41;
			p.g = 168;
			p.b = 223;
		}
		else
		{
			p.r = 45;
			p.g = 45;
			p.b = 45;
		}
		
		pixels.push(p);
	}
	
	for (var i = 0;i<pixels.length;i++)
	{
		pixels[i].lx = pixels[i].x;
		pixels[i].ly = pixels[i].y;
		
		pixels[i].y +=  pixels[i].vy;
		//pixels[i].a -= 0.5;
		
		pixels[i].ox += wind * pixels[i].influence;
		
		if (Math.abs(wind) == 0)
		{
			if (Math.random() * 10 < 2)
			{
				pixels[i].x = pixels[i].ox + Math.random() * 2 - 1;
			}
		}
		else
		{
			pixels[i].x = pixels[i].ox ;
		}
		
		pixels[i].a = Math.abs(pixels[i].a) < 0 ? 0 : pixels[i].a;
		pixels[i].a = pixels[i].y > document.height ? 0 : pixels[i].a;
		
		if (pixels[i].a == 0)
		{
			pixels.splice(i,1);
		}
	}
	
	for (var i = 0;i<pixels.length;i++)
	{
		context.beginPath();
		context.moveTo(pixels[i].lx, pixels[i].ly);
		context.lineTo(pixels[i].x, pixels[i].y);
		context.strokeStyle = 'rgba( '+pixels[i].r+', '+pixels[i].g+', '+pixels[i].b+', '+pixels[i].a+')';
    	context.stroke();
	}
}

function dothedisco()
{
	var elements = jQuery("#backgroundHolder").children();
	
	//alert(elements.length);
	var ind = Math.ceil(Math.random(elements.length-1));
	
	jQuery(elements[ind]).animate({backgroundColor:'white'},300,"linear");
	/*jQuery(elements[Math.ceil(Math.random(elements.length-1))]).animate({'backgroundColor':'#FFFFFF'},300,function(){
		jQuery(elements[Math.ceil(Math.random(elements.length-1))]).animate({'backgroundColor':'#0F0F0F'},300)
	});*/
		
}
	


