
	/* 
		window toolbag - pike*kw.nl 2005
		
		
	*/
	
	var Windows =  {
		childWindows 	: null,
		open			: WDSOpen,
		fix				: WDSFix,
		set				: WDSSet,
		max				: WDSMax,
		parseOptions	: WDSParseOptions,
		getDocDim		: WDSGetDocDim,
		getScreenDim	: WDSGetScreenDim
		
	}
	
	/*
		use 
			<a href="hier.html" target="_blank"
				onclick="return !(Windows.open('hier.html','prikbord'))">hier</a>
		will follow the link if for some reason the window does not open
		like, if you dont have javascript
		
	*/
	
	function WDSOpen(url,name,options) {
	
		// some (named) windows always have the same options,
		if (name=="extern") {
			if (!options) {
				options = "toolbar=yes,location=yes,directories=no,menubar=yes";
				options += ",status=yes,scrollbars=yes,resizable=yes";
				options += ",top=20,left=20,width=600,height=400";
				// explorer opts
				options += ",channelmode=no,fullscreen=no,titlebar=yes";
				// mozilla opts
				options += ",copyhistory=no";
			}
		}
		if (name=="formpop") {
			if (!options) {
				options = "toolbar=no,location=no,directories=no,menubar=no";
				options += ",status=yes,scrollbars=yes,resizable=yes";
				options += ",top=100,left=100,width=400,height=500";
				// explorer opts
				options += ",channelmode=no,fullscreen=no,titlebar=yes";
				// mozilla opts
				options += ",copyhistory=no";
			}
		}
		
		
		// other windows use defaults
		if (!options) {
			options = "toolbar=yes,location=yes,directories=no,menubar=yes";
			options += ",status=yes,scrollbars=yes,resizable=yes";
			options += ",top=100,left=100,width=640,height=480";
			// explorer opts
			options += ",channelmode=no,fullscreen=no,titlebar=yes"; 
			// mozilla opts
			options += ",copyhistory=no";
		}	
		
		// remember the window and focus it
		if (!this.childWindows) this.childWindows = new Array();
		this.childWindows[name] = self.open(url, name, options); 
		// ie4 bug
		if (this.childWindows[name] ) {
			if (!this.childWindows[name].focus) this.childWindows[name].focus = false;
			if (this.childWindows[name].focus) this.childWindows[name].focus();
		}
		return this.childWindows[name];
	}

	function WDSFix(options) {
		
		// this will only apply that part of the options
		// that the browser supports. for ie, thats not much
		
		var optarr = this.parseOptions(options);
		
		if (window.scrollbars && optarr["scrollbars"])		window.scrollbars.visible	= optarr["scrollbars"];
		if (window.personalbar && optarr["personalbar"])	window.personalbar.visible	= optarr["personalbar"];
		if (window.menubar && optarr["visible"])			window.menubar.visible		= optarr["visible"];
		if (window.locationbar && optarr["locationbar"])	window.locationbar.visible	= optarr["locationbar"];
		if (window.statusbar && optarr["statusbar"])		window.statusbar.visible	= optarr["statusbar"];
		if (window.toolbar && optarr["toolbar"])			window.toolbar.visible		= optarr["toolbar"];
		if (optarr["left"] || optarr["top"]) {
			if (window.moveTo)		window.moveTo(optarr["left"],optarr["top"]);
		}
		if (optarr["width"] && optarr["height"]) {
			if (window.resizeTo)	window.resizeTo(optarr["width"],optarr["height"]);
		}

	}
	
	function WDSFixx(options) {
		
		// for ie, this will open a new window with these
		// options and close the current one. this is the ugly
		// popup trick that ruined the web 5 years ago
		
		if (window.toolbar) {
			// moz
			this.fix(options);
		} else {
			// ie
			if(document.location.search.indexOf('fixWindow=done')==-1) {
				var newurl = document.location.href;
				var sep = (document.location.search=="")?"?":"&";
				newurl += sep+"fixWindow=done";
				var newwin = self.open(newurl,self.name+"fixed",options);
				newwin.opener = self.opener;
				self.close();  
			}
		}
	}
	
	//moz & ie >5
	function WDSSet(whandle,w,h,x,y) {
		if (!whandle) whandle = top;
		if (whandle.moveTo && whandle.resizeTo) {
			whandle.resizeTo(w,h);
			whandle.moveTo(x,y);
		}
	}
	
	// moz & ie > 5
	function WDSMax(whandle) {
		if (!whandle) whandle = top;
		if (whandle.moveTo && whandle.resizeTo && screen.availWidth && screen.availHeight) {
			whandle.moveTo(-4,-4);
			whandle.resizeTo(screen.availWidth+8,screen.availHeight+8);
		}
	}
	
	function WDSParseOptions(options) {
		// splits an options string in an array
		var optarr = new Array();
		var nexttok = 0;
		while (options.length) {
			nexttok = options.indexOf(",");
			if (nexttok==-1) nexttok = options.length;
			var elm = options.substring(0,nexttok);
			options = options.substring(elm.length+1,options.length);
			var key = elm.substring(0,elm.indexOf("="));
			var val = elm.substring(elm.indexOf("=")+1,elm.length);
			optarr[key]=(val=="yes"||val =="1");
			if (!optarr[key]) optarr[key]=(val=="no"||val=="0")?false:val;
		}
		//alert(key+":"+optarr[key]);
		return optarr;		
	}

	// http://www.evolt.org/article/document_body_doctype_switching_and_more
	
	function WDSGetDocDim (doc) {

		var body,w,h,roundw,roundh;
		
		if (doc.parent && doc.parent.innerWidth) {
		
			w = doc.parent.innerWidth;
			h = doc.parent.innerheight;
			
		} else {
		
			// moz & ie compat mode
			if (doc.documentElement)  body =	doc.documentElement;
			
			// ie standard
			if (doc.body)  body =	doc.body;
			
			if (body) {
				w = body.clientWidth;
				h = body.clientHeight
			} 
		}
		
		// moz bug in frameset
		if (w==0 || h==0) {
			w = screen.availWidth;
			h = screen.availHeight;
		}
		// whatever
		if (w==0 || h==0) {
			w = screen.width;
			h = screen.height;
		}
		// ok ok .. 
		if (w==0 || h==0) {
			w = 1400;
			h = 1200;
		}
		
		roundw = Math.floor(w/100+1)*100;
		roundh = Math.floor(h/100+1)*100; 
		roundw = Math.min(roundw,1400);
		roundh = Math.min(roundh,1200);
		debug("doc "+w+":"+h+"="+roundw+":"+roundh);

		return { "width" : roundw, "height" : roundh };

	}
	
	function WDSGetScreenDim () {
		var w = screen.width;
		var h = screen.height;
		var roundw = Math.floor(w/100+1)*100;
		var roundh = Math.floor(h/100+1)*100; 
		roundw = Math.min(roundw,1400);
		roundh = Math.min(roundh,1200);
		debug("screen "+w+":"+h+"="+roundw+":"+roundh);
		return { "width" : roundw, "height" : roundh };
	}

	




