// plugin definition
jQuery.fn.flashBox = function(options) {
	var defaults = {
		width: 550,
		height: 400,
		title: 'Dummy title'
	  };
	
	var opts = jQuery.extend(defaults, options);
	
	var containerWidth = Number(opts.width) + 20;
	var containerHeight = Number(opts.height) + 100;
	
	var jQueryMatchedObj = this;

	_initialize();

	function _initialize(){
		
		_createFlashBoxStructure();
		
	}
		
	function _createFlashBoxStructure(){
		
		var structure = "<div id='flashbox_overlay'></div>";
				structure += "<div id='flashbox_container'>";
					structure += "<div id='flashbox_content'>";
						if(opts.title)
							structure += "<div id='flashbox_title'><h3 class='float-left'>"+opts.title+"</h3></div>";
					structure += "</div'>";
					structure += "<div id='flashbox_swf_container' class='clear'>";
						structure += "<div id='flashbox_swf'>";
							structure += "<p>&nbsp;</p>";
						structure += "</div>";
						structure += "<div>";
						if(globalRootPath == ''){	
							structure += "<div id='flashbox_close' class='float-right'><a href='#'><img src="+ globalRootPath +"js/lightbox/images/lightbox-btn-close.gif /></a></div>";
						}else{
							structure += "<div id='flashbox_close' class='float-right'><a href='#'><img src="+ globalRootPath +"/js/lightbox/images/lightbox-btn-close.gif /></a></div>";
						}
						structure += "</div>";
					structure += "</div>";
			structure += "</div>";
			
		jQuery('body').append(structure);
		
		_setupOverlay();
	}
	
	function _setupOverlay(){
		
		// Calculate current width & height
		var arrPageMeasures = _getPageSize();
		var arrScrollMeasures = _getPageScroll();
				
		var cssOverlayObj = {
			position: 'absolute',
			top: arrScrollMeasures[1] +'px',
			left: '0',
			width: arrPageMeasures[0] +'px',
			height: arrPageMeasures[3] +'px',
			backgroundColor: '#000000',
			opacity: 0,
			overflow: 'hidden',
			zIndex:50
		};
		
		
		var cssFlashboxContainerObj = {
			display: 'none',
			position: 'absolute',
			width: containerWidth +'px',
			height: containerHeight +'px',
			top: arrScrollMeasures[1] + (arrPageMeasures[3] / 10) +'px',
			left: (arrPageMeasures[0] / 2) - (containerWidth / 2) +'px',
			backgroundColor:'#ffffff',
			opacity: 0,
			zIndex: 55
		};
		
		// If window was resized, calculate the new overlay dimensions
		$(window).resize(function() {
			// Get page sizes
			var arrPageMeasures = _getPageSize();
			// Style overlay and show it
			$('#flashbox_overlay').css({
				width: arrPageMeasures[0] +'px',
				height: arrPageMeasures[3] +'px'				
			});
			// Get page scroll
			var arrScrollMeasures = _getPageScroll();
			// Calculate top and left offset for the jquery-lightbox div object and show it
			$('#flashbox_container').css({
				top: arrScrollMeasures[1] + (arrPageMeasures[3] / 10) +'px',
				left: (arrPageMeasures[0] / 2) - (containerWidth / 2) +'px'
			});
		});
		
		// If window scrolls
		$(window).scroll(function () { 
		
			var arrScrollMeasures = _getPageScroll();
			$('#flashbox_overlay').css({
				top: arrScrollMeasures[1]+'px'
			});
			
			$('#flashbox_container').css({
				top: arrScrollMeasures[1] + (arrPageMeasures[3] / 10) +'px',
				left: (arrPageMeasures[0] / 2) - (containerWidth / 2) +'px'
			});
		})
		
		
		jQuery('#flashbox_swf_container').css({width:opts.width+'px', height:opts.height+'px'});
		jQuery('#flashbox_container').css(cssFlashboxContainerObj);
		jQuery('#flashbox_overlay').css(cssOverlayObj).fadeTo(500,.6, _showFlashBox);
		
		jQuery('#flashbox_title h3').css("color", "#3191B9");
				
	};
	
	function _showFlashBox(){
		jQuery('#flashbox_container').css("display", "block").fadeTo(200, 1, _initialiseFlashBox);
	}
	
	function _initialiseFlashBox(){
		jQuery('#flashbox_title').css({
			margin: '5px 25px'
		});
		
		jQuery('#flashbox_swf_container').css({
			margin: '5px 10px 10px'
		});
						
		var att = { data:flashboxFLVPlayerPath, width:opts.width, height:(Number(opts.height) + 40), loadPath:opts.loadPath };
        var par = { flashvars:'videoPath='+ opts.loadPath +'', allowscriptaccess:'always'};
        var id = "flashbox_swf";
        var myObject = swfobject.createSWF(att, par, id);
		
		jQuery('#flashbox_close a').click(_closeFlashBox);

	}
	
	function _closeFlashBox(){
		
		jQuery('#flashbox_container').fadeTo(200, 0, _fadeOutContainer);
		
		return false;
	}
	
	function _fadeOutContainer(){
		jQuery('#flashbox_overlay').fadeTo(500,0, _deleteFlashBox);
	}
	
	function _deleteFlashBox(){
			jQuery('#flashbox_overlay').remove();
			jQuery('#flashbox_container').remove();
	}
	
	function _getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
		
	function _getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		};
		

	return this;
};