jQuery(
    function ($) 
    {
	    $.fn.jqvideobox = function (opt) 
	    {
			return this.each(function()
			{
				//system vars
			    var overlay, center, caption, bottomContainer, so, flash, videoID, other, top;
			
			    var options = $.extend({				        
					initialWidth: 250,		// Initial width of the box (px)
					initialHeight: 250,		// Initial height of the box (px)
					defaultWidth: 425,		// Default width of the box (px)
					defaultHeight: 350,	// Default height of the box (px)
					animateCaption: false,	// Enable/Disable caption animation
					defaultOverLayFade: 0.85,	//Default overlay fade value
					closeReady : true,
					flvplayer: 'swf/flvplayer.swf'
					
				}, opt);
			    
				function init()
		        {
		        	if ($("#lbOverlay").length == 0)
		        	{
			        	var _overlay = $(document.createElement("div")).attr({"id": "lbOverlay"}).click(closeMe);
			        	var _center = $(document.createElement("div")).attr({"id": "lbCenter"}).css({'width': options.initialWidth+'px', 'height': options.initialHeight+'px', 'display': 'none'});
			        	var _bottomContainer = $(document.createElement("div")).attr({"id": "lbBottomContainer"}).css('display', 'none');
			        	var _bottom = $(document.createElement("div")).attr('id', 'lbBottom');
						var _close = $(document.createElement("a")).attr({id: 'lbCloseLink', href: '#'}).click(closeMe);
						var _clear = $(document.createElement("div")).css('clear', 'both');	

						_bottom.append(_close).append(_clear);
						_bottomContainer.append(_bottom);
			        	$("body").append(_overlay).append(_center).append(_bottomContainer);
		        	}
		        	
	        		overlay = $("#lbOverlay");
	        		center = $("#lbCenter");
	        		bottomContainer = $("#lbBottomContainer");

 					activate();
		        }
		        
		        function closeMe()
				{
					if(options.closeReady)
					{
						overlay.css('display','none');
						center.css('display','none');
						bottomContainer.css('display', 'none');
						center.html('');
					
						options.closeReady = false;
						
						overlay.remove();
						center.remove();
						bottomContainer.remove();
						
						onVideoClose();
						
						return false;
					}

				}
				
		        function activate()
		        {			
					setup(opt.video);
					top = 160;
					left= (($(window).width() / 2) - (options.defaultWidth / 2));
					
					center.css({'top': top + 'px', 'left':  left + 'px','display': 'none', 'background': '#FFF', 'height': options.contentsHeight, 'width': options.contentsWidth});					
					overlay.css('top','145px');
					overlay.css('height', '431px')
					overlay.css('display','block');
					overlay.fadeTo('slow', options.defaultOverLayFade);

					center.fadeIn('slow',
						function()
						{
							insert();
						}
					);
					return false;
		        }
		        
		        function insert()
		        {
					center.css('background','#FFF');
					so.write(center.attr("id"));
					bottomContainer.css({'top': (top + center.height() + 10) + "px", 'left': center.css('left'), 'width': options.contentsWidth+'px'});
					if (options.animateCaption)
					{
						bottomContainer.slideDown('slow');
					}
					else
					{
						bottomContainer.css('display','block');
					}
					options.closeReady = true;
		        }
		        
		        function setup(sLinkHref)
		        {

		        	overlay.css({'top': $(window).scrollTop()+'px', 'height': $(window).height()+'px'});
					//	options.contentsWidth = (aDim && (aDim[0] > 0)) ? aDim[0] : options.defaultWidth; 
					//	options.contentsHeight = (aDim && (aDim[1] > 0)) ? aDim[1] : options.defaultHeight;
					
					options.contentsWidth = options.defaultWidth;
					options.contentsHeight = options.defaultHeight;
	
			    	flash = true;
					var hRef = sLinkHref;
					var videoId = hRef.split('=');
					videoID = videoId[1];
					
					so = new SWFObject("http://www.youtube.com/v/"+videoID, "flvvideo", options.contentsWidth, options.contentsHeight, "0");
					so.addParam("wmode", "transparent");
					
				}

			

				//lets start it
		        init();
			});
		}
	}
);
