// code yanked from the Yahoo media player. Thanks, Yahoo.
if (! ("console" in window) || !("firebug" in console)) {
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group"
                 , "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
    window.console = {};
    for (var i = 0; i < names.length; ++i) window.console[names[i]] = function() {};
}



/********			Menu			***********/
function showMenu(P_MenuId)
{
	$(P_MenuId).setStyle({display:'block'});
}                                     

function hideMenu(P_MenuId)
{
	$(P_MenuId).hide();
}

/******** 			Tableau			************/
function formatTable(P_TableId)
{
	console.log(P_TableId);
	// last cell (body)
	$(P_TableId).select('td:Last-Child').each(function(E){E.addClassName('right');});
	
	// last cell (head)
	$(P_TableId).select('th:Last-Child').each(function(E){E.addClassName('right');});
	
	// last row
	$(P_TableId).select('tr:Last-Child td').each(function(E){E.addClassName('bottom');});
	
	// first cell (body)
	$(P_TableId).select('td:first-child').each(function(E){E.addClassName('left');});
	
	// first cell (head)
	$(P_TableId).select('th:first-child').each(function(E){E.addClassName('left');});
	
	// first row
	$(P_TableId).select('tr:first-child th').each(function(E){E.addClassName('top');});
	
	// col texte
	// parse head
	var coltype = $H();
	var col_find = false;
	var L_HeadArray = $(P_TableId).select('thead tr:Last-Child th');
	for(var i=0;i<L_HeadArray.length;++i)
	{
		//console.log(P_TableId);
		var L_class= L_HeadArray[i].getAttribute('col_type');
		if(L_class!=null)
		{
			coltype.set(i, L_class);
			col_find = true;
		}
		else
		{
			coltype.set(i, null);
		}
	}
	// set lines
	if(col_find)
	{
		//console.log(coltype.inspect());
		
		$(P_TableId).select('tbody tr').each(function(R){
			var L_Cells = R.select('td');
			var i = 0;
			while(i < L_Cells.length)
			{
			    var L_Class= coltype.get(i);
				if(L_Class!=null)
					L_Cells[i].addClassName(L_Class);
				
				var L_Colspan = L_Cells[i].getAttribute('colspan');
				if(L_Colspan !=null)
					i = i + parseInt(L_Colspan);
				else
					i++;
			}
		});
	}
}

function highlightRow(P_TableId, P_RowName)
{
	$(P_TableId).select('TR').each(
		function(E)
		{
			if(E.readAttribute('idrow') != P_RowName)
			{
				E.select('TD').each(function(R){R.setStyle({opacity:0.2});});
			}
		}
	);
}

function highlightCol(P_TableId, P_ColName)
{
	var L_ColNum = 0;
	
	// get col id
	var L_THs = $(P_TableId).select('TH');
	for(var i=0 ; i<L_THs.length;++i)
	{
		if(L_THs[i].readAttribute('idcol')==P_ColName)
		{
			L_ColNum = i;
		}
	}
	
	// set highlight
	var L_TRs = $(P_TableId).select('TR');
	for(var i=0 ; i<L_TRs.length;++i)
	{
		var L_TDs = L_TRs[i].select('TD');
		for(var j = 1 ; j < L_TDs.length ; ++j)
		{
			if(j != L_ColNum)
			{
				L_TDs[j].setStyle({opacity:0.2});
			}
		}
	}
}

/************************************************************/
/* 	tooltip competences										*/
/************************************************************/
var lotro_competence_cache = new Array();
var lotro_competence_id = 0;

function lotro_competence(P_Id)
{
	lotro_competence_id = P_Id;

	if(lotro_competence_cache[P_Id])
	{
		var L_Item = lotro_competence_cache[P_Id];
		var L_OverDiv = $('overDiv');
	
		if(L_OverDiv)
		{
			L_OverDiv.update(L_Item);
		}
	}
	else
	{
		new Ajax.Request('/objets/competences.php?id='+P_Id, {
			onSuccess: function(response) {
					lotro_competence_cache[P_Id] = response.responseText;
					var L_OverDiv = $('overDiv');
	
					if(L_OverDiv)
					{
						L_OverDiv.update(response.responseText);
					}
			}});
	}
}
/*
73function jol_bbcodepopup(id)
74{
75 jol_bbcodepopup_id = id;
76 if (jol_bbcodepopup_ajax == null) {
77 jol_bbcodepopup_ajax = new vB_AJAX_Handler(true);
78 jol_bbcodepopup_ajax.onreadystatechange(jol_bbcodepopup_ajax_receiver);
79 }
80 if (jol_bbcodepopup_cache[id]) {
81 var item = jol_bbcodepopup_cache[id];
82 var obj = fetch_object('overDiv');
83 if (obj)
84 {
85 obj.innerHTML = item;
86 }
87 } else {
88 jol_bbcodepopup_ajax.send('/jol/bbcodepopups/ajax.php', 'id=' + id);
89 }
90 return false;
91}
92function jol_bbcodepopup_ajax_receiver()
93{
94 var id = jol_bbcodepopup_id;
95 if (jol_bbcodepopup_ajax.handler.readyState == 4
96 && jol_bbcodepopup_ajax.handler.status == 200
97 && jol_bbcodepopup_ajax.handler.responseXML)
98 {
99 var rep = jol_bbcodepopup_ajax.handler.responseXML;
100 var group = rep.getElementsByTagName("item")[0];
101 var itemId = group.getElementsByTagName('id')[0].firstChild.nodeValue;
102 var html = group.getElementsByTagName('html')[0].firstChild.nodeValue;
103 jol_bbcodepopup_cache[itemId] = html;
104 var obj = fetch_object('overDiv');
105 if (obj)
106 {
107 obj.innerHTML = html;
108 }
109 }
110} */
/************************************************************/
/* 	scripts globaux											*/
/************************************************************/
function formatAllTables()
{
	$$('TABLE').each(function(T){
		if(!T.hasClassName('layout'))
		{
			formatTable(T);
		}
	});
}

/************************************************************/
/* 	scripts au chargement									*/
/************************************************************/
document.observe("dom:loaded", function() {

	console.log('1');
	formatAllTables();

});
