var nbJour;
var nbSlot;
var drawElement;
var currentWidth;
var currentHeight;
var w;
var h;
var currentRectA;
var currentRectX;
var currentRectY;
var currentRectW;
var currentRectH;
var dispos;
var jg;

function object2String(obj) {
    var val, output = "";
    if (obj) {
        output += "{";
        for (var i in obj) {
            val = obj[i];
            switch (typeof val) {
                case ("object"):
                    if (val[0]) {
                        output += i + ":" + array2String(val) + ",";
                    } else {
                        output += i + ":" + object2String(val) + ",";
                    }
                    break;
                case ("string"):
                    output += i + ":'" + escape(val) + "',";
                    break;
                default:
                    output += i + ":" + val + ",";
            }
        }
        output = output.substring(0, output.length-1) + "}";
    }
    return output;
}

function array2String(array) {
    var output = "";
    if (array) {
        output += "[";
        for (var i in array) {
            val = array[i];
            switch (typeof val) {
                case ("object"):
                    if (val[0] != null) {
                        output += array2String(val) + ",";
                    } else {
                        output += object2String(val) + ",";
                    }
                    break;
                case ("string"):
                    output += "'" + escape(val) + "',";
                    break;
                default:
                    output += val + ",";
            }
        }
        output = output.substring(0, output.length-1) + "]";
    }
    return output;
}

function string2Object(string) {
    eval("var result = " + string);
    return result;
}

function string2Array(string) {
    eval("var result = " + string);
    return result;
}

function pair(chiffre) {
    chiffre = parseInt(chiffre);
    return ((chiffre & 1) == '0')?true:false;
}

function dispoSelectEvent(element, type, index) {
    var x, y, choix;
    if(type == 'all') {
        for(x = 0; x < nbJour; x++) {
            choix = document.getElementById("col" + x);
            choix.checked = element.checked;
            for(y = 0; y < nbSlot*2; y++) {
                dispos[x][y] = element.checked;
            }
        }
        for(y = 0; y < nbSlot; y++) {
            choix = document.getElementById("row" + y);
            choix.checked = element.checked;
        }
    } else if(type == 'row') {
        for(x = 0; x < nbJour; x++) {
            dispos[x][index*2] = element.checked;
            dispos[x][index*2+1] = element.checked;
        }
    } else if(type == 'col') {
        for(y = 0; y < nbSlot*2; y++) {
            dispos[index][y] = element.checked;
        }
    }
    dispoDraw();
}

function dispoDraw() {
    var x, y;
    jg.clear();
    jg.setColor("#ffffff");
    jg.fillRect(0, 0, currentWidth, currentHeight);

    for(x = 0; x < nbJour; x++) {
        for(y = 0; y < nbSlot*2; y++) {
            if(dispos[x][y]) {
                jg.setColor("#0000ff");
            } else {
                jg.setColor("#ffffff");
            }
            jg.fillRect(x*w, y*h, w+1, h+1);
        }
    }
    // Grille
    jg.setColor("#000000");
    for(x = 0; x < nbJour; x++) {
        jg.drawLine(x * w, 0, x * w, currentHeight);
    }

    for(y = 0; y < nbSlot*2; y++) {
        if(pair(y)) {
            jg.setStroke(1);
        } else {
            jg.setStroke(Stroke.DOTTED);
        }
        jg.drawLine(0, y * h, currentWidth, y * h);
    }
    jg.setStroke(1);
    jg.drawLine(currentWidth, 0, currentWidth, currentHeight);
    jg.drawLine(0, currentHeight, currentWidth, currentHeight);
    if(currentRectA) {
        jg.setColor("#FFFF00");
        var localRectX = currentRectX;
        var localRectY = currentRectY;
        var localRectW = currentRectW;
        var localRectH = currentRectH;

        if (localRectW < 0) {
            localRectW = 0 - localRectW;
            localRectX = localRectX - localRectW + 1;
            if (localRectX < 0) {
                localRectW += localRectX;
                localRectX = 0;
            }
        }
        if (localRectH < 0) {
            localRectH = 0 - localRectH;
            localRectY = localRectY - localRectH + 1;
            if (localRectY < 0) {
                localRectH += localRectY;
                localRectY = 0;
            }
        }
        jg.drawRect(localRectX, localRectY, localRectW, localRectH);
    }
    jg.paint();
}

function dispoMouseDown(ev) {
    if(ev.pageX) {
        currentRectX = ev.pageX - ev.currentTarget.offsetLeft;
        currentRectY = ev.pageY - ev.currentTarget.offsetTop;
    } else {
        currentRectX = ev.x;
        currentRectY = ev.y;
    }
    currentRectW = 0;
    currentRectH = 0;
    currentRectA = true;
}

function dispoMouseMove(ev) {
    if(currentRectA){
        if(ev.pageX) {
            currentRectW = ev.pageX - ev.currentTarget.offsetLeft - currentRectX;
            currentRectH = ev.pageY - ev.currentTarget.offsetTop - currentRectY;
        } else {
            currentRectW = ev.x - currentRectX;
            currentRectH = ev.y - currentRectY;
        }
        dispoDraw();
    }
}

function dispoMouseUp(ev) {
    //Make the width and height positive, if necessary.
    if (currentRectW < 0) {
        currentRectW = 0 - currentRectW;
        currentRectX = currentRectX - currentRectW + 1;
        if (currentRectX < 0) {
            currentRectW += currentRectX;
            currentRectX = 0;
        }
    }
    if (currentRectH < 0) {
        currentRectH = 0 - currentRectH;
        currentRectY = currentRectY - currentRectH + 1;
        if (currentRectY < 0) {
            currentRectH += currentRectY;
            currentRectY = 0;
        }
    }
    for(var x = parseInt(currentRectX / w); x <= parseInt((currentRectX + currentRectW) / w) && x < dispos.length; x++) {
        for(var y = parseInt(currentRectY / h); y <= parseInt((currentRectY + currentRectH) / h) && y < dispos[0].length; y++) {
            dispos[x][y] = !dispos[x][y];
        }
    }
    currentRectA = false;
    document.getElementById("disponibilite:cachedDispo").value = array2String(dispos);
    dispoDraw();
}

function initDispo(jour, slot, disposArray) {
    nbJour = jour;
    nbSlot = slot;
    drawElement = document.getElementById("dispoDraw");
    currentWidth = drawElement.offsetWidth;
    currentHeight = drawElement.parentNode.offsetHeight;
    w = currentWidth / nbJour;
    h = currentHeight / (nbSlot*2);

    currentRectA = false;
    currentRectX = 0;
    currentRectY = 0;
    currentRectW = 0;
    currentRectH = 0;
    dispos = disposArray;

    jg = new jsGraphics("dispoDraw");

    dispoDraw();
}
