var baseURI = 'http://www.gagatree.com/wordpress/wp-content/themes/adepted-10/';

var plakobGettext = { preview: 'Preview' };

// --- Event Handler Functions --- //
// from: http://www.scottandrew.com/weblog/articles/cbs-events
function addEvent(obj, evType, fn, useCapture) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
	else
		alert("Handler could not be attached");
}

function removeEvent(obj, evType, fn, useCapture) {
	if (obj.removeEventListener) {
		obj.removeEventListener(evType, fn, useCapture);
		return true;
	}
	else if (obj.detachEvent) {
		var r = obj.detachEvent("on"+evType, fn);
		return r;
	}
	else
		alert("Handler could not be removed");
}


// --- other functions --- //
function include(uri) {
	// the HTML way cause the DOM way explodes!
	document.write('<script src="'+ uri +'" type="text/javascript"></script>');
}

function HTMLSpecialChars(str) {
	if (typeof str == 'string') {
		str = str.replace(/\</g,'&#060;');
		str = str.replace(/\>/g,'&#062;');
	}
	return str;
}

function print_r(array) {
	if (typeof array == 'object' && array[0]) {
		var str = '';
		for (var i=0; i<array.length; i++) {
			array[i] = print_r(array[i]);
			str += array[i];
			if (array[i+1]) str += ', ';
		}
		str = 'Array( '+ str +')';
		return HTMLSpecialChars(str);
	}
	return HTMLSpecialChars(array);
}

function randomInt(length) {
	if (!length) length = 4;
	return Math.round(Math.random() * Math.pow(10,length));
}

function randomString(length) {
	return randomInt(length).toString();
}

function $(id) {
	return document.getElementById(id);
}

function $tags(name) {
	return document.getElementsByTagName(name);
}

function $new(type, id) {
	var element = document.createElement(type);
	if (id) element.id = id;
	return element;
}

function $text(text) {
	return document.createTextNode(text);
}



// --- Get positon of element --- //
// from: http://www.quirksmode.org/js/findpos.html
function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}// --- Comment preview functions --- //
function commentPreview() {
	var CommentText = $("comment").value;
	var AuthorText = $("author").value;
	var AuthorUrl = $("url").value;
	var CommentElement = $("preview-div");
	if(AuthorText == '')
		AuthorText = "Anonymous";
	CommentElement.innerHTML = '';
	
	var h3 = $new("h3");
	if (AuthorUrl) {
		var a = $new("a");
			a.href = AuthorUrl;
			a.appendChild($text(AuthorText));
		h3.appendChild($text('Comment By '));
		h3.appendChild(a);
	}
	else
		h3.appendChild($text('Comment By '+ AuthorText));
	CommentElement.appendChild(h3);
	
	var p = $new("p");
	CommentText = CommentText.split(/\n/).join('<br />');
	p.innerHTML = CommentText;
	CommentElement.appendChild(p);
	
	CommentElement.style['display'] = 'block';
	return;
}


function addCommentPreview() {
	if ($('comment-buttons')) {
		var previewDiv = $new('div', 'preview-div');
		var button = $new('input');
			button.setAttribute('name', 'preview');
			button.setAttribute('type', 'button');
			button.setAttribute('tabindex', '6');
			button.setAttribute('value', 'Preview');
			button.setAttribute('class', 'button');
			button.setAttribute('onclick', 'commentPreview()');
		$('comment-buttons').appendChild(button);
		document.forms['commentform'].appendChild(previewDiv);
	}
	return;
}
addEvent(window, "load", addCommentPreview);


// --- two column function --- //
function twoCol() {
	// TODO: not split up tags
	
	// Check for the two-col element.
	if ($('two-col')) {
		// Grab the two-col element and create
		// the two new column elemets. As well
		// as a "clearer" element.
		var twoCol = $('two-col');
		var rightCol = $new('div');
			rightCol.setAttribute("style", "float:right;width:48%;");
		var leftCol = $new('div');
			leftCol.setAttribute("style", "float:left;width:48%;");
		var clearCol = $new('br');
			clearCol.setAttribute("style", "clear:both;");
		
		// Split the words into two columns
		var leftWords = new String();
		var rightWords = new String();
		var ogHTML = twoCol.innerHTML;
		var words = ogHTML.split(' ');
		for (var i=0; i<words.length; i++) {
			if ( i < (words.length/2) )
				leftWords += words[i] +' ';
			else
				rightWords += words[i] +' ';
		}
		
		leftCol.innerHTML = leftWords +"</p>";
		rightCol.innerHTML = "<p>"+ rightWords;
		
		twoCol.innerHTML = '';
		twoCol.appendChild(leftCol);
		twoCol.appendChild(rightCol);
		twoCol.appendChild(clearCol);
	}
	return;
}
addEvent(window, "load", twoCol);


// --- Easter Egg --- //
var easterEggKeys = new Array('t','e','h','m','o','n','k','e','y');
var easterEggCurrent = 0;

function easterEgg(e) {
	var key = String.fromCharCode(e.keyCode).toLowerCase();
	if (easterEggKeys[easterEggCurrent].toLowerCase() == key) {
		easterEggCurrent++;
		if (easterEggCurrent >= easterEggKeys.length) {
			loadEasterEgg();
			easterEggCurrent = 0;
		}
	}
	else {
		easterEggCurrent = 0;
	}
}
addEvent(window, 'keydown', easterEgg);

function loadEasterEgg() {
	// see other.css for styles
	var div = $new('div', 'easterEgg');
	var a = $new('a');
		a.setAttribute("href", "javascript:closeEasterEgg();");
	var img = $new('img');
		img.setAttribute("src", "/dropbox/smurfs.png");
		img.setAttribute("alt", "Teh Smurfs!");
	a.appendChild(img);
	div.appendChild(a);
	$tags('body')[0].appendChild(div);
}

function closeEasterEgg() {
	if ($('easterEgg'))
		$tags('body')[0].removeChild($('easterEgg'));
}/* Simple AJAX Code-Kit (SACK) */
/* 2005 Gregory Wild-Smith */
/* www.twilightuniverse.com */
/* Software licenced under a modified X11 licence, see documentation or authors website for more details */

function sack(file){
	this.AjaxFailedAlert = "Your browser does not support the enhanced functionality of this website, and therefore you will have an experience that differs from the intended one.\n";
	this.requestFile = file;
	this.method = "POST";
	this.URLString = "";
	this.encodeURIString = true;
	this.execute = false;

	this.onLoading = function() { };
	this.onLoaded = function() { };
	this.onInteractive = function() { };
	this.onCompletion = function() { };

	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (err) {
				this.xmlhttp = null;
			}
		}
		if(!this.xmlhttp && typeof XMLHttpRequest != "undefined")
			this.xmlhttp = new XMLHttpRequest();
		if (!this.xmlhttp){
			this.failed = true;
		}
	};
	
	this.setVar = function(name, value){
		if (this.URLString.length < 3){
			this.URLString = name + "=" + value;
		} else {
			this.URLString += "&" + name + "=" + value;
		}
	}
	
	this.encVar = function(name, value){
		var varString = encodeURIComponent(name) + "=" + encodeURIComponent(value);
	return varString;
	}
	
	this.encodeURLString = function(string){
		varArray = string.split('&');
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split('=');
			if (urlVars[0].indexOf('amp;') != -1){
				urlVars[0] = urlVars[0].substring(4);
			}
			varArray[i] = this.encVar(urlVars[0],urlVars[1]);
		}
	return varArray.join('&');
	}
	
	this.runResponse = function(){
		eval(this.response);
	}
	
	this.runAJAX = function(urlstring){
		this.responseStatus = new Array(2);
		if(this.failed && this.AjaxFailedAlert){ 
			alert(this.AjaxFailedAlert); 
		} else {
			if (urlstring){ 
				if (this.URLString.length){
					this.URLString = this.URLString + "&" + urlstring; 
				} else {
					this.URLString = urlstring; 
				}
			}
			if (this.encodeURIString){
				var timeval = new Date().getTime(); 
				this.URLString = this.encodeURLString(this.URLString);
				this.setVar("rndval", timeval);
			}
			if (this.element) { this.elementObj = document.getElementById(this.element); }
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
					var totalurlstring = this.requestFile + "?" + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					this.xmlhttp.open(this.method, this.requestFile, true);
				}
				if (this.method == "POST"){
  					try {
						this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded')  
					} catch (e) {}
				}

				this.xmlhttp.send(this.URLString);
				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState){
						case 1:
							self.onLoading();
						break;
						case 2:
							self.onLoaded();
						break;
						case 3:
							self.onInteractive();
						break;
						case 4:
							self.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;
							self.onCompletion();
							if(self.execute){ self.runResponse(); }
							if (self.elementObj) {
								var elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea"){
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							self.URLString = "";
						break;
					}
				};
			}
		}
	};
this.createAJAX();
}