// JavaScript Document
(function($){		
	$.fn.iFrameLightwindow = function(options) {	  
		// Set/Default Vars & Defaults for thos Vars
		var defaults = {
			iFrameSrc			: '',
			iFrameHeightVars	: '',
			iFrameWidthVars		: '',
			borderSize			: 0,
			borderColor			: '#777',
			iFrameOn			: true,
			afterLoad			: function(){},
			beforeClosed		: function(){},
			afterClosed			: function(){}
		};
		
		var options = $.extend(defaults, options);
		
		$(this).click(function(){
			if($('#lightwindowWrapper').length > 0){
				//Stop duplicated... asp.net is some how calling this function multiple times...
			}else{
				var addImageHTML = ''+
					'<div id="lightwindowWrapper">'+
						'<div id="iFrameWrapper">'+
							'<iframe id="iFrameObj" frameborder="0">'+
							'</iframe >'+
						'</div>'+
						'<div id="lightwindowBg"><div>'+
					'</div>'+
				'';
				
				$(addImageHTML).prependTo('body');
				
				$('#lightwindowBg').css({
					'position'			: 'fixed',
					'top'				: '0px',
					'left'				: '0px',
					'height'			: '100%',
					'width'				: '100%',
					'background-color'	: '#000',
					'opacity'			: '0.8',
					'filter'			: 'alpha(opacity=80)',
					'z-index'			: '990'
				});
				
				$('#lightwindowWrapper').css({
					'position'			: 'fixed',
					'top'				: '0px',
					'left'				: '0px',
					'height'			: '100%',
					'width'				: '100%',
					'z-index'			: '999'
				});
				
				var windowWidth = $(window).width();
				var windowHeight = $(window).height();
				var iFrameWidth = 0;
				var iFrameHeight = 0;
				
				var percRegEx = /^(([0-9]+%)+)$/; 
				var pxRegEx = /^(([0-9]+px)+)$/; 
				
				//Determine iFrame Width
				if(options.iFrameWidthVars == '' || options.iFrameWidthVars == null || options.iFrameWidthVars == 0){
					iFrameWidth = '90%';
				}else{
					if(percRegEx.test(options.iFrameWidthVars)){
						iFrameWidth = options.iFrameWidthVars;
					}else if(pxRegEx.test(options.iFrameWidthVars)){
						iFrameWidth = options.iFrameWidthVars;
					}else{
						iFrameWidth = (windowWidth - (options.iFrameWidthVars * 2) - (options.borderSize * 2))+'px';
					}
				}
				//Determine iFrame Height
				if(options.iFrameHeightVars == '' || options.iFrameHeightVars == null || options.iFrameHeightVars == 0){
					iFrameHeight = '90%';
				}else{
					if(percRegEx.test(options.iFrameHeightVars)){
						iFrameHeight = options.iFrameHeightVars;
					}else if(pxRegEx.test(options.iFrameHeightVars)){
						iFrameHeight = options.iFrameHeightVars;
					}else{
						iFrameHeight = (windowHeight - (options.iFrameHeightVars * 2) - (options.borderSize * 2))+'px';
					}
				}
				
				var posTop = 0;
				var trimIFrameHeight = eval(iFrameHeight.replace(/(px)/g, ''));
				
				if(trimIFrameHeight >= windowHeight){
					posTop = ((trimIFrameHeight + (options.borderSize * 2)) / 2)+'px';
				}else{
					posTop = '50%'
				}
				
				$('#iFrameWrapper').css({
					'position'			: 'relative',
					'top'				: ''+posTop+'',
					'height'			: ''+iFrameHeight+'',
					'width'				: ''+iFrameWidth+'',
					//'background-image'	: 'url(\'/images/onlineDesigner/loading.gif\')',
					'background'		: '#FFF no-repeat fixed center',
					'margin'			: '0 auto',
					'border'			: options.borderSize+'px '+options.borderColor+' solid',
					'z-index'			: '995',
				});
				//Get the height of the iFrameWrapper divide by 2
				var iFrameWrapperHeight = (trimIFrameHeight + (options.borderSize * 2)) / 2;
				//Position its negative margin so that it centers
				$('#iFrameWrapper').css({
					'display'		: 'none'
				});
				//Animate to reveal iFrame
				$('#iFrameWrapper').animate({
					marginTop	: '-'+iFrameWrapperHeight+'px',
					height		: 'show',
					width		: 'show'
				}, 700, function(){
					$('#iFrameObj').css({
						'height'			: '100%',
						'width'				: '100%'
					});
					
					//Trigger the afterLoad callback
					options.afterLoad.call(this);
				});
				
				var closeBtnHTML = '<a href="javascript:void(0)"><img id="iFrameCloseBTN" onclick="$.fn.RemoveLW();" src="/images/close.png" /></a>'
				$(closeBtnHTML).prependTo($('#iFrameWrapper'));
				$('#iFrameCloseBTN').css({
					'position'		: 'absolute',
					'top'			: -13 - options.borderSize+'px',
					'left'			: eval((eval(iFrameWidth.replace('px', '')) + options.borderSize) - 21)+'px',
					'z-index'		: '999'
				});
				
				$.fn.RemoveLW = function(){
					options.beforeClosed.call(this);
					$('#lightwindowWrapper').remove();
					options.afterClosed.call(this);
				};
				
				if(options.iFrameOn == true){
					$('#iFrameObj').attr('src', $(this).attr('iframe_src'));
				}else{
					$('#iFrameObj').remove();
					
					var htmlContent = $(this).attr('content');
					var htmlContentCSS = $(htmlContent).attr('style');
					htmlContent = ''+
						'<div id="iFrameContent">'+
							''+$(htmlContent).html()+''+
						'</div>'+
					'';
					
					$(htmlContent).prependTo('#iFrameWrapper');
					$('#iFrameContent').attr('style', htmlContentCSS);
					$('#iFrameContent').css({
						'height'		: ''+iFrameHeight+'',
						'width'			: ''+iFrameWidth+'',
						'display'		: 'block',
						'overflow-y'	: 'scroll',
						'overflow-x'	: 'hidden'
					});
				}
				
				//Remove lightwindow
				$('#lightwindowWrapper').dblclick(function(){
					options.beforeClosed.call(this); 
					$('#lightwindowWrapper').remove();
					options.afterClosed.call(this);
				});
			};
		});
	};  
})(jQuery);
