﻿var strStatus = "";
function OnLoadPages() {
    if(document.getElementById("ddlPagesPortletReference"))
        PSCPortal.PortalService.GetPages(OnLoadPagesSuccess, OnLoadPagesFailed);
}
function OnLoadPagesSuccess(results, context, methodName) {
    DropDownListBind(Sys.Serialization.JavaScriptSerializer.deserialize(results), "ddlPagesPortletReference");
    OnLoadPortletReference(document.getElementById("ddlPagesPortletReference"));
}
function OnLoadPagesFailed(results, context, methodName) {
}

function OnLoadPortletReference(element) {
    PSCPortal.PortalService.GetPortletReferences(element.value, OnLoadPortletReferenceSuccess, OnLoadPortletReferenceFailed);
}
function OnLoadPortletReferenceSuccess(results, context, methodName) {
    var listData = Sys.Serialization.JavaScriptSerializer.deserialize(results);
    var pnDisplay = document.getElementById("DisplayPortletReference");
    if (listData.length > 0) {
        DropDownListBind(listData, "ddlPortletReference");
        pnDisplay.style.visibility = "visible";
    }
    else
        pnDisplay.style.visibility = "hidden";
}
function OnLoadPortletReferenceFailed(results, context, methodName) {
}
function OnPortletReferenceAdd() {
    
    var ddlPortletReference = document.getElementById("ddlPortletReference");
    var ddlPagesPortletReference = document.getElementById("ddlPagesPortletReference");
    var ddlPanelPortletReference = document.getElementById("ddlPanelPortletReference");
    PSCPortal.PortalService.PortletReferenceAdd(ddlPortletReference.value, ddlPagesPortletReference.value, ddlPanelPortletReference.value, OnPortletReferenceAddSuccess, OnPortletReferenceAddFailed);   
}
function OnPortletReferenceAddSuccess(results, context, methodName) {
    javascript:__doPostBack('HiddenField1', '');
}
function OnPortletReferenceAddFailed(results, context, methodName) {
}
function OnPortletEditApperance(portletid) {
    PortletGetStyle(portletid);
}
function OnPortletEditApperanceSuccess(results, context, methodName) {
    document.getElementById("portlet_"+context["PortletId"]).style.cssText = context["PortletStyle"];
}
function OnPortletEditApperanceFailed(results, context, methodName) {
}
function PortletGetStyle(portletid) {
    PSCPortal.PortalService.set_defaultUserContext(portletid);
    PSCPortal.PortalService.PortletGetStyle(portletid, PortletGetStyleSuccess, PortletGetStyleFailed);            
}
function PortletGetStyleSuccess(results, context, methodName) {
    var style = window.showModalDialog("Controls/CSSEditor.aspx", results, "dialogHeight: 500px; dialogWidth: 700px;");
    var portletid = PSCPortal.PortalService.get_defaultUserContext();
    if (style!=undefined)
    {
        var arr = new Array();
        arr["PortletId"] = portletid;
        arr["PortletStyle"] = style;
        PSCPortal.PortalService.set_defaultUserContext(arr);
        PSCPortal.PortalService.PortletChangeStyle(portletid, style, OnPortletEditApperanceSuccess, OnPortletAddFailed);
    }
}
function PortletGetStyleFailed(results, context, methodName) {
}
function OnPortletEdit(portletid) {
    PSCPortal.PortalService.PortletEditData(portletid, OnPortletEditSuccess, OnPortletEditFailed);
}
function OnPortletEditSuccess(results, context, methodName) {
    javascript:__doPostBack('HiddenField1', '');
}
function OnPortletEditFailed(results, context, methodName) {
}
function OnPanelEditApperance(element) {
    var panelId = element.id.split('_')[0];
    GetPanelStyle(panelId);         
}
function GetPanelStyle(panelId) {
    PSCPortal.PortalService.set_defaultUserContext(panelId);
    PSCPortal.PortalService.PanelGetStyle(panelId, GetPanelStyleSuccess, GetPanelStyleFailed);                   
}
function GetPanelStyleSuccess(results, context, methodName) {
    var style = window.showModalDialog("Controls/CSSEditor.aspx", results, "dialogHeight: 500px; dialogWidth: 700px;");
    var panelId = PSCPortal.PortalService.get_defaultUserContext();
    PSCPortal.PortalService.set_defaultUserContext(style);
    if (style!=undefined) {
        PSCPortal.PortalService.PanelChangeApperance(panelId, style, OnPanelEditApperanceSuccess, OnPanelEditApperanceFailed);                
    }
}
function GetPanelStyleFailed(results, context, methodName) {
}        
function OnPanelEditApperanceSuccess(results, context, methodName) {
    var panel = document.getElementById("pn" + results + "Display");
    panel.style.cssText = "border-width:1px;border-style:Solid;" + context;
    document.getElementById("pn" + results+"Title").style.width = panel.style.width;  
}
function OnPanelEditApperanceFailed(results, context, methodName)
{
}
function CheckIsEdit()
{
    PSCPortal.PortalService.IsAllowEdit(CheckIsEditSuccess, CheckIsEditFailed);
}
function CheckIsEditSuccess(results, context, methodName)
{
    if(results)
    {
        if (document.getElementById("pnTopDisplay"))
            Sortable.create("pnTopDisplay", { tag: 'div', handle: 'TitlePortlet', dropOnEmpty: true, containment: ["pnTopDisplay", "pnLeftDisplay", "pnCenterDisplay", "pnRightDisplay", "pnBottomDisplay"], onUpdate: OnChange });
        if (document.getElementById("pnLeftDisplay"))
            Sortable.create("pnLeftDisplay", { tag: 'div', handle: 'TitlePortlet', dropOnEmpty: true, containment: ["pnLeftDisplay", "pnTopDisplay", "pnCenterDisplay", "pnRightDisplay", "pnBottomDisplay"], onUpdate: OnChange });
        if (document.getElementById("pnCenterDisplay"))
            Sortable.create("pnCenterDisplay", { tag: 'div', handle: 'TitlePortlet', dropOnEmpty: true, containment: ["pnCenterDisplay", "pnLeftDisplay", "pnTopDisplay", "pnRightDisplay", "pnBottomDisplay"], onUpdate: OnChange });
        if (document.getElementById("pnRightDisplay"))
            Sortable.create("pnRightDisplay", { tag: 'div', handle: 'TitlePortlet', dropOnEmpty: true, containment: ["pnRightDisplay", "pnLeftDisplay", "pnCenterDisplay", "pnTopDisplay", "pnBottomDisplay"], onUpdate: OnChange });
        if (document.getElementById("pnBottomDisplay"))
            Sortable.create("pnBottomDisplay", { tag: 'div', handle: 'TitlePortlet', dropOnEmpty: true, containment: ["pnBottomDisplay", "pnLeftDisplay", "pnCenterDisplay", "pnRightDisplay", "pnTopDisplay"], onUpdate: OnChange });

        strStatus = Serialize();
    }
}
function CheckIsEditFailed(results, context, methodName)
{
}
function OnLoad() {
    //if (!Sys.Services.AuthenticationService.get_isLoggedIn())
        //return;
    CheckIsEdit();
}
function Serialize() {
    var top = "";
    if ($("pnTopDisplay"))
        top = Sortable.serialize("pnTopDisplay", { name: "portlet" });
    var left = "";
    if ($("pnLeftDisplay"))
        left = Sortable.serialize("pnLeftDisplay", { name: "portlet" });
    var center = "";
    if ($("pnCenterDisplay"))
        center = Sortable.serialize("pnCenterDisplay", { name: "portlet" });
    var right = "";
    if ($("pnRightDisplay"))
        right = Sortable.serialize("pnRightDisplay", { name: "portlet" });
    var bottom = "";
    if ($("pnBottomDisplay"))
        bottom = Sortable.serialize("pnBottomDisplay", { name: "portlet" });
    var temp = top + "|" + left + "|" + center + "|" + right + "|" + bottom;
    return temp;
}
function ParsePosition(strListPosition) {
    var arrPanel = strListPosition.split("|");
    var messagePositions = "[";
    for (var i = 0; i < arrPanel.length; i++) {
        if (i != 0)
            messagePositions += ", ";
        var arrElement = arrPanel[i].split("&");
        messagePositions += "[";
        if (arrElement[0].length > 0)
            for (var j = 0; j < arrElement.length; j++) {
            if (j != 0)
                messagePositions += ", ";
            messagePositions += "\"" + arrElement[j].split("=")[1] + "\"";
        }
        messagePositions += "]";
    }
    messagePositions += "]";

    return messagePositions;
}
function OnChange() {
    var temp = Serialize();
    if (temp != strStatus) {
        OnUpdatePosition(temp);
        strStatus = temp;
    }            
}
function OnUpdatePosition(strListNew) {
    var arrListOrg = Sys.Serialization.JavaScriptSerializer.deserialize(ParsePosition(strStatus));
    var arrListNew = Sys.Serialization.JavaScriptSerializer.deserialize(ParsePosition(strListNew));
    var i = 0;
    var index1 = 0;
    var index2 = 0;
    if (arrListOrg[0].length == arrListNew[0].length &&
        arrListOrg[1].length == arrListNew[1].length &&
        arrListOrg[2].length == arrListNew[2].length &&
        arrListOrg[3].length == arrListNew[3].length &&
        arrListOrg[4].length == arrListNew[4].length) {
        
        while ((index1 = CompareArray(arrListOrg[i], arrListNew[i++])) == -1);
        i = 0;
        while ((index2 = CompareArrayLast(arrListOrg[i], arrListNew[i++])) == -1);
        i--;
        var temp = arrListOrg[i][index1];
        for (var j = index1; j < index2; j++)
            arrListOrg[i][j] = arrListOrg[i][j + 1];
        arrListOrg[i][index2] = temp;
        if(CompareArray(arrListOrg[i], arrListNew[i])==-1)
            PSCPortal.PortalService.PortletChangePosition(i, index1, i, index2, OnUpdatePositionSuccess, OnUpdatePositionFailed);
        else
            PSCPortal.PortalService.PortletChangePosition(i, index2, i, index1, OnUpdatePositionSuccess, OnUpdatePositionFailed);
    }
    else {               
        var arrPanel = new Array();
        for (j = 0; j < arrListOrg.length; j++)
            if (arrListNew[j].length != arrListOrg[j].length)
                arrPanel[i++] = j;
        index1 = CompareArrayAsync(arrListOrg[arrPanel[0]], arrListNew[arrPanel[0]]);
        index2 = CompareArrayAsync(arrListOrg[arrPanel[1]], arrListNew[arrPanel[1]]);
        if(arrListOrg[arrPanel[0]].length>arrListNew[arrPanel[0]].length)
            PSCPortal.PortalService.PortletChangePosition(arrPanel[0], index1, arrPanel[1], index2, OnUpdatePositionSuccess, OnUpdatePositionFailed);             
        else
            PSCPortal.PortalService.PortletChangePosition(arrPanel[1], index2, arrPanel[0], index1, OnUpdatePositionSuccess, OnUpdatePositionFailed);             
    }
}
function OnUpdatePositionSuccess(results, context, methodName) {
}
function OnUpdatePositionFailed(results, context, methodName) {

}
function CompareArrayAsync(arrSrc, arrDest) {
    var arrLarge = arrSrc.length > arrDest.length ? arrSrc : arrDest;
    var arrSmall = arrSrc.length > arrDest.length ? arrDest : arrSrc;
    for (var i = 0; i < arrLarge.length; i++)
        if (Search(arrSmall, arrLarge[i]) == -1)
            return i;
    return -1;
}
function CompareArray(arrSrc, arrDest) {
    for (var i = 0; i < arrSrc.length; i++)
        if (arrSrc[i] != arrDest[i])
            return i;
    return -1;
}
function Search(arrSrc, element) {
    for (var i = 0; i < arrSrc.length; i++)
        if (arrSrc[i] == element)
            return i;
    return -1;
}
function CompareArrayLast(arrSrc, arrDest) {
    for (var i = arrSrc.length - 1; i >=0; i--)
        if (arrSrc[i] != arrDest[i])
        return i;
    return -1;
}
function DropDownListBind(listNameValuePair, dropdownlistName) {
    var dropdownlist = document.getElementById(dropdownlistName);
    dropdownlist.innerHTML = "";
         
    for (var i = 0; i < listNameValuePair.length; i++) {
        var item = document.createElement("option");
        item.innerHTML = listNameValuePair[i].Name;
        item.value = listNameValuePair[i].Id;
        dropdownlist.appendChild(item);
    }
}

function OnPortletRemove(portletId) {    
    PSCPortal.PortalService.PorletRemove(portletId, OnPortletRemoveSuccess, OnPortletRemoveFailed);
}
function OnPortletRemoveSuccess(results, context, methodName) {
    var portlet = document.getElementById("portlet_"+results);
    portlet.parentNode.removeChild(portlet);     
}
function OnPortletRemoveFailed(results, context, methodName){        
}
function OnPortletAdd() {
    var ddlPanels = document.getElementById("ddlPortletPanelsExist");
    var ddlPortlets = document.getElementById("ddlPortlets");
    var txtPortletName = document.getElementById("txtPortletName");
    PSCPortal.PortalService.PortletAdd(ddlPortlets.value, ddlPanels.value, txtPortletName.value, OnPortletAddSuccess, OnPortletAddFailed);
}
function OnPortletAddSuccess(results, context, methodName) {
    javascript: __doPostBack('HiddenField1', '');
}
function OnPortletAddFailed(results, context, methodName) {
}
function OnLoadPortletPanelsExist() {
    if(document.getElementById("ddlPortletPanelsExist"))
        PSCPortal.PortalService.GetPanelsExist(true, OnLoadPortletPanelsExistSuccess, OnLoadPortletPanelsExistFailed);            
}
function OnLoadPortletPanelsExistSuccess(results, context, methodName) {
    var listNameValuePair = Sys.Serialization.JavaScriptSerializer.deserialize(results);
    DropDownListBind(listNameValuePair,"ddlPortletPanelsExist");
}
function OnLoadPortletPanelsExistFailed(results, context, methodName) {
}

function OnLoadPortletReferencePanelsExist() {
    if(document.getElementById("ddlPanelPortletReference"))
        PSCPortal.PortalService.GetPanelsExist(true, OnLoadPortletReferencePanelsExistSuccess, OnLoadPortletReferencePanelsExistFailed);
}
function OnLoadPortletReferencePanelsExistSuccess(results, context, methodName) {
    var listNameValuePair = Sys.Serialization.JavaScriptSerializer.deserialize(results);
    DropDownListBind(listNameValuePair, "ddlPanelPortletReference");
}
function OnLoadPortletReferencePanelsExistFailed(results, context, methodName) {
}

function OnLoadPortlet() {
    if(document.getElementById("ddlPortlets"))
        PSCPortal.PortalService.GetPortlets(OnLoadPortletSuccess, OnLoadPortletFailed);
}
function OnLoadPortletSuccess(results, context, methodName) {
    var listNameValuePair = Sys.Serialization.JavaScriptSerializer.deserialize(results);
    DropDownListBind(listNameValuePair, "ddlPortlets");
}
function OnLoadPortletFailed(results, context, methodName) {
}

function OnLoadPanelExist() {
    if(document.getElementById("ddlPanelsExist"))
        PSCPortal.PortalService.GetPanelsExist(false, OnLoadPanelExistSuccess, OnLoadPanelExistFailed);
}
function OnLoadPanelExistSuccess(results, context, methodName) {
    var listNameValuePair = Sys.Serialization.JavaScriptSerializer.deserialize(results)
    if (listNameValuePair.length > 0)
        DropDownListBind(listNameValuePair, "ddlPanelsExist");
    else {
        var divDisplayPanelsExist = document.getElementById("divDisplayPanelsExist");
        divDisplayPanelsExist.style.visibility = "hidden";
    }
}
function OnLoadPanelExistFailed(results, context, methodName) {
}

function OnLoadPanelNonExist() {
    if(document.getElementById("ddlPanelsNonExist"))
        PSCPortal.PortalService.GetPanelsNonExist(OnLoadPanelNonExistSuccess, OnLoadPanelNonExistFailed);
}
function OnLoadPanelNonExistSuccess(results, context, methodName) {
    var listNameValuePair = Sys.Serialization.JavaScriptSerializer.deserialize(results)
    if (listNameValuePair.length > 0)
        DropDownListBind(listNameValuePair, "ddlPanelsNonExist");
    else {
        var divDisplayPanelsNonExist = document.getElementById("divDisplayPanelsNonExist");
        divDisplayPanelsNonExist.style.visibility = "hidden";
    }
}
function OnLoadPanelNonExistFailed(results, context, methodName) {
}

function OnPanelRemove() {
    var dropdownlist = document.getElementById("ddlPanelsExist"); 
    var id = dropdownlist.value;
    PSCPortal.PortalService.PanelRemove(id, OnPanelRemoveSuccess, OnPanelRemoveFailed);
}
function OnPanelRemove(id) {   
    PSCPortal.PortalService.PanelRemove(id, OnPanelRemoveSuccess, OnPanelRemoveFailed);
}
function OnPanelRemoveSuccess(results, context, methodName) {
    var panel = document.getElementById("pn" + results);
    panel.parentNode.removeChild(panel);
}
function OnPanelRemoveFailed(results, context, methodName) {
}
function OnPanelAdd() {
    var dropdownlist = document.getElementById("ddlPanelsNonExist");    
    var style = document.getElementById("txtStyle").value;
    PSCPortal.PortalService.PanelAdd(dropdownlist.value, style, OnPanelAddSuccess, OnPanelAddFailed); 
}
function OnPanelAddSuccess(results, context, methodName) {
    javascript:__doPostBack('HiddenField1', '');
}
function OnPanelAddFailed(results, context, methodName) {
}    

function DisplayToolTip(event, articleId) {
        var x = GetMouseXPosition(event);
        var y = GetMouseYPosition(event);
        var divDisplayToolTip = document.getElementById("DisplayToolTip");
        divDisplayToolTip.style.top = (y + 20)+ "px";
        divDisplayToolTip.style.left = (x - 100)+ "px";
        document.getElementById("DisplayTooltipdescription").innerHTML = "<img width='50px' height='50px' src='Images/ajax-loader.gif' alt=''/>";
        divDisplayToolTip.style.visibility = "visible"; 
        
        OnLoadArticleTooltip(articleId);
}
function HiddenTooltip() {
    document.getElementById("DisplayToolTip").style.visibility = "hidden";      
}
function OnLoadArticleTooltip(articleId) {
    PSCPortal.CMSService.GetArticleTooltip(articleId, OnLoadArticleTooltipSuccess, OnLoadArticleTooltipFailed);
}
function OnLoadArticleTooltipSuccess(results, context, methodName) {
    var at = Sys.Serialization.JavaScriptSerializer.deserialize(results);
    //document.getElementById("DisplayTooltipTitle").innerHTML = at.Title;
    document.getElementById("DisplayTooltipdescription").innerHTML = at.Description;
    var divDisplayToolTip = document.getElementById("DisplayToolTip");
    divDisplayToolTip.style.visibility = "visible";        
}
function OnLoadArticleTooltipFailed(results, context, methodName) {
}
function MovePanelDisplayTooltip(event) {
    var divDisplayToolTip = document.getElementById("DisplayToolTip");
    if (divDisplayToolTip.style.visibility == "hidden")
        return;
    var x = GetMouseXPosition(event);
    var y = GetMouseYPosition(event);
    
    divDisplayToolTip.style.top = (y + 20) + "px";
    divDisplayToolTip.style.left = (x - 100) + "px";
}
