﻿/*
	Functionality for the html sound player

	// Opens P4 Mediaplayer
	// Channel: 1-P4 Norge	2-P4 Hits	3-P4 Ballade	4-P4 Norsk	5-P4 Oldies
	// sType: nyheter, samisk, sytten, program, clip
	// iID: Converted_soundsID, MMO_ID eller PS_ID

*/

// Soul_Master custom bug fix for ticket #8052 (IE9 document.append bug)
if (jQuery.browser.msie && jQuery.browser.version >= 9) {
	jQuery.support.noCloneEvent = true
}

jQuery(document).ready(function() {
    doPlayAtLoad();
});

function openP4Player(iChannel, sType, iID){
	if (iChannel == undefined) iChannel = "";
	if (sType != undefined) sType = "&type=" + sType; else sType = "";
	if (iID != undefined) iID = "&id=" + iID; else iID = "";
	var path = "/player/player.aspx?channel=" + iChannel + sType + iID;
	var w = window.open(path, "P4Lydavspiller", "toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=0, width=717, height=500, top=0, left=250");
	try {
	    w.focus();
	} catch (e) { }
}

function openP4PlayerModal(sType, iId) {
    var playerPath = "/player/player.aspx?";
    var playerWidth = 717;
    var playerHeight = 500;

    if (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) < 8) {
        playerWidth = 718;
        playerHeight = 501;
    }


    var title = "P4 Lyttesenter";

    /*if (sType != undefined && sType == "video") {
        playerPath = "/player/playerVideo.aspx?";
        playerWidth = 468;
        playerHeight = 330;
        playerPath += "height=" + playerHeight - 30 + "&width=" + playerWidth + "&";
        title = "P4 Videoavspiller";
    }*/

    if (sType != undefined && sType == "live" && iId != undefined) {
        playerPath += "channel=" + iId;
    }
    else {
        playerPath += "type=" + sType;

        if (iId != undefined) {
            playerPath += "&id=" + iId;
        }
    }

    openModalWindow(playerPath, playerWidth, playerHeight, title);

}
var modalWindowInit = false;
function openModalWindow(path, width, height, caption) {
    if (!modalWindowInit) {
        jQuery(document.body)
      .append("<div id='modalwindow_overlay'></div><div id='modalwindow_window'><div id='modalwindow_caption'></div>"
        + "<img src='/images/gfx/modalwindow_close.png' alt='Close window'/></div>");
        jQuery("#modalwindow_window img").click(closeModalWindow);
        modalWindowInit = true;
    }

    jQuery("#modalwindow_frame").remove();
    jQuery("#modalwindow_window").append("<iframe id='modalwindow_frame' src='" + path + "' frameborder='0' scrolling='no'></iframe>");

    jQuery("#modalwindow_caption").html(caption);

    var leftPos = parseInt((990 - width) / 2);
    var topPos = parseInt((document.viewport.getHeight() / 2) - (height / 2) + document.viewport.getScrollOffsets().top);
    var topMenuPos = $("menu-top").cumulativeOffset().top;
    if (topPos < topMenuPos) {
        topPos = topMenuPos + 20;        
    }
    
    if (leftPos <= 0) leftPos = 10;
    if (topPos <= 0) topPos = 10;

    jQuery("#modalwindow_window").css({ width: width + "px", height: height + 18 + "px",
        left: leftPos + "px", top: topPos + "px"
    });

    jQuery("#modalwindow_frame").css({ height: height + "px", width: width + "px" });
    jQuery("#modalwindow_overlay").css({ height: getBodyHeight() + "px" });
    jQuery("#modalwindow_overlay").show();
    jQuery("#modalwindow_window").show();


    if (typeof (P4NO_refresh) != 'undefined') {
        clearTimeout(P4NO_refresh);
    }
}

function closeModalWindow() {
    jQuery("#modalwindow_frame").remove();
    jQuery("#modalwindow_window").animate({ opacity: 'hide' }, 'slow');
    jQuery("#modalwindow_overlay").hide();
    if (typeof (P4NO_refresh) != 'undefined') {
        P4NO_refresh = window.setTimeout(function() { window.location.href = window.location.href }, 600000);
    }
}

function doPlayAtLoad() {
    var playType = jQuery.urlParam("player");
    var playerId = jQuery.urlParam("pid");

    if (playType != 0) {
        openP4PlayerModal(playType, playerId);
    }
}

jQuery.urlParam = function(name) {
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results) { return 0; }
    return results[1] || 0;
}

function getBodyHeight() {
    ///<summary>Gets the height of the document body</summary>
    var footer = $("footer");
    var footerHeight = footer.getHeight();
    var footerOffset = footer.cumulativeOffset().top;
    return footerHeight + footerOffset;
}
