/*
bam.popModule version 1.5
Requires jQuery for DOM operations.

Aleksandar Kolundzija
*/

({
					 
	popModule: (function(){
		
		var _overlayPreviouslyDisplayed = false;
		var _fadeOutTimeoutId;
		var _cacheIdx = null;
		
		var _fixIE6iFrame = function(){
			// IE6 doesn't always display the iframe page.  re-assigning it fixes that
			if ((/MSIE (6)/.test(navigator.userAgent) && navigator.platform == "Win32" && $("#popModule iframe").length)){ 
				var src = $("#popModule iframe").attr("src");
				$("#popModule iframe").attr("src", src);
			}
		};
			
		
		var _moduleFrame = new bam.string.StringBuffer(); 
               _moduleFrame.append("<div id=\"popModule\">") 
                    .append("<table class=\"module_inner\" cellpadding=\"0\">") 
                    .append("<tr><td class=\"module_tl corner\"></td><td class=\"module_t\"></td><td class=\"module_tr corner\"></td></tr>") 
                    .append("<tr>") 
                         .append("<td class=\"module_l\">&nbsp;</td>") 
                         .append("<td id=\"module_main\">") 
                              .append("<div id=\"module_close\"><a href=\"javascript:void(0)\">CLOSE</a></div>") 
                              .append("<div id=\"module_content\"></div>") 
                         .append("</td>") 
                         .append("<td class=\"module_r\">&nbsp;</td>") 
                    .append("</tr>") 
                    .append("<tr><td class=\"module_bl corner\"></td><td class=\"module_b\"></td><td class=\"module_br corner\"></td></tr>") 
                    .append("</table>") 
               .append("</div>");
		
		var _self = {
			preExit  : null,
			postExit : null,
			preShow  : null,
			postShow : null,
			
			init: function(customProps){
				var props = {
					css         : "/shared/css/bam/bam.popModule.css",
					overlayCss  : "/shared/css/bam/bam.overlay.css"
				};
				$.extend(props, customProps);
				bam.loadCSS(props.css);
				if ($("#popModule").length){ // if popModule is already in DOM
					return;
				}
				else {
					bam.loadSync("/shared/scripts/bam/bam.overlay.js");					
					bam.overlay.init({css:props.overlayCss});					
					$(_moduleFrame.toString()).appendTo("body");
					$("#module_close a").click(bam.popModule.exit);
				}
			},
		
			exit: function(customProps){
				var module_content = $("#module_content");
				var props = {
					preExit  : _self.preExit  || function(){},
					postExit : _self.postExit || function(){}
				};
				$.extend(props, customProps);
				props.preExit();
				clearTimeout(_fadeOutTimeoutId);
				$("body").unbind("keydown");
				$("#popModule:visible").hide();
				if (!!_cacheIdx) {
					module_content.children().appendTo(_cacheIdx);
					_cacheIdx = null;
				}
				module_content.empty();
				if (!_overlayPreviouslyDisplayed){
					bam.overlay.hide();
				}
				props.postExit();
			},
			
			fadeOut: function(fadeParam){
				$("#popModule").fadeOut(fadeParam, function(){ _self.exit(); });
			},
			
			fadeOutAfterPause: function(pauseTime, fadeSpeed){
				var fadeOut = function(){
					$("#popModule").fadeOut(fadeSpeed, function(){ _self.exit(); });
				};
				_fadeOutTimeoutId = setTimeout(fadeOut, pauseTime);
			},

			loadAndShow: function(customProps){
				var props = {
					url          : "",
					forceRefresh : false,
					showElement  : null
				};
				$.extend(props, customProps);

				/////
				// returns HTML for requested URL. Looks for DOM cached version of HTML
				// first (stored in <div src="[URL]"). If cached version is not found,
				// or refresh is forced, refetch data
				//
				function __getHtml(__url,__forceRefresh,__showElement){
					var __id = __url.replace("http","").replace(/[:\/.-?&=]/gi,"");
					var __htmlCache =  $("div#"+__id);

					// if data is not cached, create the <div> for caching and fetch content from the server
					if (__htmlCache.length==0){					
						$("body").append('<div style="display:none;" id="'+__id+'"></div>');
						__htmlCache    = $("div#"+__id);
						__forceRefresh = true;
					}
					
					// if force refresh, re-fetch content
					if (!!__forceRefresh) {
						$.ajax({
							url      : __url,
							async    : false,
							dataType : "html",
							success  : function(data){
								__htmlCache.html(data);
							}
						});
					}

					if (!!__showElement) {
						return __htmlCache;
					}
					else {
						return __htmlCache.html();
					}
				}
				
				////
				// if URL gets passed to load(), pass the HTML content from the URL to show()
				//
				if (props.url!=""){
					if (!!props.showElement){
						_cacheIdx      = __getHtml(props.url, props.forceRefresh, props.showElement);
						_self.setWidth(_cacheIdx.width());
						_self.show({
							element: _cacheIdx
						});
					}
					else {
						_self.show({
							htmlContent: __getHtml(props.url, props.forceRefresh)
						});
					}
				}
			},
		
			show: function(customProps){
				var props = {
					htmlContent    : "",
					element        : null,
					overlayOpacity : 0.7,
					preShow        : _self.preShow  || function(){},
					postShow       : _self.postShow || function(){}
				};
				$.extend(props, customProps);
				
				props.preShow();
				
				if (!bam.overlay.isDisplayed()){
					bam.overlay.show({
						opacity  : props.overlayOpacity,
						callback : function(){ 
							$("#popModule").show();
							_fixIE6iFrame();
						}
					});
					_overlayPreviouslyDisplayed = false;
				}
				else {
					$("#popModule").show();
					_fixIE6iFrame();
					_overlayPreviouslyDisplayed = true;
				}
				
				if (typeof props.width != "undefined") $("#popModule #module_content").width(props.width+"px");
				if (!!props.element && typeof props.element =="object"){
					$("#module_content").empty().append(props.element.children());
				}
				else {
					$("#module_content").empty().html(props.htmlContent);
				}
				$("body").keydown(function(e){ 
					if (e.keyCode==27){ 
						e.preventDefault(); 
						_self.exit();
					}
				});
				
				props.postShow();
			},
			
			setWidth: function(w){
				$("#module_content").css("width",w+"px");
			}
		};
						
		return _self;	
	
	})()
	
})