function g(o) {return document.getElementById(o);}

function inArray(arr, v) {
	for(var i in arr) {
		if(arr[i] == v) return true;
	}
	return false;
}

if(typeof Array.prototype.push == 'undefined' ) {
	Array.prototype.push = function() {var i=0;b=this.length,a=arguments;for(i;i<a.length;i++)this[b+i]=a[i];return this.length;};
}

function projectsHTML() {
	var o = new Array();
	for(var i in projects) {
		if(i == 'push') continue;
		var p = projects[i];
		if(viewtag && !inArray(p['tags'], viewtag)) continue;
		if(p['url'].substr(0,7) != 'http://') {
			p['url'] = 'http://bodytag.org/'+p['url'];
		}
		
		o.push('<div class="project">');
		o.push('<div class="projectcontents">');
		o.push('<div class="name"><a href="'+p['url']+'">'+p['title']+'</a></div>');
		o.push('<div class="type">')
		for(var u = 0; u < p['tags'].length; u++) {
			if(u > 0) {
				o.push(', ');
			}
			o.push('<a href="#" onclick="return setTag(\''+p['tags'][u]+'\');" >'+p['tags'][u]+'</a>');
		}
		o.push('</div>');
		o.push('<div class="date">'+p['date']+'</div>');
		if(typeof(p['img']) != 'undefined' && p['img']) {
			if(p['img'].substr(0,7) != 'http://' && p['img'].substr(0,7) != 'thumbs/') {
				p['img'] = 'thumbs/'+p['img'];
			}
			o.push('<div class="img"><a href="'+p['url']+'"><img src="'+p['img']+'" alt="visit this project" /></a></div>');
		}
		if(typeof(p['desc']) != 'undefined' && p['desc']) {
			o.push('<div class="desc">'+p['desc']+'</div>');
		}
		o.push('</div>');
		o.push('</div>');
	}
	return o.join('');
};

function projectsShow() {
	g('projectslist').innerHTML = projectsHTML();
}

var tags;
var viewtag;

function tagsHTML() {
	if(!tags) {
		tags = new Array();
		var tagshash = new Array();
		for(var i in projects) {
			if(i == 'push') continue;
			for(var u = 0; u < projects[i]['tags'].length; u++) {
				if(u == 'push') continue;
				var tag = projects[i]['tags'][u];//.toLowerCase();
				if(typeof(tagshash[tag]) == 'undefined') {
					tagshash[tag] = true;
					tags.push(tag);
				}
			}
		}
	}
	var o = new Array();
	var sel = '';
	if(!viewtag) {
		sel = ' class="selected" ';
	}
	o.push('<a href="#" onclick="return setTag(\'\');" '+sel+'>all</a>');
	
	for(var i in tags) {
		if(i == 'push') continue;
		o.push(', ');
		sel = '';
		if(viewtag == tags[i]) {
			sel = ' class="selected" ';
		}
		o.push('<a href="#" onclick="return setTag(\''+tags[i]+'\');" '+sel+'>'+tags[i]+'</a>');
	}
	return o.join('');
};

function tagsShow() {
	g('tagslist').innerHTML = tagsHTML();
}

function setTag(tag) {
	viewtag = tag;
	tagsShow();
	projectsShow();
	scrollTo(0,0);
	return false;
}


/* ROLEY POLEY */
// If you want to use this on your own site, go to
// http://bodytag.org/bt_melter/

// Script below is designed to melt an entire page, so it's
// quite overkill for glenmurphy.com

var nodes = new Array();
var text = new Array();
var tumble = new Array();
var maxsize = 0;
var count = 0;

function Is(){ 
  this.ver=navigator.appVersion;
  this.agent=navigator.userAgent;
  this.dom=document.getElementById?1:0;
  this.opera5=this.agent.indexOf("Opera 5")>-1;
  this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
  this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
  this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
  this.ie=this.ie4||this.ie5||this.ie6;
  this.mac=this.agent.indexOf("Mac")>-1;
  this.safari = (this.ver.indexOf("Safari")>-1 && this.dom)?1:0; 
  this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
  this.ns4=(document.layers && !this.dom)?1:0;
  this.bw=(this.ie6||this.ie5||this.ie4||this.ns4||this.ns6||this.opera5);
  return this;
}

var is = new Is();

function spin() {
	var root = document.getElementById('titletext');
	if(is.dom && !(is.mac && (is.ie || is.safari))) godeep(root);
	root.style.visibility = 'visible';
	if(is.dom && !(is.mac && is.ie)) sponge();
}

function finished() {
	nodes = null;
	text = null;
	tumble = null;
}

var chartype = Math.floor(Math.random()*3);

function digit() {
	if(chartype == 0)
		return Math.floor(Math.random()*2);
	else if(chartype == 1)
		return ' ';
	else
		return '_';
}

var reg = new RegExp ("\r|\n", "g");
function haschars(s) {
	s = s.replace(reg,'');
	return s.length;
}
	
function godeep(o) {
	for (var i = 0; i < o.childNodes.length; i++) {
		if(o.childNodes[i].childNodes) {
			godeep(o.childNodes[i]);
		}
	  if(o.childNodes[i].nodeName == '#text' && haschars(o.childNodes[i].nodeValue)) {
	  	var p = nodes.length;
			nodes[p] = o.childNodes[i];
			text[p] = o.childNodes[i].nodeValue;//new Array();
			tumble[p] = new Array();
			for(var u = 0; u < o.childNodes[i].nodeValue.length; u++) {
				tumble[p][u] = u;
			}
			if(o.childNodes[i].nodeValue.length > maxsize) {
				maxsize = o.childNodes[i].nodeValue.length;
			}
			tumble[p].sort(randomSort);
			o.childNodes[i].nodeValue = '';
		}
	}
}

function randomSort(w1,w2) {
	return Math.floor(Math.random()*3)-1;
}

function sponge() {
	for (var i = 0; i < nodes.length; i++) {
		if(count < tumble[i].length) {
    	nodes[i].nodeValue += digit(1);
   	}
	}
 	count++;
 	if(count < maxsize) {
  	setTimeout('sponge()',20);
 	}
 	else {
 		count = 0;
  	setTimeout('unsponge()',350);
 	}
}

function repchar(str, ch, pos) {
	var out = '';
	for(var i = 0; i < str.length; i++) {
		if(i == pos) out += ch;
		else out += str.charAt(i);
	}
	return out;
}

function unsponge() {
	for (var i = 0; i < nodes.length; i++) {
		if(count <= tumble[i].length) {
    	nodes[i].nodeValue = repchar(nodes[i].nodeValue, text[i].charAt(tumble[i][count]), tumble[i][count]);
   	}
	}
  count++;
  if(count < 10) setTimeout('unsponge()',20);
	else if(count < maxsize) setTimeout('unsponge()',5);
	else finished();
}
	
window.onload = spin;
