/**
 * JS :: Webusr
 *
 * Webusr.js
 *
 * @version        $1.0$
 * @autor   	   $Peter Benicky$
 * @lastmodified   $06.04.2007$
 */
 
function Im(webroot_im) {
	chatThis = this;
	this.server_im = webroot_im;
	this.css_images_im = webroot_im+'css/user/images/';
	this.axajRequest_im = null;
	this.actived = null;
	this.index = 0;
	this.select = '';
	this.ping = null;		
	this.initialize();
	this.state_im = 'close';
	this.state_chat = 'close';
}
Im.prototype.initialize = function() {
	$('li.online_freinds a').click(chatThis.showOnlineFriends);
	$('li.online_chat a').click(chatThis.showChat);
	$('.end_im').bind('click', chatThis.endIm);
	$('.close_im').bind('click', chatThis.closeIm);
	$('.start_im').bind('click', chatThis.startIm);
	$('.close_chat').bind('click', chatThis.closeChat);
	$('a.refresh_im').bind('click', chatThis.wakeUp);
	$('#freind_online > h3').bind('mousedown', function() {$('#im_holder').draggable();});
	$('.im_header').bind('mousedown', function() {$('#im_holder').draggable();});
	$('#freind_online > ul').bind('mousedown', function() {$('#im_holder').draggable('destroy');});
	$('.im_text').bind('mousedown', function() {$('#im_holder').draggable('destroy');});
	$('#users_online > h3').bind('mousedown', function() {$('#chat_holder').draggable();});
	$('.chat_header').bind('mousedown', function() {$('#chat_holder').draggable();});
	$('#users_online > ul').bind('mousedown', function() {$('#chat_holder').draggable('destroy');});
	$('.chat_text').bind('mousedown', function() {$('#chat_holder').draggable('destroy');});
	$('input.im_input').keypress(function(e) {
		if (e.keyCode == 13) {
            chatThis.add_im(this.name);
            return false;
        }
    });
	$('input.im_input').bind('click', function() {
		if($(this).val() == 'Type ...')
      	$(this).val('');
  	});
	$('input.chat_input').keypress(function(e) {
		if (e.keyCode == 13) {
            chatThis.add_chat();
            return false;
        }
    });
	$('input.chat_input').bind('click', function() {
		if($(this).val() == 'Type ...')
      	$(this).val('');
  	});
}
Im.prototype.check = function() {
	chatThis.ajaxRequest_im = $.ajax({
		url: chatThis.server_im + 'chat_messages/ping/check/'+Math.random()+'/',
		success: function(msg) {
			if(msg == 'offline') {
				clearTimeout(chatThis.ping);
				return;
			}
			if(msg == 'im') {
				$('#loading').show();
				chatThis.showOnlineFriends();
				return false;
			}
   		}
	});
	chatThis.ping = setTimeout('chatThis.check()', 6000);
	return false;
}
Im.prototype.showOnlineFriends = function() {
	if (chatThis.state_im == 'lock')
		return false;
	$('#loading').show();
	chatThis.ajaxRequest_im = $.ajax({
		url: chatThis.server_im + 'chat_messages/ping/open_im/'+Math.random()+'/',
		success: function(msg){
			chatThis.state_im = 'lock';
			chatThis.startPing();
   		}
	});
	return false;
}
Im.prototype.showChat = function() {
	if (chatThis.state_chat == 'lock')
		return false;
	$('#loading').show();
	chatThis.ajaxRequest_im = $.ajax({
		url: chatThis.server_im + 'chat_messages/ping/open_chat/'+Math.random()+'/',
		success: function(msg){
			chatThis.state_chat = 'lock';
			chatThis.startPing();
		}
	});
	return false;
}
Im.prototype.startPing = function() {
	clearTimeout(chatThis.ping);
	if(chatThis.state_im == 'lock' && chatThis.state_chat == 'lock') {
		$('div#im_holder').show();
		$('div#chat_holder').show();	
		chatThis.pingBoth();
		return;
	}
	if(chatThis.state_im == 'lock' && chatThis.state_chat == 'close') {
		$('div#im_holder').show();
		$('div#banner_small').hide();
		$('div#banner_big').hide();	
		$('div#banner_big_border').hide();
		chatThis.pingIm();
		return;
	}
	if(chatThis.state_im == 'close' && chatThis.state_chat == 'lock') {
		chatThis.pingChat();
		$('div#chat_holder').show();
		$('div#banner_small').hide();
		$('div#banner_big').hide();	
		$('div#banner_big_border').hide();
	}
	if(chatThis.state_im == 'close' && chatThis.state_chat == 'close') {
		chatThis.check();
		$('div#banner_small').show();
		$('div#banner_big').show();
		$('div#banner_big_border').show();
	}
}
Im.prototype.pingIm = function() {
	$.getJSON(chatThis.server_im + 'chat_messages/ping/update_im/'+Math.random()+'/',
    	function(data) {
			if(data.online == 'offline') 
			{
				clearTimeout(chatThis.ping);
				$('div#users_online ul').html('');
				$('.away_li').show();
				$('.freindim').hide();
				$('li#no_freinds_li').hide();
				return;
			}
			if (!data.no_freinds) {
				$.each(data, function(i, item){
					chatThis.index = i;
					if ($('input#input_freind_im-' + chatThis.actived).val() != item.freind_id && chatThis.actived != null && i == chatThis.actived) 
						chatThis.index = 49;
					else {
						if ($('input#input_freind_im-' + chatThis.actived).val() == item.freind_id && chatThis.actived != null && i != chatThis.actived) 
							chatThis.index = chatThis.actived;
					}
					
					$('div#im-' + chatThis.index + ' .im_header').html('<a href="' + chatThis.server_im + 'users/' + item.freind_name + '/" title="' + item.freind_name + '"><img src="' + item.img + '" alt="' + item.freind_name + '" /></a>' + item.freind_name);
					$('div#im-' + chatThis.index + ' .im_text').html(item.msgs);
					$('input#input_user_im-' + chatThis.index).attr('value', item.user_id);
					$('input#input_freind_im-' + chatThis.index).attr('value', item.freind_id);
					$('input#input_freind_name_im-' + chatThis.index).attr('value', item.freind_name);
					$('a#picture_im-' + chatThis.index + ' img').attr('src', item.img);
					$('a#picture_im-' + chatThis.index + ' img').attr('alt', item.freind_name);
					$('a#picture_im-' + chatThis.index).attr('title', item.freind_name);
					$('a#start_im-' + chatThis.index).html(item.freind_name);
					chatThis.select += 'li#freindim-' + chatThis.index + ',';
					$('a#start_im-' + chatThis.index).attr('title', item.freind_name);
					chatThis.scrollDown('text-' + chatThis.index);
					if (item.state == 'received' && chatThis.index != chatThis.actived) {
						$('a#picture_im-' + chatThis.index + ' img').addClass('actived');
						$('a#start_im-' + chatThis.index).addClass('actived');
					}
			});
		}
		$('.freindim').hide();
		if(data.no_freinds) {
			$('li.away_li').hide();
			$('li#no_freinds_li').show();
		}
		else
		{
			$('li.away_li').hide();
			$('li#no_freinds_li').hide();
			$(''+chatThis.select+'').show();
		}
		chatThis.select = '';
		$('#loading').hide();
    });
	chatThis.ping = setTimeout('chatThis.pingIm()', 2000);
}
Im.prototype.pingChat = function() {
	$.getJSON(chatThis.server_im + 'chat_messages/ping/update_chat/'+Math.random()+'/',
    	function(data) {
			if(data.online == 'offline') 
			{
				clearTimeout(chatThis.ping);
				$('div#users_online ul').html('');
				$('.away_li').show();
				$('.freindim').hide();
				$('li#no_freinds_li').hide();
				return;
			}
			if(data.received) 
			{
				clearTimeout(chatThis.ping);
				$('div#im_holder').show();
				chatThis.pingBoth();
				return;
			}
			$('p.away_li').hide();
			$('div#users_online ul').html(data.users);
			$('div.chat .chat_text').html(data.messages);
			chatThis.scrollDown('chat_area');
			$('#loading').hide();
    });
	 chatThis.ping = setTimeout('chatThis.pingChat()', 2000);
}
Im.prototype.pingBoth = function() {
	$.getJSON(chatThis.server_im + 'chat_messages/ping/update_both/'+Math.random()+'/',
    	function(data) {
		if(data.online == 'offline') 
			{
				clearTimeout(chatThis.ping);
				$('div#users_online ul').html('');
				$('.away_li').show();
				$('.freindim').hide();
				$('li#no_freinds_li').hide();
				return;
			}
		$.each(data, function(i, item) {
			if(item.users) 
			{
				$('p.away_li').hide();
				$('div#users_online ul').html(item.users);
				$('div.chat .chat_text').html(item.messages);
				chatThis.scrollDown('chat_area');
			} else if(!data[0].no_freinds) {
				chatThis.index = i;
				if($('input#input_freind_im-'+chatThis.actived).val() != item.freind_id && chatThis.actived != null && i == chatThis.actived)
					chatThis.index = 49;
				else {
					if($('input#input_freind_im-'+chatThis.actived).val() == item.freind_id && chatThis.actived != null && i != chatThis.actived)
						chatThis.index = chatThis.actived;
				}
				$('div#im-'+chatThis.index+' .im_text').each(function(){this.scrollTop = this.scrollHeight});
				$('div#im-'+chatThis.index+' .im_header').html('<a href="'+chatThis.server_im+'users/'+item.freind_name+'/" title="'+item.freind_name+'"><img src="'+item.img+'" alt="'+item.freind_name+'" /></a>'+item.freind_name);
				$('div#im-'+chatThis.index+' .im_text').html(item.msgs);
				$('input#input_user_im-'+chatThis.index).attr('value', item.user_id);
				$('input#input_freind_im-'+chatThis.index).attr('value', item.freind_id);
				$('input#input_freind_name_im-'+chatThis.index).attr('value', item.freind_name);
				$('a#picture_im-'+chatThis.index+' img').attr('src', item.img);
				$('a#picture_im-'+chatThis.index+' img').attr('alt', item.freind_name);
				$('a#picture_im-'+chatThis.index).attr('title', item.freind_name);
				$('a#start_im-'+chatThis.index).html(item.freind_name);
				chatThis.select += 'li#freindim-'+chatThis.index+',';
				$('a#start_im-'+chatThis.index).attr('title', item.freind_name);
				chatThis.scrollDown('text-'+chatThis.index);
				if(item.state == 'received' && chatThis.index != chatThis.actived)
				{
					$('a#picture_im-'+chatThis.index+' img').addClass('actived');
					$('a#start_im-'+chatThis.index).addClass('actived');
				}
			}
		});
		$('.freindim').hide();
		if(data[0].no_freinds) {
			$('li#no_freinds_li').show();
			$('li.away_li').hide();
		}
		else
		{
			$('li.away_li').hide();
			$('li#no_freinds_li').hide();
			$(''+chatThis.select+'').show();
			
		}
		chatThis.select = '';
		$('#loading').hide();
    });
	chatThis.ping = setTimeout('chatThis.pingBoth()', 2000);
}
Im.prototype.add_im = function(id) {
	var freind_id = $('input#input_freind_im-'+id).val();
	var user_id = $('input#input_user_im-'+id).val();
	var msg = $('input#input_msg_im-'+id).val();
	var freind_name = $('input#input_freind_name_im-'+id).val();
	msg = msg.replace(/^\s*/, "").replace(/\s*$/, "");
	if(msg == '' || msg.length ==	 0)
		return false;
	$('input#input_msg_im-'+id).val('');
	$.post(
		chatThis.server_im+'chat_messages/add_im/'+Math.random()+'/', 
		{freind_name: freind_name, msg: msg, freind_id: freind_id, user_id: user_id, ajax: 'true'}
	);
	return false;
}
Im.prototype.add_chat = function() {
	var msg = $('input.chat_input').val();
	var user_id = $('input[name=chat_user_id]').val();
	msg = msg.replace(/^\s*/, "").replace(/\s*$/, "");
	if(msg == '' || msg.length ==	 0)
		return false;
	$('input.chat_input').val('');
	$.post(
		chatThis.server_im+'chat_messages/add_chat/'+Math.random()+'/',
		{msg: msg, user_id: user_id, ajax: 'true'}
	);
	return false;
}
Im.prototype.closeIm = function() {
	$('.im').removeClass('actived_im');
	chatThis.actived = null;
	return false;
}
Im.prototype.startIm = function() {
	$('.im').removeClass('actived_im');
	$('#im-'+this.rel).addClass('actived_im');
	$('a#picture_im-'+this.rel+' img').removeClass('actived');
	$('a#start_im-'+this.rel).removeClass('actived');
	chatThis.scrollDown('text-'+this.rel);
	chatThis.actived = this.rel;
	chatThis.axajRequest_im = $.ajax({
		url: chatThis.server_im + 'chat_messages/ping/markasread/'+Math.random()+'/'
	});
	return false;
}
Im.prototype.endIm = function() {
	$('#im_holder').hide();
	chatThis.axajRequest_im = $.ajax({
		url: chatThis.server_im + 'chat_messages/close/im/'+Math.random()+'/'
	});
	chatThis.state_im = 'close';
	chatThis.startPing();
	return false;
}
Im.prototype.wakeUp = function() {
	chatThis.axajRequest_im = $.ajax({
		url: chatThis.server_im + 'chat_messages/ping/wakeup/'+Math.random()+'/',
		success: function(msg){
			chatThis.startPing();
   		}
	});
	return false;
}
Im.prototype.closeChat = function() {
	$('#chat_holder').hide();
	chatThis.axajRequest_im = $.ajax({
		url: chatThis.server_im + 'chat_messages/close/chat/'+Math.random()+'/'
	});
	chatThis.state_chat = 'close';
	chatThis.startPing();
	return false;
}
Im.prototype.scrollDown = function(id){
	var objDiv = document.getElementById(id);
	objDiv.scrollTop = objDiv.scrollHeight;
}
