var maxLengthOfMessage = 500;

function counter()
{
	elemet = $('#microblog-comment-text-counter');
	if (elemet.length == 0)
	{
		return;
	}
	elemet.html(maxLengthOfMessage - $('#comment_body').val().length);
}

function counterEdit()
{
	$('#microblog-comment-text-counter-edit').html(maxLengthOfMessage - $('#editcomment_body').val().length);
}

function getMessageLengthEdit()
{
	counterEdit();
	$('#editcomment_body').bind('keydown keyup focus change',function()
	{
		counterEdit();
	});
}

function getMessageLength()
{
	counter();
	$('#comment_body').bind('keydown keyup focus change',function()
	{
		counter();
	});
}

//--------------------------------------------------------------
//------------------------filters class-------------------------
var filters = function()
{
	var page;
	var link;
	var editform;
	var editform_html = null;
	var editform_isshow = false;
	var contantDIV = "";
	var filterList = new Array();

    function checkFilters(e){
        if($(e.target).attr('checked')){
            if($(e.target).attr('id') == 'ajaxfilters_content_my_stocks_filtr'){
                my_favorite_filtr = $(":checkbox[id='ajaxfilters_content_my_favorite_filtr']");
                if(my_favorite_filtr.length > 0){
                    $(my_favorite_filtr[0]).attr('checked',false);
                }
                my_filtr = $(":checkbox[id='ajaxfilters_content_my_filtr']");
                if(my_filtr.length > 0){
                    $(my_filtr[0]).attr('checked',false);
                }
                return true;
            }
            if($(e.target).attr('id') == 'ajaxfilters_content_my_favorite_filtr'){
                my_stocks_filtr = $(":checkbox[id='ajaxfilters_content_my_stocks_filtr']");
                if(my_stocks_filtr.length > 0){
                    $(my_stocks_filtr[0]).attr('checked',false);
                }
                return true;
            }
            if($(e.target).attr('id') == 'ajaxfilters_content_my_filtr'){
                my_stocks_filtr = $(":checkbox[id='ajaxfilters_content_my_stocks_filtr']");
                if(my_stocks_filtr.length > 0){
                    $(my_stocks_filtr[0]).attr('checked',false);
                }
            }
        }
        return true;
    }

	function getFilters(e){
        checkFilters(e);
		date = $("#ajaxfilters__csrf_token").serialize();
        inputlist = $(":input[type='checkbox']");
		for (i = 0;i < inputlist.length;i++)
		{
			if ($(inputlist[i]).attr('checked')) 
			{
				date += ('&' + $(inputlist[i]).serialize());
			}
		};
		filterList = date;
	}
	function sentAjax(){
		contantDIV.empty();
		contantDIV.attr('class','loading');
		$.ajax({
			   type: "POST",
			   url: link,
			   data: filterList,
			   success: function(msg)
			   {
					contantDIV.html(msg);
					contantDIV.attr('class','');
					initEdit();
					reInitVote();
			   }
		});
	}
	
	function reInitVote()
	{
		$('.vote-up').click( function(e)
	    {
			commentVoting(this);
	        return false;
	    });
		$('.vote-down').click( function(e)
		{
			commentVoting(this);
		    return false;
		});
	}
	function initEdit()
	{
		editform = $('#editFormLink').attr('href');
		$('.editpost').bind('click',startEdit);
	}
	function startEdit(e)
	{
		var id_post = $(e.target).attr('rel');
		if (editform_html != null)
		{
			showEdit(editform_html);
			return false;
		}
		$.ajax({		
				type: "GET",
				url: editform,
				success: function(msg)
				{
					showEdit(msg,id_post);
				}
		});
		return false;
	}
	function bindEditEvents(id_post_pass)
	{
		$('#editcomment_id').val(id_post_pass);
		$('#CencelBtn').bind('click',function()
		{
			$('#microblog-comment-formajax').parent().parent().html(' ');
			return false;			
		});
		$('#SendAJAXBtn').bind('click',function()
		{
			form = $('#microblog-comment-formajax > input').serialize() + '&' + $('#microblog-comment-formajax > textarea').serialize();

			$.ajax({		
				type: "POST",
				data: form,
				url: editform,
				success: function(msg)
				{
					id_post = $('#editcomment_id').val();
				
					if (msg.indexOf('success') == 0)
					{
						$('#post_content-'+id_post).html(msg.substr(8))
						$('#editplace-'+id_post).html(' ');
					} else 
					{
						$('#editplace-'+id_post).html(msg);
						bindEditEvents(id_post);						
					}
				}
			});
			return false;			
		});
	}

	function showEdit(msg,id_post_pass)
	{
		if ( $('#microblog-comment-formajax').length == 0)
		{
			$('#editplace-'+id_post_pass).html(msg);
			var selected_edit_id_post = id_post_pass;
			var func1 = bindEditEvents;
		}
			setTimeout(function ()
			{
				func1(selected_edit_id_post)
                var tekst = jQuery.trim($('#post_content-' + selected_edit_id_post).html());
                tekst = (tekst.replace(/<br><br>/g,'<br>\r\n<br>'));
                tekst = tekst.replace(/<\/?[^>]+>/gi, '');
				$('#editcomment_body').html(tekst);
				getMessageLengthEdit();
			},500);
	}
				
	function reLoad(e)
	{
		getFilters(e);
		sentAjax();
	}
	function initMonkieTalk()
	{
		contantDIV = $('#blogContant');
	}
	return {
		init: function()
		{
			link = $('#ajaxLink').attr('href');
			$(":input[type='checkbox']").bind("change",reLoad);
			type = $('#content_type').val();
			filterList = new Array();
			switch (type)
			{
				case 'talk':initMonkieTalk(); break;
			};
			initEdit();
		},
		setPage: function()
		{
			
		}
	}
}();
//--------------------------------------------------------------------

$(document).ready(function()
{
	getMessageLength();
	filters.init();
	$('#unloglink').bind('click', function()
	{
		forUnloggedUser(__("microblog_you need to logon to perform this action!"));		
		return false;
	});
});


function forUnloggedUser(message)
{
    gotolink = $('#sign_in_button').attr('href');
	mess = message+'<br/>'; 
	messageFrame = Boxy.alert('<div class="box-message general-icon">'+mess+'</div>', function(val) {
        if (val == __('Ok'))
        {
        	messageFrame.hide(); 
        } 
        else
        {
        	messageFrame.hide();
        }     
      }, {title: __('Information'), closeable: true, closeAfterClick: false,
  	    behaviours: function(r) { 
  	    	$(r).hover(
  	    		function() { $(this).css("background-color", "#FFF8DC"); }, 
  	    		function() { $(this).css("background-color", "#FFFFFF"); }
  	    		);
  	    	}
  	    });
    
    return false;
}	