function APPSPage() {

    this.documentTemplateFooter = '<div class="document-brief-item">\
									<span class="document-brief-item-title${critical}">\
										{{if readOnly}}\
											<span>${title}<span class="document-brief-item-protection" {{each protections}}apps-protection="${$value}"{{/each}}><img src="/assets/images/padlock.png"/></span></span>\
										{{else}}\
											<a title="${title}" href="${url}">${title}</a>\
										{{/if}}\
									</span>\
									<span class="document-brief-item-date">${date}</span>\
									</div>';

    this.documentTemplateRelated = '<div class="document-brief-item">\
									<span class="document-brief-item-title">\
										{{if readOnly}}\
											<span>${title}<span class="document-brief-item-protection" {{each protections}}apps-protection="${$value}"{{/each}}><img src="/assets/images/padlock.png"/></span></span>\
										{{else}}\
											<a title="${title}" href="${url}">${title}</a></span>\
										{{/if}}\
									</span>\
									</div>';

    this.protectionTemplate = 'title="${intro}<br/>{{each protections}}- ${$value}<br/>{{/each}}"';

    this.eventTemplate = '<div class="apps_event_item">\
									<span class="apps_event_item_icon"><img src="${machine}/assets/images/cal_icon.png"/></span>\
									<span class="apps_event_item_title">\
										{{if url==""}}\
											${title}\
										{{else}}\
											<a target="_blank" title="${title}" href="${url}">${title}</a>\
										{{/if}}\
									</span>\
									<span class="apps_event_item_where">${where}</span>\
									<span class="apps_event_item_start">${TXT_start} : ${start}</span>\
									<span class="apps_event_item_end">${TXT_end} : ${end}</span>\
									</div>';

    this.supportTemplate = '<div class="document-brief-item"><span class="document-brief-item-title"><a href="javascript:appsEmail.showEmailDialog(\'site_feedback\', \'\', \'\')">${contactus}</a></span></div>';

    this.protectionTemplate = '<div class="apps-protection-text">${protection}</div>';

    this.appsSuite = {};
}

var appsPage = new APPSPage();

(function ($) {
    var globalObject = appsPage;

    APPSPage.prototype.setTranslations = function (_translations) {
        $.extend(globalObject.translations, _translations);
    }

    APPSPage.prototype.defaultTranslations = function () {
        var appsPageTranslations = new Object();
        appsPageTranslations['kbsearch.Protected'] = "This document is only available to the following users";
        appsPageTranslations['kbsearch.DOC_PDFDocument'] = "PDF Document";
        appsPageTranslations['kbsearch.DOC_HTMLDocument'] = "HTML Document";
        appsPageTranslations['kbsearch.PRT_CONTRACT'] = "Registered Users with a Contract";
        appsPageTranslations['kbsearch.PRT_REGULAR'] = "Registered Users";
        appsPageTranslations['common.contact_us'] = "Contact Us";
        appsPageTranslations['common.start'] = "Start";
        appsPageTranslations['common.end'] = "End";
        globalObject.translations = appsPageTranslations;
    }

    APPSPage.prototype.showDocument = function (document, templateName, divId) {
        $.tmpl(templateName, {
            title: document.title,
            url: document.url,
            date: document.date,
            documentType: globalObject.translations['kbsearch.DOC_' + document.documentType],
            language: document.language,
            readOnly: document.readOnly,
            critical: document.critical?'critical':'',
            suiteName: globalObject.appsSuite.suiteName,
            intro: globalObject.translations['kbsearch.Protected'],
            protections: document.protections
        }).appendTo(divId);
    }

    APPSPage.prototype.getDocumentList = function (sessionId, request, divId, fn) {
        APPS.getJSONObject('/json/' + request, { sessionId: sessionId }, function (data) {
            $.each(data.documents, function (i1, v1) {
                globalObject.showDocument(v1, "documentTemplateFooter", divId);
            });
            globalObject.applyTooltips();
            fn(data);
        });
    }

    APPSPage.prototype.applyTooltips = function () {
        $('.document-brief-item-protection').tooltip({
            items: "[apps-protection]",
            content: function (callback) {
                var element = $(this);
                return $.tmpl("protectionTemplate", { protection: globalObject.translations['kbsearch.PRT_' + element.attr('apps-protection')] });
            }
        });
    }

    APPSPage.prototype.getRecentDocumentsKb = function (fn) {
        globalObject.getRecentDocuments('', fn);
    }

    APPSPage.prototype.getRecentDocumentsForum = function (sessionId, fn) {
        globalObject.getRecentDocuments(sessionId, fn);
    }

    APPSPage.prototype.getRecentDocuments = function (sessionId, fn) {
        APPS.getJSONObject('/json/getRecentDocuments.json', { sessionId: sessionId }, function (data) {
            $.each(data.documents, function (i1, v1) {
                if(v1.critical) {
                    globalObject.showDocument(v1, "documentTemplateFooter", '#apps_recentitems');
                }
            });
            $.each(data.documents, function (i1, v1) {
                if(!v1.critical) {
                    globalObject.showDocument(v1, "documentTemplateFooter", '#apps_recentitems');
                }
            });
            globalObject.applyTooltips();
            fn(data);
        });
    }

    APPSPage.prototype.getEventsKb = function (fn) {
        globalObject.getEvents('', fn);
    }

    APPSPage.prototype.getEvents = function (sessionId, fn) {
        APPS.getJSONObject('/json/getEvents.json', { sessionId: sessionId }, function (data) {
            $.each(data.events, function (i1, v1) {
                $.tmpl("event", {
                    title: v1.title,
                    url: v1.url,
                    where: v1.where,
                    start: v1.start,
                    end: v1.end,
                    suiteName: globalObject.appsSuite.suiteName,
                    TXT_start: globalObject.translations['common.start'],
                    TXT_end: globalObject.translations['common.end']
                }).appendTo('#apps_events');
            });
            fn(data);
        });
    }

    APPSPage.prototype.getLeftPanel = function (fn) {
        APPS.getJSONObject('/json/getPageBody.json', { path: 'Contents/Assets/' + globalObject.appsSuite.suiteRef + '/LeftPanel/index.htm' }, function (data) {
            $('#apps_footer_leftpanel').html(data.body);
            fn(data);
        });
    }

    APPSPage.prototype.getFollowUs = function (fn) {
        APPS.getJSONObject('/json/getPageBody.json', { path: 'Contents/Assets/' + globalObject.appsSuite.suiteRef + '/FollowUs/index.htm' }, function (data) {
            $('#apps_followus_share').html(data.body);
            fn(data);
        });
    }

    APPSPage.prototype.showFooter = function () {
        globalObject.getEventsKb(function () {
            globalObject.getRecentDocumentsKb(function () { });
            globalObject.getLeftPanel(function () { });
            globalObject.getFollowUs(function () { });
        });
    }

    APPSPage.prototype.getFeaturedDocumentsKb = function (fn) {
        globalObject.getFeaturedDocuments('', fn);
    }

    APPSPage.prototype.getFeaturedDocumentsForum = function (sessionId, fn) {
        globalObject.getFeaturedDocuments(sessionId, fn);
    }

    APPSPage.prototype.getFeaturedDocuments = function (sessionId, fn) {
        globalObject.getDocumentList(sessionId, 'getFeaturedDocuments.json', '#featured-list-item', fn);
    }

    APPSPage.prototype.showSettingsDialog = function () {
        $('#container').fadeTo(200, 0.1);
        $("#settings").dialog({
            resizable: false,
            modal: true,
            width: '50%',
            height: 200,
            open: function () {
                $("#settings-content").html('<img src="/assets/images/activity.gif" style="margin-top: 50px;"/>');
                $("#settings-content").load("/settings.jsp", function () {
                    $('#settings-submit').click(function () {
                        $("#settings-content").load("/settingsResults.jsp", { appsCountryId: $('#appsCountryId').val(), appsLanguageId: $('#appsLanguageId').val() }, function () {
                            $("#settings-saved").click(function () {
                                $("#settings").dialog("close");
                                location.reload();
                            });
                        });
                    });
                });
            },
            close: function () {
                $('#container').fadeTo(100, 1);
            }
        });
    }

    APPSPage.prototype.showActivity = function () {
        $('#container').fadeTo(200, 0.1);
        $('#page_container').fadeTo(200, 0.1);
        $("#activity").show();
    }

    APPSPage.prototype.hideActivity = function () {
        $("#activity").hide();
        $('#container').fadeTo(50, 1.0);
        $('#page_container').fadeTo(50, 1.0);
    }

    APPSPage.prototype.initialise = function (appsSuite) {
        globalObject.defaultTranslations();

        $.template("documentTemplateFooter", globalObject.documentTemplateFooter);
        $.template("protectionTemplate", globalObject.protectionTemplate);
        $.template("documentTemplateRelated", globalObject.documentTemplateRelated);
        $.template("supportTemplate", globalObject.supportTemplate);
        $.template("protection", globalObject.protectionTemplate);
        $.template("event", globalObject.eventTemplate);

        globalObject.appsSuite = appsSuite;
    }

})(jQuery)