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,
	toggle	: function(context) {
		
		if($("#ply")){
			 
			try{
				$("#mediaspace:first-child").sendEvent("PLAY",false);
			}catch(err){
			 
			}
		}
		if (!$.jwbox.lightbox) {
				$.jwbox.lightbox = $(".jwbox_hidden", context);
				var video_id = $(".video", context).attr('id');
				if (video_id != '' && video_id != undefined){
					$.get("/calendar/kc_video_dyn.cfm?show_header=false&video_type=" + video_id.substring(0,1) + "&origin_id=" + video_id.substring(2), function(data) {
							$("#" + video_id).html(data);
							$.jwbox.center();
						});
				}
				$.jwbox.center();
				$("#jwbox_background").fadeIn("fast");
				$.jwbox.lightbox.css("display","block")
				$.jwbox.center();
				$("#jwbox_background").fadeTo(0, 0.8);
				
				$("object", context).each(function(){
					$.jwbox.flashplayer = document.getElementById(this.id);
				});
				$.jwbox.realplayer = $(".video", context);

				//$.jwbox.player = $(".video", context);
		} else if ((context.className == 'jwbox_content')) {
		} else {
			try {
				
				
				
				
				$.jwbox.flashplayer.sendEvent("STOP");
				
			} catch (err) {
				try{
				
				
					$.jwbox.realplayer.html("");
				
					
				}catch(err2){	
				
				}
			}
			
			$.jwbox.flashplayer = null;
			$.jwbox.realplayer = null;
			
			$.jwbox.lightbox.css("display","none");
			$.jwbox.lightbox = null;
			$("#jwbox_background").fadeOut("fast");
		}
	},
	center	: function() {
		if ($.jwbox.lightbox) {
			$.jwbox.lightbox.center();
		}
	}
}

$(document).keyup(function(event){
    if (event.keyCode == 27 && $.jwbox.lightbox) {
		$.jwbox.toggle($("#jwbox_background"));
    }
});

$(document).ready(function () {
	//alert(document.getElementById("videobody"));
	$("body").append('<div id="jwbox_background">&nbsp;</div>');
	$(".jwbox").click(function () {$.jwbox.toggle(this); return false;});
	$("#jwbox_background").click(function () {$.jwbox.toggle(this); return false;});
	$(".jwbox_content").click(function () {$.jwbox.toggle(this); return false;});
	$(window).resize(function() {$.jwbox.center();});
});
