// Version.Build.Release : 1.1.0
var RWin = Class.create(); 
RWin.prototype = {
	initialize: function() 
	{ 
		this.loaded = false;
		//this.imageDir = "/rsys/js/RWin/images/";
		this.windows = new Object();
		this.configs = new Object();
		this.initcallbacks = new Object();
		this._load = new Array();
		this.windowsOpen = new Object();
		this.autoOpen = new Array();
		this.zIndex = 2000;
		this.currentPage="";
		this.pageWidth=0;
		this.pageHeight=0;
		this.body_overflow = '';
		this.scrolls = {y:0, x:0};
		this.config = {cache:false, agent:"IE", scrollbars:false, overlay:true, theme:"magnawin", themeLoc:'', debug:false}; //theme;RWin
		this.SetAgent();
		//this.Toggles = {selectBoxes:false};
	},
	
	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);
	},

	GetWinSizes: function()
	{
		var pageWidth = 0;
		var pageHeight = 0;
		if( window.innerHeight && window.scrollMaxY ) // Firefox 
		{
			pageWidth = window.innerWidth + window.scrollMaxX;
			pageHeight = window.innerHeight + window.scrollMaxY;
		}
		else if( document.body.scrollHeight > document.body.offsetHeight ) // all but Explorer Mac
		{
			pageWidth = document.body.scrollWidth;
			pageHeight = document.body.scrollHeight;
			pageHeight += 50;
		}
		else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		{ 
			pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
			pageHeight = document.body.offsetHeight + document.body.offsetTop; 
			pageHeight += 50;
		}
		
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number'){
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		
		this.pageWidth = (myWidth>pageWidth)?myWidth:pageWidth;
		this.pageHeight = (myHeight>pageHeight)?myHeight:pageHeight;
		
		//window.alert( 'Width = ' + myWidth + ' & other = ' + pageWidth );
		//window.alert( 'Height = ' + myHeight + ' & other = ' + pageHeight );
	},
	
	// toggle all select boxe visibilities
	ToggleSelectBoxes: function(display)
	{
		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 (this.currentPage != "" && $("rwin_"+this.currentPage+"_content"))
		{
			var sels = $("rwin_"+this.currentPage+"_content").getElementsByTagName("select");
			for (var x=0; x<sels.length; x++) sels[x].style.visibility = "visible";
		}
		this.ToggleObjects(display);
	},
	
	ToggleObjects: function(display)
	{
		if (display == "visible" && this.windowsOpen.length > 0) return false;
		var objs = document.getElementsByTagName("object");
		for (var x=0; x<objs.length; x++) objs[x].style.visibility = display;
		
		
		if (this.currentPage != "" && $("rwin_"+this.currentPage+"_content"))
		{
			var objs = $("rwin_"+this.currentPage+"_content").getElementsByTagName("object");
			for (var x=0; x<objs.length; x++) objs[x].style.visibility = display=='visible'?'hidden':'visible';
		}
	},
	
	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) - (layer.offsetHeight+100))/2); // /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(move)
	{
		var offsetL, offsetT;
		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;};
		this.scrolls.x = offsetL;
		this.scrolls.y = offsetT;
		// this caused the window to not become centered in the view port.
		//if (typeof move == "boolean" && move === true) this.Move(0, offsetT+"px");
	},
	
	Move: function(x,y)
	{
		if (typeof y != "undefined" && y != 0) $('rwin_'+this.currentPage).style.top = y;
		if (typeof x != "undefined" && x != 0) $('rwin_'+this.currentPage).style.left = x;
	},
	
	Overlay: function(page, on)
	{
		//this.GetWinSizes();
		//if (!this.configs[page].overlay) return(false);
		
		if (!$("rsys_overlay_"+page) || on===true)
		{
			this.zIndex++;
			if (!$("rsys_overlay_"+page))
			{
				var div = document.createElement("div");
				div.className = 'rsys_overlay';
				div.id = 'rsys_overlay_'+page;
				//document.body.appendChild(div);
				var bodyID = document.getElementsByTagName("body")[0]; 
				bodyID.appendChild(div);
				//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;
			$("rsys_overlay_"+page).setStyle({'height':this.pageHeight+'px', 'width':this.pageWidth+'px', 'display':'inline'});
		}
		else
		{
			$("rsys_overlay_"+page).setStyle({'display':'none'});
		}
	},
	
	BringToFront: function(page)
	{
		this.zIndex++;
		$("rwin_"+page).style.zIndex = this.zIndex;
	},
	
	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";
	},
	
	// page title, pageid, page content
	SetWindow: function(config)
	{
		if (!this.loaded)
		{
			var len = this._load.length;
			this._load[len] = config;
			return true;
		}

		// Options
		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.overlay == "undefined") config.overlay = true;
		if (!this.config.overlay) config.overlay = false;
		config.overlay = true;
		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";
		else if (typeof config.oklabel == "boolean") config.oklabel = false;
		
		if (typeof config.cancellabel == "undefined") config.cancellabel = "cancel";
		if (typeof config.noclose == "undefined") config.noclose = false;
		if (typeof config.autoopen == "undefined") config.autoopen = false;
		
		var winSty = " style=\""; var cWinSty = "";
		if (typeof config.width != "undefined") winSty += "width: "+config.width+"; "; else winSty += "width: 760px; ";
		if (typeof config.height != "undefined")
		{
			cWinSty = " style=\"height: "+config.height+";\"";
			winSty += "height: "+config.height+"; ";
		}
		else
		{
			if (config.wintype == "window") config.height = "450px"; // default
			else config.height = "100px";
		}
		winSty += "\"";
		
		// content
		var content = "";
		if (config.type == "layer"){
			if ($(config.id)){
				$(config.id).style.display = "none";
				content = $(config.id).innerHTML;
				$(config.id).remove();
			} else content = "Could not find the layer with id: \""+config.id+"\"!";
		} else if (config.type=="iframe") 
		content = "<iframe name=\"rwin_"+config.refid+"_iframe\" id=\"rwin_"+config.refid+"_iframe\" src=\""+config.url+"\" frameborder=0 width=100% height=100%></iframe>";
		else content = config.content;
		
		if (config.type != "iframe" && config.wintype == "window")
		content = "<div style=\"overflow: auto; height: "+config.height+";\">"+content+"</div>";

		// controls
		var controls = "";
		if (config.wintype == "infobox") controls += '<div class="controls"><img src="'+this.config.themeLoc+'Windows/'+this.config.theme+'/load5.gif" alt="Loading..." width="67" height="17" /></div>';
		else if (config.wintype == "alert" || config.wintype == "dialog")
		{
			var noc = 'OpenWindow(\''+config.refid+'\'); '+config.cancelcallback;
			var okc = 'OpenWindow(\''+config.refid+'\'); '+config.callback;
			controls += '<div class="controls">'
				+ '<input type="button" class="ok" id="rwin_'+config.refid+'_okbtn" value=" '+config.oklabel+' " onclick="'+okc+'" />';
				if (config.wintype == "dialog") controls += '&nbsp;&nbsp;&nbsp;&nbsp;'
				+ '<input type="button" class="no" id="rwin_'+config.refid+'_cancelbtn" value=" '+config.cancellabel+' " onclick="'+noc+'" />';
			controls += '</div>';
			if (config.wintype == "alert" && config.oklabel === false) controls = "";
		}

		var div = document.createElement("div");
		div.className = this.config.theme;
		div.id = 'rwin_'+config.refid;
		var bodyID = document.getElementsByTagName("body")[0]; 
		bodyID.appendChild(div);
		
		//var html = ''//'<div class="'+this.config.theme+'" id="rwin_'+config.refid+'"'+winSty+'">'
		var html = '<div class="'+((config.wintype == "window")?"win":"dia")+'">'
				+ '<div class="rwin_head" id="rwin_'+config.refid+'_head">'
					+ '<div class="tl"></div>'
					+ '<div class="tr"></div>';
					var hideClose = (config.noclose || config.wintype != "window")?" style=\"display: none;\"":"";
					if (config.wintype == "alert" && config.oklabel === false) hideClose = "";
					html += '<div class="buttons" id="rwin_'+config.refid+'_closebtn"'+hideClose+'>'
						+ '<a href="javascript:void(0);" onclick="OpenWindow(\''+config.refid+'\');" class="close"><div></div></a>'
					+ '</div>'
					+ '<div class="tm">';
					if (config.wintype == "window") html += '<div class="title">'+config.title+'</div>';
					html += '</div>'
				+ '</div>'
				+ '<div class="rwin_body">'
					+ '<div class="l">'
						+ '<div class="r">'
							+ '<div class="content" id="rwin_'+config.refid+'_content"'+cWinSty+'>'+content+'</div>'
							+ controls
						+ '</div>'
					+ '</div>'
				+ '</div>'
				+ '<div class="rwin_foot">'
					+ '<div class="bl"></div>'
					+ '<div class="br"></div>'
					+ '<div class="bm"></div>'
				+ '</div>'
			+ '</div>';
		//+ '</div>';
		//div.innerHTML = html;

		//$('rwin_'+page).innerHTML = html;
		if ($('rwin_'+config.refid))
		{
			$('rwin_'+config.refid).style.visibility = "hidden";
			//$('rwin_'+config.refid).style.width = "760px";
			//$('rwin_'+config.refid).style.height = "450px";
			$('rwin_'+config.refid).style.width = ( (typeof config.width != "undefined")?config.width+"":"760px" );
			$('rwin_'+config.refid).style.height = ( (typeof config.height != "undefined")?config.height+"":"450px" );
		}
		
		
		
		//delete(content);
			
		if (config.initcallback != "") this.initcallbacks[config.refid] = config.initcallback;
		this.windows[config.refid] = html;
		this.configs[config.refid] = config;
		if (config.autoopen) this.autoOpen[this.autoOpen.length] = config.refid;
		delete(html);
	},
	
	LoadPage: function(page)
	{
		if ($("rwin_"+page) && $("rwin_"+page).innerHTML == '')
		{
			this.zIndex++;
			$('rwin_'+page).innerHTML = this.windows[page];
			//document.body.innerHTML = this.windows[page]+document.body.innerHTML;
			//document.appendChild(this.windows[page]);
			//alert($('bladeeblaa').innerHTML);
			//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), false);
			
			
			if (typeof this.initcallbacks[page] != "undefined") eval(this.initcallbacks[page]);
			return(true);
		}
		else
		{
			// CENTER IT
			this.CenterWindow($('rwin_'+page), false);
			return(true);
		}
	},
	
	UnloadPage: function(page)
	{
		document.body.removeChild($("rwin_"+page));
	},
	
	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)
		{
			if (this.config.scrollbars)
			{
				var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body;
				standardbody.style.overflow = (this.body_overflow=='')?'':this.body_overflow;
			}
			this.Overlay(page);
			this.windowsOpen[page] = false;
			this.ToggleSelectBoxes("visible");
			//if (!this.config.cache) this.UnloadPage(page); else 
			$("rwin_"+page).setStyle({'visibility':'hidden'});
			if (this.configs[page].wintype != "window") window.scrollTo(this.scrolls.x, this.scrolls.y);
			this.currentPage = "";
		}
		else if (!closeIfOpen)
		{
			this.currentPage = page;
			if (this.config.scrollbars)
			{
				var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body;
				//alert(standardbody.style.overflow);
				if (this.body_overflow == '') this.body_overflow = standardbody.style.overflow;
				standardbody.style.overflow = "hidden";
			}
			
			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;
					}
				}
				
				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.Overlay(page, true);
			$("rwin_"+page).setStyle({'visibility':'visible'});
			this.windowsOpen[page] = true;
			if (this.configs[page].wintype == "window") this.SaveScrollCoords(true); else { window.scrollTo(0, 50); this.SaveScrollCoords(); }
			this.ToggleSelectBoxes("hidden");
			this.BringToFront(page);
			//alert($("rwin_"+page).innerHTML);
		}
		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();
	},
	
	Setup: function(config, SkipHash)
	{
		if (typeof config.cache != "undefined") this.config.cache = config.cache;
		if (typeof config.scrollbars != "undefined") this.config.scrollbars = config.scrollbars;
		if (typeof config.overlay != "undefined") this.config.overlay = config.overlay;
		if (typeof config.theme != "undefined") this.config.theme = config.theme;
		if (typeof config.themeLoc != "undefined") this.config.themeLoc = config.themeLoc;
		if (typeof config.debug != "undefined") this.config.debug = config.debug;
		this.GetWinSizes();
		
		// Attach theme
		var headID = document.getElementsByTagName("head")[0];         
		var cssNode = document.createElement('link');
		cssNode.type = 'text/css';
		cssNode.rel = 'stylesheet';
		cssNode.href = this.config.themeLoc+this.config.theme+'.css';
		cssNode.media = 'screen';
		headID.appendChild(cssNode);
		
		// update the windows that were set when the page was no loaded.
		this.loaded = true;
		if (this._load.length > 0)
		{
			for (var i=0; i<this._load.length; i++) this.SetWindow(this._load[i]);
			//delete(this._load);
		}
		
		
		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);
			}
		}
		
		/* deprecated
		if (hash != "")
		{
			hash = hash.substr(1);
			if (typeof this.windows[hash] != "undefined") this.OpenWindow(hash);
		}
		*/
		
		if (this.autoOpen.length > 0)
		{
			for (var t=0; t<this.autoOpen.length; t++) this.OpenWindow(this.autoOpen[t]);
		}
	}
}
function OpenWindow (page, config){ R.OpenWindow(page, config); }
function SetWindow (config){ R.SetWindow(config); }
function SetContent (page, content){ R.SetContent(page, content); }


function handleError(one, two, three) {
	alert("An error has occured:\n"+one+"\nDomain:"+two+"\nLine Number:"+three);
	return true;
}
//window.onerror = handleError;