﻿var windowFocus = true;
var username;
var chatHeartbeatCount = 0;
var minChatHeartbeat = 5000;
var maxChatHeartbeat = 33000;
var chatHeartbeatTime = minChatHeartbeat;
var originalTitle;
var blinkOrder = 0;

var chatboxFocus = new Array();
var newMessages = new Array();
var newMessagesWin = new Array();
var chatBoxes = new Array();

$(document).ready(function(){ //alert('hiiiiiii');
	originalTitle = document.title;
	startChatSession();

	$([window, document]).blur(function(){
		windowFocus = false;
	}).focus(function(){
		windowFocus = true;
		document.title = originalTitle;
	});
});

function restructureChatBoxes() {
	align = 0;
	for (x in chatBoxes) {
		chatboxtitle = chatBoxes[x];

		if ($("#chatbox_"+chatboxtitle).css('display') != 'none') {
			if (align == 0) {
				$("#chatbox_"+chatboxtitle).css('right', '20px');
			} else {
				width = (align)*(225+7)+20;
				$("#chatbox_"+chatboxtitle).css('right', width+'px');
			}
			align++;
		}
	}
}

function chatWith(chatuser,profilePic)
{
//alert(profilePic);
	OpenChatBox(chatuser,profilePic);
}

function createChatBox(chatboxtitle,minimizeChatBox) { 
	if ($("#chatbox_"+chatboxtitle).length > 0) {
		if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
			$("#chatbox_"+chatboxtitle).css('display','block');
			restructureChatBoxes();
		}
		$("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
		//alert("1");
		return;
		//alert("2");
	}

	$(" <div />" ).attr("id","chatbox_"+chatboxtitle)
	.addClass("chatbox")
	.html('<div class="chatboxhead"><div class="chatboxtitle" style="padding-left:5px;font-weight:bold"> '+chatboxtitle+' </div> <div class="chatboxoptions"><a href="javascript:void(0)" onclick="javascript:toggleChatBoxGrowth(\''+chatboxtitle+'\')" style="letter-spacing:-2px;">--</a> <a href="javascript:void(0)" onclick="javascript:closeChatBox(\''+chatboxtitle+'\')">X</a></div><br clear="all"/></div> <div class="chatboxhead2">    <div style="background-color:#DEF3CF;">  </div><div style="top:1px;position:relative;"> <img src=http://avt.mobizill.com/'+minimizeChatBox+'_small.jpg alt="" style="height: 50px; width: 50px;border:solid 1px #fff;margin-left:5px" /> <div style="color:#000;font-size:9px;"> <a href="javascript:void(0)" onclick="javascript:ClearChatHistory(\''+chatboxtitle+'\')" style="color:#B61483;margin-left:9px" > Clear Chat History </a> </div> </div></div>    <div class="chatboxcontent"></div><div class="chatboxinput"> <img src="http://www.mobizill.com/ChatImg/ChatIconFile.jpg" alt="" /> <textarea class="chatboxtextarea" onkeydown="javascript:return checkChatBoxInputKey(event,this,\''+chatboxtitle+'\');"></textarea></div>')
	.appendTo($( "body" ));
			   
	$("#chatbox_"+chatboxtitle).css('bottom', '0px');
	
	chatBoxeslength = 0;

	for (x in chatBoxes) {
		if ($("#chatbox_"+chatBoxes[x]).css('display') != 'none') {
			chatBoxeslength++;
		}
	}

	if (chatBoxeslength == 0) {
		$("#chatbox_"+chatboxtitle).css('right', '20px');
	} else {
		width = (chatBoxeslength)*(225+7)+20;
		$("#chatbox_"+chatboxtitle).css('right', width+'px');
	}
	
	chatBoxes.push(chatboxtitle);

	if (minimizeChatBox == 1) {
		minimizedChatBoxes = new Array();

		if ($.cookie('chatbox_minimized')) {
			minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
		}
		minimize = 0;
		for (j=0;j<minimizedChatBoxes.length;j++) {
			if (minimizedChatBoxes[j] == chatboxtitle) {
				minimize = 1;
			}
		}
         
		if (minimize == 1) {
			$('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
			$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
		}
	}

	chatboxFocus[chatboxtitle] = false;

	$("#chatbox_"+chatboxtitle+" .chatboxtextarea").blur(function(){
		chatboxFocus[chatboxtitle] = false;
		$("#chatbox_"+chatboxtitle+" .chatboxtextarea").removeClass('chatboxtextareaselected');
	}).focus(function(){
		chatboxFocus[chatboxtitle] = true;
		newMessages[chatboxtitle] = false;
		$('#chatbox_'+chatboxtitle+' .chatboxhead').removeClass('chatboxblink');
		$("#chatbox_"+chatboxtitle+" .chatboxtextarea").addClass('chatboxtextareaselected');
	});

	$("#chatbox_"+chatboxtitle).click(function() {
		if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') != 'none') {
			$("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
		}
	});

	$("#chatbox_"+chatboxtitle).show();
}




function createChatBox3(chatboxtitle,minimizeChatBox) { 
       
	if ($("#chatbox_"+chatboxtitle).length > 0) {
		if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
			$("#chatbox_"+chatboxtitle).css('display','block');
			restructureChatBoxes();
		}
		
		$("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
		return;
	}

	$(" <div />" ).attr("id","chatbox_"+chatboxtitle)
	.addClass("chatbox")
	
	
	.html('<div class="chatboxhead"><div class="chatboxtitle" style="padding-left:5px;font-weight:bold"> '+chatboxtitle+' </div> <div class="chatboxoptions"><a href="javascript:void(0)" onclick="javascript:toggleChatBoxGrowth(\''+chatboxtitle+'\')" style="letter-spacing:-2px;">--</a> <a href="javascript:void(0)" onclick="javascript:closeChatBox(\''+chatboxtitle+'\')">X</a></div><br clear="all"/></div> <div class="chatboxhead2">    <div style="background-color:#DEF3CF;">  </div><div style="top:1px;position:relative;"> <img src=http://avt.mobizill.com/'+minimizeChatBox+'_small.jpg alt="" style="height: 50px; width: 50px;border:solid 1px #fff;margin-left:5px" /> <div style="color:#000;font-size:9px;"> <a href="javascript:void(0)" onclick="javascript:ClearChatHistory(\''+chatboxtitle+'\')" style="color:#B61483;margin-left:9px" > Clear Chat History </a> </div> </div></div>    <div class="chatboxcontent"></div><div class="chatboxinput"> <img src="http://www.mobizill.com/ChatImg/ChatIconFile.jpg" alt="" /> <textarea class="chatboxtextarea" onkeydown="javascript:return checkChatBoxInputKey(event,this,\''+chatboxtitle+'\');"></textarea></div>')
	.appendTo($( "body" ));
			   
	$("#chatbox_"+chatboxtitle).css('bottom', '0px');
	
	chatBoxeslength = 0;

	for (x in chatBoxes) {
		if ($("#chatbox_"+chatBoxes[x]).css('display') != 'none') {
			chatBoxeslength++;
		}
	}

	if (chatBoxeslength == 0) {
		$("#chatbox_"+chatboxtitle).css('right', '20px');
	} else {
		width = (chatBoxeslength)*(225+7)+20;
		$("#chatbox_"+chatboxtitle).css('right', width+'px');
	}
	
	chatBoxes.push(chatboxtitle);

	if (minimizeChatBox == 1) {
		minimizedChatBoxes = new Array();

		if ($.cookie('chatbox_minimized')) {
			minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
		}
		minimize = 0;
		for (j=0;j<minimizedChatBoxes.length;j++) {
			if (minimizedChatBoxes[j] == chatboxtitle) {
				minimize = 1;
			}
		}
         
		if (minimize == 1) {
			$('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
			$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
		}
	}

	chatboxFocus[chatboxtitle] = false;

	$("#chatbox_"+chatboxtitle+" .chatboxtextarea").blur(function(){
		chatboxFocus[chatboxtitle] = false;
		$("#chatbox_"+chatboxtitle+" .chatboxtextarea").removeClass('chatboxtextareaselected');
	}).focus(function(){
		chatboxFocus[chatboxtitle] = true;
		newMessages[chatboxtitle] = false;
		$('#chatbox_'+chatboxtitle+' .chatboxhead').removeClass('chatboxblink');
		$("#chatbox_"+chatboxtitle+" .chatboxtextarea").addClass('chatboxtextareaselected');
	});

	$("#chatbox_"+chatboxtitle).click(function() {
		if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') != 'none') {
			$("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
		}
	});

	$("#chatbox_"+chatboxtitle).show();
}

function createChatBox2(chatboxtitle,minimizeChatBox) { //alert(minimizeChatBox);
    
    
	if ($("#chatbox_"+chatboxtitle).length > 0) {
		if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
			$("#chatbox_"+chatboxtitle).css('display','block');
			restructureChatBoxes();
		}
		$("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
		return;
	}
	$(" <div />" ).attr("id","chatbox_"+chatboxtitle)
	.addClass("chatbox")
	.html('<div class="chatboxhead"><div class="chatboxtitle" style="padding-left:5px;font-weight:bold"> Online friends </div> <div class="chatboxoptions"><a href="javascript:void(0)" onclick="javascript:toggleChatBoxGrowthPopUp(\''+chatboxtitle+'\')" style="letter-spacing:-2px;">--</a> </div><br clear="all"/></div> <div class="chatboxhead2">    <div style="top:1px;position:relative;">   </div></div>    <div class="chatboxcontent2"> '+minimizeChatBox+' </div>')
	.appendTo($( "body" ));
			   
	$("#chatbox_"+chatboxtitle).css('bottom', '0px');
	
	chatBoxeslength = 0;

	for (x in chatBoxes) {
		if ($("#chatbox_"+chatBoxes[x]).css('display') != 'none') {
			chatBoxeslength++;
		}
	}

	if (chatBoxeslength == 0) {
		$("#chatbox_"+chatboxtitle).css('right', '20px');
	} else {
		width = (chatBoxeslength)*(225+7)+20;
		$("#chatbox_"+chatboxtitle).css('right', width+'px');
	}
	
	chatBoxes.push(chatboxtitle);

	if (minimizeChatBox == 1) {
		minimizedChatBoxes = new Array();

		if ($.cookie('chatbox_minimized')) {
			minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
		}
		minimize = 0;
		for (j=0;j<minimizedChatBoxes.length;j++) {
			if (minimizedChatBoxes[j] == chatboxtitle) {
				minimize = 1;
			}
		}
         
		if (minimize == 1) {
			$('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
			$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
		}
	}

	chatboxFocus[chatboxtitle] = false;

	$("#chatbox_"+chatboxtitle+" .chatboxtextarea").blur(function(){
		chatboxFocus[chatboxtitle] = false;
		$("#chatbox_"+chatboxtitle+" .chatboxtextarea").removeClass('chatboxtextareaselected');
	}).focus(function(){
		chatboxFocus[chatboxtitle] = true;
		newMessages[chatboxtitle] = false;
		$('#chatbox_'+chatboxtitle+' .chatboxhead').removeClass('chatboxblink');
		$("#chatbox_"+chatboxtitle+" .chatboxtextarea").addClass('chatboxtextareaselected');
	});

	$("#chatbox_"+chatboxtitle).click(function() {
		if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') != 'none') {
			$("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
		}
	});

	$("#chatbox_"+chatboxtitle).show();
}


function ChatListBox(chatboxtitle,minimizeChatBox) {  

$("#chatbox_Chat .chatboxcontent").html('<div  ><span >hiiii</span></div>');



var chatFriendsList="";

	if ($("#chatbox_"+chatboxtitle).length > 0) { 
		if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
			$("#chatbox_"+chatboxtitle).css('display','block');
			
			restructureChatBoxes();
		}
		return;
	}
	$(" <div />" ).attr("id","chatbox_"+chatboxtitle)
	.addClass("chatbox")
	.html('<div class="chatboxhead"><div class="chatboxtitle" style="padding-left:5px;font-weight:bold"> Online friends </div> <div class="chatboxoptions"><a href="javascript:void(0)" onclick="javascript:toggleChatBoxGrowth(\''+chatboxtitle+'\')" style="letter-spacing:-2px;">--</a> <a href="javascript:void(0)" onclick="javascript:closeChatBox(\''+chatboxtitle+'\')">X</a></div><br clear="all"/></div>   </div> </div>    <div class="chatboxcontent" style="padding-left:10px">'+minimizeChatBox+'</div>')
	.appendTo($( "body" ));
			   
	$("#chatbox_"+chatboxtitle).css('bottom', '0px');
	
	chatBoxeslength = 0;

	for (x in chatBoxes) {
		if ($("#chatbox_"+chatBoxes[x]).css('display') != 'none') {
			chatBoxeslength++;
		}
	}

	if (chatBoxeslength == 0) {
		$("#chatbox_"+chatboxtitle).css('right', '20px');
	} else {
		width = (chatBoxeslength)*(225+7)+20;
		$("#chatbox_"+chatboxtitle).css('right', width+'px');
	}
	
	chatBoxes.push(chatboxtitle);

	if (minimizeChatBox == 1) {
		minimizedChatBoxes = new Array();

		if ($.cookie('chatbox_minimized')) {
			minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
		}
		minimize = 0;
		for (j=0;j<minimizedChatBoxes.length;j++) {
			if (minimizedChatBoxes[j] == chatboxtitle) {
				minimize = 1;
			}
		}
         
		if (minimize == 1) {
			$('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
			$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
		}
	}

	chatboxFocus[chatboxtitle] = false;

	$("#chatbox_"+chatboxtitle+" .chatboxtextarea").blur(function(){
		chatboxFocus[chatboxtitle] = false;
		$("#chatbox_"+chatboxtitle+" .chatboxtextarea").removeClass('chatboxtextareaselected');
	}).focus(function(){ 
		chatboxFocus[chatboxtitle] = true;
		newMessages[chatboxtitle] = false;
		$('#chatbox_'+chatboxtitle+' .chatboxhead').removeClass('chatboxblink');
		$("#chatbox_"+chatboxtitle+" .chatboxtextarea").addClass('chatboxtextareaselected');
	});

	$("#chatbox_"+chatboxtitle).click(function() {
		if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') != 'none') {
			$("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
		}
	});

	$("#chatbox_"+chatboxtitle).show();
}


function PostChatPopUpBuild(chatPopUpString)
{

$("#chatbox_Chat .chatboxcontent").html('<div ><span >'+chatPopUpString+'</span></div>');

}

var ssssssss;
function chatHeartbeat() {   //alert('fg');
    
	var itemsfound = 0;
	
	if (windowFocus == false) {
 
		var blinkNumber = 0;
		var titleChanged = 0;
		for (x in newMessagesWin) {
			if (newMessagesWin[x] == true) {
				++blinkNumber;
				if (blinkNumber >= blinkOrder) {
					//document.title = x+' says...'; 
					titleChanged = 1;
					break;	
				}
			}
		}
		
		if (titleChanged == 0) {
			document.title = originalTitle;
			blinkOrder = 0;
		} else {
			++blinkOrder;
		}

	} else {
		for (x in newMessagesWin) { 
			newMessagesWin[x] = false;
		}
	}

	for (x in newMessages) { 
		if (newMessages[x] == true) { 
			if (chatboxFocus[x] == false) {
				//FIXME: add toggle all or none policy, otherwise it looks funny
				$('#chatbox_'+x+' .chatboxhead').toggleClass('chatboxblink');
			}
		}
	}
	
	$.ajax({
	  type: "POST",
	  url: "RSSReader.asmx/CreateUserChatDataListObj",
	  data: "{}",
	  contentType: "application/json; charset=utf-8",
	  cache: false,
	  dataType: "json",
	  error: function(response)
	  {
	      //alert(response.responseText);
	      var chatList = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
	   },
	  success: function(response)
	  {//alert(response.responseText);
	  if(response != null)
	  {
	    if(response.d !='null')
	    {
		//username = response.username;
       
         var cars = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
         if(cars != null)
         {
        for (var i = 0; i < cars.length; i++) {  //alert(cars[i].f+'-hiiiiii');
                
                 chatboxtitle = cars[i].f;
                 //alert($("#chatbox_"+chatboxtitle).length);
				if ($("#chatbox_"+chatboxtitle).length <= 0) { //alert('h');
				   if(cars[i].isChatOpen=='Y')
				   {   
                        //alert('ff');
                        var pPic=cars[i].friendPic;
                        createChatBox(chatboxtitle,pPic);
                        if(cars[i].toggleStatus=='N')
				        {
				           //createChatBox(chatboxtitle,pPic);
				    	   //$('#chatbox_'+chatboxtitle).css('height','30px');  
				    	}
				    	else
				    	{
				    	     //createChatBox(chatboxtitle,pPic);
				    	}
    		       }
				}
				if(cars[i].isChatOpen=='Y')
				   { 
				        if(cars[i].toggleStatus=='N')
				        {
				            if ($("#chatbox_"+chatboxtitle).css('display') == 'none') { 
					          $("#chatbox_"+chatboxtitle).css('display','block');
					            restructureChatBoxes();
					        }
				        }
				}
				
				if (cars[i].s == 1) {
				  //alert(cars[i].chatPopUp)
				  var cpu=cars[i].chatPopUp
				  //alert(cpu);
				  if(cpu=='Y')
				  {
					var zzzzzzzzz="";
					zzzzzzzzz=cars[i].m;
					//alert(zzzzzzzzz);
					createChatBox2("Chat",zzzzzzzzz);
					$("#chatbox_Chat .chatboxcontent2").html('<div class="chatboxmessage" style="height:300px;overflow-y:scroll;"><span class="chatboxmessagecontent"> '+zzzzzzzzz+' </span></div>');
					if ($("#chatbox_Chat").css('display') == 'none') { 
					   $("#chatbox_Chat").css('display','block');
					restructureChatBoxes();
				  }
				  }
				  
				  if(cpu=='N')
				  {
				    
				    //alert('yyyyy');
				       createChatBox2("Chat","");
				       //$("#chatbox_Chat .chatboxcontent2").html('<div class="chatboxmessage" ><span class="chatboxmessagecontent"> hii </span></div>');
				       //alert($('#chatbox_Chat .chatboxcontent2').css('display'));
				       if($('#chatbox_Chat .chatboxcontent2').css('display')=='block')
				       {
				          ExecToggleChatBoxGrowthPopUp("Chat");
				       }
				  
				}
				}

				if (cars[i].s == 2) { 
				
					$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+cars[i].m+'</span></div>');
				} else {  
				   
				   //alert('aa');
				   if(cars[i].isChatOpen=='Y')
				   { 
				   
				     if(cars[i].toggleStatus=='Y')
				     {
				     //$('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') == 'none'
				     
				       if($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display')=='block')
				       {
				          ExecToggleChatBoxGrowth(chatboxtitle);
				       }
				     }
					newMessages[chatboxtitle] = true;
					newMessagesWin[chatboxtitle] = true;
					
					if(cars[i].chatAlert=="Y")
					{
					     document.title = cars[i].f+' says...';
					}
					if(cars[i].soundAlert=="Y")
					{
					     playSound('http://www.mobizill.com/ChatSound/ChatSound.mp3');
					}
					if(cars[i].toggleStatus=='N')
				    {
					    $("#chatbox_"+chatboxtitle+" .chatboxcontent").html('<div class="chatboxmessage" ><span class="chatboxmessagecontent">'+cars[i].m+'</span></div>');
					    restructureChatBoxes();
					}
				   }
				}
                if(cars[i].isChatOpen=='Y') 
  			    { //toggleChatBoxGrowth(chatboxtitle);
  			      if(cars[i].toggleStatus=='N')
				  {
				      $("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
				      itemsfound += 1;
				  }
				}
        }
           
		 }
		 }
		for (i=0;i<chatBoxes.length;i++) { 
			chatboxtitle = chatBoxes[i];
			if(chatboxtitle=='Chat')
			{
			  $("#chatbox_"+chatboxtitle+" .chatboxcontent2").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent2")[0].scrollHeight);
			}
			else
			{
			 //alert(chatboxtitle);
			 if(cars[i].toggleStatus=='N')
			 {
			     $("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
			 }
			}
		}
		chatHeartbeatCount++;
               
		if (itemsfound > 0) {
			chatHeartbeatTime = minChatHeartbeat;
			chatHeartbeatCount = 1;
		} else if (chatHeartbeatCount >= 10) {
			chatHeartbeatTime *= 2;
			chatHeartbeatCount = 1;
			if (chatHeartbeatTime > maxChatHeartbeat) {
				chatHeartbeatTime = maxChatHeartbeat;
			}
		}
		
		setTimeout('chatHeartbeat();',chatHeartbeatTime);
		
		}
		else
		{
		  //alert('session out');
		}
	}});
}

function closeChatBox(chatboxtitle) {  
    
	$.post("chat.aspx?action=closechat", { chatbox: chatboxtitle} , function(data){ 
	chatHeartbeat();
	});
   // $('#chatbox_'+chatboxtitle).css('display','none'); 
  document.getElementById('chatbox_'+chatboxtitle).style.display="none";
}

function ChangeStatus(chatboxtitle,uStatus)
 {
	$.post("chat.aspx?action=ChangeStatus", { chatbox: chatboxtitle, status: uStatus} , function(data){ 
	chatHeartbeat();
	});
}


function ClearChatHistory(chatboxtitle) 
{ 
	$('#chatbox_'+chatboxtitle).css('display','none'); 
	$.post("chat.aspx?action=ClearChatHistory", { chatbox: chatboxtitle} , function(data){ 
	chatHeartbeat();chatHeartbeat();
	});
}

function BringChatPopUp(chatboxtitle) 
{ 

	$.post("chat.aspx?action=ClearChatHistory", { chatbox: chatboxtitle} , function(data){ 
	
	});
}

function OpenChatBox(chatboxtitle,profilePic) { //alert(profilePic);
	$.post("chat.aspx?action=openchat", { chatbox: chatboxtitle} , function(data){ //alert(data);
	if(data=='Y')
	{
	   chatHeartbeat();
	}
	else
	{
	  createChatBox(chatboxtitle,profilePic);
	  $("#chatbox_"+chatuser+" .chatboxtextarea").focus();
	}   
	});
}


function toggleChatBoxGrowth(chatboxtitle) { 

    //$('#chatbox_'+chatboxtitle).css('height','30px'); 
    $.post("chat.aspx?action=ToggleChatBox", { chatbox: chatboxtitle} , function(data){ //alert(data);
	if(data=='Y')
	{
	   chatHeartbeat();
	}
	else
	{
	  createChatBox(chatboxtitle,profilePic);
	  $("#chatbox_"+chatuser+" .chatboxtextarea").focus();
	}   
	});
	
	
    
	if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') == 'none') {  
		
		var minimizedChatBoxes = new Array();
		
		if ($.cookie('chatbox_minimized')) {
			minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
		}

		var newCookie = '';

		for (i=0;i<minimizedChatBoxes.length;i++) {
			if (minimizedChatBoxes[i] != chatboxtitle) {
				newCookie += chatboxtitle+'|';
			}
		}

		newCookie = newCookie.slice(0, -1)


		$.cookie('chatbox_minimized', newCookie);
		$('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','block');
		$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','block');
		$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
	} else {
		
		var newCookie = chatboxtitle;

		if ($.cookie('chatbox_minimized')) {
			newCookie += '|'+$.cookie('chatbox_minimized');
		}


		$.cookie('chatbox_minimized',newCookie);
		$('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
		$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
	}
	
}




function ExecToggleChatBoxGrowth(chatboxtitle) { 

	if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') == 'none') {  
		  
		var minimizedChatBoxes = new Array();
		  
		if ($.cookie('chatbox_minimized')) { 
			minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
	      
		}
          
		var newCookie = '';
          
		for (i=0;i<minimizedChatBoxes.length;i++) { 
			if (minimizedChatBoxes[i] != chatboxtitle) { 
				newCookie += chatboxtitle+'|';
				
			}
		}

		newCookie = newCookie.slice(0, -1)

         
		$.cookie('chatbox_minimized', newCookie);
		$('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','block');
		$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','block');
		$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
	} else {
		
		var newCookie = chatboxtitle;

		if ($.cookie('chatbox_minimized')) {
			newCookie += '|'+$.cookie('chatbox_minimized');
		}


		$.cookie('chatbox_minimized',newCookie);
		$('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
		$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
	}
	
}

function toggleChatBoxGrowthPopUp(chatboxtitle) { 

    $.post("chat.aspx?action=ChatPopUp", { chatbox: chatboxtitle} , function(data){ 
	if(data=='Y')
	{
	
	}
	else
	{
	
	}   
	});
    
	if ($('#chatbox_'+chatboxtitle+' .chatboxcontent2').css('display') == 'none') {  
		$('#chatbox_'+chatboxtitle+' .chatboxcontent2').css('display','block');	
        $('#chatbox_'+chatboxtitle+' .chatboxcontent2').html("<img src='http://img.mobizill.com/loader.gif' style='float:left;'/>");
		var minimizedChatBoxes = new Array();
		
		if ($.cookie('chatbox_minimized')) {
			minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
		}

		var newCookie = '';

		for (i=0;i<minimizedChatBoxes.length;i++) {
			if (minimizedChatBoxes[i] != chatboxtitle) {
				newCookie += chatboxtitle+'|';
			}
		}

		newCookie = newCookie.slice(0, -1)


		$.cookie('chatbox_minimized', newCookie);
		$('#chatbox_'+chatboxtitle+' .chatboxcontent2').css('display','block');
		$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','block');
		$("#chatbox_"+chatboxtitle+" .chatboxcontent2").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent2")[0].scrollHeight);
	} else {
		
		var newCookie = chatboxtitle;

		if ($.cookie('chatbox_minimized')) {
			newCookie += '|'+$.cookie('chatbox_minimized');
		}


		$.cookie('chatbox_minimized',newCookie);
		$('#chatbox_'+chatboxtitle+' .chatboxcontent2').css('display','none');
		$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
	}
	
}



function ExecToggleChatBoxGrowthPopUp(chatboxtitle) { 

     
	if ($('#chatbox_'+chatboxtitle+' .chatboxcontent2').css('display') == 'none') {  
		
		var minimizedChatBoxes = new Array();
		
		if ($.cookie('chatbox_minimized')) {
			minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
		}

		var newCookie = '';

		for (i=0;i<minimizedChatBoxes.length;i++) {
			if (minimizedChatBoxes[i] != chatboxtitle) {
				newCookie += chatboxtitle+'|';
			}
		}

		newCookie = newCookie.slice(0, -1)


		$.cookie('chatbox_minimized', newCookie);
		$('#chatbox_'+chatboxtitle+' .chatboxcontent2').css('display','block');
		$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','block');
		$("#chatbox_"+chatboxtitle+" .chatboxcontent2").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent2")[0].scrollHeight);
	} else {
		
		var newCookie = chatboxtitle;

		if ($.cookie('chatbox_minimized')) {
			newCookie += '|'+$.cookie('chatbox_minimized');
		}


		$.cookie('chatbox_minimized',newCookie);
		$('#chatbox_'+chatboxtitle+' .chatboxcontent2').css('display','none');
		$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
	}
	
}

function checkChatBoxInputKey(event,chatboxtextarea,chatboxtitle) { 
	 
	if(event.keyCode == 13 && event.shiftKey == 0)  { 
	    username="";
	    
		message = $(chatboxtextarea).val();
		message = message.replace(/^\s+|\s+$/g,"");
        
		$(chatboxtextarea).val('');
		$(chatboxtextarea).focus();
		$(chatboxtextarea).css('height','14px');
		if (message != '') {  
			$.post("chat.aspx?action=sendchat", {to: chatboxtitle, message: message} , function(data){   chatHeartbeat();
				message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
				$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+username+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+message+'</span></div>');
				$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
			});
		}
		chatHeartbeatTime = minChatHeartbeat;
		chatHeartbeatCount = 1;

		return false;
	}

	var adjustedHeight = chatboxtextarea.clientHeight;
	var maxHeight = 94;

	if (maxHeight > adjustedHeight) {
		adjustedHeight = Math.max(chatboxtextarea.scrollHeight, adjustedHeight);
		if (maxHeight)
			adjustedHeight = Math.min(maxHeight, adjustedHeight);
		if (adjustedHeight > chatboxtextarea.clientHeight)
			$(chatboxtextarea).css('height',adjustedHeight+8 +'px');
	} else {
		$(chatboxtextarea).css('overflow','auto');
	}
}

function startChatSession(){  //alert('yuiu');
	$.ajax({
	  type: "POST",
	  url: "RSSReader.asmx/CreateUserChatDataListObj",
	  data: "{}",
	  contentType: "application/json; charset=utf-8",
	  cache: false,
	  dataType: "json",
	  error: function(response)
	  { //alert(response.responseText);
	      var chatList = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
	   },
	  success: function(response)
	  {
		for (i=0;i<chatBoxes.length;i++) { 
		   
			chatboxtitle = chatBoxes[i];
			if(chatboxtitle=='Chat')
			{
			  $("#chatbox_"+chatboxtitle+" .chatboxcontent2").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent2")[0].scrollHeight);
			}
			else
			{
			$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
			}
			setTimeout('$("#chatbox_'+chatboxtitle+' .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);', 100); // yet another strange ie bug
		}
	setTimeout('chatHeartbeat();',chatHeartbeatTime);
		chatHeartbeat();
	}});
}




function playSound(soundfile) 
{
//    document.getElementById("soundArea").innerHTML=
//    "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
}

function SoundForNewMessage(uStatus)
 {
	$.post("chat.aspx?action=UpdateSoundForNewMessage", { status: uStatus} , function(data){ 
	chatHeartbeat();
	});
}
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
