/* AJAX Interface to ws.php*/
var WS = {};
WS.isPolling = false; /* used for WS.watchDog */
WS.longPoll = null; /* will contain ajax request object */
WS.msgPost = null;
WS.imOnline = false;
WS.url = '/modules/ws.php';

WS.disconnect = function(){
	WS.longPoll.abort();
	var greeting = new Object();
	greeting.imOffline = true;
    $.getJSON(WS.url,greeting,function(theContacts){});
};

WS.connect = function(){
	WS.longPoll.abort();
	WS.getMessages(true);

};


/* calling this method will enable a monitoring system that will periodically check if the poll has failed and restart it */
WS.watchdog = function(){
	//console.log(WS.isPolling);
}

WS.getContacts = function(contactList){
	var greeting = new Object();
//	greeting.imOnline = true;

	if(typeof contactList!= 'undefined')
	{		
		greeting.contactList = contactList;	
	}

	greeting.needContacts = 20;
	$.getJSON(WS.url,greeting,function(theContacts){
		IM.populateBuddyList(theContacts);
//		WS.getMessages();
    });
};

WS.getMessages = function(imOnline,contactID,ignoreLast){
	
	//WS.isPolling = false;
	var greeting = new Object();
	greeting.needMessages = true;
	if(imOnline == true)
	{		
		greeting.imOnline = true;	
	}

	if(typeof contactID != 'undefined')
	{		
		greeting.contactID = contactID;	
	}

	


		if(IM.lastMsg != '')
			{	
				if(WS.imOnline == false)
					{
						greeting.imOnline = true;
					}
				greeting.lastMsg = IM.lastMsg;
			}
		WS.isPolling = true;

		//console.log(greeting);


		WS.longPoll =  $.ajax({
							  	type: "POST",
								url: WS.url,
								data: greeting,
								dataType: "HTML",
								success: function(theMessages){
								
												try
												{  	
													/* try to clean the messages */
													//console.log('success');
													theMessages = $.evalJSON(theMessages,greeting);
													IM.renderMessages(theMessages);												
													if(theMessages!='')
														{
															WS.imOnline = true;
															var L=0;
															for(m in theMessages)
																{
																	L++;	
																}
															
															IM.lastMsg=theMessages[L-1].mes_id;
														}
													//console.log(L);

													
													
												}
												catch(err)
												{
													/* location.hash += 'ie_'; */
													  for(p in err){location.hash += err[p];}
													 try{console.log(err);}catch(err){}
													 //return false;
												}
											WS.isPolling = false;
												//WS.disconnect();
											WS.longPoll.abort();
											WS.getMessages();
									

	    
									
									},
								error: function(){
									/* set a 5 second timeout and try again */
									setTimeout(function(){WS.getMessages();},5000);
									//alert('error');
									}
							  });
		


};

WS.getNotifications = function(){
	$.getJSON(WS.url,function(theNotifications){
        /* split results here so we can render buddy list and messages seperately*/
		IM.populateBuddyList(theNotifications.theContacts);
        IM.renderMessages(theNotifications.theMessages);
    });
};

WS.sendMessage = function(recipientID, msgText) {
	var greeting = new Object();
	greeting.sayingHello = true;
	greeting.theMessage = msgText;
	greeting.recipientID = recipientID;		
	msgPost = $.post(WS.url,greeting,function(theResponse){
		//console.log(theResponse);
    });
};
