//window onload
jQuery(document).ready(function(){
	//Send message
	jQuery('#chat_content').keydown(function(event){
		if(event.ctrlKey && event.keyCode==13){
			//you can not enter more than 4 lines for message
			if(jQuery(this).val().split("\n").length > max_input_line -1){
				alert('Message size has to be less than 4 lines of words') ;
				return false ;
			}
			
			jQuery(this).attr('value', jQuery(this).val() + "\n ");
			jQuery(this).focus();
		}else if(event.keyCode == 13){
			send_chat_content('chat_content') ;
			return false ;
		}
	}) ;
	
	//Get message and online list
	if(myNick != ''){
		query_online_user_list() ;
		get_chat_content() ;
		//Set timer
		chat_message_time = setInterval('query_online_user_list()', update_online_interval) ;
		online_time = setInterval('get_chat_content()', update_chat_message) ;
	}else{
		//get the guest number 
		if(is_empty(get_cookie('guest_name'))){
			jQuery.ajax({
				url:'chat_query_online_guest_list.php?'+Math.random(),
				type:'GET',
				dataType:'html',
				success:function(data)
				{
					var num = parseInt(eval(data)) ;
					myNick = 'Guest' + (num+1) ;
					set_cookie('guest_name', myNick) ;
					
					//query 
					query_online_user_list() ;
					get_chat_content() ;
					//Set timer
					chat_message_time = setInterval('query_online_user_list()', update_online_interval) ;
					online_time = setInterval('get_chat_content()', update_chat_message) ;
				}
			}) ;
		}else{
			myNick = get_cookie('guest_name') ;
			//query 
			query_online_user_list() ;
			get_chat_content() ;
			//Set timer
			chat_message_time = setInterval('query_online_user_list()', update_online_interval) ;
			online_time = setInterval('get_chat_content()', update_chat_message) ;
		}
	}
	
	var face = '<a href="javascript:insert_face(\'ok\', \'chat_content\')"><img src="images/chat/face/ok.gif" title="/:ok:"/></a>' ;
	face += '<a href="javascript:insert_face(\'good\', \'chat_content\')"><img src="images/chat/face/good.gif" title="/:good:"/></a>' ;
	face += '<a href="javascript:insert_face(\'sad\', \'chat_content\')"><img src="images/chat/face/sad.gif" title="/:sad:"/></a>' ;
	face += '<a href="javascript:insert_face(\'smile\', \'chat_content\')"><img src="images/chat/face/smile.gif" title="/:smile:"/></a>' ;
	face += '<a href="javascript:insert_face(\'giggle\', \'chat_content\')"><img src="images/chat/face/giggle.gif" title="/:giggle:"/></a>' ;
	//face += '<a href="javascript:"><img src="images/chat/invite_bt_bg.jpg" width="46" height="19"/></a>' ;
	//chat face
	jQuery('#chat_face').append(face) ;
}) ;

function query_online_user_list(){
	jQuery.ajax({
		url:'chat_query_online_user_list.php?user_id='+Math.random()+'&myNick=' + myNick+'&chat_mode='+chat_mode+'&chat_room='+chat_room+'&myID='+myID,
		type:'GET',
		dataType:'html',
		success:function(data)
		{
			//clear data
			if(is_empty(data)){return ;}
			jQuery('#profile_box').hide() ;
			jQuery('#online').empty() ;
			
			//Data process
			var online = eval(data) ;
			var new_user = null ;
			var left_user = null ;
			for(var i=0;i<online.length;i++){
				var div = '<li style="border-bottom:1px dotted #ccc ; height:20px ;">' ;
                div += '<span style="float:left">'+online[i]+'</span>' ;

				if(online[i].match(/guest/i)){
					if(online[i] != myNick){
						//private chat
                    	//div += '<span style="float:right">Me</span>' ;
					}else{
						div += '<span style="float:right;color:#000">Me</span>' ;
					}
				}else{
					if(online[i] == myNick){
						//private chat
						div += "<span style='float:right'>" ;
						div += "<img src='images/chat/show_profile.gif' align='absmiddle' onclick=\"show_full_profile('" + online[i]+ "') ;\" style='cursor:pointer' title=\"Show "+online[i]+"'s profile\"/>" ;
						div += "</span>" ;
					}else{
						//private chat
						div += "<span style='float:right'>" ;
						div += "<img src='images/chat/show_profile.gif' align='absmiddle' hspace='2' onclick=\"show_full_profile('" + online[i]+ "') ;\" style='cursor:pointer' title=\"Show "+online[i]+"'s profile\"/>" ;
						div += "<img src='images/chat/friend.gif' align='absmiddle' onclick=\"invite('" + online[i]+ "') ;\" style='cursor:pointer' title=\"Invite "+online[i]+" as your friend\"/>" ;
						div += "</span>" ;
					}
				}
				div +=  '</li>' ;

				jQuery('#online').append(div) ; 
				
			}//for	
			//The users just into chatroom
			if(old_online.length > 0 && online.length > 0){
				var new_online = get_new_online(online, old_online) ;
				for(var i=0;i<new_online.length;i++){
					jQuery('#chat_message').prepend('<div style="color:red;border-bottom:1px dotted #ccc ;margin-bottom:2px ;"><img src="images/chat/broadcast.gif" hspace=5/><b>'+new_online[i]+'</b> goes into this chatroom</div>' ) ; 
					//Mark the message line with new_online user as showing
					jQuery('.msg_'+new_online[i]).fadeIn('slow') ;
				}
			}
			//The users just left chatroom
			if(old_online.length > 0 && online.length > 0){
				var left_online = get_left_online(old_online, online) ;
				for(var i=0;i<left_online.length;i++){
					jQuery('#chat_message').prepend('<div style="color:red ;border-bottom:1px dotted #ccc ;margin-bottom:2px ;"><img src="images/chat/broadcast.gif" hspace=5/><b>'+left_online[i]+'</b> have left this chatroom</div>' ) ; 
					//Mark the message line with left_online user as hidden
					jQuery('.msg_'+new_online[i]).fadeOut('slow') ;
				}
			}
			//Save old data
			old_online = online.slice(0) ;
			old_online.length = online.length ;
		}//success
	}) ;
}

//Send my chat content
function send_chat_content(){
	if(is_empty(jQuery('#chat_content').val())){return ;}
	jQuery('#chat_loading').show() ;
	jQuery.ajax({
		url:'get_chat_message.php?chat_mode='+chat_mode+'&chat_room='+chat_room+'&myID='+myID,
		type:'POST',
		data:'myNick='+myNick+'&msg='+ jQuery('#chat_content').attr('value') + '&topID='+topID, 
		dataType:'html',
		success:function(data)
		{
			jQuery('#chat_loading').hide() ;
			jQuery('#chat_content').attr('value',' ') ;
			jQuery('#chat_content').focus() ;
			/*
			var tmp = eval('('+data+')') ;
			if(!is_empty(tmp.topID) && !is_empty(tmp.msg)){
				if(topID == 0){jQuery('#chat_message').html('') ;}
				topID = tmp.topID ;
				jQuery('#chat_message').prepend(tmp.msg) ;
			}
			*/
		}
	}) ;
}

//Get all public chat content
function get_chat_content(){
	jQuery.ajax({
		url:'get_chat_message.php?chat_mode='+chat_mode+'&chat_room='+chat_room+'&myID='+myID+'&login_time='+login_time,
		type:'POST',
		data:'myNick='+myNick + '&topID=' + topID , 
		dataType:'html',
		success:function(data)
		{
			if(data == null || data == ''){return ;}
			var tmp = eval('(' + data + ')') ;
			if(topID == 0){jQuery('#chat_message').html('') ;}
			if(!is_empty(tmp.topID) && !is_empty(tmp.msg)){
				topID = tmp.topID ;
				jQuery('#chat_message').prepend(tmp.msg) ;
			}
		}
	}) ;
}

//Insert face picture into text field
function insert_face(face, id){
	jQuery('#'+id).setCaret();
	jQuery('#'+id).insertAtCaret('/:'+face+':'); 
	jQuery('#'+id).focus() ;
}

function clear_content(id){
	jQuery('#' + id).attr('value',' ') ;
	jQuery('#' + id).focus() ;
}

function show_full_profile(nick){
	//jQuery('#container').css({visibility:'hidden'}) ;
	if(myNick.match(/guest/i)){
		alert('This functionality is only available for members !') ;
	}else{
		jQuery('#show_full_profile').attr('title', nick + "' profile") ;
		jQuery('#show_full_profile').attr('href', 'show_profile2.php?username='+nick+'&FB_iframe=true&width=650&height=460').click() ;
	}
}

function invite(nick){
	if(myNick.match(/guest/i)){
		alert('This functionality is only available for members !') ;
	}else{
		if(!confirm('Do you want to send invitation?')){return false ;}
		jQuery('#sending_invitation').show() ;
		
		jQuery.ajax({
			url:'v_send_invitation2.php',
			type:'POST',
			data:'username='+nick, 
			dataType:'html',
			success:function(data)
			{
				jQuery('#sending_invitation').hide() ;
				if(data == 'OK'){
					alert('Send invitation successfully !') ;
				}else{
					alert(data) ;
					//alert('Send failure, please refresh this page and try again !') ;
				}
			},
			error:function()
			{
				jQuery('#sending_invitation').hide() ;
				alert('Send failure, please check your network !') ;
			}
		}) ;
	}
}

//Í¨ÓÃº¯Êý
function in_online_array(element, online){
	for(var i=0;i<online.length;i++){
		if(element == online[i]){
			return true ;
		}
	}
	
	return false ;
}

function get_new_online(new_online, old_online){
	var _new = new Array() ;
	var k = 0 ;
	for(var i=0;i<new_online.length;i++){
		if(!in_online_array(new_online[i], old_online)){
			_new[k++] = new_online[i];
		}
	}//for
	
	return _new ;
}

function get_left_online(old_online, new_online){
	var _left = new Array() ;
	var k = 0 ;
	for(var i=0;i<old_online.length;i++){
		if(!in_online_array(old_online[i], new_online)){
			_left[k++] = old_online[i] ;
		}
	}//for
	
	return _left ;
}

