// scrollToBottom                        -  40;
// chat.debug                            -  70;
// chat.addFriend                        -  230;
// chat.removeFriend                     -  80;
// chat.getFriend                        -  93;
// chat.buildFriends                     - 113;
// chat.getNewMessages                   - 130;
// chat.receiveMessage                   - 145;
// chat.sendmsg                          - 220;
// chat.chatwith                         - 243;
// chat.chatFocus                        - 196;
// chat.openProfile_window               - 202;
// chat.init                             - 208;
// chat.getRecent                        - 251;
// chat.getBrowser                       - 275;
// chat.loadBrowser                      - 296;
// chat.buildbrowser                     - 309;
// chat.DoRequest_new                    - 326;
// chat.GetRespons_new                   - 334;
// chat.buildHistory_new                 - 346;
// chat.getHistory_new                   - 353;
// chat.getUserid                        - 363;
// chat.loadUserid                       - 368;
// chat.getFilter                        - 418; 
// chat.buildFilter                      - 434;
// chat.preparehtml                      - 445;
// chat.showLoading                      - 452;
// chat.getFilter                        - 418;

// chat.openVideoChatWindow

var chat = 
{
	ajaxURL: '/ajax/ajaxchat.php',
	user_id: null,
	user_to:null,
	//	online: 'online',
	new_filter: 0,
	user_name: null,
	params:null,
	view:0,
	view_class_left:'icons list_pic active',
	view_class_right:'icons list',
	http_request:null,
	referenceNode_history:null,
	user_sex_id: null,
	user_location_id: null,	
	user_online:null,
	user_wp:null,
	page: 1,
	perpage: 5,
	userresult: null,
	htmlprepared: 0,
	pagescache: Array(),
	chatcache: Array(),
	chathistory: null,
	focustotar: null,
	msgresult: null,
	username: null,
	user_name: null,
	unread_messages: 0,
	unread_messages_buffer: 0,
	unread_messages_flag: 0,
	friends: Array(),
	blocked: Array(),
	buffer: Array(),
	firstid: Array(),
	getNewMessages_from_timer: Array(),
	cookie_now: '',
	videoChatWindow: false,

	debug:function(msg)//display messages for programs stages
	{
		if (msg == '' ) return;
		$('debug').innerHTML += msg;	
		$('debug').innerHTML += '<BR />';	
	},//end debug:function(msg)

	getChatWindows: function()
	//get cookie and build new popups for every one individual chat
	{
		this.getChatCookie();
		if (this.cookie_now!=null && this.cookie_now!='')
		{
			var brokenstring = this.cookie_now.split("/////");
			for (var ii=0; ii<brokenstring.length; ii++)
			{
				if (this.cookie_now!=null && this.cookie_now!='') 
				{
					var cookie_info=brokenstring[ii].split("-----");
					this.chatwith(cookie_info[0], cookie_info[1], 1);
					$('chatrcvd'+cookie_info[0]).innerHTML = '<span style="font-size:12px;">Loading messages now...</span>';
					this.getLastMessages(cookie_info[0]);
				}
			}
		}
	},//end getChatWindows: function()
		
	getLastMessages: function(id)
	{
		var the_div = $('chatrcvd'+id);
		new Ajax.Request(this.ajaxURL ,{ method: 'post', parameters: 'getLastMessages_from=1&id='+id, onCreate: function() {the_div.update('Loading....');},
			onSuccess: function(transport) {the_div.update(transport.responseText);}
			});
	},//end getLastMessages: function(id)
		
	getChatCookie: function()
	//get info for open chat windows
	{
		this.cookie_now = Get_Cookie('chat');
	},//end getChatCookie: function()
		
	setChatCookie: function(id, name)
	//put into chat cookie whene open new window
	{
		this.getChatCookie();
		if (this.cookie_now==null) this.cookie_now='/';
		if (this.cookie_now.indexOf(id)=='-1')
		{
			var cookie_new = this.cookie_now+'/////'+id+'-----'+name;
			cookie_new = cookie_new.replace(/null/,'');//delete null from start/end cookie
			cookie_new = cookie_new.replace(/^\/+/,'');//delete //// from start/end cookie
			if ( this.cookie_now!=null) Set_Cookie('chat', cookie_new, '', '/', '', '');
				else Set_Cookie('chat', id+'------'+name, '', '/', '', '');
		}
	},// setChatCookie: function(id, name)
		
	delChatCookie: function(e)
	//delete chat cookie info for closed windows
	{
		chat.getChatCookie();
		if (chat.cookie_now!=null )
		{
			var cookie_new='';
			var brokenstring=chat.cookie_now.split("/////");
			var id=e.getId();
			id = id.replace(/chat/,'');
			for (i=0; i<brokenstring.length; i++)
			{
				if (brokenstring[i].indexOf(id)=='-1') cookie_new += "/////"+brokenstring[i];
			}
			cookie_new = cookie_new.replace(/null/,'');//delete null from start/end cookie
			cookie_new = cookie_new.replace(/^\/+/,'');//delete //// from start/end cookie
			Set_Cookie('chat', cookie_new, '', '/', '', '');
			clearInterval(chat.getNewMessages_from_timer[id]);
			return false;
		}
	},//end delChatCookie: function(e)
		
	block_user: function(id)
	//block selected user from id
	{
		var flag = confirm ('Na blokeren ontvangt u geen berichten meer van deze gebruiker! Doorgaan met blokeren?');
		if (flag)
			{
				new Ajax.Request(this.ajaxURL, { parameters:'addblock=1&id='+id, onSuccess: this.block_user_ok });	
			}
		return false;
	},//end block_user: function(id)
		
	block_user_ok: function()
	{
		chat.getBlocked();
	},

	getUnreaded_messages: function()
	{
		var id = this.user_id;
		if (id!=0 && id!=null)
		{
			new Ajax.Request(this.ajaxURL, { parameters:'get_unreaded_messages=1&id='+id,	onSuccess: this.sendUnread_messages });
		}
		return false;
	},//end getUnreaded_messages: function()
	
	sendUnread_messages: function(transport)
	{
		this.unread_messages = eval('(' + transport.getResponseHeader('X-JSON') + ')');
		if (this.unread_messages>chat.unread_messages_buffer)
		{
			chat.unread_messages_buffer = this.unread_messages;
			//	chat.playMusic('cowbell.wav');
		}
		$('unread_messages').innerHTML = this.unread_messages;
		return false;
	},//end sendUnread_messages: function(transport)
	
/*					
	playMusic: function(music_file)//start sound file in embed - in index.tpl under body
		{
			$('music').innerHTML = '<embed id="soundeffect" hidden=true loop=1 src="music/'+music_file+'" type="application/x-mplayer2"></embed>';
		},
*/
		
	removeBlocked: function(id,username) 
		{
			new Ajax.Request(this.ajaxURL, { parameters:'delblocked=1&id='+id, onSuccess: function(transport) {chat.getBlocked();} });		
		},
		
	getBlocked: function()//get blocked from chat_blocked table 
		{
			new Ajax.Request(this.ajaxURL, { parameters:'getblocked=1',	onSuccess: function(transport) 
				{ 
					temp = eval('(' + transport.getResponseHeader('X-JSON') + ')');
					chat.blocked = Array();

					temp.each(function(result) 
						{
							chat.blocked[result['id']] = Array();
							chat.blocked[result['id']]['username']=result['username'];
							chat.blocked[result['id']]['status']=result['logged'];	
						});
					chat.buildBlocked();
				} });		
		},

	buildBlocked: function()//built chatbox_blockedResult div
		{
			temphtml='';
			tempf = this.blocked;
			for (i = 0; i< tempf.length ; i++) 
				{
					if (tempf[i] == undefined ) continue; 
						else result= tempf[i];
					if (result['status'] == 1) status = 'online'; 
						else status='offline';
					tempvars = { id: i, chatbox: chat.chatbox, username: result['username'], status: status}
					temphtml += chat.blockedTemplate.evaluate(tempvars);
				}
			$(chat.chatbox+'_blockedResult').innerHTML = temphtml;
		}, 

	addFriend: function(id,username) 
		{
			if (this.friends[id] == undefined) 
				{
					new Ajax.Request(this.ajaxURL, { parameters:'addfriend=1&id='+id, onSuccess: function(transport) {chat.getFriends();}	});		
				}
		},

	removeFriend: function(id,username) 
		{
			new Ajax.Request(this.ajaxURL, { parameters:'delfriend=1&id='+id, onSuccess: function(transport) {chat.getFriends();} });		
		},
		
	allUsers: function()
	{
		new Ajax.Request(this.ajaxURL, { parameters:'getallusers=1', onSuccess: function(transport) 
		{ 
			var chatbox_newinthechatResult = document.getElementById('chatbox_newinthechatResult');
			chatbox_newinthechatResult.innerHTML = transport.responseText;

		} });
		
	},
		
	getFriends: function()//get friends from friends table 
		{
//						chat.debug('Build friends');

			new Ajax.Request(this.ajaxURL, { parameters:'getfriends=1',	onSuccess: function(transport) 
				{ 
//								chat.debug('Receive friends');
					temp = eval('(' + transport.getResponseHeader('X-JSON') + ')');
					chat.friends = Array();

					temp.each(function(result) 
						{
							chat.friends[result['id']] = Array();
							chat.friends[result['id']]['username']=result['username'];
							chat.friends[result['id']]['status']=result['logged'];	
						});
					chat.buildFriends();
				} });		
		},

	buildFriends: function()//built chatbox_userlistResult div
		{
			temphtml='';
			tempf = this.friends;
			for (i = 0; i< tempf.length ; i++) 
				{
					if (tempf[i] == undefined ) continue; 
						else result= tempf[i];
					if (result['status'] == 1) status = 'online'; 
						else status='offline';
					tempvars = { id: i, chatbox: chat.chatbox, username: result['username'], status: status}
					temphtml += chat.friendsTemplate.evaluate(tempvars);
				}
			$(chat.chatbox+'_userlistResult').innerHTML = temphtml;
		}, 
		
	getNewMessages: function() 
		{
			new Ajax.Request(this.ajaxURL, {parameters:'getmsg=1&',	onSuccess: function(transport) 
				{
					chat.msgresult = eval('(' + transport.getResponseHeader('X-JSON') + ')');
					chat.msgresult.each(function(result) 
						{
							chat.receiveMessage( result['from'], Base64.decode(result['msg']), result['username'], result['id'] );
//										chat.addFriend(result['from'], result['username']);
						}); } 
				});
//						this.debug('get_new_message');
		}, 
		
	receiveMessage: function(id,msg,username,msgid)
	{
		if (this.firstid[id] == null) {this.firstid[id] = msgid;}
		if (this.chatcache[id] == undefined) 
		{
			this.setChatCookie(id, username);//update chat cookie for new oper Window
			this.chatcache[id] = new Window('chat'+id, {title: 'chat with '+username, width: 600, height: 280,  resizable: false, maximizable: false, onFocus: chat.chatFocus, onClose:chat.delChatCookie } ); 
			this.chatcache[id].getContent().innerHTML = this.chatTemplate.evaluate({id: id});	
			this.chatcache[id].username = name;
		}
		
		Stamp = new Date(); 
		var h = String(Stamp.getHours()); 
		var m = String(Stamp.getMinutes()); 
		var s = String(Stamp.getSeconds());
		h = (h.length > 1) ? h : "0"+h; m = (m.length > 1) ? m : "0"+m;
		$('chatrcvd'+id).innerHTML += '<span style="color:#504E7C; font-size:12px;">['+h+':'+m+'] '+username+': '+msg+'</span><br />';
		this.chatcache[id].show();		
		setTimeout("scrollToBottom('chatrcvd"+id+"')", 125);
	},//end receiveMessage: function(id,msg,username,msgid)
		
	getNewMessages_from: function( id ) 
		{
			new Ajax.Request(this.ajaxURL, {method:'post', parameters:'getmsg_from=1&from='+id, onSuccess: function(transport) 
				{
					chat.msgresult = eval('(' + transport.getResponseHeader('X-JSON') + ')');
					chat.msgresult.each(function(result) 
						{
							chat.receiveMessage( result['from'], Base64.decode(result['msg']), result['username'], result['id'] );
						}); } 
				});
		},
		
	sendmsg: function(e,ta,id) 
		{
			if (this.username==null) this.username='Mijn';//If chat individual from not chat page

			if (e.keyCode == 13) 
				{
					Stamp = new Date(); 
					var h = String(Stamp.getHours()); 
					var m = String(Stamp.getMinutes()); 
					var s = String(Stamp.getSeconds());
					h = (h.length > 1) ? h : "0"+h; m = (m.length > 1) ? m : "0"+m;
					$('chatrcvd'+id).innerHTML += '<span style="color:#663333; font-size:12px;">['+h+':'+m+'] '+this.username+': '+ta.value+'</span><br />';
					setTimeout("scrollToBottom('chatrcvd"+id+"')", 125);
					msg = ta.value;
					temp = Base64.encode(msg);
					new Ajax.Request(this.ajaxURL, {parameters:'sendmsg=1&id='+id+'&msg='+escape(temp), onSuccess: function(transport) {eval(transport.responseText);} });
//								this.addFriend(id, this.chatcache[id].username);
					ta.value = '';
					ta.focus();
					return false;
				}
		},

	chatwith: function(id, name, status)
	// open new windwos if you go to chat with some users
	{
		this.setChatCookie(id, name);
		if (status==1) this.getNewMessages_from_timer[id] = setInterval("chat.getNewMessages_from("+id+");", 5000);
		
		if (this.chatcache[id] == undefined) 
			{
				this.chatcache[id] = new Window ('chat'+id, {title: 'chat with '+name, width: 600, height: 280,  resizable: true, maximizable: false, onFocus: chat.chatFocus, onClose:chat.delChatCookie } ); 
				this.chatcache[id].getContent().innerHTML = this.chatTemplate.evaluate({id: id});	
				this.chatcache[id].username = name;
			}
		this.chatcache[id].show();	
		this.chatcache[id].toFront();	
	},//end chatwith: function(id, name, status)

	chatFocus:function(e)
	{
		chat.focustotar = $('chatta'+e.getId().substr(4));
		setTimeout("chat.focustotar.focus()",150);
	},//end chatFocus:function(e)
		
	openProfile_window:function(profile_id)
	//open new window with user_profile
	{
		window.open('http://dutchcontacts.nl/index.php?task=adview&id='+profile_id+'&profileview=profile&chat_profile=1','profile','height=500, width=520, left=100, top=100, resizable=no, scrollbars=yes, toolbar=no, status=yes');
		return false;
	},//end openProfile_window:function(profile_id)

	init:function(divobj, full, chat_with_id, chat_with_name)
	{
		if (full==1)
		//open all windows in chat system
		{
			//	this.debug('Init');
			this.loading = new Template('<div id="#{chatbox}_loading"><img src="images/loading.gif">Loading ...</div>');
			this.browserTemplate = new Template('<div id="#{chatbox}_browser"><div class="list_icons"><a  id="list_icons_left" href="#" onclick="chat.getView (\'1\'); return false;" class="'+chat.view_class_right+'"> </a><a href="#" id="list_icons_right" onclick="chat.getView(\'0\'); return false;" class="'+chat.view_class_left+'"> </a><span class="title">View: </span></div><div class="browser_pages"><span><a href="#" onclick="return chat.first();">&laquo; eerste</a></span><span><a href="#" onclick="return chat.prev();">&lsaquo; terug</a></span><span><a href="#" onclick="return chat.next();">vooruit &rsaquo;</a></span><span><a href="#" onclick="return chat.last();">laatste &raquo;</a></span></div> <div id="#{chatbox}_browser_result"></div></div><div id="#{chatbox}_userlist_top"><span><a href="" onclick="return   chat.viewAll();"></a></span><span><a href="" onclick="return chat.buildFilter();">zoeken</a></span><span><a href="" onclick="return chat.getHistory_new();">history</a></span></div><div class="browser_pages"><span><a href="#" onclick="return chat.first();">&laquo; eerste</a></span><span><a href="#" onclick="return chat.prev();">&lsaquo; terug</a></span><span><a href="#" onclick="return chat.next();">vooruit &rsaquo;</a></span><span><a href="#" onclick="return chat.last();">laatste &raquo;</a></span></div>');	
			this.browserResultTemplate = new Template('<div class="browser_result"><div class="title">#{name}<span><a href="#" onclick="chat.addFriend(#{id});return false;"><img src="/images/chatbox/status_friends.jpg" border="0" alt="Add friend" /></a><a href="#" onclick="chat.chatwith(#{id},\'#{name}\');return false;"><img src="/images/chatbox/chat.jpg" border="0"></a></span></div><div class="vcouter"><div class="vcinner"><div class="vcmid"><a href="#" onclick="chat.chatwith(#{id},\'#{name}\');return false;"><img src="/pics/#{picture}" /></a></div></div></div><div class="details"><span class="#{status}">#{status}</span> | <span>#{location}</span> | <span>#{years} jaar  | <span>#{sex}</span></span></div><div class="info">#{info}<br /><br /><a href="#" onclick="chat.openProfile_window(#{id});">klik hier voor uitgebreid profiel</a></div><div class="clr" style=\"text-align:right;\" ></div></div>');
			
			this.browserTemplate_ = new Template ('<div>Chat_userlisting here:</div>');
			this.browserResultTemplate_ = new Template('<div class="browser_result"><table><tr><td style="font-size:14px; width:100px; font-weight:bold;">#{name}</td><td style="width:130px;"><a href="#" onclick="chat.addFriend(#{id});return false;"><img src="/images/chatbox/status_friends.jpg" border="0" alt="Add friend" /></a></td><td  style="width:70px;"><a href="#" onclick="chat.chatwith(#{id},\'#{name}\');return false;"><img src="/images/chatbox/chat.jpg" border="0"></a></td><td class="details" style="width:250px;"><span class="#{status}"> #{status}</span> | <span>#{location}</span> | <span>#{years} jaar  | #{sex} </span><td><a href="#" onclick="chat.openProfile_window(#{id});">Profiel</a></td></table></div><div class="clr" style=\"text-align:right;\" ></div></div>');
			
			this.chatTemplate = new Template('<div style="text-align:right; margin-right:15px;"><a href="#" onclick="return chat.openVideoChatWindow();">Nieuw Video Chat carrousel!</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="chat.block_user(#{id}); return false;">Blokkeer gebruiker!</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="chat.openProfile_window(#{id});">Bekijk profiel!</a></div><div style="background:#FFFFFF URL(/images/chatbox/chatbg_left.jpg) repeat-y left;margin:5px;padding-left:2px;"><div style="height:190px; background:transparent URL(/images/chatbox/chatbg_top.jpg) repeat-x top;overflow-y:scroll;padding:5px;text-align:left;font-size:12px;font-weight:bold;" id="chatrcvd#{id}"></div></div><div style="margin:5px;"><textarea style="height:40px; width:586px !important;" onkeyPress="return chat.sendmsg(event,this,#{id})" id="chatta#{id}"></textarea></div>');
			this.friendsTemplate = new Template('<div class="friends_entry" style="width:300px;"><img src="/images/chatbox/status_#{status}.jpg" /><span class="username"><a href="#" onclick="chat.chatwith(#{id},\'#{username}\');return false;" >#{username}</a></span><a href="#" onclick="return chat.getHistory_new(#{id});"><img src="/images/chatbox/status_history.jpg" border="0" alt="view History" /></a><a href="#" onclick="chat.removeFriend(#{id});return false;"><img src="/images/chatbox/status_delete.jpg" border="0" alt="Delete" /></a></div>');
			this.blockedTemplate = new Template('<div class="friends_entry" style="width:300px;"><img src="/images/chatbox/status_#{status}.jpg" /><span class="username"><a href="#" onclick="chat.chatwith(#{id},\'#{username}\');return false;" >#{username}</a></span><a href="#" onclick="return chat.getHistory_new(#{id});"><img src="/images/chatbox/status_history.jpg" border="0" alt="view History" /></a><a href="#" onclick="chat.removeBlocked(#{id});return false;"><img src="/images/chatbox/status_delete.jpg" border="0" alt="Delete" /></a></div>');

			this.chatbox = divobj;
			this.chatboxt = {chatbox:this.chatbox};
			this.showLoading();
//								if (chat_with_id>0 && chat_with_name!=0) this.chatwith(chat_with_id, chat_with_name);
//						this.debug('after loading');
//						this.getBrowser();
			this.getUserid();
			this.viewOnline();
			this.getNewMessages();
			this.getChatWindows();//get chat windows from lasted page 
//								setInterval("chat.getUnreaded_messages();", 5000);
			setInterval("chat.getUnreaded_messages();", 5000);
			setTimeout("chat.getFriends();",1000);
			setTimeout("chat.getBlocked();",1000);
			setTimeout("chat.allUsers();",2000);
			setInterval("chat.getNewMessages();", 10000);
			setInterval("chat.getFriends();", 30000);
			setInterval("chat.getBlocked();", 30000);	
			setInterval("chat.pagescache = Array();", 120000);	
		}
		if (full==0)
		//open chat window for chat only
		{
			this.getUserid();
			this.getUser_name();
			this.chatbox = divobj;
			setInterval("chat.getUnreaded_messages();", 5000);
			this.chatboxt = {chatbox:this.chatbox};
											
			this.chatTemplate = new Template('<div style="text-align:right; margin-right:15px;"><a href="#" onclick="return chat.openVideoChatWindow();">Nieuw Video Chat carrousel!</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="chat.block_user(#{id}); return false;">Blokkeer gebruiker!</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="chat.openProfile_window(#{id});">Bekijk profiel!</a></div><div style="background:#FFFFFF URL(/images/chatbox/chatbg_left.jpg) repeat-y left;margin:5px;padding-left:2px;"><div style="height:190px; background:transparent URL(/images/chatbox/chatbg_top.jpg) repeat-x top;overflow-y:scroll;padding:5px;text-align:left;font-size:10px;font-weight:bold;" id="chatrcvd#{id}"></div></div><div style="margin:5px;"><textarea style="height:40px; width:586px !important;" onkeyPress="return chat.sendmsg(event,this,#{id})" id="chatta#{id}"></textarea></div>');
			this.getChatWindows();//get chat windows from lasted page
		}
	},//end init:function(divobj, full, chat_with_id, chat_with_name)
		
	getRecent: function(id,lastid) 
	{
		$('chatrecent'+id).innerHTML = '';
		new Ajax.Request(this.ajaxURL, {
		parameters:'getrecent=1&id='+id+'&firstid='+this.firstid[id],
		onSuccess: function(transport) 
		{
			recents = eval('(' + transport.getResponseHeader('X-JSON') + ')');
			recents.each(function(result) {
											id = result['from'];
											hostid = result['hostid'];	
											msg = Base64.decode(result['msg']);
											username = result['username'];
											Stamp = new Date(); 
											var h = String(Stamp.getHours()); 
											var m = String(Stamp.getMinutes()); 
											var s = String(Stamp.getSeconds());
											h = (h.length > 1) ? h : "0"+h; m = (m.length > 1) ? m : "0"+m;
											$('chatrecent'+hostid).innerHTML += '<span style="color:#777777;">['+h+':'+m+'] '+username+': '+msg+'</span><br />';
											});
		} });	
	},//end getRecent: function(id,lastid) 

	getBrowser:function()
	{
		if (this.new_filter==1) 
		{
			this.page=1;
			this.new_filter=0;
		}
		//	this.debug('before ajax');
		if (chat.view==0)new Ajax.Request(this.ajaxURL, { parameters:'page='+this.page+'&perpage='+this.perpage+'&online='+this.online+'&user_location_id='+this.user_location_id+'&user_name='+this.user_name+'&user_sex_id='+this.user_sex_id+'&user_wp='+this.user_wp,	onSuccess: this.loadBrowser	});
		if (chat.view==1)
		{
			this.perpage=20;
			new Ajax.Request(this.ajaxURL, { parameters:'page='+this.page+'&perpage='+this.perpage+'&online='+this.online+'&user_location_id='+this.user_location_id+'&user_name='+this.user_name+'&user_sex_id='+this.user_sex_id+'&user_wp='+this.user_wp,	onSuccess: this.loadBrowser	});
		}
	},//end getBrowser:function()
				
	getView: function(view)
	{
		if (view=='0')	
			{
				this.view=0;
				styleView(view);
			}
		if (view=='1')	
			{
				this.view=1;
				styleView(view);							
			}
		this.getBrowser();
	},//end getView: function(view)
		
	failure: function(transport) 
	{
		alert('krache');
		return false;
	},//end failure: function(transport) 

	loadBrowser:function(transport)
	{
		if ( chat.htmlprepared == 0 ) 
			{
				chat.htmlprepared = 1;
				chat.preparehtml();	
			}
		chat.userresult = eval('(' + transport.getResponseHeader('X-JSON') + ')');
		//	chat.debug("JSON RECEIVED");
		eval(transport.responseText);
		chat.buildbrowser();
	},//end loadBrowser:function(transport)

	buildbrowser:function()
	{
		//	this.debug('build browser start');
		temphtml='';
		this.userresult.each(function(result) 
		{
			if (result['logged']) status = 'online'; 
				else status='offline';
			tempvars = { id: result['id'], chatbox: chat.chatbox, picture: result['picture']+'__thb.jpg', name: result['username'], status: status, info: result['info'], location: result['location'], years: result['years'], sex: result['sex']}
			if (chat.view==0) temphtml += chat.browserResultTemplate.evaluate(tempvars);
			if (chat.view==1)  temphtml += chat.browserResultTemplate_.evaluate(tempvars);
			//	temphtml += chat.browserResultTemplate.evaluate(tempvars);
		});
		$(chat.chatbox+'_browser_result').innerHTML = temphtml;
				
		//	if (this.online!=0)	$('chatbox_userlist_top').innerHTML = '<div id="#{chatbox}_userlist_top"><span><a href="" onclick="return   chat.viewAll();"></a></span><span><a href="" onclick="return chat.buildFilter();">zoeken</a></span><span><a href="" onclick="return chat.getHistory_new();">history</a></span></div>';
		//	else $('chatbox_userlist_top').innerHTML = '<div id="#{chatbox}_userlist_top"><span><a href="" onclick="return   chat.viewOnline();">[online >>]</a></span><span><a href="" onclick="return chat.buildFilter();">[zoeken]</a></span><span><a href="" onclick="return chat.getHistory_new();">[history>>]</a></span></div>';
		this.pagescache[this.page] = temphtml;
	},//end buildbrowser:function()
		
	clearHistory_new:function(id)
	//mark history_from=0 and history_to=0 for user session id
	{
		var flag = confirm ('Na \'Clear history\' verliest u alle oude chatberichten! Doorgaan?');
		if (flag)
			{
				if (id=='undefined') Windows.close("history_new_undefined");
					else Windows.close("history_new_"+id);
				
				this.params = 'clearhistory_new=1';
				this.DoRequest_new(this.ajaxURL, this.params);
			}
		return false;
	},//end clearHistory_new:function(id)
		
	DoRequest_new:function (page, params)
	{
		var http_request = GetHttpObject();
		http_request.onreadystatechange = function() { chat.GetRespons_new(http_request); };
		http_request.open('POST',page, true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send(this.params);
	},//end DoRequest_new:function (page, params)
		
	GetRespons_new:function (http_request)
	{
		if (http_request.readyState == 4)
		{
			if (http_request.status == 200)
			{
				var resp = http_request.responseText;
				this.buildHistory_new(resp);
			} 
		}
	},//end GetRespons_new:function (http_request)
		
	buildHistory_new:function(resp)
	{
		this.chathistory_new = new Window('history_new_'+this.user_to, {title: 'history', width: 600, height: 300, resizable: true,destroyOnClose:true, maximizable: false } ); 
		this.chathistory_new.show();
		this.chathistory_new.getContent().innerHTML = resp;
	},//end buildHistory_new:function(resp)
		
	getHistory_new:function(user_to)
	{
		this.user_to=user_to;
		Windows.close("history_new_undefined");
		this.params = 'gethistory_new=1&user_to='+this.user_to;
		this.DoRequest_new(this.ajaxURL, this.params);
		//	this.debug('Ajax request: '+this.ajaxURL+this.params);
		return false;
	},//end getHistory_new:function(user_to)
		
	getUserid:function()
	{
		new Ajax.Request(this.ajaxURL, { parameters:'getuserid=1',	onSuccess: this.loadUserid });	
	},//end getUserid:function()
	
	loadUserid:function(transport)
	{
		this.user_id = chat.user_id = eval('(' + transport.getResponseHeader('X-JSON') + ')');
	},//end loadUserid:function(transport)

	getHistory:function(user_to)
	{
		new Ajax.Request(this.ajaxURL, { parameters:'gethistory=1&user_to='+user_to,	onSuccess: this.loadHistory	});	
		//	this.debug('Ajax request: '+this.ajaxURL+'?gethistory=1&user_to='+user_to);
		return false;
	},//end getHistory:function(user_to)
		
	loadHistory:function(transport)
	{
		if ( chat.htmlprepared == 0 ) 
		{
			chat.htmlprepared = 1;
			chat.preparehtml();	
		}
		//	chat.userresult = eval('(' + transport.getResponseHeader('X-JSON') + ')');
		//	alert (chat.userresult['to']);

		chat.userresult = eval(transport.responseText);
		//	chat.debug("JSON RECEIVED");
		//	alert (chat.userresult);
		chat.buildHistory();
	},//end loadHistory:function(transport)
		
	buildHistory:function()
	{
		//	alert ('history');
		temphtml='';
		this.userresult.each(function(result) 
		{
			if (chat.user_id==result['from'])
			{
				temphtml += '<span style="color:black;">' + result['date'] + '</span><span style="color:red">To:'+result['username_to']+'<b>Message:'+Base64.decode(result['msg'])+'</b></span><span><a href="" onclick="chat.chatwith('+result['to']+',\''+result['username_to']+'\');return false;"><img src="/images/chatbox/chat.jpg" border="0"></a></span><br />';
			}
			else
			{
				temphtml += '<span style="color:black;">' + result['date'] + '</span><span style="color:blue">From:'+result['username_from']+'<b>Message:'+Base64.decode(result['msg'])+'</b></span><span><a href="" onclick="chat.chatwith('+result['from']+',\''+result['username_from']+'\');return false;"><img src="/images/chatbox/chat.jpg" border="0"></a></span><br />'
			}
		});

		if (this.chathistory == undefined) this.chathistory = new Window('history', {title: 'history', width: 400, height: 300, resizable: true, maximizable: false } ); 
		this.chathistory.show();
		this.chathistory.getContent().innerHTML = temphtml;
	},//end buildHistory:function()
		
	getFilter:function(form_data)
	//get filter options from filter tpl and transport to getBrouser function
	{
		this.new_filter=1;
		this.user_name = document.chat_filter.user_name.value;
		this.user_sex_id = document.chat_filter.sex_id.value;
		this.user_location_id = document.chat_filter.location_id.value;

		if(document.chat_filter.user_online.checked==true) this.online='online';
			else this.online=null;
		if(document.chat_filter.user_wp.checked==true) this.user_wp=1;
			else this.user_wp=null;
		
		this.getBrowser();
		return false;
	},//end getFilter:function(form_data)
		
	buildFilter: function()
	//build and display filter tpl
	{
		if (this.filter == undefined) 
		{
			this.filter = new Window ('filter', {title: 'select options', width: 300, height: 200, resizable: true, maximizable: false } ); 
			this.filter.getContent().innerHTML = '<center><form name="chat_filter" action=""><table style="color:red; text-align:left; "><tr style="height:40px;"><td>Profiel naam:</td><td><input type="text" name="user_name" maxlenght="20"></td></tr><tr><td>Sex :</td><td><select name="sex_id" style="width:150px;"><option value="0">all</option><option value="1">man</option><option value="2">vrouw</option><option value="3">TV/TS</option><option value="4">stel</option></select></td></tr><tr>&nbsp;</tr><tr><td>Lokatie:</td><td><select name="location_id" style="width:150px;"><option value="0">Overal</option><option value="1">Antwerpen</option><option value="2">Waals Brabant</option><option value="3">Brussels</option><option value="4">Drente</option><option value="5">Flevoland</option><option value="6">Friesland</option><option value="7">Gelderland</option><option value="8">Groningen</option><option value="9">Henegouwen</option><option value="10">Luik</option><option value="11">Noord-Holland</option><option value="12">Luxemburg</option><option value="13">Namur</option><option value="14">Limburg</option><option value="15">Noord-Brabant</option><option value="16">Oost-Vlaanderen</option><option value="17">Overijssel</option><option value="18">Utrecht</option><option value="19">Vlaams Brabant</option><option value="20">West-Vlaanderen</option><option value="21">Zeeland</option><option value="22">Zuid-Holland</option></select></td></tr><tr>&nbsp;</tr><tr><td>Online:</td><td><input type="checkbox" name="user_online"></td></tr><tr>&nbsp;</tr><tr><td>Met foto:</td><td><input type="checkbox" name="user_wp"></td></tr><tr><td>&nbsp;</td><td style="text-align: right;"><input type="submit" onclick="chat.getFilter(this); return false;" value="Zoeken"></td></tr></table></form></center>';	
		}
		this.filter.show();
		document.chat_filter.user_name.focus();
		return false;
	},//end buildFilter: function()

	preparehtml:function() 
	{
		result = this.browserTemplate.evaluate(this.chatboxt);
		$(this.chatbox).update(this.browserTemplate.evaluate(this.chatboxt));
		//	this.debug('render browserTemplate');
	},//end preparehtml:function() 

	showLoading:function()
	//show gif to the top under loading parameters interval 
	{
		$(this.chatbox).update(this.loading.evaluate(this.chatboxt));
		$(this.chatbox+'_loading').style.display='block';
		//	this.debug('render loading');
	},//end showLoading:function()

	first:function() 
	{
		this.page = 1;	
		this.getBrowser();
		return false;
	},//end first:function() 

	prev:function()
	{
		if (this.page == 1) this.page = this.allpages; else this.page -=1;	
		this.getBrowser();
		return false;
	},//end prev:function()

	next:function()
	{
		if (this.page == this.allpages) this.page =1; else this.page +=1;		
		this.getBrowser();
		return false;
	},//end next:function()
		
	last:function()
	{
		this.page = this.allpages;	
		this.getBrowser();	
		return false;
	},//end last:function()
		
	viewOnline:function()
	{
		this.new_filter=1;
		this.online = 'online';
		this.getBrowser();
		return false;
	},//end viewOnline:function()
		
	viewAll:function()
	{
		this.new_filter=1;
		this.online = 0;
		this.getBrowser();
		return false;
	},//end viewAll:function()
		
	getUser_name:function()
	{
		new Ajax.Request(this.ajaxURL, { parameters:'getusername=1', onSuccess: this.loadUser_name });	
	},//end getUser_name:function()
	
	loadUser_name:function(transport)
	{
		this.user_name = eval('(' + transport.getResponseHeader('X-JSON') + ')');
	}//end loadUser_name:function(transport)
	
	,openVideoChatWindow: function()
	{
		if ( !this.videoChatWindow )//build new window for videochat
		{
			this.videoChatWindow = window.open('http://dutchcontacts.nl/index.php?task=videochat&videochatWindow=1','mywindow','width=820,height=720')	
		}
		else if ((this.videoChatWindow.closed)==true)//build new window for videochat
		{
			this.videoChatWindow = window.open('http://dutchcontacts.nl/index.php?task=videochat&videochatWindow=1','mywindow','width=820,height=720')	
		}
		else//maximize and focus builded before window
		{
			this.videoChatWindow.focus();
		}
		return false;
	}
	
		
}//end var chat =
			
function scrollToBottom(id) 
{
   $(id).scrollTop = $(id).scrollHeight - $(id).clientHeight;
}
			
function displayTrigger(id)
{
	if ($(id).style.display == "none") $(id).setStyle({'display': 'block'});
		else $(id).setStyle({'display': 'none'});
	return false;
}
	
function setVisible(id)
{
	$(id).setStyle({'display': 'block'});
	return false;
}
	
function setUnvisible(id)
{
	$(id).setStyle({'display': 'none'});
	return false;
}

function setUnderline(id)
{
	$(id).setStyle({'text-decoration': 'underline'});	
	return false;
}

function setNounderline(id)
{
	$(id).setStyle({'text-decoration': 'none'});	
	return false;
}
	
function styleView(view)
{
	if (BrowserDetect.browser=="Explorer")//Browser detect is in ajaxinit.js
	{
		if (view==0)
		{
			document.getElementById('list_icons_left').setAttribute("className", "icons list");
			document.getElementById('list_icons_right').setAttribute("className", "icons list_pic active");
		}
		if (view==1)
		{
			document.getElementById('list_icons_left').setAttribute("className", "icons list active");
			document.getElementById('list_icons_right').setAttribute("className", "icons list_pic");
		}
	}
	else
	{
		if (view==0)
		{
			document.getElementById('list_icons_left').setAttribute("class", "icons list");
			document.getElementById('list_icons_right').setAttribute("class", "icons list_pic active");
		}
		if (view==1)
		{
			document.getElementById('list_icons_left').setAttribute("class", "icons list active");
			document.getElementById('list_icons_right').setAttribute("class", "icons list_pic");
		}
	}
}//end function styleView(view



