var globalTimer;
addLoadEvent(initExternalLinks);
addLoadEvent(PopBox_init);
addLoadEvent(toggleAdminStuff);
addLoadEvent(GalleryMagnify_init);

function GalleryMagnify_init() { gallery = getElementsByClassName("gallery profile")[0]; if(gallery != null) { images = gallery.getElementsByTagName("img"); } else { return false; }
	for(var i = 0; i < images.length; i++) { images[i].onclick = function() { if(this.className == "active") { this.className = ""; } else {
		var images = this.parentNode.parentNode.getElementsByTagName("img"); for(var i = 0; i < images.length; i++) { images[i].className = ""; }
		this.className = "active";
	} return false;
} } }

function toggleAdminStuff() {
	var adminLink = document.getElementById("adminStuffToggle");
	if(adminLink != null) { adminLink.onclick = function() {
		var adminStuff = getElementsByClassName("admin");
		this.rel = (this.rel == "block") ? "none" : "block";
		for(var i = 0; i < adminStuff.length; i++) { adminStuff[i].style.display = this.rel; }
		return false;
	} }
}

function PopBox_init() { var popBox = new PopBox(getElementsByClassName("popBox_link")); }
function PopBox(links) { this.links = links; var thisObject = this; for(var i = 0; i < this.links.length; i++) { this.links[i].onclick = function() { thisObject.doPop(this.rel); } } }
PopBox.prototype.doPop = function(id) { var popBox = document.getElementById(id), popBox_close = popBox.getElementsByClassName("popBox_close")[0], thisObject = this; popBox.style.display = "block"; popBox_close.onclick = function() { thisObject.unPop(this.parentNode); }; return false; }
PopBox.prototype.unPop = function(el) { el.style.display = "none"; return false; }
		
function initExternalLinks() {
	var hyperlinks = getElementsByRelName("external");
	for(var i = 0; i < hyperlinks.length; i++) { hyperlinks[i].target = "_blank"; }
}

// Helper functions...
function setOpacity(element, opacity) {
	element.style.opacity = opacity / 10;
	element.style.filter = 'alpha(opacity=' + opacity * 10 + ')';
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function') { window.onload = func; } else {
		window.onload = function() {
			if(oldonload) { oldonload(); }
			func();
		}
	}
}

function getElementsByClassName(classname) {
	var node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i = 0, j = els.length; i < j; i++) if(re.test(els[i].className)) a.push(els[i]);
	return a;
}

function getElementsByRelName(relname) {
	var node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + relname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i = 0, j = els.length; i < j; i++) if(re.test(els[i].rel)) a.push(els[i]);
	return a;
}

function clearGlobalTimer() { //clearInterval(globalTimer); }
}

// Tween Class
// David Kirkbride, 1st May 2008
function Tween(el, attr, tMin, tMax, inc, vel) {
	this.el = el;
	this.attr = attr;
	this.tMin = tMin;
	this.tCur = this.tMin;
	this.tMax = tMax;
	this.inc = inc;
	this.vel = vel;
	


	this.start();
}

Tween.prototype.start = function() {	
	this.updatePos();
	var t = this;
	globalTimer = setInterval(function() { t.tween(); }, this.vel);
}

Tween.prototype.updatePos = function() {
	switch(this.attr) {
		case "x": this.el.style.left = this.tCur + "%"; break;
		case "y": this.el.style.top = this.tCur + "%"; break;
		case "a":
			if(!document.all) {
				this.el.style.opacity = this.tCur / 10;
			} else {
				this.el.style.filter = "alpha(opacity=" + this.tCur / 100 + ")";
			}
		break;
	}
}

Tween.prototype._onTweenStopped = function() {
	clearInterval(globalTimer);
	this.onTweenStopped();
}

Tween.prototype.onTweenStopped = function() {
	return false;
}

Tween.prototype.tween = function() {
	//if(this.tMin < this.tMax) {
		//if(this.tCur < this.tMax) { this.tCur += this.inc; } else { this._onTweenStopped(); }
//	} else {
		if(this.tCur >= (this.tMax + 5)) { this.tCur -= this.inc; } else { this._onTweenStopped(); }
	//}
	this.updatePos();
}
