/**
* This class is supposed to be used in the webfrontends of the freemailers.
*
* @author L.Kaiser
*
* @param visibleCssClass {String} The name of the css-class used to make HTML-nodes visible in the method 'adjustHyperlinksIn()'
* @param optionsPage {String} The fragment of the URL of the options-page (Optionenseite). The method adjustHyperlinksIn will append the from- and to-parameter!
*/
var p945 = function(visibleCssClass, optionsPage){

	/*
	 * Only Windows is supported. but so far no VISTA support!
	 *
	 * Windows 95 = Windows 95 or Win95
	 * Windows 98 & 98 SE = Windows 98 or Win98
	 * Windows CE = Windows CE
	 * Windows ME = Windows 9x 4.90
	 * Windows NT 4.0 = Windows NT 4.0
	 * Windows 2000 = Windows NT 5.0
	 * Windows XP = Windows NT 5.1
	 * Windows Server 2003 / XP x64 edition = Windows NT 5.2
	 * Windows Vista = Windows NT 6.0
	 */
	var isPlatformSupported = (navigator.userAgent.indexOf("Windows NT 5.") > -1);	


	var invitationSchema = "uimminvite"; // This will change to 'uimminvite' with the next release of MultiMessenger (V3.40)
	var validEmailRegex = /^(([a-zA-Z0-9][a-zA-Z0-9_.\-]*|\"([^\\\x80-\xff\015\012\"]|\\[^\x80-\xff])+\")\@([a-zA-Z0-9][a-zA-Z0-9._\-]*\.)*[a-zA-Z0-9][a-zA-Z0-9._\-]*\.[a-zA-Z]{2,5})?$/;


	/**
	* This method adjusts all hyperlinks in a given HTML-node, depending on platform and email-address to invite. 
	* It then changes the css-class of the HTML-node according to the constructor-parameter.
	*
	* @param vElementID {String} The ID of the tmpEl that contains the Hyperlinks to the MultiMessenger site
	* @param vFrom {String} The JID of the contact to add to the roster
	* @param vTo {String} The JID of the mailbox-owner
	* @param vMmIntegrationSetting {Number} 0-go to options page / 1-Do not display anything / 2-create XMPP-/UIMMINVITE-link
	*/
	this.adjustHyperlinksIn = function(vElementID, vFrom, vTo, vMmIntegrationSetting){
		vFrom = decodeURIComponent(vFrom);
		vTo = decodeURIComponent(vTo);
		if(isPlatformSupported && validEmailRegex.test(vFrom)){
			var divElement = document.getElementById(vElementID);
		
			// change all embedded hyperlinks
			recursivelyModifyHyperlinks(divElement, vFrom, vTo, vMmIntegrationSetting);
		
			// apply CSS-class to DIV to make it visible
			divElement.className = visibleCssClass;
		}
	};
	
	
	/**
	* Recursively traverses the DOM-tree from a given element and changes  
	* all hyperlink nodes found. 
	* 
	* @param vElement {Element} The root tmpEl for DOM-traversal
	* @param vFrom {String} The JID of the contact to add to the roster
	* @param vTo {String} The JID of the mailbox-owner
	* @param vMmIntegrationSetting {Number} 0-go to options page / 1 Do not display anything- / 2-create XMPP-/UIMMINVITE-link
	*/
	var recursivelyModifyHyperlinks = function(vElement, vFrom, vTo, vMmIntegrationSetting){
		for(var i=0; i < vElement.childNodes.length;i++){
			var tmpEl = vElement.childNodes[i];
			if(tmpEl.nodeName.toLowerCase() == "a"){
				if(vMmIntegrationSetting == 0 || vMmIntegrationSetting == 1){
					tmpEl.href = optionsPage + "&from=" + encodeURIComponent(vFrom) + "&to=" + encodeURIComponent(vTo);
				} else if(vMmIntegrationSetting == 2){
					tmpEl.href = invitationSchema + ":" + vFrom;	
					tmpEl.onclick = function(e){
						var wnd = window.open(invitationSchema + ":" + vFrom);
						wnd.close();
						return false;
					};
				}
			}
			recursivelyModifyHyperlinks(tmpEl, vFrom, vTo, vMmIntegrationSetting);
		}
	};
};