﻿var popupStatus = 0;
var bgBox;
var pBox;
var video;

function loadPopup(){
	if(popupStatus==0){
		$(bgBox).css({
			"opacity": "0.7"
		});
		$(bgBox).fadeIn("slow");
		$(pBox).fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(){
    if (popupStatus == 1) {
        swfobject.removeSWF("flashPlayerContent");
		$(bgBox).fadeOut("fast");
		$(pBox).fadeOut("fast");
		popupStatus = 0;
		bgBox = null;
		pBox = null;
	}
}

function centerPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(pBox).height();
	var popupWidth = $(pBox).width();
	
	$(pBox).css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	
	// handle IE6
	$(bgBox).css({
		"height": windowHeight
	});
	
}

function buildPopUp(){
	pBox.html(	$("<a href='#' class='playerBoxClose'><img src='/images/closePlayer.gif' width='19' height='19' alt='Close player' /></a>"
				+ "<div id='flashPlayerContent'>Du skal have flash player 9 eller højere for at kunne se denne film."
				+ "Hent den nyeste flash player her, det er ganske gratis: <a href='http://get.adobe.com/flashplayer/'>http://get.adobe.com/flashplayer</a></div>") );
				
	var flashvars = {};
	var attributes = {};
	var params = { scale: "exactFit", menu: false, wmode: "opaque", allowFullScreen: true };
	swfobject.embedSWF("/flash/player.swf?src=" + video, "flashPlayerContent", "640", "360", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
	
	$(".playerBoxClose").click(function(){
		disablePopup();
	});
}


$(document).ready(function () {

    $(".openPlayer").click(function () {
        video = $(this).attr("href");
        pBox = $(this).parent().children('.plBox');
        //pBox = $(this).next('div');
        bgBox = $(pBox).next('div');
        buildPopUp();
        centerPopup();
        loadPopup();
        return false;
    });

    $(".playerBackground").click(function () {
        disablePopup();
    });

    $(document).keypress(function (e) {
        //Press Escape event!
        if (e.keyCode == 27 && popupStatus == 1) {
            disablePopup();
        }
    });

});


