﻿var TemplateUrl = '', AppRelDirectory = '';
var BglLstStartPaneImageHtml = '', BglLstPrevPaneImageHtml = '', BglLstNextPaneImageHtml = '', BglLstClosePaneImageHtml = '';
var BglLstFullSizeImageHtml = '', BglLstAdaptSizeImageHtml = '';

function InitializePage(templateId, templateUrl, appRelDir)
{
    ActiveTemplate = templateId;
    TemplateUrl = templateUrl;
    AppRelDirectory = appRelDir;
    try
    {
        PageInit();
        TemplatePageResize();
    }
    catch (e)
    {
        BaseAppError();
    }
}

function BaseAppError()
{
    alert('Error in base application.\nPage could not be initiated.');
}

function ClearTemplateContent()
{
    CntPhObj.innerHTML = '';
    CntPhObj.innerText = '';
}

function TemplateDefaultContentIframe()
{
    return '<iframe id=\"'+ CntIfrId + '\" src=\"' + TemplateUrl + 'html\/blank.html\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"auto\" class=\"' + CntIfrId + '\" style=\"width:100%;height:' + GetContentPlaceholderHeight() + 'px\"></iframe>';
}

function TemplateContentPlaceHolder()
{
    var ph;
    ph = document.getElementById(CntIfrId);
    if (!ph)
    {
        ClearTemplateContent();
        CntPhObj.innerHTML = TemplateDefaultContentIframe();
        ph = document.getElementById(CntIfrId);
    }
    return ph;
}

function LoadContent(pwId, pwCultureId, itmId, itmType, itmContentType, itmFrontPageType, itmDetailId)
{
    var cph = TemplateContentPlaceHolder();
    var ifrSrc = '';
    if (cph)
    {
        var qpc = new QsPrmCollection();
        if (itmType == FolderType.Folder)
        {
            if (itmContentType == ContentType.Normal)
            {
                if (itmFrontPageType == FolderFrontPageType.File)
                {
                    qpc.QsParams.push(new QsPrm(QsProjectWebId, pwId));
                    qpc.QsParams.push(new QsPrm(QsFileId, itmId));
                    qpc.QsParams.push(new QsPrm(QsCultureId, pwCultureId));
                    qpc.QsParams.push(new QsPrm(QsIsFrontPageFile, "1"));
                    ifrSrc = AppRelDirectory + 'public/OpenFile.aspx' + qpc.QueryString(false);
                }
                else if (itmFrontPageType == FolderFrontPageType.Url)
                {
                    qpc.QsParams.push(new QsPrm(QsProjectWebId, pwId));
                    qpc.QsParams.push(new QsPrm(QsFolderId, itmId));
                    ifrSrc = AppRelDirectory + 'public/OpenUrl.aspx' + qpc.QueryString(false);
                }
                else if (itmFrontPageType == FolderFrontPageType.Html)
                {
                    qpc.QsParams.push(new QsPrm(QsFolderId, itmId));
                    ifrSrc = AppRelDirectory + 'ifr/FolderFrontPage.aspx' + qpc.QueryString(true);
                }
            }
            else if (itmContentType == ContentType.Bildegalleri)
            {
                qpc.QsParams.push(new QsPrm(QsProjectWebId, pwId));
                qpc.QsParams.push(new QsPrm(QsFolderId, itmId));
                ifrSrc = AppRelDirectory + 'public/BildeGalleri.aspx' + qpc.QueryString(false);
            }
            else if (itmContentType == ContentType.Listemappe)
            {
                qpc.QsParams.push(new QsPrm(QsProjectWebId, pwId));
                qpc.QsParams.push(new QsPrm(QsFolderId, itmId));
                ifrSrc = AppRelDirectory + 'public/ListeMappe.aspx' + qpc.QueryString(false);
            }
            else if (itmContentType == ContentType.Notiser)
            {
                qpc.QsParams.push(new QsPrm(QsProjectWebId, pwId));
                qpc.QsParams.push(new QsPrm(QsFolderId, itmId));
                if (itmDetailId != null)
                {
                    if (itmDetailId != EmptyGuid)
                    {
                        qpc.QsParams.push(new QsPrm(QsNoteId, itmDetailId));
                    }
                }
                ifrSrc = AppRelDirectory + 'public/Notiser.aspx' + qpc.QueryString(false);
            }
        }
        else if (itmType == FolderType.File)
        {
            qpc.QsParams.push(new QsPrm(QsProjectWebId, pwId));
            qpc.QsParams.push(new QsPrm(QsFileId, itmId));
            qpc.QsParams.push(new QsPrm(QsCultureId, pwCultureId));
            qpc.QsParams.push(new QsPrm(QsIsFrontPageFile, "0"));
            ifrSrc = AppRelDirectory + 'public/OpenFile.aspx' + qpc.QueryString(false);
        }
        else if (itmType == FolderType.Page)
        {
            if (itmContentType == ContentType.Webside)
            {
                qpc.QsParams.push(new QsPrm(QsProjectWebId, pwId));
                qpc.QsParams.push(new QsPrm(QsFolderId, itmId));
                ifrSrc = AppRelDirectory + 'public/WebPage.aspx' + qpc.QueryString(false);
            }
        }
        else if (itmType == FolderType.Url)
        {
        }
        if (ifrSrc != '')
        {
            document.getElementById(CntIfrId).src = ifrSrc;
        }
    }
}

function TemplatePageResize(e)
{
    try
    {
        winWidth = GetWinWidth();
        winHeight = GetWinHeight();
        MenuWidth = GetElmWidth(MnuObjId);
        MenuHeight = GetElmHeight(MnuObjId);
        ContentWidth = GetElmWidth(CntObjId);
        ContentHeight = GetElmHeight(CntObjId);
        if (!isNaN(parseInt(ActiveTemplate)))
        {
            switch (parseInt(ActiveTemplate))
            {
                case 2:
                    {
                        var hdrHeight = GetElmHeight(HdrObjId);
                        try
                        {
                            MnuObj.style.height = (winHeight - hdrHeight) + 'px';
                            MainObj.style.height = (winHeight - hdrHeight) + 'px';
                        }
                        catch (e)
                    { }
                        break;
                    }
            }
        }
        var mainObj = document.getElementById(MainObjId);
        if (mainObj)
        {
            if ((!isNaN(ActiveTemplate)) && (parseInt(ActiveTemplate) != 4))
            {
                mainObj.style.margin = '0px 0px 0px ' + MenuWidth + 'px';
            }
        }
        var cph = document.getElementById(CntIfrId);
        if (cph)
        {
            cph.style.width = '100%';
            cph.style.height = GetContentPlaceholderHeight() + 'px';
        }
    }
    catch (e)
    { }
}
