var commentCount = 0;
var commentCache = new Array();


//Anzeigen des Inhaltes
function OnContentSuccess(content) {
    //Wenn bereits verworfen, dann Layer nicht mehr anzeigen
    if (getFromElement('commandBoxTitle') == '___DeDeContent_hide___')
        return;

    //In Cache ablegen
    if (!commentCache[commentCache['current']])
        commentCache[commentCache['current']] = new Array();
    commentCache[commentCache['current']]['content'] = content;

    // --------------- content ---------------
    writeToElement('commandBoxContent', content);

    commentCount++;

    if (commentCount > 1) {
        showLayer('commentBox');
    }
}

function OnTitleSuccess(title) {
    //Wenn bereits verworfen, dann Layer nicht mehr anzeigen
    if (getFromElement('commandBoxTitle') == '___DeDeContent_hide___')
        return;

    //In Cache ablegen
    if (!commentCache[commentCache['current']])
        commentCache[commentCache['current']] = new Array();
    commentCache[commentCache['current']]['title'] = title;

    // --------------- title --------------- 
    writeToElement('commandBoxTitle', title);

    commentCount++;

    if (commentCount > 1) {
        showLayer('commentBox');
    }
}

// Verstecken des Kommentar-Feldes
function HideComment() {
    hideLayer('commentBox');
    writeToElement('commandBoxTitle', '___DeDeContent_hide___');
}

function showLayer(id) {
    document.getElementById(id).style.visibility = "visible";
}

function hideLayer(id) {
    document.getElementById(id).style.visibility = "hidden";
}

function writeToElement(id, content) {
    if (document.getElementById) {
        document.getElementById(id).innerHTML = content;
    } else if (document.layers) {
        document.layers[id].document.write(content);
    } else if (document.all) {
        document.all[id].innerHTML = content;
    }
}

function getFromElement(id) {
    if (document.getElementById) {
        return document.getElementById(id).innerHTML;
        //}else if (document.layers) {
        //      return document.layers[id].document.;
    } else if (document.all) {
        return document.all[id].innerHTML;
    }
}

function ShowComment(e, guid) {
    commentCount = 0;
    commentCache['current'] = guid;

    if (document.getElementById('commentBox').style.visibility == "hidden")
        writeToElement('commandBoxTitle', '');

    if (commentCache[guid] && commentCache[guid]['title']) {
        commentCount++;
        writeToElement('commandBoxTitle', commentCache[guid]['title']);
    } else {
        DeDeNet.DeDeNet.Web.Application.Services.DeDeContentService.GetCommentTitle(guid, OnTitleSuccess);
    }

    if (commentCache[guid] && commentCache[guid]['content']) {
        commentCount++;
        writeToElement('commandBoxContent', commentCache[guid]['content']);
    } else {
        DeDeNet.DeDeNet.Web.Application.Services.DeDeContentService.GetCommentContent(guid, OnContentSuccess);
    }

    if (commentCount > 1) {

        showLayer('commentBox');
    }



    // --------------- e ---------------
    var topPixel = e.clientY + 10;
    var leftPixel = e.clientX + 10;

    if (navigator.appName.indexOf("Internet Explorer") != -1) {
        topPixel += document.documentElement.scrollTop;
        leftPixel += document.documentElement.scrollLeft;
    }
    else {
        topPixel += window.pageYOffset;
        leftPixel += window.pageXOffset;
    }

    document.getElementById('commentBox').style.top = topPixel + 'px';
    document.getElementById('commentBox').style.left = leftPixel + 'px';
}

function ShowFixedComment(e, title, content) {
    OnTitleSuccess(title);
    OnContentSuccess(content);

    // --------------- e ---------------
    var topPixel = e.clientY + 10;
    var leftPixel = e.clientX + 10;

    if (navigator.userAgent.indexOf("MSIE") != -1) {
        topPixel += document.body.scrollTop;
        leftPixel += document.body.scrollLeft;
    }
    else {
        topPixel += window.pageYOffset;
        leftPixel += window.pageXOffset;
    }

    document.getElementById('commentBox').style.top = topPixel + 'px';
    document.getElementById('commentBox').style.left = leftPixel + 'px';
}

function setLayerPosition(id, type, pos) {
    if (document.all) {
        layer = document.all[id].style;
    } else if (document.layers) {
        layer = document.layers[id];
    } else {
        layer = document.getElementById(id).style;
    }

    switch (type) {
        case 'x':
            layer.left = pos + 'px';
            break;
        case 'y':
            layer.top = pos + 'px';
            break;
        case 'w':
            layer.width = pos + 'px';
            break;
        case 'h':
            layer.height = pos + 'px';
            break;
    }
}

function getLayerPosition(id, pos) {
    var x;
    var y;
    var w;
    var h;

    if (document.all) {
        x = document.all[id].offsetLeft;
        y = document.all[id].offsetTop;
        w = document.all[id].scrollWidth;
        h = document.all[id].scrollHeight;
    } else if (document.layers) {
        x = document[id].left;
        y = document[id].top;
        w = document[id].clip.width;
        h = document[id].clip.height;
    } else {
        x = document.getElementById(id).style.left;
        y = document.getElementById(id).style.top;
        w = document.getElementById(id).offsetWidth;
        h = document.getElementById(id).offsetHeight;
    }

    switch (pos) {
        case "w":
            return (w);
            break;
        case "h":
            return (h);
            break;
        case "x":
            return x;
            break;
        case "y":
            return y;
            break;
    }
}


