var settings = {};
$(document).ready(function() {
	amxchat_presence_tabs();	
	
	// Get the current settings
	$.post('/chat/settings', {}, function(data) {
		for(var key in data.settings) {
			settings[key] = data.settings[key];
		}
		// Check settings and startup
		amxchat_presence_startup();		
	}, 
	'json');
	
	$("#presence-settings input").change(function() {
		var map = {};
		map['field'] = $(this).attr('name');
		map['value'] = $(this).val();
		$.post('/chat/settings', map, function(data) {
			for(var key in data.settings) {
				settings[key] = data.settings[key];
			}
			window.top.location = window.top.location;
		}, 
		'json');
	});
	
	$("#presence .presence-friend").click(function() {
		var id = $(this).children('input[name=id]').val();
		if($("#"+id+"-chat-window").html() == null) {
			$("#presence-ui").append('<div id="'+id+'-chat-tab" class="tab"><span><img src="/public/images/icon_online_friends.png" /> '+$(this).children('.name').html()+'</span><div id="'+id+'-chat" class="notification chat"><div class="title-wrapper"><div class="close">X</div><div class="min">-</div><div class="title">'+$(this).children('.name').html()+'</div><div class="clear"></div></div><div class="content">'+$(this).html()+'<div id="'+id+'-chat-window" class="chat-window"></div><div id="'+id+'-chat-box" class="chat-box"><textarea name="'+id+'-chat-text" id="'+id+'-chat-text" class="chat-text"></textarea></div><input type="button" id="'+id+'-btn-send" value="Send Message" class="btn" /></div></div></div>');
			amxchat_presence_tabs();
			amxchat_presence_show_tab($("#"+id+"-chat-tab span"));
		}
	});
	
	// Setup the click for "Send Instant Message"
	$("a.presence-im").click(function() {
		var props = $(this).attr('rel').split(":");
		if($("#"+props[0]+"-chat-window").html() == null) {
			$("#presence-ui").append('<div id="'+props[0]+'-chat-tab" class="tab"><span><img src="/public/images/icon_online_friends.png" /> '+ props[1] +'</span><div id="'+props[0]+'-chat" class="notification chat"><div class="title-wrapper"><div class="close">X</div><div class="min">-</div><div class="title">'+props[1]+'</div><div class="clear"></div></div><div class="content"><div class="picture"><img src="'+props[2]+'" width="35" height="35" alt="Picture" /></div><div class="name">'+props[1]+'</div><div class="clear"></div><input type="hidden" name="id" value="'+props[0]+'" /><div id="'+props[0]+'-chat-window" class="chat-window"></div><div id="'+props[0]+'-chat-box" class="chat-box"><textarea name="'+props[0]+'-chat-text" id="'+props[0]+'-chat-text" class="chat-text"></textarea></div><input type="button" id="'+props[0]+'-btn-send" value="Send Message" class="btn" /></div></div></div>');
			amxchat_presence_tabs();
			
			// Show the new tab
			amxchat_presence_show_tab($("#"+props[0]+"-chat-tab span"));
		}
	});
	
	$("textarea.chat-text").live('keydown', function(e) {
		if(e.keyCode == 13 && $(this).val().length > 0) {
			var id = $(this).attr('id').match(/^[0-9]*/);
			var map = {
				id_user:id,
				text:$(this).val()
			};
			$(this).val('');
			amxchat_presence_chat_post(map);				
			return false;
		}
	});
	$("input[type=button].btn").live('click', function(e) {
		var id = $(this).attr('id').match(/^[0-9]*/);		
		if($("#"+id+"-chat-text").val().length > 0) {
			var map = {
				id_user:id,
				text:$("#"+id+"-chat-text").val()
			};
			$("#"+id+"-chat-text").val('');
			amxchat_presence_chat_post(map);
			return false;
		}
	});
	
});

function amxchat_presence_tabs() { 	
	$("#presence-ui .tab span").unbind().click(function() {
		amxchat_presence_show_tab(this);
	});
}

function amxchat_presence_show_tab(e) {
	$("#presence-ui .tab").removeClass('active').children('.notification').hide();
	$(e).parent().addClass('active').removeClass('blink').children('.notification').show().find('.min').click(function() {
		$(this).parents('.notification').hide();		
		$(this).parents('.tab').removeClass('active').click(function() { 
			amxchat_presence_tabs(this);
		});
	});
	$(e).next().find('.close').click(function() {
		$(this).parents('.tab.active').remove();
	});
}

function amxchat_presence_chat_get() {
	$.get('/chat/get/'+Math.floor(Math.random() * 100), {}, function(data) {
		// We have new messages so lets figure out where to put them
		if(data.result) {
			$.each(data.msgs, function(i, item) { 
				amxchat_presence_add_msg(item);
			});
		}
		
		// Call the same function to start another request
		if(settings['is_online'] == 1)
			amxchat_presence_chat_get();
	}, 'json');
}

function amxchat_presence_chat_post(map) {
	$.post('/chat/post', map, function(data) {
		if(data.result) {
			$("#"+data.id_user+"-chat-text").val('');
			$("#"+data.id_user+"-chat-window").append('<div class="msg"><div class="from_name">'+data.from_name+':</div><div class="text">'+data.text+'</div></div>').scrollTo('max');
			if(settings['is_online'] == 0) {
				$.post('/chat/settings', {field:'is_online', value:1}, function(data) {
					for(var key in data.settings) {
						settings[key] = data.settings[key];
					}
				}, 'json');
			}
		}
	}, 'json');
}

function amxchat_presence_add_msg(msg) {
	// Chat window already opened
	if($("#"+msg.id_user+"-chat-window").html() == null) {
		var id = msg.id_user;
		$("#presence-ui").append('<div id="'+id+'-chat-tab" class="tab"><span><img src="/public/images/icon_online_friends.png" /> '+msg.from_name+'</span><div id="'+id+'-chat" class="notification chat"><div class="title-wrapper"><div class="close">X</div><div class="min">-</div><div class="title">'+msg.from_name+'</div><div class="clear"></div></div><div class="content">'+msg.banner+'<div id="'+id+'-chat-window" class="chat-window"></div><div id="'+id+'-chat-box" class="chat-box"><textarea name="'+id+'-chat-text" id="'+id+'-chat-text" class="chat-text"></textarea></div><input type="button" id="'+id+'-btn-send" value="Send Message" class="btn" /></div></div></div>');
		amxchat_presence_tabs();
		amxchat_presence_show_tab($("#"+id+"-chat-tab span"));		
	}
	
	// Add message
	$("#"+msg.id_user+"-chat-window").append('<div class="msg"><div class="from_name">'+msg.from_name+':</div><div class="text">'+msg.text+'</div></div>').scrollTo('max');
	$("#"+msg.id_user+"-chat-tab").addClass('blink');
}

function amxchat_presence_startup() {
	if(settings['is_online'] == 1)
		amxchat_presence_chat_get();
}