﻿if (document.domain.indexOf("gastro.rosenheim24.de") > 0) document.domain = "gastro.rosenheim24.de";

MainNavigationCommon = new Object();
MainNavigationCommon.strSSOErrorMessage 			= "Fehler: Benutzername oder Passwort falsch";
MainNavigationCommon.strAsHomepage1 				= "VOL als";
MainNavigationCommon.strAsHomepage2 				= "Startseite";
MainNavigationCommon.strLogin						= "Anmelden";
MainNavigationCommon.strLogout						= "Abmelden";
MainNavigationCommon.strRegister					= "Registrieren";
MainNavigationCommon.strOr							= "oder";

MainNavigationCommon.strRegisterUrl					= "http://freunde.vol.at/register.html";
MainNavigationCommon.strLoginUrl					= "http://vol.at/engine.aspx?page=login";
MainNavigationCommon.strUNameFieldName				= "SSOUserName";
MainNavigationCommon.strPWFieldName					= "SSOPassword";
MainNavigationCommon.strUNameValue					= "Benutzername";
MainNavigationCommon.strPWValue						= "Passwort";
MainNavigationCommon.strProfileUrl					= "http://freunde.vol.at/###username###/";
MainNavigationCommon.strSSOWebServiceUrl			= "/snpeservice.asmx";
MainNavigationCommon.strSSOWebServiceAuthMethod		= "AuthenticateAsObjectAndLogin";
MainNavigationCommon.strSSOWebServiceLogoutMethod	= "Logout";				

function PortalUser(strUserName, strPasswordHash) {
	//properties
	this.strUserName 		= null;
	this.strPasswordHash 	= null;
		
	//set given values
	if (strUserName 	&& strUserName != "") 		this.strUserName 		= strUserName;
	if (strPasswordHash && strPasswordHash != "") 	this.strPasswordHash 	= strPasswordHash;
	
}

function MainNavigationBar () {
	//properties
	this.ULNavigation 				= null;
	this.strActiveNodeID1			= null;
	this.strActiveNodeID2			= null;
	
	this.nodeActive           = null;
	this.nodeHover            = null;
	this.nodeFallback         = null;
	
	this.Switch               = null;
	this.Fallback             = null;
	this.User						      = null;
		
	this.strRegisterUrl				= MainNavigationCommon.strRegisterUrl;
	this.strLoginUrl				= MainNavigationCommon.strLoginUrl;
	this.strLogoutUrl				= null;
	this.strOverrideLoginUrl		= null; //this is an extra property so we can see if there is an override
		
	this.strFormAction				= null;
	this.strUNameFieldName			= MainNavigationCommon.strUNameFieldName;
	this.strPWFieldName				= MainNavigationCommon.strPWFieldName;
	this.strProfileUrl				= MainNavigationCommon.strProfileUrl;
	this.boolShowLogin              = true; //sets wether the login-stuff is visible or not
	
	//try to fetch user info
	this.User =  this._ReadUserCookie();
}

	//"Private" methods

	//internal method for deactivating all top navigation elements
	MainNavigationBar.prototype._DeactivateTrees = function() {
		
		if (this.nodeActive != null) {
		    if (this.nodeActive.className.match(/Double/)) this.nodeActive.className = "Double";
            else this.nodeActive.className = "";
		}
	};
	
	//internal method for reading cookie 
	MainNavigationBar.prototype._GetCookie = function (name){
	   var i=0;  //Suchposition im Cookie
	   var suche = name+"=";
	   var cook = null;
	   while (i<document.cookie.length){
	      if (document.cookie.substring(i, i+suche.length)==suche){
	         var ende = document.cookie.indexOf(";", i+suche.length);
	         ende = (ende>-1) ? ende : document.cookie.length;
	         cook = unescape(document.cookie.substring(i+suche.length, ende));
	      }
	      i++;
	   }
	  
    return cook;
	};
	
	
	MainNavigationBar.prototype._OpenTree = function(LIActive, thisNavigationBar) {
	    if (LIActive == thisNavigationBar.nodeHover|| LIActive == thisNavigationBar.nodeFallback) {
	    
	        //set all other elements passive
		    thisNavigationBar._DeactivateTrees();
		
		    LIActive.className += " Active";
		    thisNavigationBar.nodeActive  = LIActive;
	    }
	}
	
	
	//internal method: mouseoverevent for the list elements
	MainNavigationBar.prototype._Hover = function(LIActive) {
	    var thisNavigationBar   = this;
	    
	    window.clearTimeout(thisNavigationBar.Switch);
	    window.clearTimeout(thisNavigationBar.Fallback);
		thisNavigationBar.nodeHover = LIActive;
		thisNavigationBar.Switch = window.setTimeout(function () {MainNavigationBar.prototype._OpenTree(LIActive, thisNavigationBar); }, 250);
	};
	
	//internal method: mouseoutevent for the list elements
	MainNavigationBar.prototype._HoverOut = function() {
	    var thisNavigationBar   = this;
	    
	    window.clearTimeout(thisNavigationBar.Switch);
	    window.clearTimeout(thisNavigationBar.Fallback);
      thisNavigationBar.Fallback = window.setTimeout(function () {MainNavigationBar.prototype._OpenTree(thisNavigationBar.nodeFallback, thisNavigationBar); }, 2000);
	};
	
	//internal method: find active list nodes for a given navigation level
	MainNavigationBar.prototype._FindActiveListNode = function (nodeParent, iLevel, strActiveNodeID, boolStandard) {
		var thisNavigationBar = this;
		var LI = nodeParent.firstChild;
		var nodeFound = null;
		var iNum = 0;
		
		do {
			if (LI && LI.nodeName == "LI") {
				//things todo on first level nav...
				if (iLevel == 0) {
					LI.onmouseover = function () { thisNavigationBar._Hover(this); };
					LI.onmouseout = function () { thisNavigationBar._HoverOut(); };
					//set this treenode active (if set)
					if (boolStandard) {
						if (iNum == 0) {
							this._Hover(LI);
							nodeFound = LI;	
						}				
					}
					else {
						if (LI.id == strActiveNodeID) {
							this._Hover(LI);
							nodeFound = LI;
						}
					}
				}
				//things todo on second level nav...
				else {
					if (boolStandard) {
						if (iNum == 0) {
							if (LI.className.match(/Last/)) LI.className = "Last Active";
							else LI.className = "Active";
							nodeFound = LI;
						}
						else {
							if (LI.className.match(/Last/)) LI.className = "Last";
							else LI.className = "";
						}
					}
					else {
						if (LI.id == strActiveNodeID) {
							if (LI.className.match(/Last/)) LI.className = "Last Active";
							else LI.className = "Active";
							nodeFound = LI;
						}
						else {
							if (LI.className.match(/Last/)) LI.className = "Last";
							else LI.className = "";
						}
					}				
				}
				iNum++;
			}
			LI = LI.nextSibling;
		}
		while (LI);
		return nodeFound;
	}
	
	//opens the login form overlay
	MainNavigationBar.prototype._OpenLoginForm = function () {
		//clean up a bit...
		var nodeSSOMessages 	=	document.getElementById("SSOMessages");
		while (nodeSSOMessages.firstChild)
			nodeSSOMessages.removeChild(nodeSSOMessages.firstChild);	
		var formLogin		= document.forms["SSOLoginForm"];
		var inputUserName	= formLogin["SSOUser"];
		var inputPassword	= formLogin["SSOPassword"];
		inputUserName.value = MainNavigationCommon.strUNameValue;	
		inputPassword.value = MainNavigationCommon.strPWValue;	
			
		var nodeLoginForm = document.getElementById("SSOLogin");
		nodeLoginForm.style.visibility = "visible";
	};
	
	//closes the login form overlay
	MainNavigationBar.prototype._CloseLoginForm = function () {
		var nodeLoginForm = document.getElementById("SSOLogin");
		nodeLoginForm.style.visibility = "hidden";
	};
	
	
	//reads user info from cookie and returns a PortalUser object
	MainNavigationBar.prototype._ReadUserCookie = function () {
		var NavBar = this;
		var strUserName;
		var strPasswordHash;
		var strActive;
		var User = null;
	    var SSOCookie = null;
		
		//get the cookie
		try 
		{
		    SSOCookie = NavBar._GetCookie("SsoSessionCookie");
		    
		}
		catch(e) {
		    alert (e.description);
		}
		//if (!SSOCookie) alert("No Cookie...");
		
		if (SSOCookie == null) return null;
		
		
				
		var CookieVals = SSOCookie.split(/\&/);
		
		
				
		for (var i = 0; i < CookieVals.length; i++) {
			var Val = CookieVals[i].split(/\=/);
			//alert("cookie: " + CookieVals[i]);
			
			if (Val[0] == 'username') 			strUserName 	= (""+Val[1]).replace(/ /, "\\20");
			if (strUserName != "guest") {
				if (Val[0] == 'passwordhash') 	strPasswordHash = (""+Val[1]).replace(/\+/, "%2B") + "==";
				if (Val[0] == 'active') 		strActive 		= ""+Val[1];
			}
		}
		
		
		
		//alert("Active: " + strActive);
		if (strActive == "True") {
			User = new PortalUser(strUserName, strPasswordHash);
		}
		return User;
	}
	
		
	//"Public" methods
	
	//method for setting the active navigation node(s)
	MainNavigationBar.prototype.SetActiveNodes = function(strActiveNodeID1, strActiveNodeID2) {
		this.strActiveNodeID1 = strActiveNodeID1;
		this.strActiveNodeID2 = strActiveNodeID2;
	};
	
	
	//method to authenticate user
	MainNavigationBar.prototype.SSOAuthenticateUser = function(strUserName, strPassword, boolKeepLoggedIn) {
		var UseSoap = false;
		var NavBar = this;
		var Params = new SOAPClientParameters();
				
		//alert(strUserName + " " + strPassword + " " + boolKeepLoggedIn);
		
		Params.add("user", strUserName);
		Params.add("pass", strPassword);
		Params.add("bKeepLoggedIn", boolKeepLoggedIn);
		SOAPClient.invoke (UseSoap, MainNavigationCommon.strSSOWebServiceUrl, MainNavigationCommon.strSSOWebServiceAuthMethod, Params, false, NavBar._SSOAuthenticateUserCallback );
	}
	
	//callback method for authentication webservice (called when the service gave a response)
	MainNavigationBar.prototype._SSOAuthenticateUserCallback = function (xml) {
		var nodeSSOMessages 	=	document.getElementById("SSOMessages");
		while (nodeSSOMessages.firstChild)
			nodeSSOMessages.removeChild(nodeSSOMessages.firstChild);

		if (xml.getElementsByTagName("user")[0]) {
			//login successfull
			//alert (xml.getElementsByTagName("user-name")[0].firstChild.data);
			//refresh the page
			location.reload();
		}
		else {
			//login failed
			nodeSSOMessages.className = "error";
			nodeSSOMessages.appendChild(document.createTextNode(MainNavigationCommon.strSSOErrorMessage));
			nodeSSOMessages.style.display = "block";
		}
	}
	
	MainNavigationBar.prototype.SSOLogoutUser = function() {
	    var UseSoap = false;
		var NavBar = this;
		var Params = new SOAPClientParameters();
		SOAPClient.invoke (UseSoap, MainNavigationCommon.strSSOWebServiceUrl, MainNavigationCommon.strSSOWebServiceLogoutMethod, Params, false, NavBar._SSOLogoutUserCallback );
	}
	
	MainNavigationBar.prototype._SSOLogoutUserCallback = function (xml) {
	    
		if (xml.getElementsByTagName("*")[0].tagName.toLowerCase() == "snpewebservicemessage") {
			//logout successfull
			//alert ("logout!");
			//refresh the page
			//location.reload();
			
			window.location.href = window.location.href + " ";
			location.reload();
			
		}
		
		else {
			//logout failed (and now??? )
		}
	}
	
	
	//method to set the SSO state display (user logged-in or not etc..)
	MainNavigationBar.prototype.SetSSOFragment = function () {
		var NavBar				= this;
		var nodeSSOFragment 	= document.getElementById("SSOFragment");
		var	spanUserName		= document.createElement("span");
		var	aUserName			= document.createElement("a");
		var spanFade			= document.createElement("span");
		var	spanLoginRegister	= document.createElement("span");
		var	spanEmptyPane	    = document.createElement("span");
		var	aLogin				= document.createElement("a");
		var	aLogout				= document.createElement("a");
		var	aRegister			= document.createElement("a");
		var	aSetAsHomepage		= document.createElement("a");
		
		spanUserName.className		= "UserName";
		spanEmptyPane.className		= "EmptyPane";
		spanFade.className			= "Fade";
		spanLoginRegister.className	= "LoginRegister";
		aSetAsHomepage.className	= "SetAsHomepage";
				
		//clear the Fragment first...
		while (nodeSSOFragment.firstChild)
			nodeSSOFragment.removeChild(nodeSSOFragment.firstChild);
		
		aSetAsHomepage.appendChild(document.createTextNode(MainNavigationCommon.strAsHomepage1));
		aSetAsHomepage.appendChild(document.createElement("br"));
		aSetAsHomepage.appendChild(document.createTextNode(MainNavigationCommon.strAsHomepage2));
		aSetAsHomepage.href = "http://www.vol.at/features/vol-als-homepage";
		
		//do only if login-stuff should be visible
		if (this.boolShowLogin) {
							
		    //check if user is logged in...
		    if (this.User) {
			    //do something...
			    aUserName.appendChild(document.createTextNode(this.User.strUserName));
			    //link to user profile
			    aUserName.href= this.strProfileUrl.replace(/###username###/, encodeURI(this.User.strUserName));
			    spanUserName.appendChild(aUserName);
			    spanUserName.appendChild(spanFade);
			    spanUserName.appendChild(document.createElement("br"));
			    aLogout.appendChild(document.createTextNode(MainNavigationCommon.strLogout));
			    aLogout.title = MainNavigationCommon.strLogout;
			    if (this.strLogoutUrl) {
				    aLogout.href = this.strLogoutUrl;
			    }
			    else {
				    aLogout.href = "#";
				    aLogout.onclick = function () {NavBar.SSOLogoutUser(); return false; }
			    }	
    				
    			
			    spanUserName.appendChild(aLogout);
    			
			    nodeSSOFragment.appendChild(spanUserName);
		    }
    		
		    else {
			    //do something else :) ...
			    aLogin.appendChild(document.createTextNode(MainNavigationCommon.strLogin));
			    aLogin.href = this.strLoginUrl;	
			    //only append the onclick event if there is no override for the loginurl		
			    if (!this.strOverrideLoginUrl) {
				    aLogin.onclick = function () { NavBar._OpenLoginForm(); return false };
				    aLogin.href = this.strLoginUrl;
			    }
			    else {
				    aLogin.href = this.strOverrideLoginUrl;
				    aLogin.onclick = function (){};
			    }	
			    aRegister.appendChild(document.createTextNode(MainNavigationCommon.strRegister));
    			
			    aRegister.href = this.strRegisterUrl;
    						
			    spanLoginRegister.appendChild(aLogin);
			    spanLoginRegister.appendChild(document.createTextNode(" " + MainNavigationCommon.strOr + " "));
			    spanLoginRegister.appendChild(document.createElement("br"));
			    spanLoginRegister.appendChild(aRegister);
    			
			    nodeSSOFragment.appendChild(spanLoginRegister);
    			
			    //set stuff in login pane
			    var aRegisterLP 	= document.getElementById("SSOLinkRegister");
			    var formLogin		= document.forms["SSOLoginForm"];
			    var inputUserName	= formLogin["SSOUser"];
			    var inputPassword	= formLogin["SSOPassword"];
			    var aSubmitButton	= document.getElementById("SSOLinkSubmit");
    			
			    aRegisterLP.href 	= aRegister.href;
    			
			    //only use the formaction if there is one defined via the override method
			    // - otherwise we use a onclickevent which tries to login the user via a call
			    //	 to the SNPEWebservice.
			    if (this.strFormAction) {
				    formLogin.action 		= this.strFormAction; 
				    aSubmitButton.onclick	= function () { document.forms['SSOLoginForm'].submit(); return false; };
				    formLogin.onsubmit      = function () {};
			    }
			    else {
				    formLogin.onsubmit		= function () { NavBar.SSOAuthenticateUser(document.forms['SSOLoginForm']["SSOUser"].value, document.forms['SSOLoginForm']["SSOPassword"].value, document.forms['SSOLoginForm']["SSOKeepLoggedIn"].checked); return false; }; 
				    aSubmitButton.onclick 	= function () { NavBar.SSOAuthenticateUser(document.forms['SSOLoginForm']["SSOUser"].value, document.forms['SSOLoginForm']["SSOPassword"].value, document.forms['SSOLoginForm']["SSOKeepLoggedIn"].checked); return false; };
			    }
    			
			    inputUserName.name	= this.strUNameFieldName;
			    inputPassword.name	= this.strPWFieldName;
    						
		    }
		}
		//do only if login-stuff should not be visible
		else {
		    nodeSSOFragment.appendChild(spanEmptyPane);
		}
		nodeSSOFragment.appendChild(aSetAsHomepage);
		
	};
	
	//method: override settings of the navigation bar
	MainNavigationBar.prototype.OverrideSettings = function(strLoginUrl, strLogoutUrl, strRegisterUrl, strFormAction, strUNameFieldName, strPWFieldName, strProfileUrl, boolShowLogin) {
		if (strLoginUrl) 		    this.strOverrideLoginUrl 		= strLoginUrl;
		if (strLogoutUrl) 		    this.strLogoutUrl 				= strLogoutUrl;
		if (strRegisterUrl) 	    this.strRegisterUrl 			= strRegisterUrl;
		if (strFormAction) 		    this.strFormAction				= strFormAction;
		if (strUNameFieldName) 	    this.strUNameFieldName			= strUNameFieldName;
		if (strPWFieldName) 	    this.strPWFieldName				= strPWFieldName;
		if (strProfileUrl) 		    this.strProfileUrl				= strProfileUrl;
		if (boolShowLogin != null)  this.boolShowLogin				= boolShowLogin;
	}
	
	//method: override the user data
	MainNavigationBar.prototype.OverrideUser = function(strUsername, strPasswordHash) {
		if (strUsername) this.User = new PortalUser (strUsername, strPasswordHash);
		else this.User = null;
	}
		
		
	//initialize the navigation
	MainNavigationBar.prototype.Init = function() {
		this.ULNavigation = document.getElementById("MainNavigationBar");
		
		var boolFoundFirst 	= false;
		var boolFoundSecond	= false;
		
		//walk through tree
		var thisNavigationBar = this;
		var ActiveNode1 = this._FindActiveListNode(this.ULNavigation, 0, this.strActiveNodeID1, false);
		
		//if active first level node was found, try to get active second level node
		if (ActiveNode1) {
			var UL = ActiveNode1.firstChild;
			do {
				if (UL && UL.nodeName == "UL") this._FindActiveListNode(UL, 1, this.strActiveNodeID2, false);
				UL = UL.nextSibling;
			}
			while (UL);
		}
		
		//use standard tab(s) if first level node not found
		else {
			var ActiveNode1 = this._FindActiveListNode(this.ULNavigation, 0, "", true);
			if (ActiveNode1) {
				var UL = ActiveNode1.firstChild;
				do {
					if (UL && UL.nodeName == "UL") this._FindActiveListNode(UL, 1, "", true);
					UL = UL.nextSibling;
				}
				while (UL);
			}
		}
		
		if (ActiveNode1) {
        this.nodeActive     = ActiveNode1;
        this.nodeFallback   = ActiveNode1;  
    }
		
		this.SetSSOFragment();
	}
	