google.load("swfobject", "2.2");
loadjscssfile("/css/jwplayer/jwplayer.css", "css"); ////dynamically load and add this .css file
google.load("jquery", "1.3.2");
google.setOnLoadCallback(OnLoad);
var player;
function playerReady(obj) {
	//alert('the videoplayer '+obj['id']+' has been instantiated');
	player = document.getElementById(obj['id']);
        $.jwbox.player = player;
        //console.log($.jwbox.player);
        $.jwbox.center();
        $.jwbox.player.sendEvent("PLAY");
}



function OnLoad()
{
    $(document).ready(function () {
            // .png overlay
            $("img.play").closest("a").css({"position":"relative","display":"block"}).prepend("<span style='background-image:url(\"/css/img/play/play.png\");display:block;position:absolute;overflow:visible;height:150px;width:200px;z-index:50;'></span>");



            // zwevende box toevoegen aan pagina
            $("body").append('<div id="jwbox_background">&nbsp;</div>');
            $("body").prepend('<div id="jwbox_hidden">&nbsp;</div>');
            $("#jwbox_hidden").append('<div id="jwbox_content">&nbsp;</div>');
            $("#jwbox_content").append('<div id="player">&nbsp;</div>');
            $("#jwbox_content").append('<div id="form">&nbsp;</div>');
            $("#jwbox_content").prepend('<div class="kopyo">&nbsp;</div>');

            // actie bij klikken
            $("#jwbox_background,#jwbox_hidden,#jwbox_content,a.playerlink")
                .click(function () {$.jwbox.toggle($(this)); return false;});
            $("#form")
                .click(function (e) {
                    e.stopPropagation();
                    $.jwbox.toggle($(this));});

            //bij resizen centreren
            $(window).resize(function() {$.jwbox.center();});
    });


    jQuery.fn.center = function () {
            this.css("position","fixed");
            this.css("top", ( $(window).height() - this.outerHeight() ) / 2 + "px");
            this.css("left", ( $(window).width() - this.outerWidth() ) / 2 + "px");
        return this;
    };

    jQuery.jwbox = {
            lightbox	: null,
            player	: null,
            form        : null,
            toggle	: function(context) {
                    //console.log(context.attr('id'));
                    if (!$.jwbox.lightbox) {
                                    //var waarde = context.attr("id");
                                    loadflv(context.closest("div").attr("id"),context.find("img").attr("src"));
                                    $.jwbox.form = loadform(context.closest("div").attr("id"));
                                    //var flvfilm = context.attr("href");
                                    $.jwbox.lightbox = $("#jwbox_hidden");
                                    $("#jwbox_content div.kopyo").html('<h2 class="kopyo">'+context.siblings("h2").text()+'</h2>');
                                    $.jwbox.center();
                                    $("#jwbox_background").fadeIn("fast");
                                    $.jwbox.lightbox.css("display","block");
                                    $("#jwbox_background").fadeTo(0, 0.8);

                    } else if ((context.attr('id') == 'jwbox_content')) {
                        $('input#input').focus();
                    } else if ((context.attr('id') == 'form')) {
                        //alert('jaja');
                    } else {
                            try {
                                    $.jwbox.player.sendEvent("STOP");
                                    $.jwbox.form = null;
                                    //$.jwbox.player = null;
                            } catch (err) {
                            }
                            //console.log($.jwbox.player);
                            //$.jwbox.player.sendEvent("STOP");
                            //$.jwbox.player.sendEvent("LOAD",'');
                            $.jwbox.lightbox.css("display","none");
                            $.jwbox.form = null;
                            $.jwbox.lightbox = null;
                            $("#jwbox_background").fadeOut("fast");
                    }
            },
            center	: function() {
                    if ($.jwbox.lightbox) {
                            $.jwbox.lightbox.center();
                    }
            }
    };

    var keylen = 4;

    var keycodes = {
        0: true,
        8: true,
        13: true,
        37: true,
        39: true,
        46: true
    };


    $(document).keypress(function(e){
        if ($.jwbox.lightbox){
            if (e.keyCode == 27) {
                    $.jwbox.toggle($("#jwbox_background"));

            }
            else{
                var key = 0;
                var isCtrl = false;

                if (window.event) {
                    key = e.keyCode;
                    isCtrl = window.event.ctrlKey;

                } else if(e.which) {
                    key = e.which;
                    isCtrl = e.ctrlKey;
                }

                if (isNaN(key)){
                    return true;
                }
                // check for backspace or delete, or if Ctrl was pressed
                if (isCtrl || keycodes[key]){
                    return true;
                }

                keychar = String.fromCharCode(key);
                var reg = new RegExp("[0-9]");
                return (reg.test(keychar));
                //return false;
            }
        }
    });
}

function loadflv(kruisID,imgsrc){
    $.post(
        "/ajax/ajax/filmurl/",
        { kruisIDee: kruisID },
        function(data){
          swfobject.embedSWF(
                "/flash/jwplayer/player-licensed.swf",
                "player",
                "450",
                "300",
                "8.0.0",
                0,
                {file: data.flv,autostart: "false",controlbar:"none",image:imgsrc,stretching:"exactfit"},      //flashvars
                {allowscriptaccess:"always",menu:"false",wmode:"opaque"}, //attributes
                {id:"player",name:"player"}
          );
        },
        "json"
    );
}

function loadform(kruisID){
  $.post("/ajax/ajax/dialform/", { kruisIDee: kruisID },
  function(formdata){
    $("#form").html(formdata);
    $('input#input').focus();
  });
}

function loadjscssfile(filename, filetype){
 var fileref;
 if (filetype=="js"){ //if filename is a external JavaScript file
  fileref=document.createElement('script');
  fileref.setAttribute("type","text/javascript");
  fileref.setAttribute("src", filename);
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  fileref=document.createElement("link");
  fileref.setAttribute("rel", "stylesheet");
  fileref.setAttribute("type", "text/css");
  fileref.setAttribute("href", filename);
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref);
}

//loadjscssfile("myscript.js", "js") //dynamically load and add this .js file
//loadjscssfile("javascript.php", "js") //dynamically load "javascript.php" as a JavaScript file





