// Settings
var _slideSpeed			= 30;
var _initialSlidePos1	= 0;
var _initialSlidePos2	= 520;
var _slideStopPos 		= 15;
//*******************
var _direction			= 1;
var _direction0			= 1;
var inProgress			= false;
var underTrans			= new Array(2);



function init() {
	//{{{
	// Wrap up DOM load
	if (arguments.callee.done) return;

	arguments.callee.done = true;

	if (_timer) {
		clearInterval(_timer);
	
		_timer = null;
	}
	
	// Cycle through phpbb tabs
	if(window.phpbb != null) {
		window.phpbbActiveTab = window.phpbb[0];
		setTabActive(window.phpbb[0], "p");
		
		for(var i = 1; i < window.phpbb.length; i++) {
			setTabInactive(window.phpbb[i], "p");
		}
		//window.onunload = function() { window.phpbb = null; }
	}
	
	// Cycle through phpbb3 tabs
	if(window.phpbb3 != null) {
		window.phpbb3ActiveTab = window.phpbb3[0];
		setTabActive(window.phpbb3[0], "p3");
		
		for(var i = 1; i < window.phpbb3.length; i++) {
			setTabInactive(window.phpbb3[i], "p3");
		}
		//window.onunload = function() { window.phpbb = null; }
	}
	
	// Cycle through ipb tabs
	//alert(window.ipb);
	if(window.ipb != null) {
		window.ipbActiveTab	= window.ipb[0];

		setTabActive(window.ipb[0], "i");
		//alert(window.ipb.length);
		for(var i = 1; i < window.ipb.length; i++) {
			//alert(window.ipb[i]);
			setTabInactive(window.ipb[i], "i");
		}
		//window.onunload = function() { window.ipb = null; }
	}
	
	setIntroHeight();
	document.getElementById("content").style.height		= "0px";
	document.getElementById("content").style.display	= "none";
	document.getElementById("intro").onmouseover		= expandIntro;
	document.getElementById("intro").onmouseout			= collapseIntro;
	document.getElementById("intro").onmouseleave		= collapseIntro;
	
	updateStats();

	setInterval(updateStats, 2000);
	
	var flags = getElementsByClass("lang", document.getElementById("flags"));	
	for(var i = 0; i < flags.length; i++) {
		flags[i].onmouseover = switchFlag;
		flags[i].onmouseout = switchFlag;
	}
	
	news = getElementsByClass("newsitem");
	news[0].style.display = "block";
	if(navigator.appName == "Microsoft Internet Explorer")
		news[0].style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
	else
		news[0].style.opacity = "1.0";
	
	for(i = 1; i < news.length; i++) {
		news[i].style.display = "none";
		if(navigator.appName == "Microsoft Internet Explorer")
			news[i].style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=75)";
		else
			news[i].style.opacity = "0.0";
	}
	
	if(news.length > 1) document.getElementById("news-next").onclick = nextNewsClick;
	
	newsticker = setInterval(nextNews, 10000);
	//}}}
}

/* for Mozilla */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			init(); // call the onload handler
		}
	};
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			init(); // call the onload handler
		}
	}, 10);
}

/* for other browsers */
window.onload = init;



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



function setTabActive(tab, prefix) {
	document.getElementById(prefix + "b" + tab).style.display = "block";
	document.getElementById(prefix + "t" + tab).className = "tab active";
	document.getElementById(prefix + "t" + tab).onclick = null;
}

function setTabInactive(tab, prefix) {
	document.getElementById(prefix + "b" + tab).style.display = "none";
	document.getElementById(prefix + "t" + tab).className = "tab";
	document.getElementById(prefix + "t" + tab).onclick = switchTab;
	//alert(prefix + "t" + tab);
}

function slideTab() {
	//{{{
	var slideEnd	= false;
	//alert(_direction + '-' + _direction0);
	//alert(_slideStopPos + ':' + parseInt(underTrans[1].style.left));
	if (_direction < _direction0) {
		if(parseInt(underTrans[1].style.left) < _slideStopPos) {
			underTrans[0].style.left = (parseInt(underTrans[0].style.left) + _slideSpeed) + "px";
			underTrans[1].style.left = (parseInt(underTrans[1].style.left) + _slideSpeed) + "px";
			setTimeout(slideTab, 1);
		} else slideEnd = true;
	} else {
		if(parseInt(underTrans[1].style.left) > _slideStopPos) {
			underTrans[0].style.left = (parseInt(underTrans[0].style.left) - _slideSpeed) + "px";
			underTrans[1].style.left = (parseInt(underTrans[1].style.left) - _slideSpeed) + "px";
			setTimeout(slideTab, 1);
		} else slideEnd = true;
	}

	if (slideEnd) {
		underTrans[0].style.display		= "none";
		underTrans[1].style.overflow	= "auto";
		underTrans[1].style.left		= _slideStopPos;
		inProgress						= false;
		underTrans[0]					= null;
		underTrans[1]					= null;
	}
	//}}}
}

function switchTab(e) {
	//{{{
	if (inProgress) return;
	if (!e) var e = window.event;
	
	var prefix3 	= this.id.substr(0,2);
	var prefix;
	var name;
	if (prefix3 == "p3")
	{
		prefix = prefix3;
		name 		= this.id.substr(3);
	}
	else
	{
		 prefix 		= this.id.substr(0,1);
		 name 		= this.id.substr(2);
	}
	
	var direction 	= this.getAttribute('_dir');
	//alert(this.getAttribute('_dir') + ':' + this.id);
	_direction0		= _direction;
	//alert(11);
	if (direction) _direction = direction;
	
	if(prefix == "p") {
		var tabs		= window.phpbb;
		var activeTab	= window.phpbbActiveTab;

		window.phpbbActiveTab	= name;
		underTrans[0]			= document.getElementById(prefix + "b" + activeTab);
		underTrans[1]			= document.getElementById(prefix + "b" + name);
	} else 
	if (prefix == "p3")
	{
		var tabs		= window.phpbb3;
		var activeTab	= window.phpbb3ActiveTab;

		
		window.phpbb3ActiveTab	= name;
		underTrans[0]			= document.getElementById(prefix + "b" + activeTab);
		underTrans[1]			= document.getElementById(prefix + "b" + name);
	}
	else {
		var tabs		= window.ipb;
		var activeTab	= window.ipbActiveTab;
		//alert(tabs);	
		window.ipbActiveTab = name;

		underTrans[0] = document.getElementById(prefix + "b" + activeTab);
		underTrans[1] = document.getElementById(prefix + "b" + name);
	}
	
	underTrans[0].style.position	= "absolute";
	underTrans[1].style.position	= "absolute";
	underTrans[0].style.overflow	= "hidden";
	underTrans[1].style.overflow	= "hidden";

	if (_direction < _direction0) {
		underTrans[0].style.left		= -(_initialSlidePos1) + "px";
		underTrans[1].style.left		= -(_initialSlidePos2) + "px";
	} else {
		underTrans[0].style.left		= _initialSlidePos1 + "px";
		underTrans[1].style.left		= _initialSlidePos2 + "px";
	}

	
	tu1 = underTrans[0].id.substr(0,2);
	if (tu1 == 'p3')
	{
		 arg2_0 = tu1;
		 arg1_0 = underTrans[0].id.substr(3);
	}
	else
	{
	     arg2_0 = underTrans[0].id.substr(0,1);	
	     arg1_0 = underTrans[0].id.substr(2);
	}
	
	tu2 = underTrans[1].id.substr(0,2);
	if (tu2 == 'p3')
	{
		 arg2_1 = tu2;
		 arg1_1 = underTrans[1].id.substr(3);
	}
	else
	{
	     arg2_1 = underTrans[1].id.substr(0,1);	
	     arg1_1 = underTrans[1].id.substr(2);
	}
	
	setTabInactive(arg1_0, arg2_0);
	setTabActive(arg1_1, arg2_1);
	
	underTrans[0].style.display		= "block";
	underTrans[1].style.display		= "block";
	inProgress						= true;
	
	setTimeout(slideTab, 1);

	//{{{ old
	//setTimeout(slideTab, 2);
	//setTimeout(slideTab, 3);
	//setTimeout(slideTab, 4);
	
	/*for(var i = 0; i < tabs.length; i++) {
		if(tabs[i] != name) setTabInactive(tabs[i], prefix);
	}
	setTabActive(name, prefix);*/
	//}}}

	//}}}
}


//var fading = false;
var introHeight = 0;
var state = "collapsed";

function setIntroHeight() {
	if(window.getComputedStyle) {
		introHeight = window.getComputedStyle(document.getElementById('content'), null).height;
	} else if(document.getElementById('content').currentStyle) {
		introHeight = document.getElementById('content').currentStyle.height;
		if(introHeight == "auto") introHeight = document.getElementById('content').offsetHeight;
	}
	introHeight = parseInt(introHeight) + 1;
}

function expandIntro(e) {
	if(!e) var e = window.event;
	if(state != "collapsed" && state != "collapsing") return;
	//document.getElementById("content").style.height = "0px";
	document.getElementById("content").style.display = "block";
	state = "expanding";
	setTimeout(expandIntroStep, 100);
}

function expandIntroStep() {
	if (state != "expanding") return;

	var oldheight = document.getElementById("content").style.height;
	var newheight = parseInt(oldheight) + 5;

	if(newheight < introHeight) {
		document.getElementById("content").style.height = newheight + "px";
		setTimeout(expandIntroStep, 100);
	} else {
		document.getElementById("content").style.height = introHeight + "px";
		state = "expanded";
	}
}

function collapseIntro(e) {
	if(!e) var e = window.event;
	if(state != "expanded" && state != "expanding") return;
	state = "collapsing";
	setTimeout(collapseIntroStep, 100);
}

function collapseIntroStep() {
	if(state != "collapsing") return;
	var oldheight = document.getElementById("content").style.height;
	var newheight = parseInt(oldheight) - 5;
	if(newheight > -1) {
		document.getElementById("content").style.height = newheight + "px";
		setTimeout(collapseIntroStep, 100);
	} else {
		document.getElementById("content").style.display = "none";
		state = "collapsed";
	}
}

function switchIntro(e) {
	if(!e) var e = window.event;
	if(document.getElementById("content").style.display == "none") {
		document.getElementById("content").style.display = "block";
	} else {
		document.getElementById("content").style.display = "none";
	}
}



function getXMLHTTPRequest() {
	try {
		var req =  new XMLHttpRequest();
	} catch(err1) {
		try {
			var req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(err2) {
			try {
				var req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(err3) {
				var req = false;
			}
		}
	}
	return req;
}

var http		= getXMLHTTPRequest();
var statsurl	= "ajax/stats.php";
var updating	= false;
var updates		= 0;

function changeStats(forums, users, threads, posts) {
	document.getElementById("stats_forums").innerHTML = forums;
	document.getElementById("stats_users").innerHTML = users;
	document.getElementById("stats_threads").innerHTML = threads;
	document.getElementById("stats_posts").innerHTML = posts;

	updating = false;
}

function proccessStats() {
	if (http.readyState == 4) {
		if(http.status == 200) {
			changeStats(http.responseXML.getElementsByTagName("forums")[0].childNodes[0].nodeValue, http.responseXML.getElementsByTagName("users")[0].childNodes[0].nodeValue, http.responseXML.getElementsByTagName("threads")[0].childNodes[0].nodeValue, http.responseXML.getElementsByTagName("posts")[0].childNodes[0].nodeValue);
		}
	}
}

function updateStats() {
	bd	= new Date(2007, 03, 25);
	d	= new Date();
	tm	= d.valueOf() - bd.valueOf();
	
	sf	= tm/95543;
	su	= tm/11881;
	st	= tm/3999;
	sp	= tm/1181;

	changeStats(Math.ceil(sf), Math.ceil(su), Math.ceil(st), Math.ceil(sp));
	
	/*
	if (updating == true) return;

	if ((updates % 3) == 0) {
		updating = true;

		var rand = parseInt(Math.random()*999999999999999);
		var requrl = statsurl + "?rand=" + rand;

		http.open("GET", requrl, true);
		http.onreadystatechange = proccessStats;
		http.send(null);
	} else {
	}
	//alert(updates);
	updates++;
	*/
}



function switchFlag(e) {
	if(!e) e = window.event;
	if(this.className == "lang active") {
		this.className = "lang";
	} else {
		this.className = "lang active";
	}
}


var active = 0;
var changeto = -1;
var news = new Array();
var newsticker;

function nextNewsClick() {
	clearInterval(newsticker);
	nextNews();
}

function prevNewsClick() {
	clearInterval(newsticker);
	prevNews();
}

function nextNews() {
	if(changeto > -1) return;
	if(active < (news.length - 1)) {
		changeto = active + 1;
	} else {
		changeto = 0;
	}
	if(changeto == 0) {
		document.getElementById("news-prev").src = "resources/images/arrow_left_disabled.png";
		document.getElementById("news-prev").onclick = null;
	} else {
		document.getElementById("news-prev").src = "resources/images/arrow_left_enabled.png";
		document.getElementById("news-prev").onclick = prevNewsClick;
	}
	if(changeto == (news.length - 1)) {
		document.getElementById("news-next").src = "resources/images/arrow_right_disabled.png";
		document.getElementById("news-next").onclick = null;
	} else {
		document.getElementById("news-next").src = "resources/images/arrow_right_enabled.png";
		document.getElementById("news-next").onclick = nextNewsClick;
	}
	fadeNewsOut();
}

function prevNews() {
	if(changeto > -1) return;
	if(active > 0) {
		changeto = active - 1;
	} else {
		changeto = news.length - 1;
	}
	if(changeto == 0) {
		document.getElementById("news-prev").src = "resources/images/arrow_left_disabled.png";
		document.getElementById("news-prev").onclick = null;
	} else {
		document.getElementById("news-prev").src = "resources/images/arrow_left_enabled.png";
		document.getElementById("news-prev").onclick = prevNewsClick;
	}
	if(changeto == (news.length - 1)) {
		document.getElementById("news-next").src = "resources/images/arrow_right_disabled.png";
		document.getElementById("news-next").onclick = null;
	} else {
		document.getElementById("news-next").src = "resources/images/arrow_right_enabled.png";
		document.getElementById("news-next").onclick = nextNewsClick;
	}
	fadeNewsOut();
}

function fadeNewsOut() {
	if(parseFloat(news[active].style.opacity) > 0 && navigator.appName != "Microsoft Internet Explorer") {
		news[active].style.opacity = parseFloat(news[active].style.opacity) - 0.1;
		setTimeout(fadeNewsOut, 50);
	} else {
		news[active].style.display = "none";
		active = changeto;
		news[active].style.opacity = "0.0";
		news[active].style.display = "block";
		fadeNewsIn();
	}
}

function fadeNewsIn() {
	if(parseFloat(news[active].style.opacity) < 1 && navigator.appName != "Microsoft Internet Explorer") {
		news[active].style.opacity = parseFloat(news[active].style.opacity) + 0.1;
		setTimeout(fadeNewsIn, 50);
	} else {
		changeto = -1;
	}
}
