//                                                                         //
// ______    comments are for pussies            __________                //
// ___  /_________      ________________________ __  /___(_)__   ______    //
// __  __ \_  ___/_______  ___/_  ___/  _ \  __ `/  __/_  /__ | / /  _ \   //
// _  /_/ /  /   _/_____/ /__ _  /   /  __/ /_/ // /_ _  / __ |/ //  __/   //
// /_.___//_/           \___/ /_/    \___/\__,_/ \__/ /_/  _____/ \___/    //
//                                     (C)2010 oliver@br-creative.com      //

if (/msie/i.test (navigator.userAgent)) {
	document.nativeGetElementById = document.getElementById;
	document.getElementById = function(id) {
		var elem = document.nativeGetElementById(id);
		if(elem) {
			if(elem.attributes['id'].value == id)
				return elem;
			else {
				for(var i=1;i<document.all[id].length;i++) {
					if(document.all[id][i].attributes['id'].value == id) 
						return document.all[id][i];
				}
			}
		}
		return null;
	};
}

// screen centering
function centerit(ele) {
	var c = document.getElementById(ele);
	var s = c.style;
	var screen = screendim();
	var cx = c.offsetWidth || screen.w;
	var cy = c.offsetHeight || screen.h;

	s.top    = ((screen.h - cy)>0)?(screen.h - cy)/2+"px":"0px";
	s.left   = ((screen.w - cx)>0)?(screen.w - cx)/2+"px":"0px";
	s.width  = cx+"px";
	s.height = cy+"px";
}

function screendim(){
	var wx, wy;
	if(window.innerWidth) {	// browsers
		wx = window.innerWidth;
		wy = window.innerHeight;
	} else {				// msie
		if(document.documentElement) {	// modern msie
			wx = document.documentElement.clientWidth;
			wy = document.documentElement.clientHeight;
		} else {						// older msie
			wx = document.body.clientWidth;
			wy = document.body.clientHeight;
		}
	}
	return {w:wx, h:wy};	
}

function showkey(theKey) {
	document.getElementById(theKey).style["visibility"] = "visible";
}

function hidekey(theKey) {
	document.getElementById(theKey).style["visibility"] = "hidden";
}

function navscreen(theKey) {
	var strNavi = "No entries found for this key.";
	flevSlideDiv('apDivalternativekeyb',243,580,20,3,0);
	// getting rid of the flash movie
	if(document.getElementById("apDivleftscreen2").style.display != "none")
		document.getElementById("apDivleftscreen2").style.display = "none";
		
	// clearing content screens
	letter=theKey;
	if(!mxrunning) {
		document.getElementById("mainscreencontent").innerHTML = "<div id=\"thematrix\"></div>";	
		startthematrix(theKey);
		// document.getElementById("apDivrightscreen").style.visibility = "hidden";
		document.getElementById("apDivrightscreen").innerHTML = '<img id="rsi" class="rightscreenimage" src="/images/anim/rsanim.gif" />';
	}	
	// building the navigation list
	if(navHTM[theKey]) {
		var t = new Array();
		var e = navHTM[theKey];
		for(var x in e) 
			t.push('<span class="navlink" onClick="showonmain(\'' + e[x][1] + '\');">' + e[x][0] + '</span>');
		strNavi = t.join('<br />');	
	}
	document.getElementById("apDivnavscreen").innerHTML = strNavi;
}

function showonmain(thePage) {
	//in case of an external page open new window
	if(thePage.substr(0,4) == "http") {
		var newwin = window.open(thePage, "_external");
		newwin.focus();
		return;
	}

	// else update content screen(s)
	stopthematrix();
	var tmpCnt = "";
	document.getElementById("mainscreencontent").innerHTML = '<span style="padding-top:7em;color:#666">page loading...</span>';
	switch(thePage) {
		case "galleries":
			tmpCnt = getpage("/galleries/index_htm.php");
			break;
		case "hacking":
			flevSlideDiv('apDivalternativekeyb',243,732,20,3,0);
			tmpCnt = getpage("crpage.php?snip=hacking");
			break;
		case "_feedback":
			var f = document.forms["feedback"].elements;
			var fsubj = f["fb_subject"].value;
			var fbody = f["fb_body"].value;
			var pstrply = postpage("/feedback.php", "subject="+fsubj+"&body="+fbody);
			if(pstrply == "OK") 
				tmpCnt = getpage("crpage.php?snip=_feedback");
			break;
		default:
			tmpCnt = getpage("crpage.php?snip=" + thePage);
	}
	document.getElementById("mainscreencontent").innerHTML = (tmpCnt) ? tmpCnt : "### PAGE ERROR ###";
	
	if(imgScreen[thePage]) {
		strRS = (imgScreen[thePage][1]) ? '<a href="'+imgScreen[thePage][1]+'" target="_new" style="border: 0px;"><img id="rsi" class="rightscreenimage" src="' + imgScreen[thePage][0] + '" /></a>' : '<img id="rsi" class="rightscreenimage" src="' + imgScreen[thePage][0] + '" />';
		document.getElementById("apDivrightscreen").innerHTML = strRS;
		document.getElementById("apDivrightscreen").style.visibility = "visible";
	} else {
// 		document.getElementById("apDivrightscreen").style.visibility = "hidden";
// 		document.getElementById("apDivrightscreen").innerHTML = "";
		document.getElementById("apDivrightscreen").innerHTML = '<img id="rsi" class="rightscreenimage" src="/images/anim/rsanim.gif" />';
		document.getElementById("apDivrightscreen").style.visibility = "visible";
	}
}

// image swapping...
iori = "";
function iswap(ipath){
	iori = document.getElementById("rsi").src;
	document.getElementById("rsi").src = ipath;
}
function irestore(){
	document.getElementById("rsi").src = iori;
}

// flash movies
function movielist() {
	var strMain = getpage("crpage.php?snip=_hackersfunretreat");
	document.getElementById("mainscreencontent").innerHTML = strMain;

	var ret_movies = getpage("/movies.php");
	if(!ret_movies.length) {
		document.getElementById("apDivnavscreen").innerHTML = "";	
		return;
	}
	var movies = ret_movies.split('###');
	var mw = 430;
	var mh = 378;
	var mwidth = mw;
	var mheight = mh;
	var or = mw/mh;
	var t = new Array();
	for(var x in movies) {
		var minfo = movies[x].split(';');
		var mtitle = minfo[0].split('.')[0].replace(/[\W_]/g," ");
		if(minfo[1]) {
			var mr = minfo[1] / minfo[2];
			mwidth = (or<mr) ? mw : minfo[1]*mh/minfo[2];
			mheight = (or<mr) ? minfo[2]*mw/minfo[1] : mh;
		}
		var mplayer = (minfo[0].split('.')[1] == "swf") ? "mshow" : "mplay";
		t.push('<span class="navlink" onClick="'+mplayer+'(\''+minfo[0]+'\','+parseInt(mwidth)+','+parseInt(mheight)+');">' + mtitle + '</span>\n');
	}
	strNavi = t.join('<br />');	
	document.getElementById("apDivnavscreen").innerHTML = strNavi;		
}
function mplay(swf,swidth,sheight) {
	swidth=440;
	sheight=380;
	var tmp = [];
	tmp.push('<object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="playerIE" width="'+swidth+'" height="'+sheight+'">');
	tmp.push('<param name="movie" value="player.swf" />');
	tmp.push('<param name="allowfullscreen" value="false" />');
	tmp.push('<param name="allowscriptaccess" value="always" />');
	tmp.push('<param name="flashvars" value="autostart=true&backcolor=#0a404d&file=/matrix/swf/'+swf+'" />');
	tmp.push('<embed type="application/x-shockwave-flash" id="player" src="player.swf" allowscriptaccess="always" ');
	tmp.push('width="'+swidth+'" height="'+sheight+'" flashvars="autostart=true&backcolor=#0a404d&file=/matrix/swf/'+swf+'" allowfullscreen="false" />');
	tmp.push('</object>');
	document.getElementById("mainscreencontent").innerHTML = tmp.join('');
}
function mshow(swf,swidth,sheight) {
	var tmm = (380-sheight)/2;
	var tmp = [];
	tmp.push('<div id="htmmovie" style="width:440px; height:'+(380-tmm)+'px; text-align:center; vertical-align:middle; margin-top:'+tmm+'px;">');
	tmp.push('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" ');
	tmp.push('width="'+swidth+'" height="'+sheight+'" title="'+swf+'" id="flashmovie">');
	tmp.push('<param name="movie" value="/matrix/swf/'+swf+'" /><param name="play" value="true" />');
	tmp.push('<param name="quality" value="high" /><param name="LOOP" value="false" /><param name="BGCOLOR" value="#02010B" />');
	tmp.push('<embed src="/matrix/swf/'+swf+'" width="'+swidth+'" height="'+sheight+'" play="true" loop="false" quality="high" ');
	tmp.push('pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" ');
	tmp.push('type="application/x-shockwave-flash" bgcolor="#02010B"></embed></object></div>');
	document.getElementById("mainscreencontent").innerHTML = tmp.join('');
}

// AJAX page loading GET & POST
function getpage(theurl){
	xmlhttp=null;
	if (window.XMLHttpRequest)
	  xmlhttp=new XMLHttpRequest();
	else if (window.ActiveXObject)
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	if (xmlhttp!=null) {
		try {
		  xmlhttp.open("GET",theurl,false);
		  xmlhttp.send(null);
		} catch (e) {
			return false;
		}
	  return xmlhttp.responseText;
    } else {
	  alert(lang_XMLHTTP_error);
    }
}
function postpage(theurl,theparams){
	xmlhttp=null;
	if (window.XMLHttpRequest)
	  xmlhttp=new XMLHttpRequest();
	else if (window.ActiveXObject)
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	if (xmlhttp!=null) {
		try {
		  xmlhttp.open("POST",theurl,false);
		  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		  xmlhttp.setRequestHeader("Content-length", theparams.length);
		  xmlhttp.setRequestHeader("Connection", "close");
		  xmlhttp.send(theparams);
		} catch (e) {
			return false;
		}
	  return xmlhttp.responseText;
    } else {
	  alert(lang_XMLHTTP_error);
    }
}

function jumptourl() {
	setTimeout("welcome()",15000);
	var par = location.href.split('?')[1];
	if(par && par.split('=')[0] == "snip")
		showonmain(par.split('=')[1]);
}

strt=0;
strWelcome = "Wake up...           \nThe Matrix has you.           \n\nKnock, knock...        ";
function welcome() {
	if(!document.getElementById("wakeup"))
		return;
	strt++;
	document.getElementById("wakeup").innerHTML = "<pre>" + strWelcome.substring(0, strt) + "</pre>";
	if(strt < strWelcome.length)
		setTimeout("welcome()",250);
	else {
		document.getElementById("mainscreencontent").innerHTML = "<div id=\"thematrix\"></div>";
		startthematrix(' ');
	}
}

