
var jsModalPopup = {
	
	modal: null,
	popup: null,
	secondPopup: null,
	addedCSS: [],
	parentCSS: [],

	init: function(el,args) {
		
		if(args[1] == "open") {
            el.removeEvents("click");
            eval("el.addEvent('click', function(e) { eval('jsModalPopup.open(false,\""+el.title+"\");')})");
			// stop the default link action
			el.onclick = function() { return false; }
			el.title = "";
		}
				
		if(args[1] == "close") {
			el.addEvent('click',jsModalPopup.close);
		}
	},

	open: function() {
		jsModalPopup.modal = new Modalizer();
		// handle old style (false,url) parameters
		if(arguments.length > 1) {
			url = arguments[1];
		} else {
			url = arguments[0];
		}

		var ajaxOptions = {
			method: "get",
			onComplete: function(txt) {
				// grab just the <body> content for the popup
				var matches = txt.match(/<body([^>]*)>([\w\W\s\S\n\r]*)<\/body>/i);
				var bodyAttrs = matches[1]; // need this to handle some css issues
				var bodyText = matches[2];
				var bodyClass = "";
				if(bodyAttrs.length > 0) {
					bodyClass = bodyAttrs.match(/class=\"([^"]*)\"/i)[1];
				}
				
				// we might need some additonal css
				// Firefox loads the css in the link tags
				// but IE and possibly other browser's don't
				// so, let's load any missing css needed by the modal content
				
			/*	
				$$('link').each(function(el,i) {
					jsModalPopup.parentCSS.push(el.getAttribute("title",1));
				});


				var linkTags = txt.match(/<link([^>]*)\/>/gi);
				for(var l=0; l<linkTags.length; l++) {
					var href = linkTags[l].match(/href=\"([^"]*)\"/i);
					if(jsModalPopup.parentCSS.indexOf(href[1]) < 0) {
						// we need to inject this into the document if not done so already
						if(jsModalPopup.addedCSS.indexOf(href[1]) < 0) {
							jsModalPopup.addedCSS.push(href[1]);
							var link = new Element("link");
							link.setProperty("rel","stylesheet");
							link.setProperty("type","text/css");
							link.setProperty("href",href[1]);
							
							if(!document.location.href.contains("selectCatAmd") && !document.location.href.contains("allocateCatGrp")) {

								$$("head").adopt(link);
							}
						}
					}
				}
				// send the body content to the screen after all css is loaded

				// get the original script tags
				// eval anything inside the body of the script tags
				var scripts = txt.match(/<script([^>]*)>([\w\W\s\S\n\r]*?)<\/script>/gi);
				for(var i=0; i< scripts.length; i++) {
					var scriptContent = scripts[i].replace(/<\/?script([^>])*>/gi,"");
					if(scriptContent.length > 0) {
						eval(scriptContent);
					}
				}
                */
				jsModalPopup.display(bodyText,bodyClass);
			}
		}
		// load the content for the popup
		new Ajax(url,ajaxOptions).request();
	},
	
	display: function(content,bodyClass) {
		
		if(!$("DynamicModalPopup")) {
			var winWidth = (window.ie) ? (window.getScrollWidth()-20): window.getScrollWidth();
			var winHeight = window.getScrollHeight() + "px";
			jsModalPopup.modal.modalShow(
				{	hideOnClick:false,
					modalStyle:
					{	'width':winWidth+'px',
						'height':winHeight,
						'z-index':'2000'
					}
				});
			// hide select elements in IE only
			
			if(window.ie) {
                popIFrame = new Element('iframe');
				popIFrame.setProperty("id","DynamicModalPopupIFrame");
				popIFrame.setStyles(
					{	"z-index":"1000",
						"position":"absolute",
						"top":"0",
						"left":"0",
						"border":"5",
						"filter":"alpha(opacity=0)",
						"width": winWidth+'px',
						"height": winHeight
					});
                $(document.body).adopt(popIFrame);
            }
			
			var popDiv = new Element("div");
			popDiv.setProperty("id","DynamicModalPopup");

			var popDivContent = new Element("div");
			popDivContent.innerHTML = content;
			popDivContent.setStyle("position","relative");
			popDivContent.addClass(bodyClass);
			popDiv.adopt(popDivContent);
			$(document.body).adopt(popDiv);

			popDiv.setStyle("z-index","5000");
			popDiv.setStyle("position","absolute");
			popDiv.setStyle("top",(window.getScrollTop()+80)+"px");
			popDiv.setStyle("left",Math.floor(winWidth/2)-200+"px");
			popDiv.show();

			// initialize the new content
			Unobtrusive.init(popDiv);
			popDiv.getElements(".jsModalPopup_close").each(function(el) {
					el.nullifyLink();
					el.removeEvents("click").addEvent("click",function() { 
						jsModalPopup.close(); 
					});
			});

			popDiv.focus();

		
		} else {
			// hide the first popup behind the iframe
			$("DynamicModalPopup").hide();
			$("DynamicModalPopup").setStyle("z-index","99"); // put it behind the iframe
			$("DynamicModalPopup").show();
			var spopDiv = new Element("div");
			spopDiv.setProperty("id","DynamicSecondaryModalPopup");
			
			var spopDivContent = new Element("div");
			spopDivContent.innerHTML = content;
			spopDivContent.setStyle("position","relative");
			spopDivContent.addClass(bodyClass);
			spopDiv.adopt(spopDivContent);
			$(document.body).adopt(spopDiv);

			spopDiv.setStyle("z-index","5000");
			spopDiv.setStyle("position","absolute");
			spopDiv.setStyle("top",(window.getScrollTop()+100)+"px");
			spopDiv.setStyle("left","30px");
			spopDiv.show();	

			// initialize the new content
			Unobtrusive.init(spopDiv);
			spopDiv.getElements(".jsModalPopup_close").each(function(el) {
					el.nullifyLink();
					el.removeEvents("click").addEvent("click",function() { 
						jsModalPopup.close(); 
					});
			});

			
		}
	},

	close: function() {
		if($("DynamicSecondaryModalPopup")) {
			$("DynamicSecondaryModalPopup").remove();
			// IE blanks out the content after we bring it back
			// so, we're going to hide it before bringing it on top of the iframe
			// and then show it again so it re-paints the screen
			$("DynamicModalPopup").hide();
			$("DynamicModalPopup").setStyle("z-index","5000");
			$("DynamicModalPopup").show();
		} else {
			try {
				$("modalOverlay").remove();
				$("DynamicModalPopup").remove();
				if(window.ie) { $("DynamicModalPopupIFrame").remove(); }
			} catch(err) {}
		}
		
	},

	refresh: function() {
		if($("DynamicSecondaryModalPopup")) {
			$("DynamicSecondaryModalPopup").hide();
			$("DynamicSecondaryModalPopup").show()
		} else if($("DynamicModalPopup")) {
			$("DynamicModalPopup").hide();
			$("DynamicModalPopup").show();
		}
	}
}


