//cleans up image url to return just name, sans extension
function stripurl (url) {
	var cleanurl = url.substring(url.lastIndexOf('/')+1);
	var cleanurl = cleanurl.split('.');
	return cleanurl[0];
}

function gobonus() {
	//var a2s = document.getElementById('content').getElementsByTagName('a');
	var a2s = document.getElementsByTagName('a');
	for (var i = 0, a2sLen = a2s.length; i < a2sLen; i++) {
		if (a2s[i].className == "bonus") {
			a2s[i].onclick = function() {
			//get clean url for id
			var urltostrip = this.href;
			var stripped = stripurl(this.href);
			
			//check if exists. if does, remove. else, create
			if (document.getElementById(stripped)) {
				var d = document.getElementById(stripped);
  				this.parentNode.parentNode.removeChild(d);
  				return false;
			} else {
				var thediv = document.createElement('div');
				thediv.className = "prize";
				thediv.setAttribute('id',stripped);
				var theimage = document.createElement('img');
				theimage.src = this.href;
				theimage.alt = this.title;
				var thep = document.createElement('p');
				thecaption = document.createTextNode(this.title);
				thep.appendChild(thecaption);
				var thehint = document.createElement('p');
				thehint.className = "hint";
				thehinttext = document.createTextNode('Click to clothes.');
				thehint.appendChild(thehinttext);			
				thediv.appendChild(theimage);
				thediv.appendChild(thep);
				thediv.appendChild(thehint);
				thediv.onclick = function() {this.parentNode.removeChild(this);};
				this.parentNode.parentNode.insertBefore(thediv, this.parentNode.nextSibling);
				return false;
			}
			}
		}
		
		// reveal links on bad color combo days
		if (a2s[i].className == "reveal") {
			a2s[i].onclick = revealLinks;
		}
	}	
}

function gosearch() {
	if (!document.getElementById) return ;
	if (!document.getElementById("s")) return false;
	var searchbox = document.getElementById("s");
	if (searchbox) {
		searchbox.onfocus = function () {
			if(this.value == 'search') {
				this.value = '';
			}
			this.className = 'focus';
		}
		searchbox.onblur = function() {
			if(this.value.length == 0) {
				this.value = 'search';
			}
			this.className = 'default';
		}
		
	}
}

function gooutgoing() {
	if (!document.getElementById) return;
	if (!document.getElementById("extras")) return;
	var as = document.getElementById("extras").getElementsByTagName("a");
	for (var i = 0; i < as.length; i++) {
		as[i].onclick = function () {
			var url = this.getAttribute("href").replace(/[\/]/g,".");
			urchinTracker("/outgoing/" + url);
			return true;				
		}
	}
}

// called by gobonus onload
function revealLinks() {
	var revs = document.getElementById("sense").getElementsByTagName("a");
	if (revealed==1) {
		for (var i = 0; i < revs.length; i++) {
			var classy = revs[i].className;
			revs[i].className = classy.substr(0,classy.length-9);
		}		
		revealed = 0;
	} else {
		for (var i = 0; i < revs.length; i++) {
			revs[i].className = revs[i].className + " revealed";
		}
		revealed = 1;
	}
	return false;
}

var revealed;
window.onload =	function(e) {
	gobonus();
	gosearch();
	gooutgoing();
}
