var initDOM = false;
var change_clicked = false;



$(document).ready(function()
{
	//Enable all functions
    initDOM = true;
	$status_content = $("#status_content");
	$status_arrow = $("#status_arrow");
	
	$("#status_input").live("keyup", function(event)
	{
		if (event.keyCode == 13) 
		{
			if ($(this).val() == '')
			{}
			else
				chatter_add($(this).val());
		}
	});
	
	$("#comment_change").click(function()
	{
		if (change_clicked == true)
			return false;
			
		change_clicked = true;
		
		if ($("#status_input").val() == '')
			return false;
			
		var status_text = $("#status_content").html();	
		$('#status_content').html('<div class="statusInput"><input type="text" id="status_input" value="'+status_text+'"></div>'); 
		$("#status_input").focus();
								
		return false;
	});
    
    /*  TODO: add normal edit in place */
    $("#status_content").click(function()
    {
        if (!chatter.allow_input)
            return false;
        if (change_clicked == true)
            return false;
            
        change_clicked = true;
        
        if ($("#status_input").val() == '')
            return false;
            
        var status_text = $("#status_content").html();    
        $('#status_content').html('<div class="statusInput"><input type="text" id="status_input" value="'+status_text+'"></div>'); 
        $("#status_input").focus();
                                
        return false;
    });    
    
	
	/* hit esc key */
	$(document).keyup(function(event)
	{
		if (event.keyCode == 27 && change_clicked == true) 
		{
			if ($("#status_input").val() != '')
			{
				change_clicked = false;
				$status_content.html($("#status_input").val());
			}
			return false;
		}
	});
	
	$("#comment_remove").click(chatter_remove);
});



/**
global namespace 
chatter.source_id
chatter.source_type
chatter.chatter_id - id текущего статуса
chatter.total
chatter.allow_input
chatter.is_active - есть ли активный статус
*/

function show_input()
{
	$status_content.html('<div class="statusInput"><input type="text" id="status_input"/></div>');
}

function chatter_remove()
{
	$status_content.html('<img src="indicator.gif" id="indicator">');
	
	$.ajax({
		type: "POST", 
		url: "processing.php", 
		data: {
			'page': 'chatter',
			'action': 'remove_chatter', 
			'source_id': chatter.source_id,
			'source_type': chatter.source_type,
			'rnd': Math.random(100)
		},
		dataType: "json",
        beforeSend: function(xhr) 
        {
            xhr.setRequestHeader("Cookie", document.cookie);
        },
		success: function(data)
		{
			if (data.success == -1)
			{
				auth();
				return;
			}
			if (data.success == 1)
			{
				$("#status_input").focus();
				//Не разрешаем клик на перо
				change_clicked = true;
				//Увеличиваем количество чаттеров
				chatter_counter++;
				chatter.total++;
				show_left_arrow($status_arrow);
				hide_right_arrow($status_arrow);
				
				chatter.chatter_id = chatter.total;
				chatter.is_active = false;
				
				show_input();
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
		}
	});
}

function chatter_add(text)
{
	$status_content.html('<img src="indicator.gif" id="indicator">');
	
	$.ajax({
		type: "POST", 
		url: "processing.php", 
		data: {
			'page': 'chatter',
			'action': 'add_chatter', 
			'text': text,
			'source_id': chatter.source_id,
			'source_type': chatter.source_type,
			'rnd': Math.random(100)
		},
		dataType: "json",
        beforeSend: function(xhr) 
        {
            xhr.setRequestHeader("Cookie", document.cookie);
        },
		success: function(data)
		{
			if (data.success == -1)
			{
				auth();
				return;
			}
			if (data.success == 1)
			{
				//Разрешаем клик на перо
				change_clicked = false;
				//Увеличиваем количество чаттеров
				chatter_counter++;
				chatter.total++;
				hide_right_arrow($status_arrow);
				if (chatter.total > 1)
					show_left_arrow($status_arrow);
				chatter.chatter_id = data.id;
				chatter.is_active = true;
				$('#status_content').html(text);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
		}
	});
}

function chatter_prev()
{
	$status_content.html('<img src="indicator.gif" id="indicator">');
	
	$.ajax({
		type: "POST", 
		url: "processing.php", 
		data: {
			'page': 'chatter',
			'action': 'prev_chatter', 
			'source_id': chatter.source_id,
			'source_type': chatter.source_type,
			'chatter_id': chatter.chatter_id,
			'rnd': Math.random(100)
		},
		dataType: "json",
        beforeSend: function(xhr) 
        {
            xhr.setRequestHeader("Cookie", document.cookie);
        },
		success: function(data)
		{
			if (data.success == -1)
			{
				auth();
				return;
			}
			if (data.success == 1)
			{
				if (data.id == -1)
				{
					chatter_counter = 0;
					return;
				}
				chatter_counter--;
				if  (chatter_counter <= 1)
					hide_left_arrow($status_arrow);
				show_right_arrow($status_arrow);				
				chatter.chatter_id = data.id;
				$('#status_content').html(data.response);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
		}
	});
}

function chatter_next()
{
	if  (chatter_counter == chatter.total-1)
	{
		if (chatter.allow_input == true && chatter.is_active == false)
		{
			hide_right_arrow($status_arrow);
			show_input();
			chatter.chatter_id++;
			chatter_counter++;
			return;
		}
	}
	
	$status_content.html('<img src="indicator.gif" id="indicator">');
	
	$.ajax({
		type: "POST", 
		url: "processing.php", 
		data: {
			'page': 'chatter',
			'action': 'next_chatter', 
			'source_id': chatter.source_id,
			'source_type': chatter.source_type,
			'chatter_id': chatter.chatter_id,
			'rnd': Math.random(100)
		},
		dataType: "json",
        beforeSend: function(xhr) 
        {
            xhr.setRequestHeader("Cookie", document.cookie);
        },
		success: function(data)
		{
			if (data.success == -1)
			{
				auth();
				return;
			}
			if (data.success == 1)
			{
				if (data.id == -1)
				{
					chatter_counter = chatter.total;
					return;
				}
				chatter_counter++;
				if  (chatter_counter >= chatter.total)
				{
					if (chatter.allow_input == false || chatter.is_active == true)
						hide_right_arrow($status_arrow);
				}
				show_left_arrow($status_arrow);
				chatter.chatter_id = data.id;
				$('#status_content').html(data.response);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
		}
	});
}

function hide_left_arrow($arrors)
{
	$arrors.children(":first").hide();
	$arrors.children(":last").css("margin-left", "19px");
}

function show_left_arrow($arrors)
{
	$arrors.children(":first").show();
	$arrors.children(":last").css("margin-left", "0px");
}

function hide_right_arrow($arrors)
{
	$arrors.children(":last").hide();
}

function show_right_arrow($arrors)
{
	$arrors.children(":last").show();
}
