var bn_content;
var bn_battleBind;
var bn_connectionString = "/images/connector.php";
//var bn_connectionString = "http://localhost:8080/BattleNet/battlenet/connector.php";
var bn_battleDivArray = new Object();
var bn_login = "";
var bn_template = "";
var bn_connectionError = "<div style=\"height:100%; border-style:dotted; border-color:darkgray; border-width:1px;\"><br><span style=\"position:relative; width:100%; color:gray\">&nbsp;&nbsp;&nbsp;BattleNet: Serwer tymczasowo niedostępny</span></div>";

function BN_Echo()
{
	alert(bn_login);
}

function BN_GetBattleNetHeader()
{
	var bn_header = '<div id="BattleNet" style="display: "block"; position: relative; z-index:1000"><table width="100%" cellpadding="2" cellspacing="1" border="0" class="forumline"><tr><th colspan="2" width="100%" nowrap="nowrap">&nbsp;BattleNet&nbsp;</th><th width="50" nowrap="nowrap">&nbsp;100%&nbsp;</th><th width="50" nowrap="nowrap">&nbsp;&lt;100%&nbsp;</th><th width="150" nowrap="nowrap">&nbsp;Akcje&nbsp;</th></tr><tr><td class="cat" colspan="5" align="left"><span class="cattitle">Najbliższe strzelanki sojuszu</span></td></tr><tr><td class="row3" colspan="5"><span class="gensmall">Najbliższe strzelanki sojuszu</span></td></tr>';//<tr><td><button onclick="BN_RefreshBattles()">X</button></td></tr>';
	return bn_header;
}

function BN_GetBattleNetClose()
{
	var bn_close = '</table></div>';
	return bn_close;
}

function BN_GetBattleNetContent()
{
	// Głowna zmienna
	var content = "";//"aaaaaaaaaaaaa";
	
	// Połączenie
    var req = mint.Request();

	// Dodanie parametrów
    req.AddParam("action","getbattles");       

	// Event sukcesu
    req.OnSuccess = function()
    {       	  	  
		var data = this.responseText;
		//alert(this.responseText);
		var userList = data.split('|');		
		for (user in userList)
		{						
			var attributeList = userList[user].split(';');			
			if (attributeList.length == 5)
			{			
				content += "<tr>";
				// Obrazek
				content += '<td class="row1" onmouseover="onv(this);" onmouseout="ont(this);" align="left" width="10" height="50" width="50" valign="middle"><img src="templates/'+bn_template+'/images/folder_big.gif" alt="" title="BattleNet"></td>';
				// Druga kolumna
				content += '<td style="" class="row1" onmouseover="onv(this);" onmouseout="ont(this);" colspan="1" width="100%" height="50">';
				content += '<span class="forumlink"><span class="forumlink">' + attributeList[1] + '</span></span><br>';
				content += '<a href="' + attributeList[2] + '" class="genmed">' + attributeList[2] + '<br></span><br>';
				// Trzecia kolumna
				content += '</td>';	
				// Czwarta kolumna
				content += '<td style="" class="row2" onmouseover="onv2(this);" onmouseout="ont(this);" align="center" height="50" valign="middle"><span id="battle_' + attributeList[0] + '" class="gensmall" onmouseover="BN_GetBattleBind(event,id)" onmouseout="BN_HideTip(event)">' + attributeList[3] + '</span></td>';
				content += '<td style="" class="row2" onmouseover="onv2(this);" onmouseout="ont(this);" align="center" height="50" valign="middle"><span id="battle_' + attributeList[0] + '" class="gensmall" onmouseover="BN_GetBattleBind(event,id)" onmouseout="BN_HideTip(event)">' + attributeList[4] + '</span></td>';
				content += '<td style="" class="row2" onmouseover="onv2(this);" onmouseout="ont(this);" align="center" height="50" valign="middle">';
				content += '<span class="gensmall">';
				content += '<select id="list_' + attributeList[0] + '" name="test">';
				content += '<option>100%</option>';
				content += '<option>90%</option>';
				content += '<option>80%</option>';
				content += '<option>70%</option>';
				content += '<option>60%</option>';
				content += '<option>50%</option>';
				content += '<option>Zrezygnuj</option>';
				content += '</select>';
				content += '<input id="submit_' + attributeList[0] + '" type="button" class="button" value="Głosuj" onclick="BN_Bind(id)">';
				content += '</span></td>';
				//alert(content);
			}
			//for (attribute in attributeList)
			//{
				//content += "<td>" + attributeList[attribute] + "</td>"; 
				//alert(attributeList[attribute]);
				//alert(content);
			//}
			content += "</tr>";			
		}		
		
		bn_content = content;
		bn_content = BN_GetBattleNetHeader() + content +  BN_GetBattleNetClose();			
		//alert(bn_content);
		$("BattleNet").innerHTML = bn_content;
		//alert(bn_content);		
    }
	

    // Event błędu
    req.OnAbort =  function() 
    {        
		$("BattleNet").innerHTML = bn_connectionError;
    }

    // Wysłanie żądania
    req.Send(bn_connectionString);
	
	//alert(bn_content);
	//content = content + "Bbbbbbbbbb";
	//alert(bn_content);
	return bn_content;
}

function BN_RefreshBattles()
{
	//var content = "";
	BN_GetBattleNetContent();
}

function BN_SendBind(battleid, percent) 
{
	// Połączenie
    var req = mint.Request();

	// Dodanie parametrów
    req.AddParam("action","bind");
    req.AddParam("battleId",battleid);
	req.AddParam("percent",percent);
    //req.AddParam("login",bn_login);    
	
    // Event sukcesu
    req.OnSuccess = function()
    {
		//alert(this.responseText);
		BN_RefreshBattles();
    	//$("response").innerHTML = this.responseText;        
		BN_RefreshBind();
    } 

    // Event błędu
    req.OnAbort =  function() 
    {
        $("BattleNet").innerHTML = bn_connectionError;
    }

    // Wysłanie żądania
    req.Send(bn_connectionString);
}

// Głowna metoda odpowiedzialna za głosowanie. Wywoływana przez zdarzenie onclick jednego z przycisków 'głosuj'
function BN_Bind(buttonId)
{

	// Zabezpieczenie
	if (buttonId.length == 0) return; 
	
	// Pobieram ID strzelanki
	var id = buttonId.replace("submit_", "");	
	if (id.length == 0) return; 	
	
	// Tworzę id listy dla tej strzelanki
	var listId = "list_" + id;	
	
	// Pobieram wartość z listy procentowej
	var index = $(listId).selectedIndex;
	var percent = 0;
	if (index==0) percent = 100;
	if (index==1) percent = 90;
	if (index==2) percent = 80;
	if (index==3) percent = 70;
	if (index==4) percent = 60;
	if (index==5) percent = 50;
	if (index==6) percent = 0;
	BN_SendBind(id, percent);		
}







// Pobiera listy użytkowników strzelanek do tablicy haszowej -------------------------------------------------
function BN_RefreshBind()
{
	var req = mint.Request();	    
	    req.AddParam("action","getactivebattlebinds");	    

	    req.OnSuccess = function()
	    {	
	        //alert(this.responseText);
	        var response = this.responseText;
			//alert(response);
	        var battleList = response.split('@');	        
	        for (battle in battleList)
	        {
				//alert(battleList[battle]);
				var contentList = battleList[battle].split('|');
				if (contentList.length == 2)
				{					
					var battleName = "battle_" + contentList[0];					
					bn_battleDivArray[battleName] = contentList[1];					
				}				
	        }	        	       
	    }
	    
	    // Event błędu
	    req.OnAbort =  function() 
	    {	    	
	        $("BattleNet").innerHTML = bn_connectionError;
	    }

	    req.Send(bn_connectionString); 






}








// Pobiera stringa z tabelką obecności na danej strzelance. W przypadku poprawnego połaczenia przy użyciu
// Ajaxa wywołuje funkcję wyświetlającą 'chmurkę'.
function BN_GetBattleBind(oEvent, battleid) 
{	
	var content = '<table><tr><td style="color: #FFF"><b>Gracz</b></td><td style="color: #FFF"><b>Ekipa</b></td><td><b style="color: #FFF">Procent</b></td></tr>';
	content += bn_battleDivArray[battleid];
	content += "</table>";
	if ((bn_battleDivArray[battleid] != null) & (bn_battleDivArray[battleid]!=""))
	{	
		$("BN_ToolTip").innerHTML = content;
		BN_ShowTip(oEvent, battleid);
		//alert(content);
	}
/*
	// Połączenie
    var req = mint.Request();
	var id = battleid.replace("battle_", "");	

	// Dodanie parametrów
    req.AddParam("action","getbattlebind");
    req.AddParam("battleId",id);     

    // Event sukcesu
    req.OnSuccess = function()
    {        
		//bn_battleBind = this.responseText;
		$("BN_ToolTip").innerHTML = this.responseText;
		BN_ShowTip(oEvent, battleid);
		//alert('ok');
    } 

    // Event błędu
    req.OnAbort =  function() 
    {
        alert("Serwer ma problemy z odebraniem zapytania. Spróbuj ponownie późnej.");
    }

    // Wysłanie żądania
    req.Send(bn_connectionString);
	*/
} 

function BN_ShowTip(oEvent, id) 
{
	//BN_GetBattleBind(id);

	var sender = document.getElementById(id);	
    var oDiv = document.getElementById("BN_ToolTip");	
    oDiv.style.visibility = "visible";
    var posX = oEvent.clientX+document.body.scrollLeft-50;
    var posY = oEvent.clientY+20+document.body.scrollTop;
    oDiv.style.left = posX + "px";
    oDiv.style.top = posY + "px";
	
	//$("BN_ToolTip").innerHTML = bn_battleBind;
	

	//oDiv.style.left = sender.style.left + "px";
    //oDiv.style.top = sender.style.left.top + "px";	
}

function BN_HideTip(oEvent) 
{
    var oDiv = document.getElementById("BN_ToolTip");
    oDiv.style.visibility = "hidden";
}

<!--

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
// document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  document.Show.MouseX.value = tempX
  document.Show.MouseY.value = tempY
  return true
}
