var sessionUpdater = Class.create({
	initialize: function() {
		this.url = '/login/flash.php';
		this.REFRESH_TIME = 60000;
		this.pendingRequest = false;
		this.refreshTimer;
		this.setTimeout;
		this.aktion = '';
		this.windowTitle = document.title;
		
		// letzte fehler- bzw. erfolgsmeldung  
		this.message = '';
		
		// user eingeloggt
		this.loggedIn = false;
		this.loginError = false;
		
		// userid des eingeloggten users 
		this.userid = '';
		
		this.home_active        = false;
		this.botschaften_active = false;
		this.abos_active        = false;
		this.freunde_active     = false;
		this.dudf_active        = false;
		
		this.dudf_sender = '';
		
		this.notifications_home        = new Array();
		this.notifications_botschaften = new Array();
		this.notifications_freunde     = new Array();
		this.notifications_abos        = new Array();
		this.notifications_dudf        = new Array();
		
		this.buddiesonline  = new Array();
		this.buddiesoffline = new Array();
		this.notifications  = new Array();
		
		// zähler
		this.cnt_botschaften   = 0;
		this.cnt_freundeonline = 0;
		this.cnt_abos          = 0;
		this.cnt_home          = 0;
		
		// notification layer
		this.notificationalert = 0;
		
		
		// login formular abschicken???
		this.submit_login = false;
		
		this.response = null;
		
		if (typeof($('loginLoad'))=='undefined') {
			this.missingProgress = true;
		}
		else {
			this.missingProgress = false;
		}
		
		if (typeof($('offline'))=='undefined' || typeof($('online'))=='undefined') {
			this.missingFrame = true;
		}
		else {
			this.missingFrame = false;
		}
		
		// Überprüfen, ob client schon eingeloggt ist.
		var boundCallRequest = this.callRequest.bind(this);
		boundCallRequest('readSessionData');
	},
	
	
	
	callRequest: function(func, pars) {
	   
        var params = {
                        engine: 'ajax',
                        operation: func
                     };
        Object.extend(params, pars);
        
        if (!this.pendingRequest || func == 'readSessionData' || func == 'refreshSessionData')  {

            new Ajax.Request(this.url,
                     {
                       method:'post',
                       parameters: params,                  
                       onSuccess: this.ajaxUpdate.bind(this)
                     }
                    );
        }
	
	},
	
	inProgress: function(i) {
		if (!this.missingProgress) {
			if (i==1) {
			    this.pendingRequest = true;
			    $('loginLoad').style.display='block';
			} else {
			    this.pendingRequest = false;
				$('loginLoad').style.display='none';
			}
		}
	},  
	
	ajaxUpdate: function(ajaxResponse) {
		this.inProgress(0);
		this.response = ajaxResponse.responseXML;
		if (this.response.getElementsByTagName("funktion")[0].hasChildNodes()) {
			this.aktion = this.response.getElementsByTagName("funktion")[0].childNodes[0].nodeValue;
		}
		
		switch(this.aktion) {
			case 'doLogin':	
				this.showLogin();
				this.setFrame();
				break;
			case 'doLogout':
				this.showLogout();
				this.setFrame();
				break;
			case 'readSessionData':
				this.showReadSessionData();
				this.setFrame();
				break;
			case 'refreshSessionData':
				this.showRefreshSessionData();
				this.setFrame();
				break;
		}
	},
	
	showLogin: function() {
		if (this.response.getElementsByTagName("success")[0].childNodes.length > 0 && 
			this.response.getElementsByTagName("success")[0].childNodes[0].nodeValue=='1') {
			this.loggedIn = true;
			$('dudfStatus').innerHTML = '';
			window.location.href=window.location;
			//this.callRequest('readSessionData');
			//countIVW();
		} else {
			var status = this.response.getElementsByTagName("message")[0].childNodes[0].nodeValue;
			$('dudfStatus').style.margin='0px 0px 0px 0px';
			if (status=='Passwort vergessen?') {
			    this.loginError = true;
				$('dudfStatus').innerHTML = 'Das eingegebene Passwort stimmt nicht.';
			}
			else {
				$('dudfStatus').innerHTML = status;
			}
		}
	},
	
	showLogout: function() {
		if (this.response.getElementsByTagName("success")[0].childNodes.length > 0 && 
			this.response.getElementsByTagName("success")[0].childNodes[0].nodeValue=='1') {
			// globale variablen setzen
			this.loggedIn = false;
			this.userid = '';
			this.buddiesonline = new Array();
			this.buddiesoffline = new Array();
			
			
			this.initNotificationData();
			this.stopRefresh();
			$('dudfStatus').innerHTML = this.response.getElementsByTagName("message")[0].childNodes[0].nodeValue;
			//countIVW();
		} else {
			$('dudfStatus').innerHTML = this.response.getElementsByTagName("message")[0].childNodes[0].nodeValue;
		}
	},
	
	showReadSessionData: function() {
		if (this.response.getElementsByTagName("userid")[0].hasChildNodes()) {
			if (this.response.getElementsByTagName("userid")[0].childNodes[0].nodeValue.length > 0) {
				this.initNotificationData();
				// globale variablen setzen
				this.userid         = this.response.getElementsByTagName("userid")[0].childNodes[0].nodeValue;
				this.loggedIn       = true;
				/*
				this.buddiesonline = $A(this.response.getElementsByTagName("buddiesonline")[0].getElementsByTagName("buddy"));
				
				this.buddiesoffline = $A(this.response.getElementsByTagName("buddiesoffline")[0].getElementsByTagName("buddy"));
				
				var tmpNotifications = $A(this.response.getElementsByTagName("notification"));
				if (tmpNotifications.length>0) {
					for (var k=0;k<tmpNotifications.length;k++) {
						this.notifications[k] = new Object();
						this.notifications[k]["id"]			= "";
						this.notifications[k]["nid"]		= "";
						this.notifications[k]["sender"]		= "";
						this.notifications[k]["title"]		= "";
						this.notifications[k]["message"]	= "";
						this.notifications[k]["refid"]		= "";
						this.notifications[k]["action"]		= "";
						this.notifications[k]["created"]	= "";
						
						if (tmpNotifications[k].getElementsByTagName("id")[0].hasChildNodes()) {
							this.notifications[k]["id"]  = tmpNotifications[k].getElementsByTagName("id")[0].childNodes[0].nodeValue;
						}
						if (this.response.getElementsByTagName("notification")[k].getElementsByTagName("nid")[0].hasChildNodes()) {
							this.notifications[k]["nid"]  = tmpNotifications[k].getElementsByTagName("nid")[0].childNodes[0].nodeValue;
						}
						if (tmpNotifications[k].getElementsByTagName("sender")[0].hasChildNodes()) {
							this.notifications[k]["sender"]  = tmpNotifications[k].getElementsByTagName("sender")[0].childNodes[0].nodeValue;
						}
						if (tmpNotifications[k].getElementsByTagName("title")[0].hasChildNodes()) {
							this.notifications[k]["title"]  = tmpNotifications[k].getElementsByTagName("title")[0].childNodes[0].nodeValue;
						}
						if (tmpNotifications[k].getElementsByTagName("message")[0].hasChildNodes()) {
							this.notifications[k]["message"]  = tmpNotifications[k].getElementsByTagName("message")[0].childNodes[0].nodeValue;
						}
						if (tmpNotifications[k].getElementsByTagName("refid")[0].hasChildNodes()) {
							this.notifications[k]["refid"]  = tmpNotifications[k].getElementsByTagName("refid")[0].childNodes[0].nodeValue;
						}
						if (tmpNotifications[k].getElementsByTagName("action")[0].hasChildNodes()) {
							this.notifications[k]["action"]  = tmpNotifications[k].getElementsByTagName("action")[0].childNodes[0].nodeValue;
						}
						if (tmpNotifications[k].getElementsByTagName("created")[0].hasChildNodes()) {
							this.notifications[k]["created"]  = tmpNotifications[k].getElementsByTagName("created")[0].childNodes[0].nodeValue;
						}
					}
				} else {
					this.notifications = new Array();
				}
				
				var tmp_servertime = this.response.getElementsByTagName("timestamp")[0].childNodes[0].nodeValue;
				
				// notifications auswerten
				for (var i=0; i<this.notifications.length; i++) {
					if (this.notifications[i]["action"] == 'gb') {
						this.notifications_home[this.notifications_home.length] = this.notifications[i];
						this.home_active = true;
					} else if (this.notifications[i]["action"] == 'botschaft') {
						this.notifications_botschaften[this.notifications_botschaften.length] = this.notifications[i];
						this.botschaften_active = true;
	                } else if (this.notifications[i]["action"] == 'abo_benutzer') {
	                    if (tmp_servertime >= this.notifications[i]["message"]) {
	                        this.notifications_abos[this.notifications_abos.length] = this.notifications[i];
	                        this.abos_active = true;
	                    }
	                } else if (this.notifications[i]["action"] == 'abo_text') {
	                    this.notifications_abos[this.notifications_abos.length] = this.notifications[i];
	                    this.abos_active = true;
	                } else if (this.notifications[i]["action"] == 'abo_label') {
	                    if (tmp_servertime >= this.notifications[i]["message"]) {
	                    	this.notifications_abos[this.notifications_abos.length] = this.notifications[i];
	                        this.abos_active = true;
	                    }
	                } else if (this.notifications[i]["action"] == 'abo_forum') {
	                    this.notifications_abos[this.notifications_abos.length] = this.notifications[i];
	                    this.abos_active = true;
	                } else if (this.notifications[i]["action"] == 'abo_empfehlung') {
	                	this.notifications_abos[this.notifications_abos.length] = this.notifications[i];
	                    this.abos_active = true;
	                } else if (this.notifications[i]["action"] == 'abo_platz') {
	                	this.notifications_abos[this.notifications_abos.length] = this.notifications[i];
	                    this.abos_active = true;
	                } else if (this.notifications[i]["action"] == 'abo_gruppe') {
	                	this.notifications_abos[this.notifications_abos.length] = this.notifications[i];
	                    this.abos_active = true;
	                } else if (this.notifications[i]["action"] == 'freunde') {
	                	this.notifications_freunde[this.notifications_freunde.length] = this.notifications[i];
	                    this.freunde_active = true;
	                } else if (this.notifications[i]["action"] == 'dudf') {
	                	this.notifications_dudf[this.notifications_dudf.length] = this.notifications[i];
	                    this.dudf_active = true;
	                } 
				}
				
				this.removeDoubleJetzpageNotifications();
	            //this.sortJetztpageNotifications();
	            
	            // zähler initialisieren
	            this.cnt_botschaften   = this.notifications_botschaften.length;
	            this.cnt_freundeonline = this.buddiesonline.length;
	            this.cnt_abos          = this.notifications_abos.length;
	            this.cnt_home          = this.notifications_home.length;
	            this.cnt_dudf          = this.notifications_dudf.length;
	            */
				this.startRefresh();
				this.setLoggedInFrameStatus();
			}
		}
	},
	
	showRefreshSessionData: function() {
	    /*
		if (this.response.getElementsByTagName("userid")[0].hasChildNodes()) {
			if (this.response.getElementsByTagName("userid")[0].childNodes[0].nodeValue.length > 0) {
				this.initNotificationData();
				
				this.buddiesonline = $A(this.response.getElementsByTagName("buddiesonline")[0].getElementsByTagName("buddy"));
				
				this.buddiesoffline = $A(this.response.getElementsByTagName("buddiesoffline")[0].getElementsByTagName("buddy"));
				
				var tmpNotifications = $A(this.response.getElementsByTagName("notification"));
				if (tmpNotifications.length>0) {
					for (var k=0;k<tmpNotifications.length;k++) {
						this.notifications[k] = new Object();
						this.notifications[k]["id"]			= "";
						this.notifications[k]["nid"]		= "";
						this.notifications[k]["sender"]		= "";
						this.notifications[k]["title"]		= "";
						this.notifications[k]["message"]	= "";
						this.notifications[k]["refid"]		= "";
						this.notifications[k]["action"]		= "";
						this.notifications[k]["created"]	= "";
						
						if (tmpNotifications[k].getElementsByTagName("id")[0].hasChildNodes()) {
							this.notifications[k]["id"]  = tmpNotifications[k].getElementsByTagName("id")[0].childNodes[0].nodeValue;
						}
						if (this.response.getElementsByTagName("notification")[k].getElementsByTagName("nid")[0].hasChildNodes()) {
							this.notifications[k]["nid"]  = tmpNotifications[k].getElementsByTagName("nid")[0].childNodes[0].nodeValue;
						}
						if (tmpNotifications[k].getElementsByTagName("sender")[0].hasChildNodes()) {
							this.notifications[k]["sender"]  = tmpNotifications[k].getElementsByTagName("sender")[0].childNodes[0].nodeValue;
						}
						if (tmpNotifications[k].getElementsByTagName("title")[0].hasChildNodes()) {
							this.notifications[k]["title"]  = tmpNotifications[k].getElementsByTagName("title")[0].childNodes[0].nodeValue;
						}
						if (tmpNotifications[k].getElementsByTagName("message")[0].hasChildNodes()) {
							this.notifications[k]["message"]  = tmpNotifications[k].getElementsByTagName("message")[0].childNodes[0].nodeValue;
						}
						if (tmpNotifications[k].getElementsByTagName("refid")[0].hasChildNodes()) {
							this.notifications[k]["refid"]  = tmpNotifications[k].getElementsByTagName("refid")[0].childNodes[0].nodeValue;
						}
						if (tmpNotifications[k].getElementsByTagName("action")[0].hasChildNodes()) {
							this.notifications[k]["action"]  = tmpNotifications[k].getElementsByTagName("action")[0].childNodes[0].nodeValue;
						}
						if (tmpNotifications[k].getElementsByTagName("created")[0].hasChildNodes()) {
							this.notifications[k]["created"]  = tmpNotifications[k].getElementsByTagName("created")[0].childNodes[0].nodeValue;
						}
					}
				} else {
					this.notifications = new Array();
				}
				
				var tmp_servertime = this.response.getElementsByTagName("timestamp")[0].childNodes[0].nodeValue;
				
				// notifications auswerten
				for (var i=0; i<this.notifications.length; i++) {
					if (this.notifications[i]["action"] == 'gb') {
						this.notifications_home[this.notifications_home.length] = this.notifications[i];
						this.home_active = true;
					} else if (this.notifications[i]["action"] == 'botschaft') {
						this.notifications_botschaften[this.notifications_botschaften.length] = this.notifications[i];
						this.botschaften_active = true;
	                } else if (this.notifications[i]["action"] == 'abo_benutzer') {
	                    if (tmp_servertime >= this.notifications[i]["message"]) {
	                        this.notifications_abos[this.notifications_abos.length] = this.notifications[i];
	                        this.abos_active = true;
	                    }
	                } else if (this.notifications[i]["action"] == 'abo_text') {
	                    this.notifications_abos[this.notifications_abos.length] = this.notifications[i];
	                    this.abos_active = true;
	                } else if (this.notifications[i]["action"] == 'abo_label') {
	                    if (tmp_servertime >= this.notifications[i]["message"]) {
	                    	this.notifications_abos[this.notifications_abos.length] = this.notifications[i];
	                        this.abos_active = true;
	                    }
	                } else if (this.notifications[i]["action"] == 'abo_forum') {
	                    this.notifications_abos[this.notifications_abos.length] = this.notifications[i];
	                    this.abos_active = true;
	                } else if (this.notifications[i]["action"] == 'abo_empfehlung') {
	                	this.notifications_abos[this.notifications_abos.length] = this.notifications[i];
	                    this.abos_active = true;
	                } else if (this.notifications[i]["action"] == 'abo_platz') {
	                	this.notifications_abos[this.notifications_abos.length] = this.notifications[i];
	                    this.abos_active = true;
	                } else if (this.notifications[i]["action"] == 'abo_gruppe') {
	                	this.notifications_abos[this.notifications_abos.length] = this.notifications[i];
	                    this.abos_active = true;
	                } else if (this.notifications[i]["action"] == 'freunde') {
	                	this.notifications_freunde[this.notifications_freunde.length] = this.notifications[i];
	                    this.freunde_active = true;
	                } else if (this.notifications[i]["action"] == 'dudf') {
	                	this.notifications_dudf[this.notifications_dudf.length] = this.notifications[i];
	                    this.dudf_active = true;
	                } 
				}
				
				this.removeDoubleJetzpageNotifications();
				//this.sortJetztpageNotifications();
	            
	            // zähler initialisieren
	            this.cnt_botschaften   = this.notifications_botschaften.length;
	            this.cnt_freundeonline = this.buddiesonline.length;
	            this.cnt_abos          = this.notifications_abos.length;
	            this.cnt_home          = this.notifications_home.length;
	            this.cnt_dudf          = this.notifications_dudf.length;
	            */
				this.setLoggedInFrameStatus();
				/*
			} else {
				this.callRequest('doLogout');
			}
		} else {
			this.callRequest('doLogout');
		}*/
	},
	
	setFrame: function() {
		if (!this.missingFrame) {
			if (this.loggedIn) {
			    $('login_aus').style.display='none';
				$('offline').style.display='none';
				$('online').style.display='block';
			} else if (this.loginError) {
                $('login_aus').style.display='none';
				$('offline').style.display='block';
				$('online').style.display='none';
			}
			else {
		        $('login_aus').style.display='block';
                $('offline').style.display='none';
                $('online').style.display='none';
		    }
		}
	},
	
	initNotificationData: function() {
		this.home_active        = false;
		this.botschaften_active = false;
		this.abos_active        = false;
		this.clubs_active       = false;
	    this.freunde_active     = false;
		this.dudf_active        = false;
		this.notifications_home        = new Array();
	    this.notifications_freunde     = new Array();
		this.notifications_botschaften = new Array();
		this.notifications_abos        = new Array();
		this.notifications_dudf        = new Array();
		this.notifications				= new Array();
	
	    //this.dudf_sender = '';
	    this.returnToDudfSender = false;
	    
	    //   zähler
	    this.cnt_botschaften   = 0;
	    this.cnt_freundeonline = 0;
	    this.cnt_abos          = 0;
	    this.cnt_home          = 0;
	    this.cnt_dudf          = 0;
	}, 
	
	setLoggedInFrameStatus: function() {
		if (!this.missingFrame) {
			var anyActive = '';
			/*
		    // freunde
		    if (this.freunde_active) {
		    	$('dudfFreunde').style.border = "1px solid #D94C4C";
		    	$('dudfFreundeLink').innerHTML = this.cnt_freundeonline;
		    	anyActive = 'Freunde';
		    } else {
		    	$('dudfFreundeLink').innerHTML = this.cnt_freundeonline;
		    }
		
		    // botschaften
			if (this.botschaften_active) {
				$('dudfBotschaften').src = "/img/login/jb_on2.png";
				$('dudfBotschaften').title = "Neue Botschaften"; 
				anyActive = 'Botschaft';
			} else {
				$('dudfBotschaften').src = "/img/login/jb_off2.png";
		    }
		
		    // abos
		    if (this.abos_active) {
		        $('dudfAbos').src = "/img/login/abo_on2.png";
				$('dudfAbos').title = "Neue Abos";
		        anyActive = 'Abos';
		    } else {
		        $('dudfAbos').src = "/img/login/abo_off2.png";
		    }
		    
		    // dudf 
		    if (this.dudf_active) {
		        // letzte dudf nachricht anzeigen
		        $('dudfNachrichtLink').innerHTML = this.notifications_dudf[0]["message"];
		        $('dudfNachrichtLink').href = "javascript:dudf.returnToDudfSender=true;dudf.callRequest('deleteNotification', {path1: "+this.notifications_dudf[0]["id"]+"});";
		        
		        $('dudfNachricht').style.display = "inline";
		        $('dudfNachrichtImg').href = "javascript:dudf.callRequest('deleteNotification', {path1: "+this.notifications_dudf[0]["id"]+"});";
		        
		        if (this.notifications_dudf[0]["message"].indexOf("angesehen") != -1) 
		        	anyActive = 'Jetztpage';
		       	if (this.notifications_dudf[0]["message"].indexOf("betreten") != -1) 
		        	anyActive = 'Freunde';
		        if (this.notifications_dudf[0]["message"].indexOf("verlassen") != -1) 
		        	anyActive = 'Freunde';
		    } else {
		        $('dudfNachrichtLink').innerHTML = "";
		        $('dudfNachrichtLink').href = "";
		        
		        $('dudfNachricht').style.display = "none";
		        $('dudfNachrichtImg').href = "";
		        
		        
		    }
		    
		    // home
			if (this.home_active) {
				$('dudfHome').src = "/img/login/gb_on2.png";
				$('dudfHomeLink').href = "/jetztpage/"+this.userid+"/gaestebuch";
				$('dudfHome').title = "Neue Gästebucheinträge";
				anyActive = "Gästebuch";
		    } else {
				$('dudfHome').src = "/img/login/gb_off2.png";
				$('dudfHomeLink').href = "/jetztpage/"+this.userid;
			}
		    
		    if (anyActive.length>0 && (this.aktion=='refreshSessionData' || this.aktion=='deleteNotification')) {
	        	document.title='jetzt.de ['+anyActive+']';
	        	this.notificationalert = 1;
			} else {
				document.title = this.windowTitle;
			}
			*/
			$('dudfProfilLink').href = "/profil"
		}
	},
	
	showDeleteNotification: function() {
		// auf seite des senders springen oder notification auf server löschen
	    if (this.returnToDudfSender) {
	        window.location.href = '/jetztpage/' + this.notifications_dudf[0]["sender"];
	    } else {
	        this.notifications_dudf.shift();
	    }
	    
	    if (this.notifications_dudf.length == 0) {
	        this.dudf_active = false;
	        this.cnt_dudf    = 0;
	    } else {
	        this.dudf_active = true;   
	    	this.cnt_dudf    = this.notifications_dudf.length;
	    }
	    this.notificationalert = 0;
	    this.setLoggedInFrameStatus();
	    countIVW();
	},
	
	removeDoubleJetzpageNotifications: function() {
	    // doppelte einträge jetztpage dudf notifications herausnehmen
	    var tmp_dudf_messages = new Array();
	    var tmp_notifications_dudf = new Array();
	    for (var k=0;k<this.notifications_dudf.length;k++) {
	    	var tmp_string = this.notifications_dudf[k]["message"];
	        if (tmp_string.indexOf("jetzt-page") != -1) {
        		if (!tmp_dudf_messages.contains(this.notifications_dudf[k]["message"])) {
        			tmp_notifications_dudf[tmp_notifications_dudf.length] = this.notifications_dudf[k];
                	tmp_dudf_messages[tmp_dudf_messages.length] = this.notifications_dudf[k]["message"];
        		} else {
        			// notification auf server löschen
        			this.callRequest('deleteNotification', {path1: this.notifications_dudf[k]["id"]});
        		}
	        } else {
	            tmp_notifications_dudf[tmp_notifications_dudf.length] = this.notifications_dudf[k];
	        }
	    }    
	    this.notifications_dudf = tmp_notifications_dudf;
	},
	
	sortJetztpageNotifications: function() {
		var min = 0;
		var tmp;
		for (var i=0;i<=this.notifications_dudf.length-2;i++) {
			min = i;
			for (var j=0;j<=this.notifications_dudf.length-1;j++) {
				if (this.notifications_dudf[min]["id"]>this.notifications_dudf[j]["id"])
					min = j;
				if (min!=j) {
					tmp = this.notifications_dudf[min];
					this.notifications_dudf[min] = this.notifications_dudf[i];
					this.notifications_dudf[i]	 = tmp;
				}
			}
		}
	},
	
	startRefresh: function() {
		this.refreshTimer  = setInterval("dudf.callRequest('refreshSessionData')", this.REFRESH_TIME);
	},

	stopRefresh: function() {
		clearInterval(this.refreshTimer);
	}
});

Array.prototype.contains = function (val) {
    for (var i=0;i<this.length;i++) {
        if (this[i] == val) {
            return true;
        }
    }
    return false;
};
