//var base_href = '/askeland/cactus/';
var base_href = '/';

function getMessages(){
	
	// Kjør AJAX-kall
	$.ajax({
		type: 'GET',
		url: base_href + 'coi/get_messages.php?c='+channel+'&id='+su_id,
		cache: false,
		success: function(data){
			$(".message:first").before(data);	
			$(".message").slideDown('slow');
			$(".message_bottom").slideDown('slow');
		}
	});
	
	// ser etter nye meldinger hvert 5. sekund
	setTimeout(function(){getMessages();},5000);
}

function getPrivateMessages() {
	
	// Kjør AJAX-kall
	$.ajax({
		type: 'GET',
		url: base_href + 'coi/get_private_messages.php?c='+channel+'&from_su_id='+user_su_id+'&to_su_id='+co_su_id,
		cache: false,
		success: function(data){
			$(".message:first").before(data);	
			$(".message").slideDown('slow');
			$(".message_bottom").slideDown('slow');
		}
	});
	
	// ser etter nye meldinger hvert 5. sekund
	setTimeout(function() { getPrivateMessages(); }, 5000);
}

function getOldMessages(){
	
	// Kjør AJAX-kall
	$.ajax({
		type: 'GET',
		url: base_href + 'coi/get_oldmessages.php?c='+channel+'&id='+su_id,
		cache: false,
		success: function(data){
			$(".message:first").before(data);	
			$(".message").slideDown('slow');
			$(".message_bottom").slideDown('slow');
		}
	});
	
	// ser etter nye meldinger hvert 5. sekund
	//setTimeout(function(){getMessages(channel, su_id);},5000);
}

function getOldPrivateMessages(){
	
	// Kjør AJAX-kall
	$.ajax({
		type: 'GET',
		url: base_href + 'coi/get_private_oldmessages.php?c='+channel+'&from_su_id='+user_su_id+'&to_su_id='+co_su_id,
		cache: false,
		success: function(data){
			$(".message:first").before(data);	
			$(".message").slideDown('slow');
			$(".message_bottom").slideDown('slow');
		}
	});
}

function sendMessage(msg){
	
	// Kjør AJAX-kall
	$.ajax({
		type: 'POST',
		url: base_href + 'coi/send_message.php',
		data: 'msg='+msg+'&msisdn='+msisdn,
		cache: false,
		success: function(data){
			$(".message:first").before(data);	
			$(".message").slideDown('slow');
			$(".message_bottom").slideDown('slow');
			$("#msg_field").val('');
		}
	});
}

function sendPrivateMessage(msg) {
	
	// Kjør AJAX-kall
	$.ajax({
		type: 'POST',
		url: base_href + 'coi/send_message.php',
		data: 'msg=' + msg + '&msisdn=' + msisdn + '&user_nickname=' + user_nickname,
		cache: false,
		success: function(data){
			$(".message:first").before(data);	
			$(".message").slideDown('slow');
			$(".message_bottom").slideDown('slow');
			$("#msg_field").val('');
		}
	});
}

function getUsers() {
	// Kjør AJAX-kall
	$.get(base_href + 'coi/get_users.php?c='+channel+'&sid='+sid, function(data){
		$('#userlist').html(data);	
	});
	
	// sjekke etter nye brukere hvert 3. sekund
	setTimeout(
		function() {
			getUsers(channel);
		},
		10000
	);
}

function changeNick(new_nick, session_id){
	$('#new_nick_loader').show();
	
	// Kjør AJAX-kall
	$.get(base_href + 'coi/change_nick.php?sid=' + session_id + '&nick=' + new_nick, function(data){
		eval(data);
		$('#new_nick_input').fadeOut('slow', function(){
			$('#new_nick_info').html(response["html"]);
			$('#new_nick_info').fadeIn('slow');
			
			// Oppdatere nick i GUI
			if(response["status"]=="OK"){
				$('#user_nick').html(new_nick);
				nick = new_nick;
			}	
			
			// Oppdatere brukerlisten
			getUsers();
		});
	});
}

function showUserProfile(id){
	jQuery.facebox(function() { 
		  jQuery.get('coi/panels/profile_info.php?id='+id, function(data) {
		    jQuery.facebox(data);
		  });
	});	
}

function showCoComments(id){
	jQuery.facebox(function() { 
		  jQuery.get('coi/panels/co_comments.php?id='+id, function(data) {
		    jQuery.facebox(data);
		  });
	});	
}

function addCoComment(commentbox, id){
	$('#co_comment_loader').show();
	
	// Kjør AJAX-kall
	$.post(base_href + 'coi/add_co_comment.php', {
			suser: id,
			comment: commentbox.value,
			co: co_id
		}, function(data){
			eval(data);
			$('#co_comment_loader').hide();
				
			if(response["status"]=="OK"){
				$('#comment_response').html(response["html"]).slideDown();
				$('#new_co_comment').fadeOut();
			}
	});
}

function changeChannel(new_channel) {
	
}

function disableLink(link_id) {
	$.get(	
		base_href + 'coi/links_keeper.php', 
		{
			sid: sid,
			action: 'add',
			link_id: link_id
		},
		function(data) {
			var link = document.getElementById('userlist_' + link_id);
			if (link != null) {
				if (!link.disabled)	{
					link.disabled = true;
					link.removeAttribute("href");
					link.onclick = null;
					//link.style.style.cursor = 'wait';
				}
			}
			//$("div").
			//$("a").each (
			//	function() {
			//		alert('\'' + this.id + '\' \'notify_'+ link_id + '\' ' + this.id.search(/'notify_'+ link_id/));
			//	}
			//);
		}
	);
}

function openPrivateWindow(link_id, url) {
	disableLink(link_id);
	window.open(url + '&link_id=' + link_id, '_blank', 'width = 700, height = 500');
}

function closePrivateWindow(link_id) {
	$.get(	
		base_href + 'coi/links_keeper.php', 
		{
			sid: sid,
			action: 'remove',
			link_id: link_id
		},
		function(data) {
			getUsers();
		}
	);
}

function getNotification() {
	$.ajax({
		type: 'GET',
		url: base_href + 'coi/get_notification.php?c=' + channel + '&sid=' + sid,
		cache: false,
		success: function(data){
			$(".message:first").before(data);	
			$(".message").slideDown('slow');
			$(".message_bottom").slideDown('slow');
		}
	});
	setTimeout(
		function() {
			getNotification();
		}, 
		5000
	);
}
