//HiddenInputFields Designer:
//gcMoveControl
//gcMoveControlParam
//gcMoveToHereListContent		comma separated list of positions on Content that can be populated
//gcMoveToHereListHeader	comma separated list of positions in Header that can be populated
//gcMoveToHereListFooter	comma separated list of positions in Footer that can be populated
//gcClientID			ClientID of Module
//gcSpanCell
var gcBlockContextMenu = false;
var oldBodyOnClick;
// function gcShowMoveTo
// usage: (1) Add/Move Control, (2) Create Label
//              pSos   = current position of control, empty if hidden
//              id    = id of control
//              param = (1):'add'/'move' (2):'label'
//              to initialize caption: param = 'caption:<value>'
//        (3) Move Content
//              pos   = current position of cell, empty if hidden
//              id    = id (=position of cell)
//              param = 'content'
//        type = bitarray:   1: Content; 2: Header; 4: Footer

var visibleMoveTos = new Array();

function gcShowMoveTo(pos, id, param, type) {
    gcBlockContextMenu = true;
    gcr_menu_close();
    gcContextMenuHide();
    document.forms[0].gcMoveControl.value = id;
    document.forms[0].gcMoveControlParam.value = param;
    if (visibleMoveTos) {
        if (visibleMoveTos.length > 0) gcHideMoveTo(); 
    }
    visibleMoveTos = new Array();
    
    var s = "";
    if (type & 1) s += document.forms[0].gcMoveToHereListContent.value;
    if (type & 2) s += "," + document.forms[0].gcMoveToHereListHeader.value;
    if (type & 4) s += "," + document.forms[0].gcMoveToHereListFooter.value;

    var positions = s.split(',');
    var show;
    for (i = 0; i < positions.length; i++) {
        show = true;
        if (positions[i] == '') show = false;
        if (param == '' && pos == positions[i]) show = false;
        if (param == 'content' && positions[i].indexOf(pos) == 0) show = false;
        if (show) {
            var id = document.forms[0].gcClientID.value + '_gcMoveToHere_' + positions[i];
            visibleMoveTos.push(id);
            document.getElementById(id).style.display = 'inline';
        }
    }
    //  var id = document.forms[0].gcClientID.value + '_gcMoveToHere_' + pos;
    //  if (pos!='' && param != 'label') document.getElementById(id).style.display = 'none';
    document.getElementById('div_gcMoveTo').style.display = 'block';
}

function gcHideMoveTo() {
    document.getElementById('div_gcMoveTo').style.display = 'none';
    for (var i = 0; i < visibleMoveTos.length; i++) {
        document.getElementById(visibleMoveTos[i]).style.display = 'none'; 
    }
    visibleMoveTos = new Array();
    gcBlockContextMenu = false;
}

/* IE<8-Support */
function gcRefreshMoveTo() {
    for (var i = 0; i < visibleMoveTos.length; i++) {
        var obj = document.getElementById(visibleMoveTos[i]);
        obj.style.display = 'none';
        obj.style.display = 'inline';
    }
}

function gcShowSpan(id, cellid, maxrows, maxcols) {
    /* old
    gcBlockContextMenu = true;
    document.forms[0].gcSpanCell.value=id;
    document.getElementById('div_gcSpan').style.display = 'block';
    document.getElementById(document.forms[0].gcClientID.value + '_cell' + id).className += ' gammacon_editing';
    gcHidePans();
    // /old */
    var gcClientID = document.forms[0].gcClientID.value + '_';
    // Limit Cols and Rows to maxrows and maxcols
    var row = 1;
    while (document.getElementById(gcClientID + 'span' + row)) {
        document.getElementById(gcClientID + 'span' + row).style.display = row <= maxrows ? '' : 'none';
        var col = 1;
        while (document.getElementById(gcClientID + 'span' + row + 'x' + col)) {
            document.getElementById(gcClientID + 'span' + row + 'x' + col).style.display = col <= maxcols ? '' : 'none';
            col++;
        }
        row++;
    }

    gcBlockContextMenu = true;
    document.forms[0].gcSpanCell.value = id;
    var popup = document.getElementById('div_gcSpan');
    var designer = document.getElementById(gcClientID + 'panDesigner');
    var cell = document.getElementById(cellid);
    cell.className += ' gammacon_editing';

    var popupx = gcCommon_GetRelOffsetLeft(cell, designer) + cell.offsetWidth;
    var popupy = gcCommon_GetRelOffsetTop(cell, designer) + cell.offsetHeight;
    gcFlyOut(cell, popup, popupx, popupy, designer);

    gcHidePans();
}

function gcHideSpan() {
    document.getElementById('div_gcSpan').style.display = 'none';
    var cell = document.getElementById(document.forms[0].gcClientID.value + '_cell' + document.forms[0].gcSpanCell.value)
    cell.className = cell.className.replace(' gammacon_editing', '');
    gcBlockContextMenu = false;
}

function gcAlign(align) {
    gcHideAlign();
    var s = document.forms[0].gcClientID.value;
    while (s.indexOf('_') >= 0) s = s.replace('_', ':');
    __doPostBack(s, 'Align_' + align);
}

function gcSubmit(para) {
    if (!para) para = '';
    var s = document.forms[0].gcClientID.value;
    while (s.indexOf('_') >= 0) s = s.replace('_', ':');
    __doPostBack(s, para);
}

function gcShowAlign(id, cellid) {
    gcBlockContextMenu = true;
    document.forms[0].gcSpanCell.value = id;
    var gcClientID = document.forms[0].gcClientID.value + '_';
    var popup = document.getElementById('div_gcAlign');
    var designer = document.getElementById(gcClientID + 'panDesigner');
    var cell = document.getElementById(cellid);
    cell.className += ' gammacon_editing';

    var popupx = gcCommon_GetRelOffsetLeft(cell, designer) + cell.offsetWidth;
    var popupy = gcCommon_GetRelOffsetTop(cell, designer) + cell.offsetHeight;
    gcFlyOut(cell, popup, popupx, popupy, designer);

    gcHidePans();
}

function gcHideAlign() {
    document.getElementById('div_gcAlign').style.display = 'none';
    var cell = document.getElementById(document.forms[0].gcClientID.value + '_cell' + document.forms[0].gcSpanCell.value);
    cell.className = cell.className.replace(' gammacon_editing', '');
    gcBlockContextMenu = false;
}

function gcFlyOut(source, popup, x, y, designer) {
    popup.style.display = 'block';
    x = gcEnsureVisibleX(popup, x);
    y = gcEnsureVisibleX(popup, y);
    popup.style.left = '' + x + 'px';
    popup.style.top = '' + y + 'px';
    //alert(gcCommon_GetAbsOffsetLeft(source));
    var koord = '' + gcCommon_GetRelOffsetLeft(source, designer) + ',' + gcCommon_GetRelOffsetTop(source, designer) + ',' + source.offsetWidth + ',' + source.offsetHeight + ',' + x + ',' + y + ',' + popup.offsetWidth + ',' + popup.offsetHeight + ',';
    popup.style.display = 'none';
    window.setTimeout('gcFlySet(' + koord + '20);', 100);
    window.setTimeout('gcFlySet(' + koord + '40);', 200);
    window.setTimeout('gcFlySet(' + koord + '60);', 300);
    window.setTimeout('gcFlySet(' + koord + '80);', 400);
    window.setTimeout('gcFlySet(' + koord + '0); document.getElementById("' + popup.id + '").style.display = "block";', 500);
}

function gcFlySet(ox, oy, ow, oh, px, py, pw, ph, p) {
    var flyer = document.getElementById('div_Flyer');
    flyer.style.display = p == 0 ? 'none' : 'block';
    if (p != 0) {
        flyer.style.left = '' + Math.round(ox + ((px - ox) * p / 100)) + 'px';
        flyer.style.top = '' + Math.round(oy + ((py - oy) * p / 100)) + 'px';
        flyer.style.width = '' + Math.round(ow + ((pw - ow) * p / 100)) + 'px';
        flyer.style.height = '' + Math.round(oh + ((ph - oh) * p / 100)) + 'px';
    }
}

function gcEnsureVisibleX(obj, x) {
    if (x + obj.offsetWidth > gcCommon_GetInnerWidth()) x = gcCommon_GetInnerWidth() - obj.offsetWidth;
    return x;
}

function gcEnsureVisibleY(obj, y) {
    if (y + obj.offsetHeight > gcCommon_GetInnerHeight()) y = gcCommon_GetInnerHeight() - obj.offsetHeight;
    return y;
}

function gcHidePans() {
    var pan;
    pan = document.getElementById(document.forms[0].gcClientID.value + '_panCellEdit');
    if (pan) pan.style.display = 'none';
    pan = document.getElementById(document.forms[0].gcClientID.value + '_panControlEdit');
    if (pan) pan.style.display = 'none';
    pan = document.getElementById(document.forms[0].gcClientID.value + '_panSettings');
    if (pan) pan.style.display = 'none';
}

function gcSpanOver(r, c, rm, cm) {
    var id = document.forms[0].gcClientID.value + '_';
    for (var row = 1; row <= rm; row++) {
        for (var col = 1; col <= cm; col++) {
            if (col > c || row > r)
                document.getElementById(id + 'Span' + row + 'x' + col).className = 'gammacon_span_celloff';
            else
                document.getElementById(id + 'Span' + row + 'x' + col).className = 'gammacon_span_cellon';
        }
    }
}

function gcContextMenuBodyOnClick() {
    if (gcContextMenuLast) gcContextMenuHide(gcContextMenuLast);
    if (oldBodyOnClick != null) {
        document.body.onclick = oldBodyOnClick;
        oldBodyOnClick();
        oldBodyOnClick = null;
    }
}

var gcContextMenuLast = null;

function gcContextMenuShow(obj) {
    if (gcBlockContextMenu) return;
    if (gcContextMenuLast) gcContextMenuHide(gcContextMenuLast);
    gcContextMenuLast = obj;
    var o = obj;
    if (o.className != "gammacon_contextmenu" && o.className != "gammacon_pulldownmenu" && o.className != "gammacon_smarttagmenu") o = o.firstChild;
    while (o) {
        if (o.className == "gammacon_contextmenu" || o.className == "gammacon_pulldownmenu" || o.className == "gammacon_smarttagmenu") gcCommon_ShowDivInline(o);
        o = o.nextSibling;
    }
}

function gcContextMenuHide(obj) {
    if (!obj) obj = gcContextMenuLast;
    //alert(obj);
    if (!obj) return;
    gcContextMenuLast = null;
    var o = obj;
    if (o.className != "gammacon_contextmenu" && o.className != "gammacon_pulldownmenu" && o.className != "gammacon_smarttagmenu") o = o.firstChild;
    while (o) {
        if (o.className == "gammacon_contextmenu" || o.className == "gammacon_pulldownmenu" || o.className == "gammacon_smarttagmenu") gcCommon_HideDiv(o);
        o = o.nextSibling;
    }
}

function gcOpenPopup(url, name, width, height) {
    var w = window.open(url, name, "height=" + height + ",width=" + width /*+ ",toolbar=yes"*/);
    w.focus();
}

function gcSelectBoxes(visibility) {
    var tags = document.getElementsByTagName("select");
    for (var i = 0; i < tags.length; i++) {
        tags[i].style.visibility = visibility;
    }
}

function gcCallOnEnter(ev, tocall) {
    if (!tocall) //nur ein parameter übergeben
    {
        tocall = ev;
        ev = window.event;
    }
    var code;
    if (ev.which) code = ev.which;
    else code = ev.keyCode;
    if (code == 13) { eval(tocall); return false; }
}

var gcDesignerTimeOut_cmdsave;
var gcDesignerTimeOut_msg;

function gcDesignerTimeOut_Init(millisec, msg, cmdsave) {
    gcDesignerTimeOut_cmdsave = cmdsave;
    gcDesignerTimeOut_msg = msg;
    window.setTimeout("gcDesignerTimeOut_Msg()", millisec);
}

function gcDesignerTimeOut_Msg() {
    if (confirm(gcDesignerTimeOut_msg))
        document.getElementById(gcDesignerTimeOut_cmdsave).click();
}

/* SmartTag ******************************************************************************/
var gcSmartTag_ActiveTag;
var gcSmartTag_AutoCloseHandle;

function gcSmartTag_Activator_onMouseOver(obj) {
    if (!gcSmartTag_ActiveTag) document.getElementById(obj.getAttribute("gcSmartTag")).style.display = 'inline';
}
function gcSmartTag_Activator_onMouseOut(obj) {
    if (!gcSmartTag_ActiveTag) document.getElementById(obj.getAttribute("gcSmartTag")).style.display = 'none';
}
function gcSmartTag_onMouseOver(obj) {
    if (!gcSmartTag_ActiveTag) {
        obj.className = "gammacon_smarttag_hover";
        obj.style.display = "inline";
    }
    else {
        if (gcSmartTag_AutoCloseHandle) window.clearTimeout(gcSmartTag_AutoCloseHandle);
    }
}

function gcSmartTag_onMouseOut(obj) {
    if (!gcSmartTag_ActiveTag)
        obj.className = "gammacon_smarttag";
    else {
        gcSmartTag_AutoCloseHandle = window.setTimeout("gcSmartTag_onAutoClose('" + obj.id + "');", 2000);
    }
}

function gcSmartTag_onAutoClose(id) {
    if (gcSmartTag_ActiveTag) {
        gcContextMenuHide(gcSmartTag_ActiveTag);
        gcSmartTag_ActiveTag = null;
        var obj = document.getElementById(id);
        obj.className = 'gammacon_smarttag';
        obj.style.display = 'none';
    }
}

function gcSmartTag_onClick(obj) {
    if (!gcSmartTag_ActiveTag) {
        gcSmartTag_ActiveTag = obj;
        obj.className = "gammacon_smarttag_active";
        gcContextMenuShow(document.getElementById(obj.getAttribute("gcSmartTagMenu")));
    } else {
        gcContextMenuHide(gcSmartTag_ActiveTag);
        gcSmartTag_ActiveTag = null;
        obj.className = "gammacon_smarttag_hover";
    }
}
/* InlineEdit ******************************************************************************/

var gcInlineEdit_EditedObject;
var gcInlineEdit_Multiline;
var gcInlineEdit_OldValue;

function gcInlineEdit_ShowAndFocus(org, obj, multiline) {
    if (gcBlockContextMenu) return;
    if (gcContextMenuLast) gcContextMenuHide(gcContextMenuLast);
    gcInlineEdit_EditedObject = org;
    gcInlineEdit_Multiline = multiline;
    gcInlineEdit_OldValue = obj.value;
    gcBlockContextMenu = true;
    obj.style.width = "100%"; //(30+org.offsetWidth) + "px";
    obj.style.maxWidth = "100%";
    obj.style.height = "50px"; //(4+org.offsetHeight) + "px";
    obj.rows = gcInlineEdit_CountCrLf(obj.value);
    obj.style.display = 'inline';
    org.style.display = 'none';
    obj.focus();
}
function gcInlineEdit_OnKey(obj) {
    if (!window.event) return;
    var keyCode = window.event.keyCode;
    if (keyCode == 27 || (keyCode == 13 && !gcInlineEdit_Multiline)) {
        if (keyCode == 27) obj.value = gcInlineEdit_OldValue;
        obj.blur();
    }
    obj.rows = gcInlineEdit_CountCrLf(obj.value);
}
function gcInlineEdit_OnChange(obj, id) {
    //obj.style.display = 'none';
    obj.style.display = 'inline';
    gcInlineEdit_EditedObject.style.display = 'none';
    gcSubmit('InlineEdit_' + id);
}
function gcInlineEdit_OnBlur(obj) {
    if (obj.value == gcInlineEdit_OldValue) {
        obj.style.display = 'none';
        gcInlineEdit_EditedObject.style.display = 'inline';
        gcBlockContextMenu = false;
    } else {
        obj.disabled = true;
    }
}
function gcInlineEdit_CountCrLf(s) {
    s = " " + s + " ";
    var a = s.split("\n");
    return a.length; //r;	
}
/* ExtendesDropDown */
var gcEDD_actdropdown;
var gcEDD_oldonclick;
var gcEDD_ignorebodyonclick = false;

function gcEDD_init() {
    if (document.body.onclick) gcEDD_oldonclick = document.body.onclick;
    document.body.onclick = gcEDD_body_onclick;
}

function gcEDD_onmouseover(obj) {
    if (obj.getAttribute("gc_state") == "normal")
        obj.className = "gammacon_edd_controlhover";
}
function gcEDD_onmouseout(obj) {
    if (obj.getAttribute("gc_state") == "normal")
        obj.className = "gammacon_edd_control";
}
function gcEDD_onclick(obj) {
    if (obj.getAttribute("gc_state") == "normal") {
        gcEDD_menu_open(obj);
    }
    else if (obj.getAttribute("gc_state") == "active") {
        gcEDD_menu_close(obj);
    }
}
function gcEDD_entry_onmouseover(obj) {
    obj.className = "gammacon_edd_entryhover";
}
function gcEDD_entry_onmouseout(obj) {
    obj.className = "gammacon_edd_entry";
}
function gcEDD_entry_onclick(obj, multiselect) {
    var keyobj = document.getElementById(gcEDD_actdropdown.getAttribute("gc_keyid"));
    var lblobj = document.getElementById(gcEDD_actdropdown.getAttribute("gc_lblid"));
    var ekey = obj.getAttribute("key");
    var evalue = obj.getAttribute("value");
    if (!multiselect) {
        keyobj.value = ekey;
        lblobj.innerHTML = evalue;
    }
    else {
        //Eintrag schon selektiert?
        var index = -1;
        var keyarr = keyobj.value.split(",");
        for (var i = 0; i < keyarr.length; i++) {
            if (keyarr[i] == ekey) index = i;
        }
        if (index == -1) {
            //Eintrag nicht ausgewählt
            if (keyobj.value == "")
            { keyobj.value = ekey; lblobj.innerHTML = evalue; }
            else
            { keyobj.value += "," + ekey; lblobj.innerHTML += ", " + evalue; }
            document.getElementById(obj.getAttribute("chkid")).checked = true;
        }
        else {
            //key entfernen
            var newarr = keyarr.slice(0, index).concat(keyarr.slice(index + 1));
            keyobj.value = newarr.join(",");
            var valuearr = lblobj.innerHTML.split(", ");
            newarr = valuearr.slice(0, index).concat(valuearr.slice(index + 1));
            lblobj.innerHTML = newarr.join(", ");
            document.getElementById(obj.getAttribute("chkid")).checked = false;
        }
        gcEDD_ignorebodyonclick = true;
        window.setTimeout("gcEDD_ignorebodyonclick = false;", 100);
    }
}
function gcEDD_menu_open(obj) {
    if (gcEDD_actdropdown) gcEDD_menu_close(gcEDD_actdropdown);
    document.getElementById(obj.getAttribute("gc_ddid")).style.display = "block";
    obj.className = "gammacon_edd_controlactive";
    obj.setAttribute("gc_state", "active");
    //gcContextMenuShow(document.getElementById(obj.getAttribute('menuid')));
    gcEDD_actdropdown = obj;
    gcEDD_ignorebodyonclick = true;
    gcBlockContextMenu = true;
    window.setTimeout("gcEDD_ignorebodyonclick = false;", 100);
}

function gcEDD_menu_close(obj) {
    document.getElementById(obj.getAttribute("gc_ddid")).style.display = "none";
    obj.setAttribute("gc_state", "normal");
    obj.className = "gammacon_edd_control";
    gcEDD_actdropdown = null;
    gcBlockContextMenu = false;
}

function gcEDD_body_onclick() {
    if (!gcEDD_ignorebodyonclick) {
        if (gcEDD_actdropdown) gcEDD_menu_close(gcEDD_actdropdown);
    }
    if (gcEDD_oldonclick) gcEDD_oldonclick();
}

function gcD_designercontrol_onmouseover(obj) {
    if (gcBlockContextMenu) return;
    gcD_highlight(obj);
}
function gcD_designercontrol_onmouseout(obj) {
    if (gcBlockContextMenu) return;
    gcD_dehighlight(obj);
}
function gcD_designercontrol_onclick(obj) {
    //onclick ignorieren, falls ContextMenüs geblockt werden sollen
    if (gcBlockContextMenu) return;
    //onclick ignorieren, falls Menü bereits sichtbar
    if (obj.getAttribute('gcr_state') == 'contextmenu') return;
    //sonst ContextMenü öffnen
    gcr_contextmenu_open(obj);
}
function gcD_fixedcell_onmouseover(obj) {
    if (gcBlockContextMenu) return;
    gcD_highlight(obj)
}
function gcD_fixedcell_onmouseout(obj) {
    if (gcBlockContextMenu) return;
    gcD_dehighlight(obj);
}
function gcD_contentcell_onmouseover(obj) {
    if (gcBlockContextMenu) return;
    var p = obj.parentNode.parentNode.parentNode.parentNode;
    gcD_highlight(p);
}
function gcD_contentcell_onmouseout(obj) {
    if (gcBlockContextMenu) return;
    var p = obj.parentNode.parentNode.parentNode.parentNode;
    gcD_dehighlight(p);
}
function gcD_contentcell_onclick(obj) {
    //onclick ignorieren, falls Menü bereits sichtbar
    if (obj.getAttribute('gcr_state') == 'contextmenu') return;
    //sonst ContextMenü öffnen
    gcr_contextmenu_open(obj);
}

var objHighlighted;

function gcD_highlight(obj) {
    if (objHighlighted == obj) return;
    if (objHighlighted) gcD_dehighlight();
    obj.savebgcolor = obj.style.backgroundColor;
    obj.savecolor = obj.style.color;
    obj.style.backgroundColor = '#feea85';
    obj.style.color = '#000000';
    objHighlighted = obj;
}

function gcD_dehighlight(/* optional */obj) {
    if (!objHighlighted) return;
    if (obj && obj != objHighlighted) return;
    objHighlighted.style.backgroundColor = objHighlighted.savebgcolor;
    objHighlighted.style.color = objHighlighted.savecolor;
    objHighlighted.savebgcolor = undefined;
    objHighlighted.savecolor = undefined;
    objHighlighted = undefined;
}

gcEDD_init();
