var loadingImage = '<img style="border: 0px" id="loadingImage" src="/img/loading.gif">';
var xImage = '/img/x.png';
var selected = new Array();
var maxSelect = 5;

function doClick(piantaid, postoid, x, y) {
    var index = find(selected, postoid);
    //var initialCount = selected.length;

    if(index >= 0)
    {
        selected.splice(index, 1);

        unMark(postoid);
    }
    else
    {
        if(selected.length >= maxSelect) {
            alert("E' stato raggiunto il numero massimo di posti selezionabili per questo evento");
            return;
        }
    
        if(selected.length == 0)
            selected[0] = postoid;
        else
            selected[selected.length] = postoid;

        mark(piantaid, postoid, x, y);
    }
    aggiornaDivPostiSelezionati();
}

function aggiornaDivPostiSelezionati() {

    contenuto_div = "";
    if(selected.length != 0) {

        for (var i = 0; i < selected.length; i++)
            contenuto_div += "<br />"+document.getElementById('a'+selected[i]).title;
        
        contenuto_div = "Stai per aggiungere:" + contenuto_div;
    }
}
function getImgX(imgId) {
    //var ar = document.getElementById(imgId);
    //alert("vecchio="+parseInt(ar.offsetLeft));
    //alert("nuovo="+getElementPosition('img1').left);
    return getElementPosition(imgId).left;
    /*
    var ar = document.getElementById(imgId);
    return parseInt(ar.offsetLeft);
    */
}

function getImgY(imgId) {
    //var ar = document.getElementById(imgId);
    //alert("vecchio="+parseInt(ar.offsetTop));
    //alert("nuovo="+getElementPosition('img1').top);
    return getElementPosition(imgId).top;
    /*
     *var ar = document.getElementById(imgId);
    return parseInt(ar.offsetTop);
    */
}

function getElementPosition(elemID) {
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;

    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }

    if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }

    return {left:offsetLeft, top:offsetTop};
}

function getDivPosition(){
    alert('test');
    alert(getElementPosition('img1').top);
    alert(getElementPosition('img1').left);
    /*
    alert('Distanza dal bordo sinistro: ' + <strong>getElementPosition('img1').left</strong>
    + '\n Distanza dal bordo alto: ' +<strong> getElementPosition('img1').top</strong>);
    */
}

function mark(piantaid, postoid, x, y)
{
    var div = document.createElement('div');
    div.id = 'x' + postoid;
    div.style.position = 'absolute';

    div.style.left = (x - 5) + 'px'; //correzioni in base all'immagine dell x
    div.style.top = (y - 5) + 'px'; //correzioni in base all'immagine dell x
    var img = document.createElement('img');
    img.style.border = '0px solid red';
    img.src = xImage;
    img.style.cursor = 'pointer';
    div.appendChild(img);
    
    div.onclick = function(){
        doClick(piantaid, postoid, x, y);
    }
    
    
    document.body.appendChild(div);
}

function unMark(id) {
    div = document.getElementById('x' + id);
    document.body.removeChild(div);
}

function find(array, search) {
    var indice;
    for (indice in array) {
        if (array[indice] == search)
            return indice;
    }
    return -1;
}

function resize() {
    for(var i = 0; i < selected.length; i ++) {
        var divX = document.getElementById('x' + selected[i]);
        var area = document.getElementById('a' + selected[i]);
        
        var coords = area.coords.split(',');

        var coordsCentro = calcolaCentro(coords);

        divX.style.left = (getImgX(area.parentNode.name) + parseInt(coordsCentro[0] - 5)) + 'px'; //correzioni in base all'immagine dell x
        divX.style.top = (getImgY(area.parentNode.name) + parseInt(coordsCentro[1] - 5)) + 'px'; //correzioni in base all'immagine dell x
    }
}

function calcolaCentro(coords) {
    var x1 = coords[0];
    var y1 = coords[1];
    var x2 = coords[2];
    var y2 = coords[3];
    var x3 = coords[4];
    var y3 = coords[5];
    var x4 = coords[6];
    var y4 = coords[7];

    var m1;
    var m2;
    var n1;
    var n2;
    var x;
    var y;

    if(x1 == x3 && x2 == x4) {
        x = (x1 + x2) / 2;
        y = (y1 + y2) / 2;
        return [parseInt(x), parseInt(y)];
    }

    if(x1 == x3) {
        x = x1;
        m2 = (y2 - y4) / (x2 - x4);
        n2 = y2 - m2 * x2;
        y = m2 * x1 + n2;
        return [parseInt(x), parseInt(y)];
    }

    if(x2 == x4) {
        x = x2;
        m1 = (y1 - y3) / (x1 - x3);
        n1 = y1 - m1 * x1;
        y = m1 * x2 + n1;
        return [parseInt(x), parseInt(y)];
    }

    m1 = (y1 - y3) / (x1 - x3);
    m2 = (y2 - y4) / (x2 - x4);
    n1 = y1 - m1 * x1;
    n2 = y2 - m2 * x2;
    x = (n2 - n1) / (m1 - m2);
    y = m1 * x + n1;
    
    return [parseInt(x), parseInt(y)];
}
