﻿Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function BeginRequestHandler(sender, args) {
    var elem = args.get_postBackElement();
    ActivateProgressContainer('visible', 'progresscontainer', 'Processing...');
}
function EndRequestHandler(sender, args) {
    ActivateProgressContainer('hidden', 'progresscontainer', '');
}
function ActivateProgressContainer(visstring, elem, msg) {
    var adiv = $get(elem);
    adiv.style.visibility = visstring;
    adiv.innerHTML = msg;
}
function OpenPopUp(theURL, width, height) { window.open(theURL, '', 'resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,status=no,width=' + width + ',height=' + height + ''); }
function ControlToLoad(controltoload) {
    var hiddencontrol = $get("HIDDENCONTROLTOLOAD");
    if (hiddencontrol != null) { hiddencontrol.value = controltoload; }
}
function ControlToLoad(controltoload, contentVersionId, templateVersionSectorId) {
    var hiddencontrol = $get("HIDDENCONTROLTOLOAD");
    if (hiddencontrol != null) { hiddencontrol.value = controltoload; }

    var hiddenid = $get("HIDDENCONTENTID");
    if (hiddenid != null) { hiddenid.value = contentVersionId; }

    var hiddensectorid = $get("HIDDENCONTENTSECTOR");
    if (hiddensectorid != null) { hiddensectorid.value = templateVersionSectorId; }
}
function ControlToEdit(contenttypeid, contentVersionId, templateVersionSectorId) {
    var controlname = GetControlName(contenttypeid);
    if (controlname == 'generic') {
        var hiddencontenttypeid = $get("HIDDENCONTENTTYPEID");
        if (hiddencontenttypeid != null) { hiddencontenttypeid.value = contenttypeid; }
    }
    ControlToLoad(controlname, contentVersionId, templateVersionSectorId);
}
function ClearHiddenControlHolder() {
    var hiddencontrol = $get("HIDDENCONTROLTOLOAD");
    if (hiddencontrol != null) { hiddencontrol.value = ''; }
    var hiddenid = $get("HIDDENCONTENTID");
    if (hiddenid != null) { hiddenid.value = "-1"; }
    var hiddensectorid = $get("HIDDENCONTENTSECTOR");
    if (hiddensectorid != null) { hiddensectorid.value = "-1"; }
    var hiddencontentypeid = $get("HIDDENCONTENTTYPEID");
    if (hiddencontentypeid != null) { hiddencontentypeid.value = "-1"; }
}
function OnContentDrop(sender, e) {
    var container = e.get_container();
    var item = e.get_droppedItem();
    var position = e.get_position();
    var contentVersionId = parseInt(item.getAttribute("ContentVersionId"));
    var templateVersionSectorId = parseInt(container.getAttribute("TemplateVersionSectorId"));
    var pageVersionId = parseInt(item.getAttribute("PageVersionId"));

    //If the item is for Insert instanceid = 0 then we need to set the hidden fields and postback to the page
    if (contentVersionId > 0) {
        if (isNaN(pageVersionId)) {
            G2CMS.Web.Services.Content.Move(0, contentVersionId, templateVersionSectorId, position, OnMethodCompleted);
        }
        else {
            G2CMS.Web.Services.Content.Move(pageVersionId, contentVersionId, templateVersionSectorId, position, OnMethodCompleted);
        }

    }
    else {
        var contentTypeid = parseInt(item.getAttribute("typeid"));
        var controlname = GetControlName(contentTypeid);
        if (controlname == 'generic') {
            var hiddencontenttypeid = $get("HIDDENCONTENTTYPEID");
            if (hiddencontenttypeid != null) { hiddencontenttypeid.value = contentTypeid; }
        }
        ControlToLoad(controlname, contentVersionId, templateVersionSectorId);
        //Dynamicaly set javascript to simulate postback
        InsertContent();
    }
}

function GetControlName(contentTypeid) {
    var controlname = 'generic';
    switch (contentTypeid) {
        case 1:
            controlname = 'contentimage';
            break;

        case 2:
            controlname = 'contenthtml';
            break;

        case 3:
            controlname = 'contentflash';
            break;

        case 4:
            controlname = 'contentdocument';
            break;

        case 5:
            controlname = 'contentjavascript';
            break;

        case 6:
            controlname = 'contentmenu';
            break;

        case 7:
            controlname = 'contentlogin';
            break;

        case 12:
            controlname = 'contenttaskbox';
            break;
    }

    return controlname;
}
function OnContentDelete(sender, e) {
    var instanceId = parseInt(sender.getAttribute("InstanceId"));
    G2CMS.Web.Services.Content.Move(instanceId, columnNo, position, OnMethodCompleted);
}
function OnMenuItemDrop(sender, e) {
    var container = e.get_container();
    var item = e.get_droppedItem();
    var position = e.get_position();
    var instanceId = parseInt(item.getAttribute("InstanceId"));
    var parentId = parseInt(item.getAttribute("ParentId"));
    var menuId = parseInt(item.getAttribute("MenuId"));

    if (isNaN(parentId)) {
        G2CMS.Web.Services.MenuItem.Move(menuId, instanceId, 0, position, OnMethodCompleted);
    }
    else {
        G2CMS.Web.Services.MenuItem.Move(menuId, instanceId, parentId, position, OnMethodCompleted);
    }

}

function OnInsert(sender, e)
{ }
function OnMethodCompleted(results)
{ }
function CalculateRatio(widthid, heightid, ratio, isWidthBased) {
    //The default calculation based on width
    //If the width not present then we calculate from the height
    var width = $get(widthid);
    var height = $get(heightid);
    var widthValue = parseInt(width.value);
    var heightValue = parseInt(height.value)

    if (isWidthBased) {
        if (!isNaN(widthValue)) {
            height.value = parseInt(widthValue / ratio);
        }
    }
    else {
        if (!isNaN(heightValue)) {
            width.value = parseInt(heightValue * ratio);
        }
    }
}
function FCKUpdateLinkedField(id) {
    try {
        if (typeof (FCKeditorAPI) == "object") {
            FCKeditorAPI.GetInstance(id).UpdateLinkedField();
        }
    }
    catch (err) {
    }
}


function refreshParent() {
    window.opener.location.href = window.opener.location.href;

    if (window.opener.progressWindow) {
        window.opener.progressWindow.close()
    }
    window.close();
}

