/**
 *	RaboChat namespace
 */
var alerter = 0;

function NoChat(){
	alert("Helaas kan ik u op dit moment niet te woord staan.\nIk verzoek u vriendelijk om op een later tijdstip opnieuw contact met mij op te nemen.\n\nU kunt ook gebruik maken van ons contactformulier onder het kopje 'Contact'.\nAls u daarin uw contactgegevens vermeldt, zal ik spoedig contact met u opnemen.");	
}

RaboChat = {
	
	/**
	 *	RefreshTimeOut
	 */
	wachtrijRefreshTime: 600, // 3 sec, deze kan overschreven worden bij het initialiseren.
	wachtrijCheckTime: 600, // 3 sec, deze kan overschreven worden bij het initialiseren.
	popup: null,
	aantalWachtende: null,
	chatSessionID: null,
	chatSessionStatus: null,
	
	/**
	 *	Chat accepteren.
	 */
	accepteren: function(waid) {
		if (waid > 0) {
			//new RaboChat.Popup('adviseur_chat_wachten.php?waid=' + waid, 600, 500);
			location.href = 'wachten.php?waid=' + waid;
		}
	},
	
	/**
	 *	Chat weigeren.
	 */
	weigeren: function(waid) {
		if (waid > 0) {
			//new RaboChat.Popup('adviseur_chat_weigeren.php?waid=' + waid, 1, 1);
			location.href = 'weigeren.php?waid=' + waid;
		}
	},
	
	/**
	 *	Wachtrij refresh.
	 */
	checkWachtrij: function(sessionID, status) {
		if (sessionID != "") {	
		
			// Wanneer chat session nog niet bestaat aanmaken.
			if (this.chatSessionID == null) {
				this.chatSessionID = sessionID;
			}
			this.chatSessionStatus = status;
						
			// Controleren of we door mogen of dat we moeten wachten.
			if (this.chatSessionStatus == 2) {
				// Start chat client.
				location.href = 'chat.php?waid=' + this.chatSessionID + '&section=client';
			}else if (this.chatSessionStatus == 3) {
				alert("De chat is beeindigd door de tegenpartij.");
			}else if (this.chatSessionStatus == 4) {
				Geweigerd();
			}else{
				// Om een aantal seconden kijken of een gebruiker al kan gaan chatten.
				setTimeout("RaboChat.statusWachtrij()", this.wachtrijCheckTime);
			}
		}
	},
	
	/**
	 *	AJAX, haal de status van de wachtrij op.
	 */
	statusWachtrij: function() {
		new Ajax.Request('/services/status_wachtrij.php?bank=' + bank + '&waid=' + this.chatSessionID, { method:'get', onSuccess: this.succesWachtrij, onFailure: this.failureWachtrij}); 
	},
	
	/**
	 *	AJAX, wanneer succesvol de status is opgehaald hier wat mee doen.
	 */
	succesWachtrij: function(response) {	
		if (response.responseText != "") {
			if (parseInt(response.responseText)) {
				this.chatSessionStatus = parseInt(response.responseText);
			}
		}
		RaboChat.checkWachtrij(this.chatSessionID, this.chatSessionStatus);
	},
		
	/**
	 *	AJAX, er is iets misgegaan bij het controleren van de status.
	 */
	failureWachtrij: function() {
		alert("Error: Wachtrij kan niet gecontroleerd worden.");
	},
		
	/**
	 *	Wachtrij refresh.
	 */
	refreshWachtrij: function(wachtrij) {
		if (wachtrij) {
			
			// Om een aantal seconden de lijst updaten.
			setTimeout("RaboChat.refreshWachtrij(wachtrij)", this.wachtrijRefreshTime);
			
			// Wachtrij updaten.
			wachtrij.location.reload();
		}
	},
	
	/**
	 *	Controleren of een chat beeindigd is.
	 */
	end: function(sessionID) {
		
		// Wanneer chat session nog niet bestaat aanmaken.
		if (this.chatSessionID == null) {
			this.chatSessionID = sessionID;
		}
		
		setTimeout("RaboChat.checkEnd(this.chatSessionID)", this.wachtrijRefreshTime);
	},
	
	/**
	 *	Ajax request om te controleren of een chat beindigd is.
	 */
	checkEnd: function() {
		new Ajax.Request('/services/status_chat.php?bank=' + bank + '&waid=' + this.chatSessionID, { method:'get', onSuccess: this.handleEnd, onFailure: null}); 
	},
	
	/**
	 *	Response afhandelen.
	 */
	handleEnd: function(response) {
		// Wanneer hij leeg is dan gewoon doorgaan.
		if (response.responseText == "") {
			RaboChat.end();
		}else{
			Stop();
		}
	}
};

/**
 *	Dit object haalt het aantal wachtende op.
 */
RaboChat.Wachtende = {

	refreshTime: 600,
	div: null,
	adviseur: null,
	
	get: function() {
		setTimeout("RaboChat.Wachtende.request()", this.refreshTime);
	},
	
	request: function() {
		new Ajax.Request('/services/aantal_wachtende.php?bank=' + bank + '&adid=' + this.adviseur, { method:'get', onSuccess: this.handle, onFailure: this.failure}); 
	},
	
	handle: function(response) {
		if (response.responseText != "") {
			if (parseInt(response.responseText) == 1) {
				// Het aantal wachtende tonen.
				document.getElementById('aantalwachtende').innerHTML = 'Er is nog <strong>' + response.responseText + '</strong> wachtende voor u.'; 
			} else if (parseInt(response.responseText) > 1) {
				// Het aantal wachtende tonen.
				document.getElementById('aantalwachtende').innerHTML = 'Er zijn nog <strong>' + response.responseText + '</strong> wachtende voor u.'; 
			} else{
				// Aantal wachtende weer resetten naar niets.
				document.getElementById('aantalwachtende').innerHTML = '';
			}
		}
		RaboChat.Wachtende.get();
	},
	
	failure: function() {
		RaboChat.Wachtende.get();
	}
};

/**
 *	Adviseurs ophalen.
 */
RaboChat.Adviseurs = {
	
	/**
	 *	Variabele
	 */
	divOnline: null,
	divOffline: null,
	historyOnline: null,
	historyOffline: null,
	refreshTime: 5000, // 5 sec. Kan gewijzigd worden bij het aanmaken van object.
	
	/**
	 *	Refresh overzicht.
	 */ 
	refresh: function() {
		setTimeout("RaboChat.Adviseurs.request()", this.refreshTime);
	},
	
	/**
	 *	Request
	 */
	request: function() {
		if ((this.divOnline != null) && (this.divOffline != null)) {
			if(bank == 5){ // bernheze categorieën
				new Ajax.Request('/services/update_adviseurs.php?bank=' + bank + '&cat=' + cat, { method:'get', onSuccess: this.handle, onFailure: this.failure}); 					
			} else {
				new Ajax.Request('/services/update_adviseurs.php?bank=' + bank, { method:'get', onSuccess: this.handle, onFailure: this.failure}); 	
			}

		}
	},
	
	/**
	 *	Handle
	 */	
	handle: function(response) {

		if (response.responeText != "") {	
			
			// Build innerHTML op.
			var innerHTMLon = "";
			var innerHTMLoff = "<table border='0' cellpadding='10' cellspacing='0'>";
			var originHTMLon = RaboChat.Adviseurs.divOnline.innerHTML;
			var originHTMLoff = RaboChat.Adviseurs.divOffline.innerHTML;
			var adviseurs_online = 0;
			
			// Alle adviseurs in een array zetten.
			var data = response.responseText.split("|");
			for(i = 0; i < data.length; i++) {
				if (data[i] != "") {
					
					var dealer = data[i].split(";");
					if (dealer[4] == "1") {
						adviseurs_online = 1;
						cls = 'list_even';
						if (i % 2 == 0) {
							cls = 'list_oneven';
						}
						
						// Icon
						if (dealer[3] == "") {
							dealer[3] = "/img/foto_leeg.gif";
						} else {
							dealer[3] = "../upload/" + dealer[3];
						}
						
						// Online
						if(dealer[5] == " beschikbaar"){ //  status beschikbaar
							
							innerHTMLon += "<div style=\"float:left;margin-top:5px\"><table border='0' cellpadding='10' cellspacing='0'><tr onclick=\"javascript: Chat(" + dealer[0] + ")\" onmouseout=\"this.className='list_even';\" onmouseover=\"this.className='list_oneven';\" class='list_even'>";
							innerHTMLon += "<td style='width:50px'><img src='" + dealer[3] + "' width='50' height='70' border='0'></td>";
							innerHTMLon += "<td style='width:190px' nowrap><a href='javascript: Chat(" + dealer[0] + ");' class='decorated_n'><strong>" + dealer[1] + "</strong></a><br>Status: " + dealer[5] + "</td>";
							innerHTMLon += "</tr></table>"
							innerHTMLon += "</div>";
						} else { // niet beschikbaar
							innerHTMLon += "<div style=\"float:left;margin-top:5px\"><table border='0' cellpadding='10' cellspacing='0'><tr onclick=\"javascript: NoChat()\"  class='list_even'>";
							innerHTMLon += "<td style='width:50px'><img src='" + dealer[3] + "' width='50' height='70' border='0' class='adviseur_icon_offline'></td>";
							innerHTMLon += "<td style='width:190px' nowrap><a class='decorated_n'><strong>" + dealer[1] + "</strong></a><br>Status: " + dealer[5] + "</td>";
							innerHTMLon += "</tr></table>"
							innerHTMLon += "</div>";
						}
						
						
					}else{
						
						cls = 'list_even_off';
						if (i % 2 == 0) {
							cls = 'list_oneven_off';
						}
						
						// Icon
						if (dealer[3] == "") {
							dealer[3] = "x.gif";
						}
												
						// Offline
						innerHTMLoff += "<tr class='" + cls + "'margin-top:5px\>";
						innerHTMLoff += "<td style='width:50px'><img src='../upload/" + dealer[3] + "' width='50' height='70' border='0' class='adviseur_icon_offline'></td>";
						innerHTMLoff += "<td style='width:190px' nowrap><strong>" + dealer[1] + "</strong><br>Status: offline</td>";
						innerHTMLoff += "</tr><tr><td colspan=2 height=5></tr>"
					}
				}
			}
			innerHTMLon = innerHTMLon + '';
			innerHTMLoff = innerHTMLoff + '</table>';
					
			
			// Weg schrijven naar div.
			
			if(bank == 4){
				if(adviseurs_online == 1){
					if (innerHTMLon != this.historyOnline) {
						RaboChat.Adviseurs.divOnline.innerHTML = "<br><h2>Online</h2>" + innerHTMLon;
						this.historyOnline = innerHTMLon;
					}
				} else {
					RaboChat.Adviseurs.divOnline.innerHTML = "<br><h3>Momenteel zijn alle adviseurs offline</h3><table border='0' cellspacing='2' cellpadding='0' style='margin-left:20px; margin-top:20px'><tr>		<td><img src='/img/x_grijs.gif' width='190' height='1' border='0'></td>	</tr>	<tr>		<td><a href='/venlo/contact/adviesgesprek/' class='decorated'>Bel me terug</a></td>	</tr>	<tr>		<td><img src='/img/x_grijs.gif' width='190' height='1' border='0'></td>	</tr>	<tr>		<td><a href='mailto:wonen@venlo.rabobank.nl' class='decorated'>Stuur een e-mail</a></td>	</tr>	<tr>		<td><img src='/img/x_grijs.gif' width='190' height='1' border='0'></td>	</tr></table><br>";
					this.historyOnline = innerHTMLon;
				}
			} else {
			
				if (innerHTMLon != this.historyOnline) {
					RaboChat.Adviseurs.divOnline.innerHTML = innerHTMLon;
					this.historyOnline = innerHTMLon;
				}
				
				if (innerHTMLoff != this.historyOffline) {
					RaboChat.Adviseurs.divOffline.innerHTML = innerHTMLoff;
					this.historyOffline = innerHTMLoff;
				}
			}
									
			// Opnieuw kijken naar wijzigingen.
			RaboChat.Adviseurs.refresh();
		}
	},
	
	/**
	 *	Failure
	 */
	failure: function() {
		RaboChat.Adviseurs.refresh();
	}
};

/**
 *	Wachtrij
 */
RaboChat.Wachtrij = {
	
	/**
	 *	Variabele
	 */
	simple: false,
	adviseur: null,
	div: null,
	sound: true,
	refreshTime: 1000, // 3 sec. Kan gewijzigd worden bij het aanmaken van object.
	sts: 0,
		
	/**
	 *	Refresh wachtlijst.
	 */
	refreshWachtrij: function() {
		setTimeout("RaboChat.Wachtrij.request()", this.refreshTime);
	},
	
	/**
	 *	Make request
	 */
	request: function() {	
		if (this.div != null) {
			obj = document.getElementById('chkMijnStatus');
			for(i = 0; i < obj.length; i++) {
				if (obj[i].selected == true) {
					this.sts = obj[i].value;
				}
			}
			new Ajax.Request('/services/update_wachtlijst.php?bank=' + bank + '&adid=' + this.adviseur + '&status=' + this.sts, { method:'get', onSuccess: this.handle, onFailure: this.failure}); 
		}
	},
	
	/**
	 *	Handle response
	 */
	handle: function(response){
		// Veld leeg maken.
		var innerHTML = "<table border='0' cellpadding='0' cellspacing='0' width='100%'>";
		var gesprek = 0;
		var data = response.responseText.split("|");
		
		for(i = 0; i < data.length; i++) {
			if (data[i] != "") {
				// Item toevoegen.
				var item = data[i].split(";");
				innerHTML += "<tr><td style='padding:3px;background-color:#fff'>" + item[1] + " om " + item[2] + "<br></td></tr>";
				
				if (RaboChat.Wachtrij.simple == false) {
					innerHTML += "<tr><td style='padding:3px;padding-bottom:5px;border-bottom:1px solid #c4c9cc;background-color:#fff'><a href='javascript:StartChat(" +  item[0] + ");'>Accepteer</a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript: WeigerChat(" +  item[0] + ");'>Weiger</a></td></tr>";
					gesprek = 1;
				}
			}
		}
		
		// Table sluiten.
		innerHTML += "</table>";
		RaboChat.Wachtrij.div.innerHTML = innerHTML;
		
		// Geluid afsprelen.
		RaboChat.Wachtrij.isAdviseurAvailable();
		
		// POP up weergeven
		if(alerter == 0 && gesprek == 1){
			alert("Er is een inkomend gesprek.");
			alerter = 1;
		}
	
		// Opnieuw controleren.
		RaboChat.Wachtrij.refreshWachtrij();
	},
	
	/**
	 *	Controleren of de adviseur aanwezig is en of hij in gesprek is.
	 */
	isAdviseurAvailable: function() {
		new Ajax.Request('/services/adviseur_available.php?bank=' + bank + '&adid=' + this.adviseur, { method:'get', onSuccess: this.isAdviseurAvailableHandle, onFailure: null}); 
	},
	
	/**
	 *	Afhandelen.
	 */
	isAdviseurAvailableHandle: function(response) {
		if (response.responseText != "") {
			RaboChat.Wachtrij.playSound();
		}
	},
	
	/**
	 *	Play sound.
	 */
	playSound: function() {
		if (RaboChat.Wachtrij.sound == true) {
			// Create flash object.
			var so = new SWFObject("/swf/sound.swf", "sound_movie", "1", "1", "8", "#336699");
	   		so.addParam("quality", "high");
			so.addParam("play", "true");
			so.addParam("loop", "false");
			so.addVariable("sound", "/sounds/geluid.mp3");
	
			// Play sound.
			so.write("sounddiv");
		}
	},
	
	/**
	 *	Failure
	 */
	failure: function() {
		// Opnieuw controleren.
		RaboChat.Wachtrij.refreshWachtrij();
	}
};

/**
 *	RaboChat Messages.
 */
Messages = {
	
	/**
	 *	Variabele
	 */
	div: null,
	btn: null,
	section: null,
	sessionID: null,
	requestTime: 600, // 3 Sec. Kan overschreven worden met eigen instelling.
	
	/**
	 *	Haal berichten op voor client of adviseur.
	 */
	get: function() {
		setTimeout("Messages.request()", this.requestTime);
	},
	
	/**
	 *	Bericht versturen.
	 */
	send: function(message, name) {
		message = this.replaceText(message);
		new Ajax.Request('/services/set_messages.php?bank=' + bank + '&waid=' + Messages.sessionID + '&section=' + Messages.section + '&message=' + escape(message) + '&name=' + name, { method:'get', onSuccess: this.sendHandle, onFailure: this.sendFailure}); 		
	},
	
	replaceText: function(m) {
		var temp_m = m;
		while(temp_m.indexOf('€') > -1) {
			temp_m = temp_m.replace('€', '&euro;');
		}
		return temp_m;
	},

	/**
	 *	Na het plaatsen direct nieuwe berichten ophalen.
	 */
	sendHandle: function() {
	},
	
	/**
	 *	Fout bij het plaatsen van het bericht.
	 */
	sendFailure: function() {
	},
	
	/**
	 *	Do een AJAX request.
	 */
	request: function() {
		new Ajax.Request('/services/get_messages.php?bank=' + bank + '&waid=' + Messages.sessionID + '&section=' + Messages.section, { method:'get', onSuccess: this.handle, onFailure: this.failure}); 
	},
	
	/**
	 *	Handle request af.
	 */
	handle: function(response) {
	
		// Wanneer er berichten zijn doorgaan.
		if (response.responseText != "") {
		
			// Berichten array.			
			var berichten = response.responseText.split("|");
			
			// Berichten verwerken.	
			for(i = 0; i < (berichten.length - 1); i++) {
				if (berichten[i] != ""){ 
					
					// Bericht.
					var bericht = berichten[i].split("^^");
					if (bericht[4] != "client") {
						bericht[4] = "adviseur";
					}
					var html = "<div class='sender_" + bericht[4] + "'>" + bericht[0] + "</div>";
					html += "<div class='message_" + bericht[4] + "'>" + bericht[1] + "</div>";
					
					temp = Messages.div.innerHTML;
					Messages.div.innerHTML = html + temp;
				}
			}
		}
		
		// Doorgaan met het berichten ophalen.
		Messages.get();
	},
		
	/**
	 *	Error
	 */
	failure: function() {
		// Continu.
		Messages.get();
	}	
};

/**
 *	RaboChat popup.
 */
RaboChat.Popup = Class.create();
RaboChat.Popup.prototype = {
	
	/**
	 *	Variabelen
	 */
	popHeight: null,
	popWidth: null,
	popUrl: null,
	popup: null,
	popupID: Math.floor((Math.random()*5)),

	/**
	 *	Initialize
	 */
	initialize: function(url, w, h) {
		
		// Width and Height.
		this.popWidth = w;
		this.popHeight = h;
		this.popUrl = url;
		
		// Popup verwijderen.
		if (this.popup) {
			this.destroy();
		}
		
		// Create popup and set focus.
		window.open(url, this.popupID, "width=" + this.popWidth + ",height=" + this.popHeight + ",scrollbars=no,toolbar=no,resizable=no,left=" + this.left() + ",top=" + this.top()).focus();	
	},
	
	/**
	 *	Calculate left.
	 */
	left: function() {
		return ((screen.width / 2) - (this.popWidth / 2));
	},
	
	/**
	 *	Calculate top.
	 */	
	top: function() {
		return ((screen.height / 2) - (this.popHeight / 2));
	}
}
