﻿
// Global setting to allow switching between MVC action processing or webForms.
var CMS_MVC = false; // set to false for webforms

//Modal popup fx flags
var finishedTX1 = true;
var finishedTX2 = true;

function setRequestedPageStatus(pRequestedPageStatus, pPage)
{
    var msg = null;
    var confirmResult = false;
    var messagePresent = false;

    //jQuery.noConflict();

    jQuery(document).ready(function ($)
    {

        if (msg != null)
        {
            messagePresent = true;
            confirmResult = confirm(msg);
        }

        if ((messagePresent && confirmResult) || !messagePresent)
        {
            if (CMS_MVC)
            {
                $.post('/admin/setRequestedPageStatus/', { requestedPageStatus: pRequestedPageStatus },
                function (data)
                {
                    if (data == "ok")
                    {
                        if (pPage == null)
                            document.location.href = document.location.href;
                        else
                            document.location.href = pPage;
                    }
                });
            }
            else
            {
                $.post('/CMS/admin/handler.ashx', { action: 'setRequestedPageStatus', value: pRequestedPageStatus },
                function (data)
                {
                    if (data == "ok")
                    {
                        if (pPage == null)
                            document.location.href = document.location.href;
                        else
                            document.location.href = pPage;
                    }
                });
            }
        }

    });
}

function setRequestedAction(pRequestedAction, pPage)
{
    var msg = null;
    var confirmResult = false;
    var messagePresent = false;

    //jQuery.noConflict();

    jQuery(document).ready(function ($)
    {

        switch (pRequestedAction)
        {
            case "MakePreApproved":
                msg = "Are you sure you want to SEND FOR APPROVAL ?";
                break;
            case "MakeApproved":
                msg = "Are you sure you want to APPROVE ?";
                break;
            case "MakeNotApproved":
                msg = "Are you sure you want to NOT APPROVE ?";
                break;
            case "MakePublished":
                msg = "Are you sure you want to PUBLISH ?";
                break;
            case "MakeRevision":
                msg = "Are you sure you want to REVISE ?";
                break;
            case "MakeEditable":
                msg = "Are you sure you want to EDIT ?";
                break;
            case "MakeArchived":
                msg = "Are you sure you want to ARCHIVE ?";
                break;
            default:
                msg = null;
                break;
        }

        if (msg != null)
        {
            messagePresent = true;
            confirmResult = confirm(msg);
        }

        if ((messagePresent && confirmResult) || !messagePresent)
        {
            if (CMS_MVC)
            {
                $.post('/admin/setRequestedAction/', { requestedAction: pRequestedAction },
                function (data)
                {
                    if (data == "ok")
                    {
                        if (pPage == null)
                            document.location.href = document.location.href;
                        else
                            document.location.href = pPage;
                    }
                });
            }
            else
            {
                $.post('/CMS/admin/handler.ashx', { action: 'setRequestedAction', value: pRequestedAction },
                function (data)
                {
                    if (data == "ok")
                    {
                        if (pPage == null)
                            document.location.href = document.location.href;
                        else
                            document.location.href = pPage;
                    }
                });
            }

            
        }

    });
}

function setRequestedSecurity(pRequestedSecurity, pPage)
{
    var msg = null;
    var confirmResult = false;
    var messagePresent = false;

    //jQuery.noConflict();

    jQuery(document).ready(function ($)
    {

        msg = "Are you sure you want to change security settings ?";

        if (msg != null)
        {
            messagePresent = true;
            confirmResult = confirm(msg);
        }

        if ((messagePresent && confirmResult) || !messagePresent)
        {
            if (CMS_MVC)
            {

                $.post('/admin/setRequestedSecurity/', { requestedSecurity: pRequestedSecurity },
                function (data)
                {
                    if (data == "ok")
                    {
                        if (pPage == null)
                            document.location.href = document.location.href;
                        else
                            document.location.href = pPage;
                    }
                });

            }
            else
            {
                $.post('/CMS/admin/handler.ashx', { action : 'setRequestedSecurity' , value: pRequestedSecurity },
                function (data)
                {
                    if (data == "ok")
                    {
                        if (pPage == null)
                            document.location.href = document.location.href;
                        else
                            document.location.href = pPage;
                    }
                });
            }
        }

    });
}

function setViewEdit(pPage)
{
    
    //jQuery.noConflict();

    jQuery(document).ready(function ($)
    {
        if (CMS_MVC)
        {
            $.post('/admin/setViewEdit/', {},
            function (data)
            {
                if (data == "ok")
                {
                    if (pPage == null)
                        document.location.href = document.location.href;
                    else
                        document.location.href = pPage;
                }
            });
        }
        else
        {
            $.post('/CMS/admin/handler.ashx', { action: 'setViewEdit' },
            function (data)
            {
                if (data == "ok")
                {
                    if (pPage == null)
                        document.location.href = document.location.href;
                    else
                        document.location.href = pPage;
                }
            });
        }
    });
}

function showModal(pModal,pTitle,pPage)
{
    var tmpWorkingArry = pPage.split("|");
    var tmpLinkMoniker = tmpWorkingArry[0];
    var tmpLinkId = tmpWorkingArry[1];
    var tmpActualLink = "";
    
    //jQuery.noConflict();

    jQuery(document).ready(function ($)
    {
        
        switch (tmpLinkMoniker)
        {
            case "editContentArea":

                if (CMS_MVC)
                    tmpActualLink = "/admin/editContentArea/" + tmpLinkId;
                else
                    tmpActualLink = "/CMS/admin/editContentArea.aspx?id=" + tmpLinkId;
                break;

            case "viewMetaKeywords":

                if (CMS_MVC)
                    tmpActualLink = "/admin/viewMetaKeywords/";
                else
                    tmpActualLink = "/CMS/admin/viewMetaKeywords.aspx";
                break;

            case "editMetaKeywords":

                if (CMS_MVC)
                    tmpActualLink = "/admin/editMetaKeywords/";
                else
                    tmpActualLink = "/CMS/admin/editMetaKeywords.aspx";
                break;

            case "addViewComments":

                if (CMS_MVC)
                    tmpActualLink = "/admin/addViewComments/";
                else
                    tmpActualLink = "/CMS/admin/addViewComments.aspx";
                break;

            default:

                break;
        }

        if (tmpActualLink != "")
        {
            var popupHeight = $("#" + pModal).height();

            $("#" + pModal + "Frame").attr('src', tmpActualLink);
            $("#" + pModal + "Frame").attr('height', popupHeight * 0.9);
            $("#" + pModal + "Title").text(pTitle);

            //LOADING POPUP
            //centering with css
            centerPopup(pModal);
            //load popup
            loadPopup(pModal);
        }
    });
}

/***************************/
//@Author: Adrian "yEnS" Mato Gondelle - Modified by CamB of iSky
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

function setupModal(pModal)
{
    
    //jQuery.noConflict();

    jQuery(document).ready(function ($)
    {

        //CLOSING POPUP
        //Click the x event!
        $("#" + pModal + "Close").click(function ()
        {
            disablePopup(pModal);
        });

        //Click out event!
        $("#" + pModal + "Background").click(function ()
        {
            disablePopup(pModal);
        });

        //Press Escape event!
        $(document).keyup(function (e)
        {
            if (e.keyCode == 27 && finishedTX1 && finishedTX2)
            {
                disablePopup(pModal);
            }
        });

    });
}

function loadPopup(pModal)
{
    //jQuery.noConflict();

    jQuery(document).ready(function ($)
    {

        //loads popup only if all fx done
        if (finishedTX1 && finishedTX2)
        {
            finishedTX1 = false;
            finishedTX2 = false;

            $("#" + pModal + "Background").css({
                "opacity": "0.7"
            });
            $("#" + pModal + "Background").fadeIn("slow", function () { finishedTX1 = true; });
            $("#" + pModal).fadeIn("slow", function () { finishedTX2 = true; });
        }

    });
}

//disabling popup with jQuery magic!
function disablePopup(pModal)
{
    //jQuery.noConflict();

    jQuery(document).ready(function ($)
    {
        
        //disables popup only if it is enabled
        if (finishedTX1 && finishedTX2)
        {
            finishedTX1 = false;
            finishedTX2 = false;
            
            $("#" + pModal + "Background").fadeOut("slow", function () { finishedTX1 = true; });
            $("#" + pModal).fadeOut("slow", function () { finishedTX2 = true; });
        }

    });
}

function refresh(pPage)
{
    
    if (finishedTX1 && finishedTX2)
    {
        
        if (pPage == null)
            document.location.href = document.location.href;
        else
            document.location.href = pPage;
    }
    else
    {
        setTimeout("refresh(" + pPage + ")", 250);
    }
}

//centering popup
function centerPopup(pModal)
{
    //jQuery.noConflict();

    jQuery(document).ready(function ($)
    {

        //request data for centering

        //var windowWidth = document.body.clientWidth || document.documentElement.clientWidth || self.innerWidth;
        //var windowHeight = document.body.clientHeight || document.documentElement.clientHeight || self.innerHeight;

        var windowWidth = document.documentElement.clientWidth || self.innerWidth;
        var windowHeight = document.documentElement.clientHeight || self.innerHeight;

        //alert(document.body.clientHeight);
        //alert(document.documentElement.clientHeight);
        //alert(self.innerHeight);

        var popupHeight = $("#" + pModal).height();
        var popupWidth = $("#" + pModal).width();

        //centering
        $("#" + pModal).css({
            "position": "absolute",
            "top": windowHeight / 2 - popupHeight / 2,
            "left": windowWidth / 2 - popupWidth / 2
        });

        //only need force for IE6

        $("#" + pModal + "Background").css({
            "height": windowHeight
        });

    });
}


