$( function() {
	$( '.category' ).click( function() {
		$( this ).toggleClass( 'active' )
		$( this ).siblings( '.dropdown' ).toggle();
		$( this ).find( '.indicator' ).toggleClass( 'indicator-active' );
		return false;
	} );
			
	$( '#dock > li' ).hover( function() {
		id = $(this).attr("id").replace(/li/, '');
		
		$(".dock_span").fadeOut( 0 );
		
		$(".dock-active").removeClass('dock-active');
		
		var position = $( this ).position();
		var img_width = $( this ).width();
		
		var span_width = $("#span_"+id).width();
		//alert(id+"____"+span_width+'---'+img_width+"---"+$("#span_"+id).text());
		var text_indent = try_to_center(position.left, span_width, img_width);
		$( this ).addClass( 'dock-active' );
		$("#span_"+id).css("padding-left", text_indent+"px");
		$("#span_"+id).fadeIn( 200 );
		
	}).bind( "mouseleave", function() {
		//$( this ).removeClass( 'dock-active' );	
		//$( this ).children( 'span' ).fadeOut( 200 );
	} );
	
} );

function try_to_center(pos, ele_width, img_width){
	var text_indent = Math.floor((img_width-ele_width)/2);
	var new_pos = pos+text_indent;
	
	text_indent = new_pos;
	vlm=new_pos+ele_width;
	//alert('new_pos+ele_width='+vlm+'width='+$('#dock').width());
	if( new_pos > 0 && new_pos+ele_width > $('#dock').width() ){
		new_pos = $('#dock').width()-ele_width;
		text_indent = new_pos;
		//alert('new_pos+ele_width='+vlm+'width='+$('#dock').width());
	}
	if( new_pos<0 ){
		text_indent = 0;
		//alert('new_pos'+new_pos+'width='+$('#dock').width());
	}
	
	return text_indent;
}

function set_relation(postUrl, friendId, callerAnchor, userId, from_comment){
	if($("#"+callerAnchor).hasClass('user-friend-follow')){
		$.post(postUrl, { add_friend: 1, friend_id: friendId, user_id: userId, anchorId: callerAnchor },
			function(data){
				$("#"+data.anchorId).removeClass('user-friend-follow');
				$("#"+data.anchorId).addClass('user-friend-remove');
				$("#"+data.anchorId).attr("title", 'Stop following this commenter');
				if (from_comment!='comment'){
					$("#"+data.anchorId).html('stop following this commenter');
				}
				
			}, "json");
	}else{
		$.post(postUrl, { remove_friend: 1, friend_id: friendId, user_id: userId, anchorId: callerAnchor },
			function(data){
				$("#"+data.anchorId).removeClass('user-friend-remove');
				$("#"+data.anchorId).addClass('user-friend-follow');
				$("#"+data.anchorId).attr("title",'Follow this commenter');
				if (from_comment!='comment'){
					$("#"+data.anchorId).html('follow this commenter');
				}
			}, "json");
	}
	
}
function inject_into_textarea(comm_id, user_name){
	$('#comment').focus( function(){
  			if( $('#comment').attr("value").search('@<a') == -1){
				$('#comment').attr("value", '@<a href="#comment-'+comm_id+'">'+user_name+'</a>: ');
				
			}
		}
  	);
	$('#comment').focus();
	//text('@<a href="#comment-'+comm_id+'">'+user_name+'</a>');	
}
$( function() {
	$('.reshape').click( function(){
		$(this).children().trigger('focus');
	}
	);
	$(".input_field").focus( function(){
		$(".input_field").addClass('just_essencials');
		$(this).removeClass('just_essencials');
		//$(this).addClass('regular-text');
	}
	);
} );