// Client-side Javascript used for dragging edit console and opening 
// and closing menus.

var dragOn = 0
var dragDiv = null;
var dragX = 0, dragY = 0;
var zMax = 0;
var dragInit = 0;

function initDrag() {

    if (document.layers)
        document.captureEvents(Event.MOUSEMOVE | Event.MOUSEDOWN | Event.MOUSEUP);
    document.onmousemove = dragf;
    document.onmousedown = dragf;
    document.onmouseup = dragf;

    window.onmousemove = dragf;
    window.onmousedown = dragf;
    window.onmouseup = dragf;

    dragDiv = null;
    dragInit = 1;
    if (document.getElementsByTagName)
        zMax = document.getElementsByTagName("DIV").length;
    else if (document.all) zMax = document.body.all.tags("DIV").length;
    else if (document.layers) zMax = document.layers.length;
}

function dragf(arg) {
    ev = arg ? arg : event;
    if (dragDiv && ev.type == "mousedown") {
        dragOn = 1;
        dragX = (ev.pageX ? ev.pageX : ev.clientX) - parseInt(dragDiv.style.left);
        dragY = (ev.pageY ? ev.pageY : ev.clientY) - parseInt(dragDiv.style.top);
        dragDiv.style.zIndex = zMax++; // remove this line to preserve z-indexes
        return false;
    }
    if (ev.type == "mouseup") {
        dragOn = 0;
        savePosition();
    }
    if (dragDiv && ev.type == "mousemove" && dragOn) {
        dragDiv.style.left = (ev.pageX ? ev.pageX : ev.clientX) - dragX + 'px';
        dragDiv.style.top = (ev.pageY ? ev.pageY : ev.clientY) - dragY + 'px';
        return false;
    }
    if (ev.type == "mouseout") {
        if (!dragOn) dragDiv = null;
    }
}

function drag(div) {
    if (!dragInit) initDrag();
    if (!dragOn) {
        dragDiv = document.getElementById ? document.getElementById(div) :
		document.all ? document.all[div] : document.layers ? document.layers[div] : null;

        if (document.layers) dragDiv.style = dragDiv;
        dragDiv.onmouseout = dragf;
    }
}

function OpenCloseDiv(divName) {
    if (document.getElementById(divName).style.display == "none") {
        document.getElementById(divName).style.display = "block";
    }
    else {
        document.getElementById(divName).style.display = "none";
    }
    savePosition();
}

//############################################################

//***********COOKIES********************

function CreateCookie(name, values, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + values + expires + "; path=/";
}

function ReadCookie(name) {
    var ca = document.cookie.split(';');
    var nameEQ = name + "=";
    for (var i in ca) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length); //delete spaces
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

//**************************************

function savePosition() {
    if (dragDiv != null) {
        CreateCookie("Xpos", dragDiv.style.left, 60);
        CreateCookie("Ypos", dragDiv.style.top, 60);

        CreateCookie("State", document.getElementById('editConsoleOptions').style.display, 60);
    }
}

function myOnLoad() {
    //The console doesn't exist in the preview pages
    //This function should be call only in authoring mode, maybe in the console webcontrol.
    if (document.getElementById("Console1")) {

    var xCoordinate = "0px";
    var yCoordinate = "0px";
    var state = "none";

    xCoordinate = ReadCookie("Xpos");
    yCoordinate = ReadCookie("Ypos");
    state = ReadCookie("State");

    if (xCoordinate == null || yCoordinate == null || state == null) {
        // No cookies set coordinates to  0px 0px
        xCoordinate = "0px";
        yCoordinate = "0px";
        state = "none";
    }

    drag('editConsole');

    dragDiv.style.left = xCoordinate; //xCoordinate;
    dragDiv.style.top = yCoordinate; //yCoordinate;


    document.getElementById('editConsoleOptions').style.display = state;

    dragDiv = null;
    }
}


//Util para definir a posicao inicial da consola
function getCoords() {
    var x;
    var y;

    with (dragDiv.style) {
        x = left;
        y = top;
    }

    alert(parseInt(x) + " - " + parseInt(y));
}

//window.onload=myOnLoad;

//############################################################

/* Function To Make The Pop Up Windows *************/
var popUpWin = 0;

function centeredPopup(URLStr, Width, Height, docId, Scrollbars) {
    if (popUpWin) {
        if (!popUpWin.closed) popUpWin.close();
    }

    if (screen.width) {
        var winLeftPos = (screen.width - Width) / 2;
        var winTopPos = (screen.height - Height) / 2;
    }

    else {
        winLeftPos = 0;
        winTopPos = 0;
    }

    if (winLeftPos < 0) winLeftPos = 0;
    if (winTopPos < 0) winTopPos = 0;

    popUpWin = open(URLStr, docId, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=' + Scrollbars + ',resizable=no,copyhistory=yes,width=' + Width + ',height=' + Height + ',left=' + winLeftPos + ', top=' + winTopPos + ',screenX=' + winLeftPos + ',screenY=' + winTopPos + '');
}