// eScreen, The BRS Elemental Screen Overlay (3.21.11)
// Copyright Blue Ridge Solutions, Inc.  www.blueridges.com
//
// INSTRUCTIONS
// Default setup: $('.eScreen').eScreen({html:'<html>'}); // pass in html code
// Default setup: $('.eScreen').eScreen({load:'page.php #content'});
// Default setup: $('.eScreen').eScreen({id:'thehtmlid'});

(function($){
	
	function closePopup() {
		var settings = $('#overlay_container').data('settings');
		
		$('.overlay').hide(); 
		if(settings.id != '') {
			$('#overlay_content').find('input,textarea,select').each(function(){ var val = $(this).val(); this.setAttribute("value",val); }); // Update innerHTML for Mozilla
			// copy the radio buttons for mozilla
			$('#overlay_content').find('input[type=radio]:checked').each(function(){ 
				var n = $(this).attr('name'); 
				var v = $(this).val(); 
				$('#overlay_content').find('input[name='+n+']').each(function(){ 
					if($(this).attr('value') == v) {
						this.setAttribute('checked','checked'); 
						$(this).addClass('checked');
					} else  {
						$(this).removeAttr('checked'); 
						$(this).removeClass('checked'); 
					}
				});
			}); // Update innerHTML for Mozilla					
			// copy the checkboxes for mozilla
			$('#overlay_content').find('input[type=checkbox]').each(function(){ 
				if(this.checked) {
					this.setAttribute('checked','checked'); 
					$(this).addClass('checked');
				} else  {
					$(this).removeAttr('checked'); 
					$(this).removeClass('checked'); 
				}
			}); // Update innerHTML for Mozilla					
			var content = $('#overlay_content').html();  
			$('#overlay_content').empty(); 
			$('#'+settings.id).html(content);
		}
		
		else if (settings.element) {
			// replace the element
			settings.element_placeholder.after(settings.element).remove();
		}
		
		$('#overlay_content').html('Loading...');  
		$('.close_overlay').unbind('click');
		if(settings.onclose) settings.onclose();
		
		$('#overlay_container').data('settings', 0);
	}
	
	$.eScreen_close = closePopup;
	
	$.fn.eScreen = function(options){
		
		var settings = {
			html:'Content unavailable.',
			load:'',
			id:'',
			width:600,
			height:400,
			overlay_bg:'black',
			content_bg:'',
			opacity:0.7,
			closeBtn:'<a href="#" class="overlay close_overlay" style="position:absolute; top:-20px; right:0px; cursor:pointer; text-transform:uppercase; color:#fff; font-weight:bold; display:block; padding:2px; font:11px/20px "Arial Black", Gadget, sans-serif;">close</a>',
			onclose:null,
			onload:'centerOverlay()'
		};
		
		return this.each(function(e){
			var $element = $(this);
			
			// Override
			if (options) $.extend(settings, options);
			
			// Setup
			if(!$('#overlay').length) {
				$('head').append('<style></style>');
				$('body').append('<div class="overlay close_overlay" style="display:none; background:'+settings.overlay_bg+';" id="overlay"></div>');
				$('body').append('<div class="overlay" style="display:none; background:'+settings.content_bg+';" id="overlay_container">'+settings.closeBtn+'<div id="overlay_content">Loading...</div></div>');
				$('#overlay').css({opacity:settings.opacity,position:'fixed',top:0,left:0,'z-index':1000,width:'100%',height:'100%'});
			}
			
			// Load
			if(settings.html != 'Content unavailable.') { $('#overlay_content').html(settings.html); }
			else if(settings.load != '') { 
				$('#overlay_content').css({height:settings.height,width:settings.width}).load(settings.load, settings.onload).append('<div style="clear:both;"></div>'); 
				$('#overlay_content:first-child').css({float:'none'}); 
			}
			else if(settings.id != '')  { 
				$('#'+settings.id).find('input,textarea,select').each(function(){ var val = $(this).val(); this.setAttribute("value",val); }); // Update innerHTML for Mozilla
				var content = $('#'+settings.id).html(); 
				$('#'+settings.id).empty(); 
				$('#overlay_content').html(content); 
			}
			else {
				if (!settings.element) settings.element = $element;
				// Get the element
				var $el = $(settings.element);
				// Add a placeholder
				settings.element_placeholder = $('<div/>').css('display', 'none').insertAfter($el);
				$('#overlay_content').empty().append($el);
			}
			
			// Center
			var height = $('#overlay_container').outerHeight() / 2;
			var width = $('#overlay_container').outerWidth() / 2;
			$('#overlay_container').css({position:'fixed',left:'50%',top:'50%',zIndex:'1001',marginTop:'-'+height+'px',marginLeft:'-'+width+'px'});
			
			// Adjust
			if($.browser.msie && $.browser.version < 7) { 
				var height = $(window).height();
				var width = $(window).width();
				$('#overlay').css({position:'absolute',width:width+'px',height:height+'px'});
				$('#overlay_container').css({position:'absolute'});
				$('html,body').animate({scrollTop:$('body').offset().top},500);
			}
			
			// Show
			$('.overlay').show(); 
			
			// Close
			$('.close_overlay').click(function(e){ 
				e.preventDefault();
				$.eScreen_close();
			});
			
			// Save reference to overlay
			$('#overlay_container').data('settings', settings);

		}); 
	}; // eScreen Plugin
})(jQuery);


