function getFlashMovie(movieName) {
	movieName = swfobject.getObjectById(movieName);
	return movieName;
}

function fisherYates ( myArray ) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}

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);
}

var imageLoadCount = 0;
var contentLoaded = false;
var needDrag = false;
var originX = 0;
var originY = 103;
var camX = 0;
var camY = 0;
var squareWidth = 150;
var squarePadding = 20;
var edgePadding = 10;
var numSquares = 17;
var currentSort = 0;

var animationTime = 500;

var projectDisplayWidth = 1000;

var viewMode = 0;
var targetedSquare = -1;

var carousel_num = 0;
var num_objects = 0;

var divsArray = [];

var squaresArray = [];

var current_letter = "!";

var blocked = false;

//THE EXTRA p's ARE TO MAKE SURE WE DON'T ACCIDENTALLY GET INTO AN INFINITE LOOP
//THEY DON'T BREAK ANYTHING SO LEAVE THEM THERE!

var pat0 = ["dbl","p","p","p","p","p","p","p","p","p","p","p","p","p"];
var pat1 = ["p","p","p","p","p","p","p","p","p","p","p","p","p","p"];
var pat2 = ["p","p","p","p","p","p","p","p","p","p","p","p","p","p"];
var pat3 = ["p","p","p","p","p","p","p","p","p","p","p","p","p","p"];
var pat4 = ["p","p","dbl","bs","bs","bs","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p"];
var pat5 = ["p","p","dbl","bs","bs","bs","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p"];
var pat6 = ["p","p","p","bs","bs","bs","dtr","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p"];
var pat7 = ["p","p","p","bs","bs","bs","p","p","dbl","bs","bs","bs","p","p","p","p","p","p","p","p","p","p","p","p","p"];
var pat8 = ["p","p","p","bs","bs","bs","p","p","p","bs","bs","bs","dtr","p","p","p","p","p","p","p","p","p","p","p","p","p"];
var pat9 = ["p","p","p","p","p","dtr","p","p","bs","bs","bs","bs","bs","p","p","p","p","p","p","p","p","p","p","p"];
var pat10 = ["p","p","p","p","p","dtr","p","p","p","bs","bs","bs","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p"];
var pat11 = ["p","p","p","p","dbl","dbl","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p"];
var pat12 = ["p","p","p","p","dbl","dbl","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p","p"];

var patterns = [pat0, pat1, pat2, pat3, pat4, pat5, pat6, pat7, pat8, pat9, pat10, pat11, pat12];


function calcNumRows(totalNum){
	var pageWidth = $("body").width();
	
}

//A lettersquare object
function lettersquare(target, aPos, dateMod, quantity, featured){
	this.targetDiv = target;
	this.datemodified = dateMod;
	this.alphabetical = aPos;
	this.quantity = quantity;
	this.featured = featured;
	this.displayed = false;
	this.curX = 0;
	this.curY = 0;
	this.curAlpha = 1;
	this.moveSquare = moveSquare;
	this.moveToPosition = moveToPosition;
	this.updatePosition = updatePosition;
	this.checkDisplay = checkDisplay;
}



function squareClicked(){
	if (!blocked) {
		blocked = true;
		$('body').css("overflow-x", "hidden");
		//find the clicked square
		var found = -1;
		for (var i = 0; i < squaresArray.length; i++) {
			if (squaresArray[i].targetDiv == this) {
				squaresArray[i].displayed = true;
				squaresArray[i].curAlpha = 1;
				found = i;
			}
			else {
				squaresArray[i].curAlpha = 0;
				squaresArray[i].displayed = false;
			}
		}
		
		if (viewMode == 1 && targetedSquare == squaresArray[found]) {
			if (currentSort == 0) {
				sortByAlpha();
			}
			else 
				if (currentSort == 1) {
					sortByDate();
				}
				else 
					if (currentSort == 2) {
						sortByFeatured();
					}
					else 
						if (currentSort == 3) {
							sortByQuantity();
						}
						else 
							if (currentSort == 4) {
								sortByShuffle();
							}
			return false;
		}
		
		var moveOffset = 75;
		
		if ($('body').width() > projectDisplayWidth) {
			moveOffset = 75 + ($('body').width() - projectDisplayWidth) * 0.5
		}
		
		targetedSquare = squaresArray[found];
		current_letter = String.fromCharCode(97 + targetedSquare.alphabetical);
		window.location.hash = String.fromCharCode(97 + targetedSquare.alphabetical);
		if(_gaq){_gaq.push(['_trackPageview', "/viewletter.php?letter="+current_letter]);}
		moveCamera(targetedSquare.curX - moveOffset, targetedSquare.curY - edgePadding + 10);
		moveAllSquares(false);
		viewMode = 1;
		
		setTimeout("show_project_content();", animationTime);
		
	}
	
}

function show_project_content(){
	$('body').css("overflow-x", "auto");
	$('#return_square').css("left", parseInt($(targetedSquare.targetDiv).css("left"))-65);
	$('#return_square').css("top", parseInt($(targetedSquare.targetDiv).css("top"))+180);
	$('#return_square').fadeIn(animationTime);
	
	
	$('body').append("<div id=\"project_holder\"></div>");
	
	$('#project_holder').load("featuredprojects.php?letter="+targetedSquare.alphabetical+"&ajax=true",null,function(){
		
		
		
		var first = true;
		var curX = 0;
		var curY = 0;
		
		
		var cur_r = start_r;
		var cur_g = start_g;
		var cur_b = start_b;
		
		var leftCount = 0;
		
		var colour_multiplier = 8;
		var skipped = false;
		$("#feature_holder").css("left", parseInt($(targetedSquare.targetDiv).css("left"))+180);
		$("#feature_holder").css("top", parseInt($(targetedSquare.targetDiv).css("top"))+20);
		
		var sofar = 0;
		var total_num = $('.project_feature').size();
		var pattern = patterns[total_num];
		var curPos = 0;
		
		$('.project_feature').each(function(){
			
			while(pattern[curPos]!="p"){
				//do what the square says
				if(pattern[curPos]=="dtl"){
					
					$('#feature_holder').append("<div class=\"dot_top_left\"></div>");
					$('.dot_top_left').last().css("top", curY).css("left", curX);
					$('.dot_top_left').last().css("background-color", "#" + RGBtoHex(cur_r, cur_g, cur_b));
					
					curX += 150;
					leftCount++;
				}else if(pattern[curPos]=="dtr"){
					
					$('#feature_holder').append("<div class=\"dot_top_right\"></div>");
					$('.dot_top_right').last().css("top", curY).css("left", curX);
					$('.dot_top_right').last().css("background-color", "#" + RGBtoHex(cur_r, cur_g, cur_b));
					
					curX += 150;
					leftCount++;
				}else if(pattern[curPos]=="dbl"){
					
					$('#feature_holder').append("<div class=\"dot_bottom_left\"></div>");
					$('.dot_bottom_left').last().css("top", curY).css("left", curX);
					$('.dot_bottom_left').last().css("background-color", "#" + RGBtoHex(cur_r, cur_g, cur_b));
					
					curX += 150;
					leftCount++;
				}else if(pattern[curPos]=="dbr"){
					
					$('#feature_holder').append("<div class=\"dot_bottom_right\"></div>");
					$('.dot_bottom_right').last().css("top", curY).css("left", curX);
					$('.dot_bottom_right').last().css("background-color", "#" + RGBtoHex(cur_r, cur_g, cur_b));
					
					curX += 150;
					leftCount++;
				}else if(pattern[curPos]=="bs"){
					curX += 150;
					leftCount++;
				} 
				
				
				if(leftCount==5){
					leftCount=0;
					curY+=150;
					curX-=750;
					cur_r -= ((255-start_r)/colour_multiplier)*4;
					cur_g -= ((255-start_g)/colour_multiplier)*4;
					cur_b -= ((255-start_b)/colour_multiplier)*4;
				}
				
				cur_r += (255-start_r)/colour_multiplier;
				cur_g += (255-start_g)/colour_multiplier;
				cur_b += (255-start_b)/colour_multiplier;
				
				curPos++;

			}
			
			if ($(this).attr("content") == "true") {
				$('#feature_holder').append("<div pid=\"" + $(this).attr("pid") + "\" class=\"featured_project plus_sign\"></div>");
			}else {
				$('#feature_holder').append("<div pid=\"" + $(this).attr("pid") + "\" class=\"featured_project\"></div>");
			}
			$('#feature_holder .featured_project').last().css("top", curY).css("left", curX);
			
			$('#feature_holder .featured_project').last().css("background-color", "#" + RGBtoHex(cur_r, cur_g, cur_b));
			
							
			if($(this).attr("image")!=""){
				//$('#feature_holder .featured_project').last().append("<img class=\"thumb_solid\" src=\"thumb.php?src=mceuploads/projects/"+$(this).attr("image")+"&w=150&h=75&zc=1&a=t&f=2|5,"+start_r*0.3+","+start_g*0.3+","+start_b*0.3+",0.1\" />")
				//$('#feature_holder .featured_project').last().append("<img class=\"thumb_fade\" src=\"thumb.php?src=mceuploads/projects/"+$(this).attr("image")+"&w=150&h=75&zc=1&a=t\" />")
				$('#feature_holder .featured_project').last().append("<img class=\"thumb_solid\" src=\"mceuploads/thumbs/"+$(this).attr("data-thumb-bw")+"\" />")
				$('#feature_holder .featured_project').last().append("<img class=\"thumb_fade\" src=\"mceuploads/thumbs/"+$(this).attr("data-thumb")+"\" />")
				$('#feature_holder .featured_project').last().children('img').load(function(){
					$(this).fadeIn();
						
				});
			}
						
			$('#feature_holder .featured_project').last().append("<div class=\"feature_title futura\" >"+$(this).html()+"</div>");
			sofar++;
			curX += 150;
			leftCount++;
			if(leftCount==5){
				leftCount=0;
				curY+=150;
				curX-=750;
				cur_r -= ((255-start_r)/colour_multiplier)*4;
				cur_g -= ((255-start_g)/colour_multiplier)*4;
				cur_b -= ((255-start_b)/colour_multiplier)*4;
			}
			
			cur_r += (255-start_r)/colour_multiplier;
			cur_g += (255-start_g)/colour_multiplier;
			cur_b += (255-start_b)/colour_multiplier;
			
			curPos++;
			
		});
		
		
		if(leftCount==0){
			curY -= 150;
		}
		
		$('#project_list').css("left", parseInt($(targetedSquare.targetDiv).css("left"))+180);
	$('#project_list').css("top", parseInt($(targetedSquare.targetDiv).css("top"))+curY+180);
	//load a project list into the list box, on complete fade it in
	
	$('#project_list').load("projectlist.php?letter="+targetedSquare.alphabetical+"&ajax=true",null,function(){
		$('#project_list').fadeIn(animationTime,function(){
			blocked = false;
		});
		Cufon.refresh();
		
	});
		
		
		
		
		$('#feature_holder .featured_project').click(function(){
			show_project(parseInt($(this).attr('pid')));
		})
		
		$('#feature_holder .featured_project .thumb_fade').css('opacity', 0);
		$('#feature_holder .featured_project .thumb_fade').css('display', "block");
		
		$('#feature_holder .featured_project').mouseover(function(){
			$(this).children('.thumb_fade').stop().fadeTo(350, 1);
		})
		
		$('#feature_holder .featured_project').mouseout(function(){
			$(this).children('.thumb_fade').stop().fadeTo(350, 0);
		})
		
		$("#feature_holder").fadeIn();
			Cufon.refresh();
	});
	
	
}



function show_project(id){
	$('html, body').animate({scrollTop:0}, 'fast');
	$('#loading_div').css('display','block');
	$('#loading_div').fadeTo(0,1);
	viewMode = 2;
	//alert(id);
	$('#project_info_holder').css('display', 'block');

	$('#top_panel').stop().animate({
		height: 500
	},animationTime);
		
	$('#bottom_panel').stop().animate({
		top: 500,
		height: $(window).height()-500
	}, animationTime,null,function(){
		load_project_content(id);
	})
	if(_gaq){_gaq.push(['_trackPageview', "/viewproject.php?project="+slugs[id]]);}
	window.location.hash = slugs[id];
	
}

function load_project_content(id){

	$('#carousel_holder').load('get_carousel.php?id='+id+'&ajax=true', null, function(){
		init_carousel();
	});
	$('#bottom_panel_text').load("get_project_text.php?id="+id+"&ajax=true",null,function(){

		contentIsLoaded();
	});
}

function init_carousel(){




	var curX = 0;
	num_objects = $('.carousel_object').size();
	$('.carousel_object').each(function(){
		
		$(this).css('left', curX);
		curX+= 650;
	})
	
	if(carousel_num >= (num_objects-1)){
		$('#right_arrow').css("background-image", "none");
	}
		
	if(carousel_num == 0){
		$('#left_arrow').css("background-image", "none");
	}
	
	$('#right_arrow').click(function(){
		if (carousel_num < num_objects - 1) {
			$('#carousel_inner').animate({
				left: "-=650"
			}, animationTime);
			carousel_num++;	
		}
		
		if(carousel_num == (num_objects-1)){
			$('#right_arrow').css("background-image", "none");
		}else{
			$('#right_arrow').css("background-image", "url(../_graphics/right_arrow.png)");
		}
		
		if(carousel_num == 0){
			$('#left_arrow').css("background-image", "none");
		}else{
			$('#left_arrow').css("background-image", "url(../_graphics/left_arrow.png)");
		}
		
		return false;
	});
	
	$('#left_arrow').click(function(){
		if (carousel_num >0) {
			$('#carousel_inner').animate({
				left: "+=650"
			}, animationTime);
			carousel_num--;	
		}
		
		if(carousel_num == (num_objects-1)){
			$('#right_arrow').css("background-image", "none");
		}else{
			$('#right_arrow').css("background-image", "url(../_graphics/right_arrow.png)");
		}
		
		if(carousel_num == 0){
			$('#left_arrow').css("background-image", "none");
		}else{
			$('#left_arrow').css("background-image", "url(../_graphics/left_arrow.png)");
		}
		
		return false;
	});
	
	needDrag = false;
	imageLoadCount = 0;
	$('.carousel_object').each(function(){
		$(this).children('img').load(function(){
			imageLoadCount++;
			$(this).parent().css("left", (parseInt($(this).parent().css('left')))+(325-$(this).width()*0.5));
			if($(this).height()>350){
				needDrag = true;
			}
			$(this).parent().draggable({
	   	 		axis: 'y',
	    		containment: [0,473-$(this).height(),0,123],
				scroll: 'false'
			});

			
		if (needDrag&&imageLoadCount==$('.carousel_object img').size()) {
		
			$('#drag_icon').stop().fadeTo(400, 1);
			$("#carousel").mouseover(function(){
				$('#drag_icon').stop().fadeTo(400, 0);
			})
			
			$("#carousel").mouseout(function(){
				$('#drag_icon').stop().fadeTo(400, 1);
			})
			
		}
					
		})
	})
	
	
	
	
}

function contentIsLoaded(){

	$('#project_info_holder').css('display', 'block');
	contentLoaded = true;
	//hide all the background stuff to make processing better
	$('#mainwrap').css('display', 'none');
	//$('#bottom_panel').css('height','auto');
	$('#loading_div').fadeOut(animationTime,function(){
		Cufon.refresh();
		$('#bottom_panel_text').fadeIn(animationTime);	
	});
	$('#return2').fadeIn(animationTime);
	
	$('#bottom_panel').height($('#bottom_panel_text').height()+20);
	
	$('#return2').click(function(){
		hide_project();
	})
	
	$('#return2').mouseover(function(){
		$('#return2_square_dark').stop().fadeTo(40, 1);
	})
	
	$('#return2').mouseout(function(){
		$('#return2_square_dark').stop().fadeTo(400, 0);
	})
	
}

function hide_project(){
	if (current_letter != "!") {
		window.location.hash = current_letter;
		if(_gaq){_gaq.push(['_trackPageview', "/viewletter.php?letter="+current_letter]);}
	}else {
		window.location.hash = "";
		if(_gaq){_gaq.push(['_trackPageview', "/index.php"]);}
	}
	contentLoaded = false;
	//show background stuff again
	$('#mainwrap').css('display', 'block');
	$('html, body').animate({scrollTop:0}, 'fast');
	viewMode = 1;
	//alert(id);
	carousel_num = 0;
	//fade out the return button, once finished get rid of everything else
	
		$('#top_panel').stop().animate({
			height: 0
		},animationTime);
			
		$('#bottom_panel').stop().animate({
			top: 0,
			height: 0
		}, animationTime,null,function(){
			$('#project_info_holder').css('display', 'none');
			$('#return2').css('display','none')
			$('#bottom_panel_text').css('display','none');
			$('#bottom_panel_text').html('');
			$('#carousel_inner').remove();
		});
			
}

function hide_project_content(){
	$('body').css("overflow-x", "hidden");
	window.location.hash = "";
	if(_gaq){_gaq.push(['_trackPageview', "/index.php"]);}
	current_letter = "!";
	$('#return_square').fadeOut(animationTime);
	$('#project_list').fadeOut(animationTime);
	$('#feature_holder div').fadeOut(animationTime,function(){
		$(this).remove();
		
	});
	
	$('#project_holder').remove();
	
	$(".drawing_square").css("cursor", "pointer");
	
	setTimeout("$('body').css(\"overflow-x\", \"auto\");blocked = false;", animationTime*2)
	
}

function moveSquare(newX, newY){
	this.curX = newX;
	this.curY = newY;
	this.moveToPosition();
}

function moveToPosition(instant){
	
	if(instant){
		$(this.targetDiv).css("left", this.curX - camX + originX)
		$(this.targetDiv).css("top", this.curY - camY + originY)
		this.checkDisplay();
	}else{
		$(this.targetDiv).stop().animate({
			left: this.curX - camX + originX,
			top: this.curY - camY + originY,
			opacity: this.curAlpha
		}, animationTime,null,checkDisplay);

	}
	
}

function checkDisplay(){
	for (var i = 0; i < squaresArray.length; i++) {
		if(squaresArray[i].targetDiv == this){
			if(squaresArray[i].displayed==false){
				$(this).css("display", "none");
			}else{
				$(this).css("display", "block");
			}
		}
	}	
}

function updatePosition(pos){
	//calculate the newX and newY values of a square based on its new position starting from 0
	
	//calc the row first
	
	var usableWidth = $('body').width()-(edgePadding*2);
	var aSquareWidth = squareWidth+squarePadding;
	var numPerRow = Math.floor(usableWidth/(squareWidth+squarePadding));
	var newSquareWidth = usableWidth/numPerRow;
	var counter = 0;
	var tempPos = pos;
	while(tempPos>-1){
		counter++;
		tempPos-= numPerRow;
	}
	//counter now holds the row we're in indexed from 1
	counter--;
	
	this.curY = edgePadding+counter*(squareWidth+squarePadding)-10;
	
	//now calc the X position
	
	var remainder = pos%numPerRow;
	this.curX = (edgePadding+remainder*newSquareWidth)-10;
}

function moveCamera(newX, newY){
	camX = newX;
	camY = newY;
	//moveAllSquares(false);
}

function moveAllSquares(instant){
	for (var i = 0; i < squaresArray.length; i++) {
		squaresArray[i].moveToPosition(instant);
	}
}

function compareDates(a, b) {
	return b.datemodified - a.datemodified;
}

function compareQuantity(a, b) {
	return b.quantity - a.quantity;
}

function compareFeatured(a,b){
	return b.featured-a.featured;
}

function compareAlpha(a, b) {
	if(a.alphabetical>b.alphabetical){
		return 1;	
	}else{
		return -1;
	}
}


function sortByAlpha(instant){

	currentSort = 0;
	squaresArray.sort(compareAlpha);

	finishSort(instant);
}

function sortByQuantity(instant){

	currentSort = 3;
	squaresArray.sort(compareAlpha);
	squaresArray.sort(compareQuantity);
	
	finishSort(instant);

}

function sortByDate(instant){

	currentSort = 1;
	squaresArray.sort(compareAlpha);
	squaresArray.sort(compareDates);
	
	finishSort(instant);
}

function sortByFeatured(instant){

	currentSort = 2;
	squaresArray.sort(compareAlpha);
	squaresArray.sort(compareFeatured);
	
	finishSort(instant);
}

function sortByShuffle(instant){

	currentSort = 4;
	fisherYates(squaresArray);
	
	finishSort(instant);
}

function finishSort(instant){
	if(viewMode==1){
		hide_project_content();
	}
	
	
	moveCamera(0,0);
	for (var i = 0; i < squaresArray.length; i++) {
		$(squaresArray[i].targetDiv).css("display", "block");
		squaresArray[i].displayed = true;
		squaresArray[i].curAlpha = 1;
		squaresArray[i].updatePosition(i);
	}
	if (viewMode == 1) {
		setTimeout("moveAllSquares("+instant+");", animationTime);
	}else{
		moveAllSquares(instant);
	}
	
	viewMode = 0;
}


$(document).ready(function(){

	Cufon.replace(".futura", {hover:true});
	
	//$('body').css("overflow-x", "hidden");
	$(".square_button").css("display", "none");
	$("#drawing_pane").css("padding", "0px");
	var counter = 0;
	//get a ref to all the squares and make them invisible
	$(".drawing_square").each(function(){
		$(this).css("display", "none").css("position", "absolute").css("top", -1000).css("left", 0);
		divsArray[counter] = this;
		counter++;
	})
	
	counter = 0;
	//make all the square objects
	for(var i=0;i<divsArray.length;i++){
		var square = new lettersquare(divsArray[i], i, datesArray[i], quantityArray[i], featuredArray[i]);
		
		if(square.featured>0){
			squaresArray[counter] = square;
			square.displayed = true;
			$(square.targetDiv).css("display", "block")
			$(square.targetDiv).click(squareClicked);
			counter++;
		}
		
	}	
	
	sortByAlpha(true);
	
	
	
	$(window).bind("resize", resizeWindow);
	
	function resizeWindow( e ) {
		if (viewMode == 0) {
			for (var i = 0; i < squaresArray.length; i++) {
				squaresArray[i].updatePosition(i);
			}
			moveAllSquares(false);
		}else if (viewMode == 1){
			//we are in project view mode now
			var moveOffset = 75;
	
			if($('body').width()>projectDisplayWidth){
				moveOffset = 75+($('body').width()-projectDisplayWidth)*0.5
			}
			
			moveCamera(targetedSquare.curX-moveOffset, targetedSquare.curY-edgePadding+10);
			$('#return_square').css("left", parseInt($(targetedSquare.targetDiv).css("left"))-65);
			$('#return_square').css("top", parseInt($(targetedSquare.targetDiv).css("top"))+180);
			moveAllSquares(true);
			
			$("#feature_holder").css("left", parseInt($(targetedSquare.targetDiv).css("left"))+180);
			//$("#feature_holder").css("top", parseInt($(targetedSquare.targetDiv).css("top"))+20);
			
			$('#project_list').css("left", parseInt($(targetedSquare.targetDiv).css("left"))+180);
		//	$('#project_list').css("top", parseInt($(targetedSquare.targetDiv).css("top"))+180);
			
		}else if(viewMode==2){
			//move all the stuff in the background
			var moveOffset = 75;
	
			if($('body').width()>projectDisplayWidth){
				moveOffset = 75+($('body').width()-projectDisplayWidth)*0.5
			}
			
			moveCamera(targetedSquare.curX-moveOffset, targetedSquare.curY-edgePadding+10);
			$('#return_square').css("left", parseInt($(targetedSquare.targetDiv).css("left"))-65);
			$('#return_square').css("top", parseInt($(targetedSquare.targetDiv).css("top"))+180);
			moveAllSquares(true);
			
			$("#feature_holder").css("left", parseInt($(targetedSquare.targetDiv).css("left"))+180);
			//$("#feature_holder").css("top", parseInt($(targetedSquare.targetDiv).css("top"))+20);
			
			$('#project_list').css("left", parseInt($(targetedSquare.targetDiv).css("left"))+180);
			//$('#project_list').css("top", parseInt($(targetedSquare.targetDiv).css("top"))+180);
			

			
		}
	}
	
	$('#li_quantity a').click(function(){
		//sortByQuantity(false);
		sortByFeatured(false);
		return false;
	})
	
	$('#li_alphabetical a').click(function(){
		sortByAlpha(false);
		return false;
	})
	
	$('#li_newest a').click(function(){
		sortByDate(false);
		return false;
	})
	
	$('#li_featured a').click(function(){
		sortByFeatured(false);
		return false;
	})
	
	$('#li_shuffle a').click(function(){
		sortByShuffle(false);
		return false;
	})
	
	$('#return_square').click(function(){
		if (!blocked) {
			blocked = true;
			if (currentSort == 0) {
				sortByAlpha();
			}
			else 
				if (currentSort == 1) {
					sortByDate();
				}
				else 
					if (currentSort == 2) {
						sortByFeatured();
					}
					else 
						if (currentSort == 3) {
							sortByQuantity();
						}
						else 
							if (currentSort == 4) {
								sortByShuffle();
							}
		}
	})
	
	$('#return_square').mouseover(function(){
		
		$('#return_square_dark').stop().fadeTo(40,1);
	})
	
	$('#return_square').mouseout(function(){
		$('#return_square_dark').stop().fadeTo(400,0);
	})
	
	$(".drawing_square").mouseover(function(){
		$(this).children(".index_rollover").stop(true, true).fadeIn();
	});
	
	$(".index_rollover").mouseout(function(){
		$(this).stop(true,true).fadeOut();
	});
	
	//CHECK FOR A HASH IN THE URL
	if (window.location.hash.length > 2) {
		if (window.location.hash == "#about") {
			openAbout();
		}else if (window.location.hash == "#contact") {
			openContact();
		}else if (window.location.hash == "#alphabetical") {
			sortByAlpha(false);
			window.location.hash = "";
		}else if (window.location.hash == "#quantity") {
			sortByFeatured(false);
			window.location.hash = "";
		}else if (window.location.hash == "#shuffle") {
			sortByShuffle(false);
			window.location.hash = "";
		}else if (window.location.hash == "#newest") {
			sortByDate(false);
			window.location.hash = "";
		}else{
			var pro_num = findIdx(window.location.hash, slugs);
			if (pro_num != -1) {
				show_project(pro_num);
			}else{
				window.location.hash = "";
			}
		}
	}else if (window.location.hash.length == 2){
		
		var ascii = window.location.hash.charCodeAt(1);
		var num = ascii - 97;

		var found = -1;

		for (var i = 0; i < squaresArray.length; i++) {
			if(squaresArray[i].alphabetical == num){
				squaresArray[i].displayed = true;
				squaresArray[i].curAlpha = 1;
				found = i;
			}else{
				squaresArray[i].curAlpha = 0;
				squaresArray[i].displayed = false;
			}
		}
	
		if (found!=-1) {
		$('body').css("overflow-x", "hidden");
			if (viewMode == 1 && targetedSquare == squaresArray[found]) {
				if (currentSort == 0) {
					sortByAlpha();
				}else if (currentSort == 1) {
					sortByDate();
				}else if (currentSort == 2) {
					sortByFeatured();
				}else if (currentSort == 3) {
					sortByQuantity();
				}else if (currentSort == 4) {
					sortByShuffle();
				}
				return false;
			}
			var moveOffset = 75;
			
			if ($('body').width() > projectDisplayWidth) {
				moveOffset = 75 + ($('body').width() - projectDisplayWidth) * 0.5
			}
			
			targetedSquare = squaresArray[found];
			//window.location.hash = String.fromCharCode(97+targetedSquare.alphabetical);
			
			current_letter = String.fromCharCode(97+targetedSquare.alphabetical);
			if(_gaq){_gaq.push(['_trackPageview', "/viewletter.php?letter="+current_letter]);}
			moveCamera(targetedSquare.curX - moveOffset, targetedSquare.curY - edgePadding +10);
			moveAllSquares(false);
			viewMode = 1;
			
			setTimeout("show_project_content();", animationTime);
		}else{
			window.location.hash = "";
		}
	}
	
	$("#li_home").click(function(){
		if(viewMode==1){
			if (currentSort == 0) {
				sortByAlpha();
			}else if (currentSort == 1) {
				sortByDate();
			}else if (currentSort == 2) {
				sortByFeatured();
			}else if (currentSort == 3) {
				sortByQuantity();
			}else if (currentSort == 4) {
				sortByShuffle();
			}
		}
		return false;
	})
	
	$("#logo a").click(function(){
		if(viewMode==1){
			if (currentSort == 0) {
				sortByAlpha();
			}else if (currentSort == 1) {
				sortByDate();
			}else if (currentSort == 2) {
				sortByFeatured();
			}else if (currentSort == 3) {
				sortByQuantity();
			}else if (currentSort == 4) {
				sortByShuffle();
			}
		}
		return false;
	})
	
	$("#li_home2").click(function(){
		hide_project();
		return false;
	})
	
	$("#logo2 a").click(function(){
		hide_project();
		return false;
	})
	
	$("#li_home3").click(function(){
		closeAbout();
		return false;
	})

	$("#logo3 a").click(function(){
		closeAbout();
		return false;
	})
	
	$("#li_home4").click(function(){
		closeContact();
		return false;
	})
	
	$("#logo4 a").click(function(){
		closeContact();
		return false;
	})
	
	$("#li_about").click(function(){
		openAbout();
		return false;
	})
	
	$("#li_contact").click(function(){
		openContact();
		return false;
	})
	
	$("#li_about2").click(function(){
		hide_project();
		setTimeout("openAbout();",animationTime);
		return false;
	})
	
	$("#li_about3").click(function(){
		closeAbout();
		return false;
	})
	
	$("#li_about4").click(function(){
		closeContact();
		setTimeout("openAbout();",animationTime);
		return false;
	})
	
	$("#li_contact2").click(function(){
		hide_project();
		setTimeout("openContact();",animationTime);
		return false;
	})
	
	$("#li_contact4").click(function(){
		closeContact();
		return false;
	})
	
	$("#li_contact3").click(function(){
		closeAbout();
		setTimeout("openContact();",animationTime);
		return false;
	})

	
});

function openAbout(){
	$('html, body').animate({scrollTop:0}, 'fast');
		window.location.hash = "about";
		if(_gaq){_gaq.push(['_trackPageview', "/about.php"]);}
	//alert(id);
	$('#about_holder').css('display', 'block');
	
	$('#about_top_panel').stop().animate({
		height: "100%"
	},animationTime,null,function(){
		$('#mainwrap').css('display', 'none');
	});
}

function closeAbout(){
	$('#mainwrap').css('display', 'block');
	$('#about_top_panel').stop().animate({
			height: 0
		}, animationTime,null,function(){
			$('#about_holder').css('display', 'none');
		});
	if (current_letter != "!") {
		window.location.hash = current_letter;
		if(_gaq){_gaq.push(['_trackPageview', "/viewletter.php?letter="+current_letter]);}
	}else {
		window.location.hash = "";
		if(_gaq){_gaq.push(['_trackPageview', "/index.php"]);}
	}
}

function openContact(){
	$('html, body').animate({scrollTop:0}, 'fast');
		window.location.hash = "contact";
		if(_gaq){_gaq.push(['_trackPageview', "/contact.php"]);}
	//alert(id);
	$('#contact_holder').css('display', 'block');
	
	$('#contact_top_panel').stop().animate({
		height: "100%"
	},animationTime,null,function(){
		$('#mainwrap').css('display', 'none');
	});
}

function closeContact(){
		if (current_letter != "!") {
		window.location.hash = current_letter;
		if(_gaq){_gaq.push(['_trackPageview', "/viewletter.php?letter="+current_letter]);}
	}else {
		window.location.hash = "";
		if(_gaq){_gaq.push(['_trackPageview', "/index.php"]);}
	}
	$('#mainwrap').css('display', 'block');

	$('#contact_top_panel').stop().animate({
			height: 0
		}, animationTime,null,function(){
			$('#contact_holder').css('display', 'none');

		});
}


function findIdx(item, arr) {
var idx;
var last = arr.length;
for (var i = 0; i < last; i++) {
idx = (item == "#"+arr[i])?i:-1;
// quit on first "found"
if (-1 != idx) break;
}
return idx;
}


