/* Raidreport client side code */

raidreport_classes = new Array(
	'', 'Guerrier', 'Paladin', 'Chasseur', 'Voleur', 'Pr&ecirc;tre',
	'', 'Chaman', 'Mage', 'D&eacute;moniste', '', 'Druide'
);


function json_get(url, params, values, callback) {
	var httpRequest = getXMLHTTPRequest();
	httpRequest.onreadystatechange = function () {
		if (httpRequest.readyState == 4) {
			if (httpRequest.status == 200) {
				eval("var result = " + httpRequest.responseText);
				callback(httpRequest, result);
			} else {
				callback(httpRequest, null);
			}
		}
	}

	if (params instanceof Array) {
		for(var i = 0; i < params.length; i++)
		{
			url += (i == 0 ? '?' : '&') + escape(params[i]) + '=' + escape(values[i]);
		}
	}
	httpRequest.open("GET", url, true);
	httpRequest.send("");
}

function json_submit(id, callback) {
	var frameid = id+'hiddeniframe';
	var form = document.getElementById(id);
	if (document.getElementById(frameid))
		return false;
	var iframe = document.createElement('IFRAME');
	iframe.style.display = 'none';
	iframe.src = 'about:blank';
	iframe.name= iframe.id = frameid;
	document.body.appendChild(iframe);

	var onload = function() {
		if (typeof callback == 'function') {
			var content;
			if (iframe.contentDocument) {
				content = iframe.contentDocument;
			} else if (iframe.contentWindow.document) {
				content = iframe.contentWindow.document;
			} else {
				content = window.frames[frameid].document;
			}
			if (content.location.href == 'about:blank') return;
			eval("var result = " + content.body.innerHTML);
			callback(result);
			document.body.removeChild(iframe);
		}
	};
	if (iframe.addEventListener)
		iframe.addEventListener('load', onload, false);
	else if (iframe.document.addEventListener)
		iframe.document.addEventListener('load', onload, false);
	else if (iframe.attachEvent)
		iframe.attachEvent('onload', onload);
	else window.alert('Could not attach onload event on your browser');

	/* Internet explorer bug fix */
	if(window.frames[frameid].name != frameid) {
		window.frames[frameid].name = frameid;
	}
	form.setAttribute('target', frameid);
	return true;
}


function raidreport_buildrow(fields) {
	var row = document.createElement('tr');
	for (var i = 0; i < fields.length; i++) {
		var col = document.createElement('td');
		if (typeof(fields[i]) == 'object') {
			if (fields[i].nodeName == 'TD') {
				col = fields[i];
			} else {
				col.appendChild(fields[i]);
			}
		} else {
			col.innerHTML = fields[i];
		}
		row.appendChild(col);
	}
	return row;
}

/****************************************************************************/

function raidreport_formatplayer(id, player)
{
	var link = document.createElement('a');
	link.href = '';
	link.onclick = function (event) { var w=window.open('raidreport.php?player='+player.name, 'rrprt'); w.focus(); return false; };
	link.className = 'raidreportclass'+player.classid;
	link.appendChild(document.createTextNode(player.name));
	return link;
}

function raidreport_formatloot(id, loot)
{
	var td = document.createElement('td');
	td.lootuid = loot.uid;
	td.appendChild(document.createTextNode(loot.name));
	return td;
}

function raidreport_formatraid(id, raid)
{
	var link = document.createElement('a');
	link.href = '';
	link.onclick = function (event) { window.alert('kicou'); var w=window.open('raidreport.php?raid='+raid.id, 'rrprt'); w.focus(); return false; };
	link.className = 'raidreportraid';
	link.appendChild(document.createTextNode(raid.date.substr(0, raid.date.indexOf(' '))));
	return link;
}

function raidreport_filltable(id, headers, fragment) {
	var table = document.getElementById(id);
	var body = document.getElementById(id+'body');

	var head = table.createTHead();
	while(head.firstChild) head.removeChild(head.firstChild);

	var headrow = raidreport_buildrow(headers);
	head.appendChild(headrow);

	var newbody = document.createElement('tbody');
	newbody.id = id+'body';
	newbody.appendChild(fragment);
	table.replaceChild(newbody, body);
}

function raidreport_extrarows(fragment, extrarows) {
/*	if (extrarows <= 0) return;
	var row = document.createElement('tr');
	var cell = document.createElement('td');
	cell.style.height = (22*extrarows) +'px';
	row.appendChild(cell);
	fragment.appendChild(row);*/
}

/****************************************************************************/

function raidreport_showloots(id)
{
	json_get('raidreport.php', new Array('a', 'limit'), new Array('lootlist', 50),
		function(request, obj) {
			var fragment = document.createDocumentFragment();
			var lineclass= 0;
			for (var i = 0; i < obj.length; i++)
			{
				var row = raidreport_buildrow(
					new Array(
						raidreport_formatloot(id, obj[i]),
						raidreport_formatraid(id, obj[i].raid),
						raidreport_formatplayer(id, obj[i].player),
						obj[i].comment)
				);
				row.className = "raidreportrow"+lineclass;
				fragment.appendChild(row);
				lineclass = 1 - lineclass;
			}
			raidreport_extrarows(fragment, 12-obj.length);
			raidreport_filltable(id+'select', new Array('Objet', 'Date', 'Joueur', 'Commentaire'), fragment);
			raidreport_lootload(id+'select');
		}
	);
}

function raidreport_showplayers(id)
{
	json_get('raidreport.php', new Array('a'), new Array('playerlist'),
		function(request, obj) {
			var fragment = document.createDocumentFragment();
			var lineclass = 0;
			for (var i = 0; i < obj.length; i++)
			{
				var row = raidreport_buildrow(
					new Array(
						raidreport_formatplayer(id, obj[i]),
						raidreport_classes[obj[i].classid],
						obj[i].joined,
						obj[i].nbraids,
						obj[i].lastloot ? obj[i].lastloot.substr(0, obj[i].lastloot.indexOf(' ')) : 'aucun')
				);
				row.className = "raidreportrow"+lineclass;
				fragment.appendChild(row);
				lineclass = 1 - lineclass;
			}
			raidreport_extrarows(fragment, 12-obj.length);
			raidreport_filltable(id+'select', new Array('Nom', 'Classe', 'Inscription', 'Raids', 'Dernier objet'), fragment);
		}
	);
}

function raidreport_shownewraid(id)
{
	var fragment = document.createDocumentFragment();
	var row = raidreport_buildrow(new Array(
		 '<form id="raidreportuploadform" action="raidreport.php" method="post" enctype="multipart/form-data" onsubmit="return json_submit(this.id, function(msg) {window.alert(msg);})">'
		+'<input type="file" name="chatlog" />'
		+'<input type="submit" />'
		+'<input type="hidden" name="a" value="raidparse" />'
		+'</form>'));
	fragment.appendChild(row);
	raidreport_filltable(id+'select', new Array('Upload de rapport'), fragment);
}

/****************************************************************************/

function raidreport_lootload(id) {
	var uids = new Array();
	var objs = document.getElementById(id).getElementsByTagName('td');

	for (var i = 0; i < objs.length; i++) {
		if (objs[i].lootuid)
			uids.push(objs[i].lootuid);
	}

	json_get('raidreport.php', new Array('a', 'uids'), new Array('lootpostload', uids.join(',')),
		function(request, obj) {
			var j = 0;
			for (var i = 0; i < objs.length; i++) {
				if (objs[i].lootuid) {
					objs[i].innerHTML = obj[j++];
				}
			}
		}
	);
}

