
//  Oh, Hey
//
//    Welcome to the code of my portfolio, if 
//  you're here, you are probably trying to figure
//  out what I did to make this work. I can assure
//  you that this code will make your mind explode.
//
//    Not in a good way. I'm pretty sure I was 
//  crazy when I wrote this. Like all good projects,
//  this started as something that was to be totally
//  object oriented and re-useable.
//
//    Somewhere I took a turn and now there's what 
//  you have below. Hardcoded values are everywhere.
//
//    They are ugly; real ugly. For some reason 
//  there's 2 controllers. The mere fact that I 
//  called the main controller class Joystick should
//  tell you to turn back. That said, if for some 
//  reason you are still going to attempt to read
//  this, good luck!

//  February 01, 2010 : 12:58am
//
//    Update: So I wrote the above before I finished.
//  This actually turned out pretty nice. It's got 
//  some jQuery 1.4 stuff going on along with the 
//  classic stuff. I particularly enjoyed writing the
//  filter code that allows you to have an asset link
//  to multiple nav items. This turned out very data
//  driven minus the nav, that could be more dynamic
//  using some ULs instead of making them on the fly 
//  here. Unfortunately, that would mean scrapping
//  some stuff which really just doesn't matter at
//  this point.

//    If you have any questions, just email me and 
//  I'll try to be informative :)  will at dont trust
//  robots.com
//
//  <3 Will

if (typeof console == "undefined") var console = { log: function() {} };
if (typeof pageTracker == "undefined") var pageTracker = { _trackEvent: function(){},_trackPageview: function() {} };
if (typeof piwikTracker == "undefined") var piwikTracker = { trackPageView: function() {} };

$(document).ready(function(){ 
	
	initRobots();
	
});

initRobots = function(){
	
	
			
	Joystick();
	
	$("#ie").hide();
	
}


	



//JOYSTICK

function Joystick(){
	Joystick.init()
}

Joystick.init = function(){
	this.vals = {
		id: 1
	};
	
	Joystick.levelItems = Array([],[],[],[]);
	
	Joystick.navItems = Array();
	Joystick.navTops = Array(0,50,100,100);
	Joystick.assets = Array();
	Joystick.sectionLocs = Array();
	Joystick.filters = Array();
	
	var rThis = this;
	
	Joystick.buildMenu();
	
	Joystick.hideLevels();
	
	Joystick.getAssets();
	
		
	
	
	$("#randomItem").click(function(e){
		$(this).trigger("blur");
		e.preventDefault();
		//Joystick.gotoItem(Joystick.currentItem+1);
		while(Joystick.assets[(r = Math.floor(Math.random() * $(".assetItem").length))].loc != ''){
		
		};
		
		Joystick.gotoItem(r);
		Joystick.track(Joystick.assets[r].src, "random");
		pageTracker._trackEvent("buttonClick","random",Joystick.assets[r].src);
	});
	
	$(".sideItemButton").live("click",function(){
		Joystick.gotoItem($(this).attr("rel"));
		pageTracker._trackEvent("buttonClick","side",Joystick.assets[r].src);
	});
	
	
	
	$(document).bind("scroll",function(e){
		top = $(document).scrollTop();
		$('#nav').css("top",25+top);
		$('#sidedesc').css("top",280+top);
	});
	
	
	return this;
}
Joystick.getAssets = function(){
	$.getJSON("./assets.php",{fn:"list",r:Math.random()}, Joystick.processAssets)
}

Joystick.processAssets = function(d){
	
	Joystick.assets = d;
	
	//Add Assets to dom all pretty
	var highest = 0, wide = 25,row = 25;
	for(j=0; j < Joystick.assets.length; j++){
		if(j%5 == 0 && j!=0){ 
			row += (highest + 120); 
			wide = 25;
			highest = 0;
		}
		
		// build dom object and attach
		insert = "<div class='assetItem' style=\"top:"+ row +"px;left:"+ wide +"px;\">"+
		"<div class='assetImgWrap' style=\"width: "+Joystick.assets[j].width+"px; height:"+Joystick.assets[j].height+"px;\">"+
		"<img src='./gfx/fadeframe.png' class='assetFrameOver'>"+
		"<img src='./work/"+ Joystick.assets[j].section +"/"+ Joystick.assets[j].src +"-blur.jpg'>"+
		"</div>"+
		"<span class='assetInfo'><h1>"+ Joystick.assets[j].name +"</h1>";
		insert += "<ul>";
		for( ji in Joystick.assets[j].tags ){
			insert += "<li>" + Joystick.assets[j].tags[ji] + "</li>";
			//filters
			Joystick.filterPush(Joystick.assets[j].tags[ji],j);
		}
		insert += "</ul></span></div>";
		
		$("#assets").append(insert);
		
		
		// add key locations
		if(Joystick.assets[j].loc !== undefined){
			
			Joystick.sectionLocs[ Joystick.assets[j].loc ] = j;
		}
		// fill, the filters
		
		Joystick.filterPush(Joystick.assets[j].section,j);
		Joystick.filterPush("work",j);
		
		// Update layout dimensions
		wide += 120 + Joystick.assets[j].width;
		if(highest < Joystick.assets[j].height){
			 highest = Joystick.assets[j].height;
		}
	}

	
	// Now go home
	Joystick.gotoItem(Joystick.sectionLocs["home"]);
	
}
Joystick.filterPush = function(key, loc){
	if(Joystick.filters[ key ] === undefined)
		Joystick.filters[ key ] = Array();
		
	Joystick.filters[ key ].push(loc);
}
Joystick.filterDraw = function(tag,callback){
	if(callback === undefined){
		$("#sidelist").fadeOut(100,function(){
			$(this).empty();
			Joystick.filterDraw(tag,true);
		});
	}
	else{
		$("#sidelist").show();
		for(i in Joystick.filters[tag]){
			if(Joystick.assets[Joystick.filters[tag][i]].name != ""){
				$("#sidelist").append(
					"<p class='sideItemButton' rel='"+ Joystick.filters[tag][i] +"'>" + Joystick.assets[Joystick.filters[tag][i]].name + "</p>"
				);
			}
		}
		$(".sideItemButton").hide().slideToggle(250);
	}
}

Joystick.gotoItem = function(where){
	$("#assetFrame").scrollTo($(".assetItem").eq(where),2000,{easing:'swing', offset:-100, onAfter: function(){
		$("img",$(".assetItem").eq(Joystick.currentItem)).fadeOut(200);
		$(".assetInfo",$(".assetItem").eq(Joystick.currentItem)).fadeIn(300);
		$("#sidedesc").html(Joystick.assets[where].side).fadeIn(200);
	} });
	
	$(".assetItem").eq(where).children().eq(0).css("background-image",
	"url(./work/"+Joystick.assets[where].section+"/"+Joystick.assets[where].src+".jpg)");
	
	
	
	//hide old stuff
	
	$(".assetInfo",$(".assetItem").eq(Joystick.currentItem)).hide();
	$("img",$(".assetItem").eq(Joystick.currentItem)).fadeIn(500);
	$("#sidedesc").fadeOut(200);
	
	Joystick.currentItem = where;
}


Joystick.hideLevels = function(){
	//Hide menu branches
	Joystick.hideLevel(1);
	Joystick.hideLevel(2);
	Joystick.hideLevel(3);
}

Joystick.buildMenu = function(){
	//This is classy...
	Joystick.addNavItem("work",0,1);
		Joystick.addNavItem("design",1,2);
			Joystick.addNavItem("illustration",2);
			Joystick.addNavItem("identity",2);
			Joystick.addNavItem("ui-ux",2);
		Joystick.addNavItem("develop",1,3);
			Joystick.addNavItem("actionscript",3);
			Joystick.addNavItem("web",3);
	Joystick.addNavItem("about",0);
	Joystick.addNavItem("resume",0);
	Joystick.addNavItem("contact",0);
	
	$(".n-title").bind("click",function(){
		Joystick.handleMenu("home");
	});
}
Joystick.addNavItem = function(ititle,ilevel,ichildLevel){
	if(Joystick.navItems[ilevel] === undefined){ Joystick.navItems[ilevel] = Array(); }
	Joystick.navItems[ilevel].push( new navItem({title: ititle, level:ilevel, child:ichildLevel}) );
}

Joystick.level = function(what){
	var count = 0;
	var width = 0;
	for(ni in Joystick.navItems[what]){
		count++;
		width += (5*count)+$('#'+Joystick.navItems[what][ni].vals.title).width();
	}
	
	return {c: count, w: width};
};

Joystick.showLevel = function(level){
	$("#design").removeClass("levelActive1");
	$("#develop").removeClass("levelActive2");
	
	for(ni in Joystick.navItems[level]){
		$('#'+Joystick.navItems[level][ni].vals.title).show(100);
		
	}
	
	if(level == 2){
		$("#design").addClass("levelActive1");
		
	}
	if(level == 3){
		
		$("#develop").addClass("levelActive2");
	}
}
Joystick.hideLevel = function(level){
	for(ni in Joystick.navItems[level]){
		$('#'+Joystick.navItems[level][ni].vals.title).hide(100);
	}
}

Joystick.handleMenu = function(tag){
	Joystick.filterDraw(tag);
	Joystick.track(tag);
	pageTracker._trackEvent("jtrack","menuClick",tag);
	
	switch(tag){
		case "home": 
			Joystick.gotoItem(Joystick.sectionLocs[tag]); 
			Joystick.hideLevels();
			$(".n-section").removeClass("levelActive0");
			break;
		case "about":
			Joystick.gotoItem(Joystick.sectionLocs[tag]);
			break;
		case "resume":
			Joystick.gotoItem(Joystick.sectionLocs[tag]);
			break;
		case "contact":
			Joystick.gotoItem(Joystick.sectionLocs[tag]);
			break;
		case "design":
			Joystick.gotoItem(Joystick.sectionLocs[tag]);
			break;
		case "develop":
			Joystick.gotoItem(Joystick.sectionLocs[tag]);
			break;
		case "work":
			
			break;
		
	}
	
	
}

Joystick.track = function(tag,section){
	
	if(section !== undefined){
		pageTracker._trackPageview("/" + section + "/" + tag);
	}
	else{
		pageTracker._trackPageview("/" + tag);
	}
	piwikTracker.trackPageView();
}


//NAVITEM
function navItem(o){
	this.vals = {
		id: 1,
		level: o.level,
		title: o.title,
		dim: {w:0,h:0},
		child: o.child
	};
	
	this.init();
	
	return this;
}
navItem.prototype.init = function(){
	title = this.vals.title;
	$("#nav-items").append("<span class='n-section' id='"+title+"'><span class='n-section-title'>"+title+"</span></span>");
	this.vals.dims = {w: $("#"+title).width() + 20, h: $("#"+title).height() + 10};
	$("#"+title).css({width: this.vals.dims.w, height: this.vals.dims.h});
	$("#"+title).css("left", (-25*this.vals.level) + Joystick.level(this.vals.level).w);
	$("#"+title).css("top", Joystick.navTops[this.vals.level]);
	
	$(".n-section-title","#"+title).css({left: 4+Math.floor(Math.random()*4), top: 4+Math.floor(Math.random()*4)});
	
		var rThis = this;
		$("#"+title).click(function(){
			
			if(rThis.vals.level == 0){
				Joystick.hideLevels();
				rThis.active();
			}
			if(rThis.vals.level == 1){
				Joystick.hideLevel(2); 
				Joystick.hideLevel(3);
			}
			if(rThis.vals.child != null){
				
				Joystick.showLevel(rThis.vals.child);
			}
			
			Joystick.handleMenu( rThis.vals.title );
			
			//$.scrollTo(0,500);
		});
	
}

navItem.prototype.active = function(){
	$(".n-section").removeClass("levelActive0");
	$("#"+this.vals.title).addClass("levelActive"+this.vals.level);
}



