/**
 * Creates a modal overlay that can take any content passed to it
 * Floated content needs css class 'clearfix' to preserve background
 * @param content
 * @param config
 * config:
 *    headerText
 *    closeCancel: none/close(default)/cancel
 *    callback
 * @constructor
 */
com.ag.ModalOverlay = function(content, config) {

	var parentObj = this;
	this.config = {};
	this.content = {};
	/* Allows callback to be modified */
	com.ag.ModalOverlay.callback = this.config.callback;
	if(config) this.config = config;
	if(!content) {
		throw new Error('com.ag.ModalOverlay requires content');
		return;
	} else {
		this.content = $(content);
	}
	this.fogger = '';
	if ($.support.opacity) {
		this.fogger = '<div id="modalOverlayFogger" style="opacity:0.7;">&#160;</div>';
	} else {
		this.fogger = '<div id="modalOverlayFogger" style="filter:alpha(opacity=70);zoom:1;">&#160;</div>';
	}

	this.shell = $('<div id="modalOverlay" style="visibility:hidden;">'+
	               '   <div id="modalOverlayTop">'+
	               '      <div id="modalOverlayTopLeft"></div>'+
	               '      <div id="modalOverlayTopCenter">'+
	               '         <h1>&#160;</h1>'+
	               '         <div class="closeContainer"><a href="#" class="closeCancel"></a></div>'+
	               '         <div class="lower"></div>'+
	               '      </div>'+
	               '      <div id="modalOverlayTopRight"><div class="corner"></div></div>'+
	               '   </div>'+
					'	 <div id="modalOverlayMiddle">'+
					'		<div id="modalOverlayMiddleOuter">'+
					'			<div id="modalOverlayMiddleInner">'+
					'				<div id="modalOverlayMiddleCenter"></div>'+
					'			</div>'+
					'		</div>'+
	               	'   </div>'+
	               '   <div id="modalOverlayBottom">'+
	               '      <div id="modalOverlayBottomLeft"><div class="corner"></div></div>'+
	               '      <div id="modalOverlayBottomCenter"></div>'+
	               '      <div id="modalOverlayBottomRight"><div class="corner"></div></div>'+
	               '   </div>'+
	               '</div>');

	if(this.config.headerText) $(this.shell).find('#modalOverlayTopCenter h1').text(this.config.headerText);
	$(this.shell).find('#modalOverlayMiddleCenter').append(content);
	$(this.shell).find('#modalOverlayTopCenter a.closeCancel').click(function(event){
		parentObj.close();
		event.preventDefault();
	});
	switch(this.config.closeCancel) {
		case 'none':
			$(this.shell).find('#modalOverlayTopCenter a.closeCancel').remove(); break;
		case 'cancel':
			$(this.shell).find('#modalOverlayTopCenter a.closeCancel').text('cancel'); break;
		default:
			$(this.shell).find('#modalOverlayTopCenter a.closeCancel').text('close');
	}
	/**
	 * Show the overlay
	 * @param timeout Time in milliseconds before the overlay closes itself
	 */
	this.show = function(timeout) {
		var parentObject = this;
		$('body').append(this.shell);

		//Check for optional dimension config, set heights and widths (min 280px height, 580px width
		var contentHeight;
		if(this.config.height) {
				contentHeight  = this.config.height;
		} else {
			contentHeight = this.content.outerHeight(true);
		}
		if (contentHeight < 200) contentHeight = 200;

		//$('#modalOverlayMiddleLeft, #modalOverlayMiddleRight').css({'height':contentHeight+20});

		var contentWidth;
		if(this.config.width) {
			contentWidth  = this.config.width;
		} else {
			contentWidth = this.content.outerWidth(true);
		}
		if (contentWidth < 400) contentWidth = 400;
		$('#modalOverlayTopCenter, #modalOverlayBottomCenter').css({'width':contentWidth+20});
		$('#modalOverlayMiddleOuter').css({'width':contentWidth+60});

		//position the overlay
		var left = ($('body').width() - $('#modalOverlay').width())/2;
		var SCROLL_HEIGHT =  window.pageYOffset || document.documentElement.scrollTop;
		var top = SCROLL_HEIGHT + 100;

		$('#modalOverlay').css({'left':left, 'top':top});
		hideFlashObjects();
		$('body').append(this.fogger);
		$('#modalOverlay').css({'visibility':'visible'});
		if(timeout && typeof timeout == 'number') setTimeout(function(){parentObject.close();},timeout);
	};
	/**
	 * Closes the overlay & calls the callback (if present)
	 * Optional timeout before callback executes
	 */
	this.close = function(timeout) {
		$('#modalOverlay').empty().remove();
		$('#modalOverlayFogger').remove();
		showFlashObjects();
		if (com.ag.ModalOverlay.callback && typeof(com.ag.ModalOverlay.callback) == 'function') {
			if(timeout && typeof timeout == 'number') {
				setTimeout(function(){com.ag.ModalOverlay.callback();},timeout)
			}else{
				com.ag.ModalOverlay.callback();
			}

		}
	};
};
