// ------------------------------------------------------------------------------------------------------------------

// 	handy vars

var ie = navigator.userAgent.indexOf("MSIE") != -1 && !window.opera;
var ie6 = ie && navigator.userAgent.indexOf("MSIE 6") != -1;
var ie7 = ie && navigator.userAgent.indexOf("MSIE 7") != -1;
var ie8 = ie && navigator.userAgent.indexOf("MSIE 8") != -1;
var ff = navigator.userAgent.indexOf("Firefox") != -1;
var safari = navigator.userAgent.indexOf("Safari") != -1;

// ------------------------------------------------------------------------------------------------------------------

// 	kewllll... e.g. multiple onloads, multiple onmouseovers...

function addevent(obj, ev, func) {
	if (obj.addEventListener) obj.addEventListener(ev, func, false);
	else if (obj.attachEvent) obj.attachEvent("on" + ev, func);
	obj = null;
}
function deleteevent(obj, ev, func) {
	if (obj.removeEventListener) obj.removeEventListener(ev, func, false);
	else if (obj.detachEvent) obj.detachEvent("on" + ev, func);
	obj = null;
}




// ------------------------------------------------------------------------------------------------------------------

// 	whoa...! returns object or array of objects (given by parameters)

function $() {
	es = new Array();
	for (i=0; i<arguments.length; i++) {
		e = arguments[i];
		if (typeof e == "string") e = document.getElementById(e);
		if (arguments.length == 1) return e;
		es.push(e);
	}
	return es;
}



// ------------------------------------------------------------------------------------------------------------------

// 	fade in and out

function fadein(whatdiv, speed) {
var a = arguments;
if (a.length == 3) step = a[2]; else step = 0.025;
	if (document.all) {
try {
		$(whatdiv).style.filter = "blendTrans(duration=" + speed + ")";
		$(whatdiv).filters.blendTrans.apply();
		$(whatdiv).style.visibility = "visible";
		$(whatdiv).filters.blendTrans.play();
} catch(e) { }
	} else {
		$(whatdiv).style.opacity = 0;
		$(whatdiv).style.mozOpacity = 0;
		$(whatdiv).style.visibility = "visible";
		for (var i = 0; i <= 2; i += step) {
			j = Math.ceil(i * 100) / 100;
			if (j > 1) j = 1;
			window.setTimeout("$('" + whatdiv + "').style.opacity=" + j, j * 1000 * speed);
			window.setTimeout("$('" + whatdiv + "').style.mozOpacity=" + j, j * 1000 * speed);
			if (j == 1) break;
		}
	}
}

function fadeout(whatdiv,speed) {
	if (document.all) {
		$(whatdiv).style.filter = "blendTrans(duration=" + speed + ")";
		$(whatdiv).filters.blendTrans.apply();
		$(whatdiv).style.visibility = "hidden";
		$(whatdiv).filters.blendTrans.play();
	} else {
		$(whatdiv).style.opacity = 1;
		$(whatdiv).style.mozOpacity = 1;
		$(whatdiv).style.visibility = "visible";
		for (var i = 0.05; i <= 2; i += step) {
			j = Math.floor(i * 100) / 100;
			if (j > 1) j = 1;
			window.setTimeout("$('" + whatdiv + "').style.opacity=" + (1 - j), j * 1000 * speed);
			window.setTimeout("$('" + whatdiv + "').style.mozOpacity=" + (1 - j), j * 1000 * speed);
			if (j == 1) {
				window.setTimeout("$('" + whatdiv + "').style.visibility='hidden'", j * 1000 * speed);
				break;
			}
		}
	}
}



// ------------------------------------------------------------------------------------------------------------------

// 	could come out handy...

function getElementsByClass(q,node,tag) {
	var ce = new Array();
	if (node == null) node = document;
	if (tag == null) tag = "*";
	var e = node.getElementsByTagName(tag);
	var l = e.length;
	var pattern = new RegExp('(^|\\s)'+q+'(\\s|$)');
	for (i=0, j=0; i<l; i++) {
		if ( pattern.test(e[i].className) ) {
			ce[j] = e[i];
			j++;
		}
	}
	node = null;
	return ce;
}



// ------------------------------------------------------------------------------------------------------------------

// 	nifty :-)

Array.prototype.inArray = function (value) {
	for (i=0; i<this.length; i++) if (this[i] == value) return true;
	return false;
};



// ------------------------------------------------------------------------------------------------------------------

// 	javascript UF8 encode/decode !!!

var utf8 = {
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while (i < utftext.length) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}



// ------------------------------------------------------------------------------------------------------------------

// 	php functions port (why don't they have these in javascript eh??)

function str_replace(needle, rep, haystack) {
	if (haystack == "" || needle == "" || haystack == null) return "";
	var fakerep = "££%%";
	while (haystack.indexOf(needle) != -1) haystack = haystack.replace(needle, fakerep);
	while (haystack.indexOf(fakerep) != -1) haystack = haystack.replace(fakerep, rep);
	return haystack;
}
function trim(str) {
	if (str == "") return "";
	return str.replace(/^\s+/,'').replace(/\s+$/,'');
}
function ord(c) {
	return c.charCodeAt(0);
}
function chr(n) {
	return String.fromCharCode(n);
}
function addslashes(str) {
	return str.split(chr(39)).join(chr(92)+chr(39));
}
function stripslashes(str) {
	return str_replace(chr(92), "", str);
}
function strip_tags(str) {
	return str.replace(/<[^>]*>/gi, '') ;
}


// ------------------------------------------------------------------------------------------------------------------

// CUSTOM FUCTIONS - based on above
// kill alt tags onload, check for mouseovers and preload 

var u_preloads = new Array();
addevent(window,"load",function () {
	var im = document.getElementsByTagName("img");
	for (i=0; i<im.length; i++) {
		if (im[i].getAttribute("noalt") != null) im[i].setAttribute("alt","");
		if (im[i].getAttribute("swap") != null) {
			soff = im[i].getAttribute('src'); son = soff.replace("_off","_on");
			u_preloads[i] = new Image(); u_preloads[i].src = son;
			im[i].onmouseover = new Function("this.src='"+son+"'");
			im[i].onmouseout   = new Function("this.src='"+soff+"'");
		}
	}
	var l = document.location.href.substring(7, 99) + "/";
	var thisdomain = l.substring(0, l.indexOf("/"));
	var ah = document.getElementsByTagName("a");
	for (i=0; i<ah.length; i++) {
		if (ah[i].href.indexOf(thisdomain) == -1 && ah[i].href.indexOf("javascript:") == -1 && ah[i].href.indexOf("mailto:") == -1) {
			ah[i].setAttribute("target", "_blank", 0);
		}
	}
});

var mouse_sx, mouse_sy;
var mouse_dx, mouse_dy;
var mouse_ex, mouse_ey;
var mouse_obj;

addevent(document, "mousemove", function (e) {
	if (!e) e = window.event;

	if (e.target) t = e.target; else t = e.srcElement;

	mouse_obj = t;
	mouse_sx = e.screenX;
	mouse_sy = e.screenY;
	mouse_dx = e.clientX;
	mouse_dy = e.clientY;
	mouse_ex = (e.target ? e.clientX : e.offsetX);

	mouse_ey = (e.target ? e.clientY : e.offsetY);
	if (e.target) {
		a = getpos($('thedummy'));
		mouse_ex = mouse_dx - a[0];
		mouse_ey = mouse_dy - a[1];
	}
});



// ------------------------------------------------------------------------------------------------------------------

// 	position functions

function getRealLeft(el){
	if (el == null) return false;
	xPos = $(el).offsetLeft;
	tempEl = $(el).offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}
	return xPos;
}
function getRealLeft2(el){
	if (el == null) return false;
	xPos = $(el).offsetLeft;
	tempEl = $(el).parentNode;
	while (tempEl != null) {
		if (tempEl.tagName == "BODY") break;
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.parentNode;
	}
	return xPos;
}

function getRealTop(el){
	if (el == null) return false;
	yPos = $(el).offsetTop;
	tempEl = $(el).offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	return yPos;
}

function getpos(isID){
	trueX = getRealLeft(isID);
	trueY = getRealTop(isID);
	return Array(trueX,trueY);
}
function getpos2(isID){
	trueX = getRealLeft2(isID);
	trueY = getRealTop(isID);
	return Array(trueX,trueY);
}



// ------------------------------------------------------------------------------------------------------------------

// 	error handling !
var keypressed;
var error_console = new Array();
//if (ie) window.onerror = er;
function er(sMsg,sUrl,sLine,sHuh) {
	var msg = sMsg;
	var file = sUrl;
	var line = sLine;
	var huh = sHuh;
	error_console.push(new Array(new Date().getTime(), msg, line, file, huh));
	return true;
}

var keyspressed = "";
addevent(document, "keydown", function(e) {
	var k = ie ? e.keyCode : e.which;
	keypressed = k;
	keyspressed += "," + k;
	while (keyspressed.substring(0,1) == ",") keyspressed = keyspressed.substring(1,keyspressed.length);
	while (keyspressed.substring(keyspressed.length-1,keyspressed.length) == ",") keyspressed = keyspressed.substring(0,keyspressed.length-1);
	if (keyspressed.indexOf(",17,69") != -1) { // CTRL + E
		keyspressed = "";
		showjserrors();
	}
});
addevent(document, "keyup", function(e) {
	var k = ie ? e.keyCode : e.which;
	keypressed = k;
	keyspressed = str_replace("," + k + ",", "", "," + keyspressed + ",");
	while (keyspressed.substring(0,1) == ",") keyspressed = keyspressed.substring(1,keyspressed.length);
	while (keyspressed.substring(keyspressed.length-1,keyspressed.length) == ",") keyspressed = keyspressed.substring(0,keyspressed.length-1);

});

function showjserrors() {

	var msg ="";
	var e = error_console;
	var now = new Date().getTime();
	for (var i=0; i<e.length; i++) {
		ago = Math.ceil(now - e[i][0]);
		er = e[i][1];
		line = e[i][2];
		file = e[i][3];
		uri = file.split("?")[0].split("/");
		uri = uri[uri.length-1];
		if (file.substring(0,8) == "file:///") {
			file = file.substring(8,999);
			file = str_replace("/", "\\", file);
		}
		msg += "<TR>";
		msg += "<TD WIDTH=70%><B>" + er + "</B></TD>";
		msg += "<TD WIDTH=24%>" + uri + "</TD><TD WIDTH=6% ALIGN=RIGHT>" + line + "</TD>";
		msg += "</TR>";
		if (i<e.length-1) msg += "<TR><TD COLSPAN=3><HR NOSHADE SIZE=1 COLOR=\"#E0E0E0\"></TD></TR>";
	}
	try { erwin.close() }
	catch(e) { void(0) }
	try {
		erwin = window.open("about:blank", "erwin", "width=640,height=480");
	} catch(e) {
		window.setTimeout("showjserrors();", 100); return;
	}
	erwin.document.open();
	erwin.document.write('<HTML><HEAD><TITLE>Javascript Error Console</TITLE><STYLE>* { font-family:tahoma; font-size:11px }</STYLE></HEAD><BODY>');
	erwin.document.write('<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 BORDER=0 WIDTH=100%>'+msg+'</TABLE>');
	erwin.document.write('</BODY></HTML>');
	erwin.document.close();
}




// ------------------------------------------------------------------------------------------------------------
// FW4's re-re-modified AJAX routine
// ------------------------------------------------------------------------------------------------------------
// Usage:
//
// ajax(url[,method][,httpheaders][,cache][,callback])
//
// url: (string): have a guess :-)
// method: (string or false): GET/POST (default GET)
// httpheader: (array or false): headers to be sent e.g. new Array("Connection: Close", "Cookie: firstName=John") - use false if not wanted
// cache: (bool): same guess! - note: seting to false may yield errors due to forged parameter on some websites
// callback: (function or false): ommit if no callback is required, else use function(h,t) { } where h are the headers returned and t is the text returned
//
// notes:
//	if server yield a 302 or 304 header (found, moved, ...) ajax will stop
//	results longer than 32k will go bananas
// ------------------------------------------------------------------------------------------------------------

var http = new Array();
var pid=0;
var o;

function ajax() {
	var a = arguments;
	if (a.length == 0) return; else url = a[0];
	var method = "GET"; if (a.length >= 2) method = a[1];
	var extraheaders = false; if (a.length >= 3) extraheaders = a[2];
	var cache = false; if (a.length >= 4) cache = a[3];
	var callback = false; if (a.length >= 5) callback = a[4];
	var params = ""; if (method == "POST" && url.indexOf("?") != -1) { s = url.split("?"); url = s[0]; params = s[1]; }
	if (!cache) url = url + ((url.indexOf("?") != -1) ? "&" : "?") + "randomajax=" + new Date().getTime();
	var o = http[pid];
	o = getHTTPObject();
	o.open(method, url, true);
	if (extraheaders) {
		for (var i = 0; i < extraheaders.length; i++) {
			h = extraheaders[i].split(": ");
			o.setRequestHeader(h[0], h[1]);
		}
	}
	if (method == "POST") o.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	o.setRequestHeader("Accept-Charset", "ISO-8859-1");
	o.onreadystatechange = function() {
		var h;
		var t;
		var txt;
		var head;
		if (o.readyState == 4) {
			if (o.status > 0) {
				if (callback) {
					t = typeof callback;
					txt = o.responseText.replace(/^\s+/,'').replace(/\s+$/,'');
					head = "HTTP/1.1 " + o.status + " " + o.statusText + "\r\n" + o.getAllResponseHeaders();
					if (t == "function") callback(head, unescape(txt));
					if (t == "string") eval(eval("callback")+"(head,unescape(txt))");
					o = null;
				}
			} else {
				self.status = "AJAX error: " + o.statusText + " (" + o.status + ")";
			}
		}
	};
	o.send(method == "GET" ? null : params);
	pid++;
}


// do not touch below, it'll be screwed up... trust me

function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) {
			try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (E) { xmlhttp = false; }
		}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp = false; }
	}
	return xmlhttp;
}

/*
ajax("File:\\\\C:\\test.doc", "GET", new Array("Connection: Keep-alive"), true, function(h,t) {
	alert(h+t);
});
*/



function getcookie(n) {
	var e;
	var c = document.cookie.split("; ");
	for (var i=0; i<c.length; i++) {
		e = c[i].split("=");
		if (e[0] == n) return e[1];
	}
	return false;
}
function setcookie(name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	loc = document.location.href.substring(7,99);
	loc = loc.substring(0, loc.indexOf("/"));
	document.cookie = name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+";path=/;domain="+(!ie?".":"")+loc);
}



