// Version.Build.Release : 0.1.0
var RWin = Class.create(); 
RWin.prototype = {
	initialize: function() 
	{ 
		this.loaded = false;
		this.imageDir = "http://www.discountmore.com/js/RWin/images/";
		this.windows = new Object();
		this.initcallbacks = new Object();
		this._load = new Array();
		this.window_refids = new Array(); 
		this.windowsOpen = new Object();
		this.zIndex = 2000;
		this.pageWidth=0;
		this.pageHeight=0;
		this.scrolls = {y:0, x:0};
		this.config = {cache:false, agent:"IE", noScrollBars:false};
		this.SetAgent();
	},
	
	getZindex: function(tag, elm)
	{
		if (this.zIndex > 0) return(this.zIndex++);
	
		if (tag.match(",")) tag = tag.split(",");
		else tag = [tag];
	
		if (typeof(elm) == "undefined") var mylist = document.body;
		else var mylist = $("canvas");
		
		var zindex = 0;
		for(var i=0; i<tag.length; i++)
		{
			var tagName = tag[i];
			var listitems = mylist.getElementsByTagName(tagName);
			
			if (listitems.length == 0) continue;
			for (i=0; i<listitems.length; i++)
			{
				if (listitems[i].style.zIndex > zindex) zindex = Number(listitems[i].style.zIndex);
			}
		}
		this.zIndex = Number(zindex+1);
		return(this.zIndex);
	},
	
	// toggle all select boxe visibilities
	ToggleSelectBoxes: function(display, page)
	{
		if (display == "visible" && this.windowsOpen.length > 0) return false;
		var sels = document.getElementsByTagName("select");
		for (var x=0; x<sels.length; x++) sels[x].style.visibility = display;
		if (!$("rwin_"+page+"_content")) return;
		var sels = $("rwin_"+page+"_content").getElementsByTagName("select");
		for (var x=0; x<sels.length; x++) sels[x].style.visibility = "visible";
	},
	
	GetWinSizes: function()
	{
		if( window.innerHeight && window.scrollMaxY ) // Firefox 
		{
			this.pageWidth = window.innerWidth + window.scrollMaxX;
			this.pageHeight = window.innerHeight + window.scrollMaxY;
		}
		else if((document.body.scrollHeight && document.body.offsetHeight) && (document.body.scrollHeight > document.body.offsetHeight)) // all but Explorer Mac
		{
			this.pageWidth = document.body.scrollWidth;
			this.pageHeight = document.body.scrollHeight+50;
		}
		else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		{ 
			this.pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
			this.pageHeight = document.body.offsetHeight + document.body.offsetTop; 
		}
	},
	
	// page title, pageid, page content
	SetWindow: function(config)
	{
		if (!this.loaded)
		{
			this._load[this._load.length] = config;
			return true;
		}
		
		/* PAGE
		<div class="page-con" id="rwin_glossary">
			<div class="page-wrap">
				<div class="page">
					<div class="close">
						<div class="close-button"><a href="#" onclick="OpenWindow('glossary');"><img src="images/close.png" border="0" /></a></div>
						<div class="title">REAL ESTATE GLOSSARY</div>
					</div>
					<div class="page-content">
						<p>The real estate glossary is listed here.</p>
					</div>
				</div>
			</div>
		</div>
		*/
		
		if (typeof config.type == "undefined")
		{
			if (typeof config.url != "undefined") config.type = "iframe";
			else if (typeof config.id != "undefined") config.type = "layer";
			else config.type = "text";
		}
		
		if (typeof config.title == "undefined") config.title = "";
		if (typeof config.callback == "undefined") config.callback = "";
		if (typeof config.cancelcallback == "undefined") config.cancelcallback = "";
		if (typeof config.initcallback == "undefined") config.initcallback = "";
		if (typeof config.wintype == "undefined") config.wintype = "window";
		if (typeof config.oklabel == "undefined") config.oklabel = "Okay";
		if (typeof config.cancellabel == "undefined") config.cancellabel = "Cancel";
		if (typeof config.noclose == "undefined") config.noclose = false;
		
		var html = posStyle = "";
		if (this.config.agent == "IE")
		{
			//posStyle = "left: 25%;"
		}
		
		var winSty = " style=\"";
		if (typeof config.width != "undefined") winSty += "width: "+config.width+"; "; else winSty += "width: 760px; ";
		if (typeof config.height != "undefined") winSty += "height: "+config.height+"; ";
		winSty += "\"";
		
		// Header
		if (config.wintype == "window")
		{
			html += '<div class="win-con" id="rwin_'+config.refid+'"'+winSty+'><div class="win-wrap"'+winSty+'><div class="win"><div class="close">';
			var cs = (config.noclose)?" style=\"display: none;\"":"";
			html += '<div class="close-button" id="rwin_'+config.refid+'_closebtn"'+cs+'><a href="#" onclick="OpenWindow(\''+config.refid+'\');">'
			+ '<img src="'+this.imageDir+'close.png" border="0" /></a></div>';
			html += '<div class="title">'+config.title+'</div></div><div class="win-content" id="rwin_'+config.refid+'_content">';
		}
		else if (config.wintype == "alert" || config.wintype == "dialog" || config.wintype == "infobox")
		{
			html += '<div class="alert-con" id="rwin_'+config.refid+'"'+winSty+'><div class="alert-wrap"'+winSty+'><div class="alert">';
			if (config.title != "") html += '<div class="title">'+config.title+'</div>';
			html += '<div class="alert-content" id="rwin_'+config.refid+'_content">';
		}
		
		// Content
		if (config.type == "layer"){
			if ($(config.id)){
				$(config.id).style.display = "none";
				html += $(config.id).innerHTML;
				$(config.id).remove();
			} else html += "Could not find the layer with id: \""+config.id+"\"!";
		} 
		else if (config.type=="iframe") 
		html += "<iframe name=\"rwin_"+config.refid+"_iframe\" id=\"rwin_"+config.refid+"_iframe\" src=\""+config.url+"\" frameborder=0 width=100% height=100%></iframe>";
		else html += config.content;
		html += "</div>";
		
		// Footer
		if (config.wintype == "alert" || config.wintype == "dialog") 
		{
			html += '<div class="alert-controls">';
			html += '<input type="button" id="rwin_'+config.refid+'_okbtn" style="font-weight:bold;" onclick="OpenWindow(\''+config.refid+'\'); '+config.callback+'" value="'+config.oklabel+'">  ';
			if (config.wintype == "dialog") 
			html += '<input type="button" id="rwin_'+config.refid+'_cancelbtn" style="font-style: italic;" onclick="OpenWindow(\''+config.refid+'\'); '+config.cancelcallback+'" value="'+config.cancellabel+'">';
			html += '</div>';
		}
		
		if (config.wintype == "infobox") 
		{
			html += '<div class="alert-controls" id="rwin_'+config.refid+'_controls">';
			html += '<img src="'+this.imageDir+'load-ball.gif">';
			html += '</div>';
		}
		
		html += '</div></div></div>';
		if (config.initcallback != "") this.initcallbacks[config.refid] = config.initcallback;
		this.windows[config.refid] = html;
		this.window_refids[this.window_refids.length] = config.refid;
		delete(html);
	},
	
	AnyOpen: function()
	{
		var bool = false;
		for (var i=0; i<this.window_refids.length; i++)
		{
			if (this.windowsOpen[this.window_refids[i]])
			{
				bool = true;
				break;
			}
		}
		return(bool);
	},
	
	OpenWindow: function(page, config)
	{
		
		if (!this.windows[page] && typeof config != "undefined")
		{
			config.refid = page;
			this.SetWindow(config);
		}
		
		if (!this.loaded) return false;
		
		var closeIfOpen = false;
		if (typeof config != "undefined")
		{
			if (typeof config.close != "undefined")
			{
				if (config.close) closeIfOpen = true;
			}
		}
		
		if (this.windowsOpen[page]==true)
		{
			window.scrollTo(this.scrolls.x,this.scrolls.y);
			
			if (this.config.noScrollBars && !this.AnyOpen())
			{
				var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body;
				standardbody.style.overflow = "auto";
			}
			
			$("rsys_overlay_"+page).setStyle({'display':'none'});
			$("rwin_"+page).setStyle({'visibility':'hidden'});
			this.windowsOpen[page] = false;
			if (!this.config.cache) this.UnloadPage(page);
			this.ToggleSelectBoxes("visible", page);
		}
		else if (!closeIfOpen)
		{
			if (this.config.noScrollBars)
			{
				var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body;
				standardbody.style.overflow = "hidden";
			}
			
			if (!$("rwin_"+page)) this.LoadPage(page);
			
			// config allows alteration after the window is created.
			if (typeof config != "undefined")
			{
				if (typeof config.params != "undefined")
				{
					if ($("rwin_"+page+"_iframe"))
					{
						var url = $("rwin_"+page+"_iframe").src;
						url += config.params;
						$("rwin_"+page+"_iframe").src = url;
					}
				}
				
				if (typeof config.content != "undefined")
				{
					if ($("rwin_"+page+"_content"))
					{
						$("rwin_"+page+"_content").innerHTML = config.content;
					}
				}
				
				if (typeof config.oklabel != "undefined")
				{
					if ($("rwin_"+page+"_okbtn"))
					{
						$("rwin_"+page+"_okbtn").value = config.oklabel;
					}
				}
				
				if (typeof config.cancellabel != "undefined")
				{
					if ($("rwin_"+page+"_cancelbtn"))
					{
						$("rwin_"+page+"_cancelbtn").value = config.cancellabel;
					}
				}
				
				/* NOT WORKING
				if (typeof config.callback != "undefined")
				{
					if ($("rwin_"+page+"_okbtn"))
					{
						$("rwin_"+page+"_okbtn").onClick = 'OpenWindow(\''+page+'\'); '+config.okcallback;
					}
				}
				
				if (typeof config.cancelcallback != "undefined")
				{
					if ($("rwin_"+page+"_cancelbtn"))
					{
						$("rwin_"+page+"_cancelbtn").onClick = 'OpenWindow(\''+page+'\'); '+config.cancelcallback;
					}
				}
				*/
				if ($('rwin_'+page+'_closebtn'))
				{
					if (typeof config.noclose != "undefined")
					{
						if (config.noclose) $('rwin_'+page+'_closebtn').style.display = "none";
						else $('rwin_'+page+'_closebtn').style.display = "inline";
					}
					else
					{
						$('rwin_'+page+'_closebtn').style.display = "inline";
					}
				}
			}
			this.SaveScrollCoords();
			this.SetOverlay(page);
			$("rsys_overlay_"+page).setStyle({'display':'inline'});
			$("rwin_"+page).setStyle({'visibility':'visible'});
			this.windowsOpen[page] = true;
			
			window.scrollTo(0,50);
			this.ToggleSelectBoxes("hidden", page);
		}
		return(true);
	},
	
	SetContent: function(page, content)
	{
		if (!$("rwin_"+page)) return(false);
		if ($("rwin_"+page+"_content"))
		{
			$("rwin_"+page+"_content").innerHTML = content;
		}
	},
	
	ToggleControls: function(page)
	{
		$("rwin_"+page+"_controls").toggle();
	},
	
	CenterWindow: function(layer, doNotAddOffsets)
	{
		if(typeof layer=="string"){layer=document.getElementById(layer);};
		if(layer){
			var parent=layer.parentNode;/*unless body tag, must have position to relative or absolute*/
			parent.style.overflow="auto";
			layer.style.position="absolute";/*much better if top and left are specified in style, with 'px'*/
			layer.style.top=layer.style.top||layer.offsetTop+'px';
			layer.style.left=layer.style.left||layer.offsetLeft+'px';
			var clientH=0, clientW=0, offsetT=0, offsetL=0, top=0, left=0;
			if(parent && parent.nodeType==1/*a tag*/){
				if(parent.nodeName=="BODY"){
					if(typeof window.innerHeight!="undefined"){clientH=window.innerHeight; clientW=window.innerWidth;}
					else if(document.documentElement && document.documentElement.clientHeight){clientH=document.documentElement.clientHeight; clientW=document.documentElement.clientWidth;}
					else if(document.body.clientHeight){clientH=document.body.clientHeight; clientW=document.body.clientWidth;}
					else{clientH=parent.clientHeight; clientW=parent.clientWidth;};
					//
					if(typeof pageYOffset!="undefined"){offsetT=pageYOffset; offsetL=pageXOffset;}
					else if(document.documentElement && document.documentElement.scrollTop){offsetT=document.documentElement.scrollTop; offsetL=document.documentElement.scrollLeft;}
					else if(document.body && typeof document.body.scrollTop!="undefined"){offsetT=document.body.scrollTop; offsetL=document.body.scrollLeft;}
					else{offsetT=0; offsetL=0;};
					top=Math.abs(parent.offsetTop + ((clientH/2) - (layer.offsetHeight/2)));
					left=Math.abs(parent.offsetLeft + ((clientW/2) - (layer.offsetWidth/2)));
				}
				else
				{
					clientH=parent.offsetHeight; clientW=parent.offsetWidth;
					offsetT=parent.scrollTop; offsetL=parent.scrollLeft;
					top=Math.abs(((clientH/2) - (layer.offsetHeight/2))); left=Math.abs(((clientW/2) - (layer.offsetWidth/2)));
				};
				if(!doNotAddOffsets){top+=offsetT; left+=offsetL;};
				layer.style.top=top+'px';//comment out to avoid positioning and allow returning only
				layer.style.left=left+'px';//comment out to avoid positioning and allow returning only
				return [top, left, top+'px', left+'px'];
			};
		};
	},
	
	SaveScrollCoords: function()
	{
		this.scrolls.x = (document.all)?document.body.scrollLeft:window.pageXOffset; 
  	this.scrolls.y = (document.all)?document.body.scrollTop:window.pageYOffset; 
	},
	
	LoadPage: function(page)
	{
		this.SetOverlay(page);
		this.zIndex++;
		if (this.config.agent == "IE") document.body.innerHTML += this.windows[page];
		else document.body.insert({bottom: this.windows[page]});
		
		$('rwin_'+page).style.zIndex = this.zIndex;
		$('rwin_'+page).style.visibility = "hidden";
		
		// CENTER IT
		this.CenterWindow($('rwin_'+page), true);
		
		if (typeof this.initcallbacks[page] != "undefined") eval(this.initcallbacks[page]);
	},
	
	UnloadPage: function(page)
	{
		document.body.removeChild($("rwin_"+page));
	},
	
	SetOverlay: function(page)
	{
		this.GetWinSizes();
		if (!$("rsys_overlay_"+page))
		{
			this.zIndex++;
			if (this.config.agent != "IE") document.body.insert({bottom: "<div class=\"rsys_overlay\" id=\"rsys_overlay_"+page+"\"></div>"})
			else document.body.innerHTML += "<div class=\"rsys_overlay\" id=\"rsys_overlay_"+page+"\"></div>";
			$("rsys_overlay_"+page).style.zIndex = this.zIndex;
		}
		if (this.config.agent != "IE") this.pageHeight += 100;
		$("rsys_overlay_"+page).setStyle({'height':this.pageHeight+'px', 'width':this.pageWidth+'px'});
	},
	
	SetAgent: function()
	{
		if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) this.config.agent = "FF";
		else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) this.config.agent = "IE";
		else if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) this.config.agent = "MZ";
	},
	
	Setup: function(config, SkipHash)
	{
		// update the windows that were set when the page was no loaded.
		this.loaded = true;
		for (var i=0; i<this._load.length; i++) this.SetWindow(this._load[i]);
		delete(this._load);
		
		if (typeof config.cache != "undefined") this.config.cache = config.cache;
		if (typeof config.noScrollBars != "undefined") this.config.noScrollBars = config.noScrollBars;
		
		var hash = window.location.hash;
		if (hash != "")
		{
			var loadHash = true;
			if (typeof config.SkipHash != "undefined")
			{
				config.SkipHash = config.SkipHash.split(",");
				for(var i=0; i<config.SkipHash.length; i++)
				{
					if (hash == config.SkipHash[i])
					{
						loadHash = false;
						break;
					}
				}
			}
			if (loadHash)
			{
				hash = hash.substr(1);
				if (typeof this.windows[hash] != "undefined") this.OpenWindow(hash);
			}
		}
	}
}

function handleError(one, two, three) {
	alert("An error has occured:\n"+one+"\nDomain:"+two+"\nLine Number:"+three);
	return true;
}
//window.onerror = handleError;
