/*
 WLib Javascript Library
 Auhtor: Matthias.Wiede@web.de
 
 WSystem
 
*/

WSystem = {

	isIE : navigator.appVersion.indexOf("MSIE") != -1,
	ieVersion : (navigator.appVersion.indexOf("MSIE") != -1) ? parseFloat(navigator.appVersion.split("MSIE")[1]) : 1000,
	fSystemContextMenu : true,
	fIgnoreDocMouseDown : false,
	menuList : Array (),

	getStyle : function (o, attr) {
		var ret = 0;
		if (o.currentStyle) {
			var tokens = attr.split("-");
			var newName = '';
			for (var i=0;i<tokens.length;i++) {
				var f = tokens[i].charAt(0).toUpperCase();
				if (i>0)
					newName+=(f + tokens[i].substr(1));
				else
					newName+=tokens[i];
			}
			ret = o.currentStyle[newName];		
		}
		else 
		if (window.getComputedStyle) {
			ret = document.defaultView.getComputedStyle(o,null).getPropertyValue(attr);			
		}
		return ret;
	},
	
	getStyleInt : function (o, attr) {
		var num = parseInt (this.getStyle (o, attr));
		if (isNaN (num))
			return 0;
		return num;
	},
	
  getBorderWidth : function (obj, side) {
		if (obj.currentStyle["border" + side + "Style"] == "none")
			return 0;
		var n = parseInt(obj.currentStyle["border" + side + "Width"]);
		return n || 0;
	},

	addEvent : function (o, type, fn) {
		if (o.addEventListener) {
			o.addEventListener( type, fn, false );
		} 
		else 
		if (o.attachEvent) {
			o["e"+type+fn] = fn;
			o[type+fn] = function() { o["e"+type+fn]( window.event ); }
			o.attachEvent( "on"+type, o[type+fn] );
		}
	},

	removeEvent : function (o, type, fn) {
		if (o.removeEventListener) {
			o.removeEventListener( type, fn, false );
		} else if (o.detachEvent) {
			o.detachEvent( "on"+type, o[type+fn] );
			o[type+fn] = null;
			o["e"+type+fn] = null;
		}
	},

	setClass : function (a,o,c1,c2) {
		switch (a){
			case 'swap':
				o.className=!this.setClass('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
				break;
			case 'add':
				if(!this.setClass('check',o,c1)){o.className+=o.className?' '+c1:c1;}
				break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
				break;
			case 'check':
				return new RegExp('\\b'+c1+'\\b').test(o.className)
				break;
		}
	},

	setOpacity : function (o, opacity) {
		if (navigator.appName.indexOf("Netscape")!=-1 && parseInt(navigator.appVersion)>=5) {
	   	o.style.MozOpacity=opacity/100; // Mozilla, Netscape
			o.style.KhtmlOpacity=opacity/100; // Safari, Chrome, KHTML
		}
		else 
		if (this.isIE)
	    o.style.filter="alpha(opacity="+opacity+")"; // IExpl.
		else
			o.style.opacity=opacity/100; // Opera, other
	},

	getMouseCoords : function (evt) {
		if (this.isIE) {
			return {x: evt.clientX, y: evt.clientY};
		}
		return {x: evt.pageX, y: evt.pageY};
	},

	getAbsoluteCoords : function (o) {
		var coords = {x: 0, y: 0 };
		while (o) {
			coords.x += o.offsetLeft;
			coords.y += o.offsetTop;
			o = o.offsetParent;
		};
		return coords;
	},
	
	getScrollPos : function () {
	  var x = 0, y = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    // Netscape compliant
	    x = window.pageXOffset;
	    y = window.pageYOffset;
	  } 
		else 
		if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    // DOM compliant
			x = document.body.scrollLeft;
	    y = document.body.scrollTop;
	  } 
		else 
		if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    // IE6 standards compliant mode
	    x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
	  }
	  return { x: x, y: y };
	},
	
	getWindowDim : function () {
		// Get size of window with scrollbars
		var wndWidth  = 0;
		var wndHeight = 0;

	 	if( typeof(window.innerWidth) == 'number') {
		  // Non-IE
		  wndWidth  = window.innerWidth;
		  wndHeight = window.innerHeight;
		} 
		else 
		if( document.documentElement && 
			 (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		  // IE 6+ in 'standards compliant mode'
		  wndWidth = document.documentElement.clientWidth;
		  wndHeight = document.documentElement.clientHeight;
		} 
		else 
		if( document.body && 
			 (document.body.clientWidth || document.body.clientHeight)) {
		  // IE 4 compatible
		  wndWidth = document.body.clientWidth;
			wndHeight = document.body.clientHeight;
		}
		
		// Iterate all elements in <body> and get element with biggest width
		var maxOffsetWidth = 0;
		var marginTop = null;
		var marginBottom = null;

		var o = document.body.childNodes[0];
		while (o) {
	  	if (o.offsetWidth) {
				if (marginTop==null)
					marginTop = this.getStyleInt (o, 'margin-top');
				marginBottom = this.getStyleInt (o, 'margin-bottom'); 				
				if (o.offsetWidth>maxOffsetWidth) {				
					maxOffsetWidth = o.offsetWidth + this.getStyleInt (o, 'margin-left');
				}
	  	}
		  o=o.nextSibling;
	  }	

	  return {
			windowWidth: wndWidth, 
			windowHeight: wndHeight,
			clientWidth: document.body.clientWidth,
			clientHeight: document.body.clientHeight,
			documentWidth: maxOffsetWidth,
			documentHeight: document.body.clientHeight+marginTop+marginBottom			  
		};
	},


	getURLParam : function (name) {
	  attr = attr.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( window.location.href );
	  if( results == null )
	    return "";
	  else
	    return results[1];
	},
	
	disableSystemContextMenu : function () {
		WSystem.fSystemContextMenu = false;
	},
	
	enableSystemContextMenu : function () {
		WSystem.fSystemContextMenu = true;
	},
	
	showContextMenu : function (menuItems, coords) {
		if (WSystem.fIgnoreDocMouseDown)
			return;
		WSystem.closeContextMenu ();
		var menu = new WMenu (document.body, "contextmenu", coords.x, coords.y, menuItems, false);
		WSystem.fIgnoreDocMouseDown=true;
		WSystem.disableSystemContextMenu ();
	},
	
	closeContextMenu : function () {
		if (WSystem.menuList["contextmenu"]) {
			WSystem.menuList["contextmenu"].closeMenu ();
			WSystem.menuList["contextmenu"] = null;
		}
	},	
	
 	onDocumentMouseDown : function (evt) {
		if (WSystem.fIgnoreDocMouseDown) {
			WSystem.fIgnoreDocMouseDown=false;
			return false;
		}
		WSystem.closeContextMenu ();
		WSystem.enableSystemContextMenu ();
		return false;
	}
};


WSystem.addEvent (document, "contextmenu", function (evt) {
	if (!WSystem.fSystemContextMenu) {
		if (window.event) {
			window.event.returnValue = false;
	   	window.event.cancelBubble = true;
		}
		else {
			evt.stopPropagation();
		}
	}
	else {
		if (window.event) {
			window.event.returnValue = true; 
	   	window.event.cancelBubble = false; 
		}
	}
}, false);

WSystem.addEvent (document, "mousedown", WSystem.onDocumentMouseDown, false);
