window.onload = function() {
    var popupStatus = 0;

    $(".mediaVideo").click(
        function(){
            var windowWidth = document.documentElement.clientWidth;
            var windowHeight = document.documentElement.clientHeight;
            var popupHeight = $("#popupContent").height();
            var popupWidth = $("#popupContent").width();

            var yOffset = 0;
            if (self.pageYOffset){// all except Explorer
                yOffset = self.pageYOffset;
            }
            else if (document.documentElement && document.documentElement.scrollTop){// Explorer 6 Strict{
                yOffset = document.documentElement.scrollTop;
            }
            else if (document.body){ // all other Explorers
                y = document.body.scrollTop;
            }

            $("#popupContent").css({
                "position": "absolute",
                "top": (windowHeight/2+yOffset)-(popupHeight/2),
                "left": windowWidth/2-popupWidth/2
            });
            //only need force for IE6
            $("#backgroundPopup").css({
                "height": windowHeight
            });

            if(popupStatus==0){
                $("#backgroundPopup").css({
                    "opacity": "0.7"
                });
                $("#backgroundPopup").fadeIn("slow");
                $("#popupContent").fadeIn("slow");
                $("#videoWrapper").fadeIn("slow");             
            }

            $("#videoWrapper").css({
               "position":"relative",
               "top":"0"
            });

            $("#popupVideoTitle").text(this.title);
            var mediahref = $(this).attr('href');

            $("#embed_code").val('<object width="480" height="360" data="http://flowplayer.org/swf/flowplayer-3.1.2.swf?0.3968513055819596" type="application/x-shockwave-flash"><param name="movie" value="http://flowplayer.org/swf/flowplayer-3.1.2.swf?0.3968513055819596" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="flashvars" value=\'config={"clip":{"url":"http://www.unodc.org' + mediahref + '", "scaling":"orig", "autoPlay":false},"canvas":{"backgroundColor":"#000000"}}\' /></object>');

            flashembed("videoWrapper",{
                src:'/misc/flowplayer/FlowPlayerLight.swf',
                width:480,
                height:360
            },
            {
                config: {
                    videoFile:mediahref,
                    initialVolumePercentage:75,
                    autoPlay:true,
                    loop:false,
                    autoRewind:true,
                    initialScale:'orig',
                    useNativeFullScreen:true,
                    showFullScreenButton: true,
                    menuItems: [false, true, true, false, true, false, false]
                }
            });
            popupStatus = 1;
        });

    $(".mediaAudio").click(
        function(){
            var windowWidth = document.documentElement.clientWidth;
            var windowHeight = document.documentElement.clientHeight;
            var popupHeight = $("#popupContent").height();
            var popupWidth = $("#popupContent").width();

            var yOffset = 0;
            if (self.pageYOffset){// all except Explorer
                yOffset = self.pageYOffset;
            }
            else if (document.documentElement && document.documentElement.scrollTop){// Explorer 6 Strict{
                yOffset = document.documentElement.scrollTop;
            }
            else if (document.body){ // all other Explorers
                y = document.body.scrollTop;
            }

            $("#popupContent").css({
                "position": "absolute",
                "top": (windowHeight/2+yOffset)-(popupHeight/2),
                "left": windowWidth/2-popupWidth/2
            });
            //only need force for IE6
            $("#backgroundPopup").css({
                "height": windowHeight
            });

            if(popupStatus==0){
                $("#backgroundPopup").css({
                    "opacity": "0.7"
                });
                $("#backgroundPopup").fadeIn("slow");
                $("#popupContent").fadeIn("slow");
                $("#videoWrapper").fadeIn("slow");             
            }

            $("#popupVideoTitle").text(this.title);
            var mediahref = $(this).attr('href');

            $("#embed_code").val('<object width="480" height="360" data="http://flowplayer.org/swf/flowplayer-3.1.2.swf?0.3968513055819596" type="application/x-shockwave-flash"><param name="movie" value="http://flowplayer.org/swf/flowplayer-3.1.2.swf?0.3968513055819596" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="flashvars" value=\'config={"clip":{"url":"http://www.unodc.org' + mediahref + '", "scaling":"orig"},"canvas":{"backgroundColor":"#000000"}}\' /></object>');

            $f("videoWrapper",{
                src:'/misc/flowplayer/FlowPlayerLight.swf',
                width:480,
                height:30
            },
            {
                    videoFile:mediahref,
                    initialVolumePercentage:75,
                    autoPlay:true,
                    loop:false,
                    autoRewind:true,
                    initialScale:'orig',
                    useNativeFullScreen:true,
                    showFullScreenButton: false,
                    showMenu: false,
                    plugins: {
                        audio: {
                            url: '/misc/flowplayer/flowplayer.audio-3.0.4.swf'
                        }
                    }
            });
            
            $("#popupContent").css({
                "background":"#000000 url('/img/podcast_bigger.jpg') 20px 20px no-repeat"
            });

            $("#videoWrapper").css({
               "position":"absolute",
               "top":"350px"
            });

            popupStatus = 1;
        });

    $("#popupContentClose").click(function(){
        if(popupStatus==1){
            $("#backgroundPopup").fadeOut("slow");
            $("#popupContent").fadeOut("slow");
            $("#videoWrapper").fadeOut("fast");
            popupStatus = 0;
        }
    });
    $("#backgroundPopup").click(function(){
        if(popupStatus==1){
            $("#backgroundPopup").fadeOut("slow");
            $("#popupContent").fadeOut("slow");
            $("#videoWrapper").fadeOut("fast");
            popupStatus = 0;
        }
    });
}
