function showDetails(title, space, credits, inputsString, outputsString)
{
    var inputs = parseCommodityString(inputsString);
    var outputs = parseCommodityString(outputsString);
    
    document.getElementById("detailsTable").style.display = "block";
    document.getElementById("detailTitle").innerHTML = title;
    document.getElementById("detailSpace").innerHTML = space;
    document.getElementById("detailCredits").innerHTML = credits;
    
    var inputsBody  = document.getElementById("detailsInputs");
    var outputsBody = document.getElementById("detailsOutputs");
    
    // remove rows
    removeRows(inputsBody);
    removeRows(outputsBody);
    // insert rows
    insertRows(inputsBody, inputs);
    insertRows(outputsBody, outputs);
}

function parseCommodityString(commoditiesString)
{
    var commodities = commoditiesString.split("|");
    var commodityArr = new Array();
    
    for (var i = 0; i < commodities.length - 1; i++)
    {
        commodityArr[i] = new Array();
        var fields = commodities[i].split(",");
        
        for (var j = 0; j < fields.length; j++)
        {
            commodityArr[i][j] = fields[j];
        }
    }
    return commodityArr;
}

function removeRows(tbody)
{
    var numRows = tbody.rows.length;
    
    for (var i = 0; i < numRows; i++)
    {
        tbody.removeChild(tbody.rows[0]);
    }
}

function insertRows(tbody, commodities)
{
    for (var i = 0; i < commodities.length; i++)
    {
        var newRow = document.createElement("tr");
        
        // resource cell
        var cellOne = document.createElement("td");
        var resImg = document.createElement("img");
        resImg.src = "http://static.pardus.at/images/res/" + commodities[i][0] + ".png";
        resImg.height = "16";
        resImg.width = "16";
        cellOne.appendChild(resImg);
        newRow.appendChild(cellOne);
        
        // amount cell
        var cellTwo = document.createElement("td");
        cellTwo.innerHTML = commodities[i][1];
        newRow.appendChild(cellTwo);
        
        // max/min cell
        var cellThree = document.createElement("td");
        cellThree.innerHTML = commodities[i][2];
        newRow.appendChild(cellThree);
        
        // price cell
        var cellFour = document.createElement("td");
        cellFour.innerHTML = commodities[i][3];
        newRow.appendChild(cellFour);
        
        // add new row to the table
        tbody.appendChild(newRow);
    }
}

function submitForm()
{
    document.getElementById("form1").submit();
}

function sendmsg(player,subject)
{
    window.open("http://orion.pardus.at/sendmsg.php?to="+player+"&subj="+subject,"_blank","width=540,height=434,left=0,top=0");
}

document.onload = onload;

function onload()
{
    // onload
    window.setTimeout("submitForm();", 300000); // refresh page every 5 mins
    
    // show top building's details
    try
    {
        var firstLink = document.getElementsByTagName("a")[11];
        var detailsStr = firstLink.href.substring(23, firstLink.href.length - 2);
        eval("showDetails(" + detailsStr + ")");
    }
    catch(ex) {}
}


