$(document).ready(function() {
	
	$('.nyroModal').nyroModal({'bgColor':'#BAD3D7'});
	
	$('.sphrase').focus(function() { $(this).addClass('active'); });
	$('.sphrase').blur(function() { if ($(this).val() == '') $(this).removeClass('active'); });	
	
	$('.confirm').bind('click', function() {
		var answer = confirm($(this).attr('title'));
		
		if (answer)
		{
			return true;
		}
		else
		{
			return false;
		}
		
	});	

    // messeges
    if ($('#sysMsg').length > 0)
    {
        $('#sysMsg').fadeIn();
    }
    
	$('.ajax').bind('click', function() {
		
		$ajaxCaller = $(this);
		
		$.ajax({
			  url: $(this).attr('href'),
			  success: function(data) 
			  {
				bindAjaxAction($ajaxCaller, data);
			  }
		});
		
		return false;
	});     


	$('#close-sys-msgs').bind('click', function(){
		closeMsg();
	});

	setTimeout('closeMsg()', 4000);
	
	$('.limited').keyup(function() { checkWordLen($(this)) } );
	
	$('.comment-box').bind('focus', function() {  
	
	});
	
	$('.comment-box').bind('blur', function() {  
		
	});	
	
	$('.cancel-button').bind('click', function() {
		history.go(-1);
	});
	
	 $(".options-bar a").click(function () {
	        $("li.replyForm").fadeOut(200);
	        var g = $("form#add-comment-entry");
	        var d = $(this).closest("li.comment");
	        var e = $('<li class="replyForm"></li>').append(g);
	        var f = d.find("h5 a:last");
	        e.find("p.info a").not(".cancel").attr("class", f.attr("class")).text(f.text());
	        e.find("p.info").show();
	        e.find("p.letters-counter").hide();
	        $("textarea", e).val("@" + d.find(".head-left-part a:first").text() + ": ").focus();
	        $(".info a:first", e).text(d.find(".head-left-part a:first").text());
	        $(".info a:first", e).attr('href',d.find(".head-left-part a:first").attr('href'));
	        $("#parentId", e).val($(this).metadata().id_root);
	        
	        //$("input.replyTo", e).val($(this).metadata().id_root);
	        d.after(e);
	        $(".cancel", e).click(function () {
	            e.find("p.info").hide();
	            e.fadeOut(200);
	            $("#comments-list-entry").after(g);
	            $("input.replyTo").val("");
	            $("form#add-comment-entry").fadeIn(200);
	            $("p.info", "form#add-comment-entry").hide();
	            e.remove();
	            return false
	        });
	        return false
	    });
	 
		$("ul.comments-list > li a.show-comment").live("click", function () {
	        show_comment(jQuery(this).parent().parent().parent().parent());
	        return false;
	    });
	    $("ul.comments-list > li a.hide-comment").live("click", function () {
	        hide_comment(jQuery(this).parent().parent().parent().parent());
	        return false;
	    });	 
	
});

function closeMsg()
{
	$('#sysMsg').fadeOut();
}


function checkWordLen(obj)
{
	var len = obj.val().split(/[\s\.\?]+/);
			
	if( len.length > obj.attr('limit'))
	{
		limitedVal = obj.val().substring(0,obj.attr('limit')*2);
		
		obj.val( limitedVal );
				
		//alert("You cannot put more than " + obj.attr('limit') + " words in this text area.");
				
		return false;
	}
	return true;
}

function show_comment(a) {
    //a.find("a.plus-icon:not(.voted), a.minus-icon:not(.voted)").removeClass("hide");
    a.find("div.comment-content").slideDown(500);
    a.find("a.toggle-comment").removeClass("show-comment").addClass("hide-comment").html("[ <span>Hide comment</span> ]")
}
function hide_comment(a) {
    
    //a.find("a.plus-icon").addClass("hide");
    //a.find("p, div.options-bar").css("display", "none");
    //a.find("h5.head a, h5.head strong").css("color", "#666666");
    //a.addClass("hidden");
	a.find("div.comment-content").slideUp(500);
    a.find("a.toggle-comment").removeClass("hide-comment").addClass("show-comment").html("[ <span>Show comment</span> ]");
    
};

function bindAjaxAction($obj, $json)
{		
	if($obj.hasClass('comment-plus') || $obj.hasClass('comment-minus'))
	{
		var result = JSON.parse($json);
				
		$( '#crating_' + result.id ).html( result.v );
	}
}

