/****************************************************************************/
/* Generic functions */

function getXMLHTTPRequest() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

/****************************************************************************/
/* Visual effects */

function startScroll2(trigger, view, targets, increment, width)
{
	var obj = document.getElementById(view);
	if (!obj.xscroll)		obj.xscroll = 0;
	if (!obj.xscrolli)		obj.xscrolli= 0;

	obj.xscrolli += increment > 0 ? 1 : -1;
	if (obj.xscrolli < 0)	obj.xscrolli = 0;
	if (obj.xscrolli >= targets.length) obj.xscrolli = targets.length-1;

	while (obj.xscrolli > 0 && targets[obj.xscrolli-1] > width - obj.offsetWidth)
		obj.xscrolli--;

	var target = targets[obj.xscrolli];
	if (target > width - obj.offsetWidth)
		target = width - obj.offsetWidth;

	var timeout = window.setInterval(function () {
			obj.xscroll += increment;
			increment+= increment > 0 ? 1 : -1;
			if ((increment > 0 && obj.xscroll >= target)
			 || (increment < 0 && obj.xscroll <= target)) {
				obj.xscroll = target;
				window.clearInterval(timeout);
			}
			obj.style.backgroundPosition = -obj.xscroll + "px 0px";			
		}, 60);
}

/****************************************************************************/
/* Specific actions */

function activateTab(bnodeid, hnodeid, index, url, anchor) {
	var hnode = document.getElementById(hnodeid);
	if (!hnode) return;
	hnode = hnode.firstChild;

	var i = 0;
	while(hnode)
	{
		if (hnode.className == 'tab')
		{
			if (index == i++) {
				hnode.style.display = '';
				if ((typeof url != 'undefined') && (hnode.innerHTML == '')) {

					var closure = function(hnode) {
						var httpRequest = getXMLHTTPRequest();
						httpRequest.onreadystatechange = function () {
							if (httpRequest.readyState == 4) {
								if (httpRequest.status == 200) {
									hnode.innerHTML = httpRequest.responseText;
									if (typeof anchor != 'undefined') {
										window.location.hash = '#'+anchor;
									}
									portal_onTabActivated(index);
								} else {
									hnode.innerHTML = '<div style="text-align: center;">' + httpRequest.status + '</div>';
								}
							}
						};
						httpRequest.open("GET", url, true);
						httpRequest.send("");
					};
					hnode.innerHTML = '<div style="text-align: center; color: rgb(114, 114, 114);">Chargement...</div>';
					closure(hnode);
				} else {
					if (typeof anchor != 'undefined') {
						window.location.hash = '#'+anchor;
					}
				}
			} else {
				hnode.style.display = 'none';
			}
		}
		hnode = hnode.nextSibling;
	}
}


/****************************************************************************/
/* Events */

function portal_onLoad() {
	/* Perform tag detection for direct access into dynamic content */
	var tag = window.location.hash;
	if (tag.substr(1, 4) == 'news') {
		var anchorid = tag.substr(1);
		if (!document.getElementById(anchorid)) {
			activateTab('righttabheader', 'righttabcontent', 1, window.location.pathname+'?a=archives', anchorid);
		}
	} else if (tag.substr(1, 5) == 'raids') {
		activateTab('righttabheader', 'righttabcontent', 2, window.location.pathname+'?a=raids');
	} else if (tag.substr(1, 4) == 'chat') {
		activateTab('righttabheader', 'righttabcontent', 3, window.location.pathname+'?a=chat');
	}

	portal_onTimer();
}

function portal_onTabActivated(index) {
	if (index == 2) {
		raidreport_showloots('raidreport');
	} else if (index == 3) {
		new Chat(document.getElementById('chatmessages'),
				 document.getElementById('chatinput'),
				 document.getElementById('chatbutton'),
				 'chat.php');
	}
}

function portal_onTimer() {
	var obj = document.getElementById('announce');
	var date = new Date();
	var timeout = 1236427200000 - date.getTime(); // Mar 7 2009, 13h00 GMT+1
	if (timeout < 0) {
		obj.innerHTML = (((date.getTime() % 1000) < 800) ? 'J-0 00:00:00' : '');
		setTimeout('portal_onTimer()', 200);
		return;
	}

	var millis = timeout % 1000; timeout = (timeout - millis) / 1000;
	var seconds = timeout % 60; timeout = (timeout - seconds) / 60;
	var minutes = timeout % 60; timeout = (timeout - minutes) / 60;
	var hours = timeout % 24; timeout = (timeout - hours) / 24;

	millis = Math.floor(millis/100);
	seconds= (seconds < 10 ? '0' : '') + seconds;
	minutes= (minutes < 10 ? '0' : '') + minutes;
	hours= (hours < 10 ? '0' : '') + hours;

	obj.innerHTML = 'IRL: J-' + timeout + ' ' + hours + ':' + minutes + ':' + seconds + '.' + millis;
	setTimeout('portal_onTimer()', 100);
}

