/* CWB video
 * Simplified Version
 *
 * @author Gerson Goulart
 */
(function($) {
	var methods = {
		init : function(settings) {
			var videoFrame = '<iframe frameborder="0" scrolling="no" src="/video/videojwuk.asp" style="width:640px;height:360px;"></iframe><a href="#" class="close_video" style="right:-9px;top:-9px;position:absolute;"><img src="http://www.castingworkbook.com/images/close.png" alt="Close" /></a>';
			$('#video')/*.css({'width':'919px','height':'533px'})*/.html(videoFrame);
			// Render template
			methods.paint();
			methods.open();
			// If close button is clicked
			$('.close_video').click(function(){ methods.close(); });
		},
		paint : function(resize) {
			var paintvideo = function(){
				$('#mask').css({'height':0});
				// Get the window height and width
				var winH = $(window).height();
				var winW = $(window).width();
				var docH = $(document).height();
				// Set the hight and width to mask to fill up the whole screen and Transition effect
				$('#mask').css({'height':docH});
				//*/ Set the popup window to center and Transition effect
				$('#video').css('top',winH/2-$('#video').outerHeight(true)/1.5);
				$('#video').css('left',winW/2-$('#video').outerWidth(true)/2);
			}
			if(resize) {
				var isOpen = $('#video').is(':visible');
				if(isOpen) paintvideo();
			}
			else {
				paintvideo();
			}
		},
		open : function() {
			var isOpen = $('#video').is(':visible');
			if(!isOpen) $('#mask').fadeIn();//.fadeTo("medium",0.6);
			$('#video').fadeIn();
		},
		close : function() {
			$('#video').fadeOut('slow');
			$('#mask').fadeOut('slow', function(){
				$(this).css({'height':0});
			});
		}
	}
	$.video = function(options) {
		// video's default settings
		var settings = {
			tmpl : null,
			data : {},
			load : false
		};
		// If options exist, merge them with default settings and init video
		if(options) {
			$.extend( settings, options );
		}
		methods.init(settings);
	}
	// Expose methods
	$.video.paint = function(){ methods.paint(); }
	$.video.close = function(){ methods.close(); }
	// Inject necessary HTML in the page
	$(document).ready(function(){
		$('body').append('<div id="video" style="width:640px;height:360px;position:fixed;z-index:120;background:#000;z-index:9999;display:none;"></div><div id="mask" style="position:absolute;top:0;right:0;height:0;left:0;z-index:999;background-color:#000;"></div>');
		$(window).resize(function(){ methods.paint(true); });
		var videohash = window.location.hash;
		if(videohash == '#/video') $.video();
	});
})(jQuery);
