
var intervalidlist;
intervalidlist = new Array();

var intervalpointers;
intervalpointers = new Array();

var mouseInsideBody = true;
var hideButtonsTimeout;

function addInterval(intervalid, intervalpointer) {
    intervalidlist.push(intervalid);
    intervalpointers.push(intervalpointer);
}

function deleteInterval(intervalid) {
    for (var i = 0; i < intervalidlist.length; i++) {
        if (intervalidlist[i] == intervalid) {
            window.clearInterval(intervalpointers[i]);
            intervalidlist.splice(i, 1);
            intervalpointers.splice(i, 1);
            break;
        }
    }
}

function scroll(divid, interval) {

    var d;
    d = document.getElementById(divid);
    var n;
    n = d.scrollTop;
    n++
    d.scrollTop = n;
    if (d.scrollTop < n) {
        window.setTimeout("scrollstop('" + divid + "', " + interval + ")", 4000);
        deleteInterval(divid);
    }
}

function scrollstop(divid, interval) {
    var d;
    d = document.getElementById(divid);
    d.scrollTop = 0;
    window.setTimeout("scrollreset('" + divid + "', " + interval + ")", 4000);
}

function getScrollSpeedInterval(speedName) {
    switch (speedName) {
        case 'slow':
            return 40;
            break;
        case 'slower':
            return 30;
            break;
        case 'default':
            return 20;
            break;
        case 'faster':
            return 15;
            break;
        case 'fast':
            return 10;
            break;
    }
}

function scrollreset(divid, interval) {

    var d;
    d = document.getElementById(divid);

    var scrollid;
    scrollid = window.setInterval("scroll('" + divid + "', " + interval + ")", interval);
    addInterval(divid, scrollid);
}

function toggleDisplay(elementid) {
    if (document.getElementById(elementid).style.display == 'none')
        document.getElementById(elementid).style.display = 'block';
    else document.getElementById(elementid).style.display = 'none';
}

function DisplayNone(elementid) {
    document.getElementById(elementid).style.display = 'none';
}

function DisplayBlock(elementid) {
    document.getElementById(elementid).style.display = 'block';
}

function StayAlive(elementId) {
    try {
        var http_request;
        http_request = CreateAjax();

        http_request.onreadystatechange = function() {
            if (http_request.readyState == 4) {
                WriteIntoElement(elementId, "<img src='images/workflow/greensm.gif' />"); //http_request.responseText
                window.setTimeout("WriteIntoElement('" + elementId + "', ''); ", 1000);
            }
        };

        http_request.open('GET', '_System/Handler/TimeValue.ashx', true);
        http_request.send(null);
    } catch (err) { }
}

function WriteIntoElement(elementid, value) {
    $('#' + elementid).html(value);
}

function CreateAjax() {
    var http_request;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return http_request;
}

function toggleModule(ele, id, idfeet) {
    var moduleDiv = $('#' + id + ':visible');
    var button = $(ele);
    var moduleFeetDiv = $('#' + idfeet);

    var iconSpan = $('.pbIcon', button);

    if (moduleDiv.length > 0) {
        moduleDiv.slideUp();
        iconSpan.removeClass('pbIcon_up');
        iconSpan.addClass('pbIcon_down');
    }
    else {

        moduleDiv = $('#' + id);


        moduleDiv.slideDown();
        iconSpan.addClass('pbIcon_up');
        iconSpan.removeClass('pbIcon_down');
    }
}

function initPWPButton(buttonId) {
    var pwpButton = $('#' + buttonId);

    pwpButton.dblclick(function() { document.location = this.href; }).click(function() {
        showPWPMenu($(this)); return false;
    }).mouseenter(function() { showPWPMenu($(this)); }).css("z-index", "999");
    $('body').click(function() { $('#contextMenu:visible').hide(); });
    $('#pwpmenu_more_pageedit').click(function() { pwpmenu_showEdit(); return false; });
    $('#pwpmenu_more_administration').click(function() { pwpmenu_showAdmin(); return false; });
}

function showButtons() {
    mouseInsideBody = true;
    $('.AdminIcons,.pwpButton,.item_buttons').stop(true, true).show();
    $('body').unbind('mousemove');
}

function hideButtons() {
    if ($('.pwpButton:visible').length > 0) {

        mouseInsideBody = false;
        clearTimeout(hideButtonsTimeout);
        hideButtonsTimeout = setTimeout('hideButtonsExecute()', 500);
    }
}

function hideButtonsExecute() {
    if (!mouseInsideBody) {
        $('body').mouseenter(showButtons).mousemove(showButtons);
        $('.AdminIcons,.pwpButton,.item_buttons,#contextMenu').fadeOut(300);
    }
}

function showPWPMenu(button) {
    var menu = $('#contextMenu:visible');

    if (menu.length == 0) {
        menu = $('#contextMenu');
        var buttonOffset = button.offset();
        menu.css('top', buttonOffset.top + button.height() + 'px').css('left', buttonOffset.left + 'px').slideDown
(200);
    }
    else {
        menu.hide();
    }
}

function editModuleFrameOnload(frame) {
    var f = $(frame);
    var dlg = f.parent();

    f.height(f.contents().find("body").height() + 20);

}

function initAdminIcons() {

    $('body').append('<div class="item_highlight"></div>');

    $('.AdminIcons').each(function() {
        var ai = $(this);

        var module = ai.closest('.module');


        var off = module.offset();

        if ($('.ModuleTitle', module).length == 0) {
            off.top = off.top - 24;
        }

        if ($('.button_menu', ai).length > 0) {
            $('.pbLink', ai).mouseenter(function() { $('.button_menu', ai).hide(); $(this).next().show(); });
            //   $('.pbLink', ai).mouseleave(function() { $(this).next().hide(); });
        }

        $('.button_menu', ai).mouseleave(function() { $(this).hide() });
        ai.mouseenter(function() { highlight(module); });
        ai.mouseleave(aminIconsLeave);


    });

}

function initItemButtons() {


    $('.item_buttons').each(function() {

        var buttons = $(this);

        if (buttons.hasClass('item_table_buttons')) return;

        var container = buttons.closest('.relative_container');

        buttons.mouseenter(function() { highlight(container); });
        buttons.mouseleave(aminIconsLeave);


    });
}


function aminIconsLeave() {
    $('.item_highlight').hide();
    $('.button_menu').hide();
}


function itemButtonsHover() {
    var item = $(this).parents('.relative_container').eq(0);
    highlight(item);
}

function itemButtonsLeave() {
    $('.item_highlight').hide();
}

function highlight(element) {


    // var highlightContainer = $('.item_highlight_container');
    var highlight = $('.item_highlight');

    highlight.width(element.outerWidth(true) - 2).height(element.outerHeight(true) - 2);


    if (element.css("position") == "static") {
        highlight.appendTo($('.relative_container', element).eq(0));

    } else {
        highlight.appendTo(element);
    }

    highlight.show();

}

var InputFocus; InputFocus = false;


function InitInputEvents() {

    $('input,textarea,select').focus(function() { InputFocus = true; }).blur(function() { InputFocus = false; });
}

function handleKeyboardNavigation(e, parentUrl, firstChildUrl, leftUrl, rightUrl) {
    if (!InputFocus) {
 
        var key = e.which;

        if (key == 119 && parentUrl!='#')
            window.location = parentUrl;
        if (key == 115 && firstChildUrl != '#')
            window.location = firstChildUrl;
        if (key == 97 && leftUrl != '#')
            window.location = leftUrl;
        if (key == 100 && rightUrl != '#')
            window.location = rightUrl;

    }
}

function handleKeyboardLoginLogoff(e, url) {
    
    if (!InputFocus) {
        var key = e.which;
        
        if (key == 181)
            window.location = url;
    }
}

function initUploadFrame(iframe, frameSrc) {
    var frame = iframe;

    if (!frame.attr('src') || frame.attr('src') == undefined) {

        frame.attr('src', frameSrc);

        var dlg = frame.parents('.ui-dialog-content');
        frame.load(function() { hideDialogLoader(dlg); });
        showDialogLoader(dlg, 'Einen Moment bitte...');
    }
}


function showDialogLoader(dialog, message) {
    var dlg = dialog;

    var loader = dlg.next('.dialog_loader');

    if (loader.length == 0) {
        dlg.after('<div class="dialog_loader" style="text-align:center"><div><img id="uploadProgress" alt="Ladevorgang" src="js/tiny_mce/themes/advanced/skins/default/img/progress.gif" /><p></p></div></div>');
        loader = dlg.next('.dialog_loader');
        loader.css('position', 'absolute');
    }
    loader.show();

    p = $('p', loader);

    loader.css('left', '0');
    loader.css('top', '0');
    loader.css('width', dlg.outerWidth() + 'px');
    loader.css('height', dlg.outerHeight() + 'px');
    p.text(message);
    dlg.css('visibility', 'hidden');
}

function hideDialogLoader(dialog) {
    var dlg = dialog;
    dlg.css('visibility', 'visible');
     var loader = dlg.next('.dialog_loader');
     loader.hide();
}

