﻿/**
* PKA specifik javascript
**/
var PKA = {
    frontPage: function () {
        if ($('#page').hasClass('frontPage')) {
            var widgetHeight = 340,
                lastUnit = $('.size620').next('.lastUnit'),
                lastUnitHeight = lastUnit.height(),
                topHeight = lastUnit.find('.wpPadding:first').height(),
                bottomHeight = lastUnit.find('.marginTpx .wpPadding:first').outerHeight(),
                bottomWPHeight = lastUnit.find('.marginTpx .ms-WPBody:first').height(),
                difference = widgetHeight - (topHeight + bottomHeight),
                changedHeight = bottomWPHeight + difference;

            if (widgetHeight > lastUnitHeight) {
                lastUnit.find('.marginTpx .ms-WPBody').each(function () {
                    $(this).css('height', changedHeight);
                });
            }
        }
    },
    async: {
        endHandler: function (sender, args) {
            var senderID = sender._postBackSettings.panelID;
            if (senderID.indexOf('Sitemap') > 1) {
                var sitemapWidget = $('div[id$=sitemapWidget]').attr('id'),
                    senderType = senderID.split('Alfabetisk');
                if (owp.common !== undefined) {
                    if (sitemapWidget !== 'undefined') { owp.common.siteMap.init(sitemapWidget, senderType); }
                }
            }
        }
    },
    init: function () {
        var pageType = $('#pageType').val(),
            sitemapWidget = $('div[id$=sitemapWidget]').attr('id'),
            frontPage = $('#page').hasClass('frontPage'),
            rightCol = $('#rightCol');
        if (rightCol.length > 0) {
            rightCol.find('.wpPadding:not(.addThisCol) table:not(:has(ms-WPHeader))').find('.ms-WPBody').removeClass()
        }

        //format dates on newpages to dd/mm/yyyy
        if ($('.newsDateLabel').length > 0) {
            $('.newsDateLabel').text($.datepicker.formatDate("dd/mm/yy", $.datepicker.parseDate('dd-mm-yy', $('.newsDateLabel').text())));
        }

        if (owp.common !== undefined) {
            if (sitemapWidget !== 'undefined') { owp.common.siteMap.init(sitemapWidget); };
        }
        if (frontPage === true) {
            PKA.frontPage();
        }
    }
}
$('document').ready(function () {
    if (editMode == undefined) {
        var editMode = $('#contentArea').is(':has(.ms-SPZone,.ms-formfieldvaluecontainer)');
    }
    if (editMode !== true) {
        PKA.init();
    }
    var pkaRequestInstance;
    try {
        pkaRequestInstance = Sys.WebForms.PageRequestManager.getInstance();
        pkaRequestInstance.add_endRequest(PKA.async.endHandler);
    }
    catch (err) {
        log('Could not set request handlers: ' + err.Message);
    }
});
