$(function()
{	
	function ajax_read_click(where)
	{
		$('.ajax_read', where).click(function()
		{
			$.getJSON(__basePath+'user/messages/ajax_read/'+this.href.substr(this.href.lastIndexOf('/')+1)+'/', function(data)
			{
				$("#message_form").hide();
				$('#errors').hide();
				
				if(data.item.item_type && data.messages[0].item_id > 0)
				{
					var _type = __labels.__globals.FORMS_FIELDS[data.item.item_type+'_type'];
					_type = _type ? _type[data.item[data.item.item_type+'_type']] : '';
					
					$('#id_'+data.item.item_type+'_brand')		.text(data.item.brand);
					$('#id_'+data.item.item_type+'_model')		.text(data.item.model);
					$('#id_type_'+data.item.item_type+'_type')	.text(_type);
					$('#id_engine')			.text(data.item.engine);
					$('#id_gearbox')		.text(__labels.__globals.FORMS_FIELDS.gearbox[data.item.gearbox]);
					$('#id_home_postcode')	.text(data.item.postcode);
					$('#id_town')			.text(data.item.town);
					$('#id_fuel')			.text(__labels.__globals.FORMS_FIELDS.fuel[data.item.fuel]);
					$('#id_age_from')		.text(data.item.age_from);
					$('#id_age_to')			.text(data.item.age_to);
					$('#id_buying_limit')	.text(data.item.price);
					$('#id_requirements')	.text(data.item.requirements);
					
					// $('#message > div:eq(0)').addClass('col w2');
					$('#request_data').show();
				}
				else
				{
					$('#request_data').parent().hide();
					$('#message > div:eq(0)').removeClass('col w2');
				}
				
				$message = $('#message'); 
				$message.find('.message-container:not(:last)').remove(); 
				
				$cnt = $message.find('.message-container').show();
				
				var _data;
				$(data.messages).each(function()
				{
					_data = this;
					$cnt.clone(true).insertBefore($cnt)
						.find('.display_name').text(_data.display_name).end()
						.find('.time').text(_data.time).end()
						.find('.delete').attr('href', _data['delete']).end()
						.find('.content').html(_data.content).end()
						.find('.images').html(_data.image ? '<a href="'+_data.image+'" target="_blank"><img width="200" src="'+_data.image+'"/></a>' : '').end()
						.find('.contacts').attr('href', _data.contacts)
					// END     $cnt.clone().appendTo($cnt.parent())
				});
				
				$message.find('.message-container:last').remove();
				$message.find('.message-container:not(:last)').find('.contacts, .ajax_send').remove();
				
				$message.find('.message-container:last .ajax_send').attr('href', _data.ajax_send).unbind('click').click(function()
				{
					$.getJSON(__basePath+'user/messages/ajax_send/'+this.href.substr(this.href.lastIndexOf('#')+1)+'/', function(_data)
					{
						$('#message_form').show();
						$('#id_parent_id').val(_data.parent_id);
						$('#id_receiver_id').val(_data.receiver_id);
						$('#id_system_type').val(_data.system_type);
						$('#id_replying').val(_data.replying);
						$('#id_item_id').val(_data.item);
						$('#id_username, #id_user_name').val(_data.username);
						$('#id_title').val('');
						$('#id_content').val('');
						
						if(_data.system_type > 0){ $('#send_images').show(); }
						
						$('.ajax_send').hide();
					});
					return false;
				});
				
				_data.already_a_contact ? $message.find('.contacts').hide() : $('#message').find('.contacts').show();
				// _data.no_reply ? $message.find('.ajax_send').hide() : $('#message .ajax_send').show();
				
				$message.show();
				
			});
			return false;
		});
	};
	ajax_read_click(null);
	
	$('#messages_list li a.plus').click(function()
	{
		var $anchor = $(this);
		$ul = $anchor.parents('li:eq(0)').find('.ajax');
		
		if($ul.is(':visible'))
		{
			$ul.hide().empty();
			$anchor.attr('class', 'plus');
			return false;
		}
		
		$anchor.attr('class', 'loading');
		
		$ul.load(__basePath+'user/messages/get_parent_messages/'+this.href.substr(this.href.lastIndexOf('#')+1)+'/', function(data)
		{
			$anchor.attr('class', 'minus');
			
			$(this).show();
			
			ajax_read_click(this);
			
			
			$(':checkbox', this).change(function()
			{
				if ( !$(this).is(':checked') )
				{
					$("#id_toggle").removeAttr("checked");
					$("#msgs_select_all").removeAttr('rel').text("Select all");
				}
			});
			$("#id_toggle").removeAttr('checked');
			
			$('.icon.delete', this).click(function()
			{
				return confirm(__labels.__globals.ERRORS.listing_delete) ? true : false;
			});
		});
		
		return false;
	});
	
	$('#message .ajax_send').unbind('click').click(function()
	{
		$.getJSON(__basePath+'user/messages/ajax_send/'+this.href.substr(this.href.lastIndexOf('#')+1)+'/', function(data)
		{
			$('#message_form').show();
			$('#id_parent_id').val(data.parent_id);
			$('#id_receiver_id').val(data.receiver_id);
			$('#id_system_type').val(data.system_type);
			$('#id_replying').val(data.replying);
			$('#id_item_id').val(data.item);
			$('#id_username, #id_user_name').val(data.username);
			$('#id_title').val('');
			$('#id_content').val('');
			$('.ajax_send').hide();
		});
		
		return false;
	});
	
	$('#messages_list li .has_unread a.plus').click();
	
	
	$("#message .cancel").click(function(){
		$("#message_form").hide();
		$('.ajax_send').show();
		return false;
	});
	$("#message .hide").click(function(){
		$('.ajax_send').show();
		$("#message, #message_form").hide();
		return false;
	});
});