 $(document).ready(function () {

     // emoticons to textarea position
     $('.emoticons a').click(function(e) {
     	$('#message').replaceSelection($(this).attr("title"), true);
     	e.preventDefault();
     });
        		
     $("#new_message").hide();
     
     // add toggle
     $("#add_new_thread").click(function(event) {
         event.preventDefault();
         $("#new_message").slideToggle('normal');
     });

     // ajax function for editing forum messages         
     $('.message_control a.ajax').click(function(ev) {
         id = $(this).attr('id');
         
         tmp = id.split("_");
         action = tmp[0];
         id = tmp[1];
         
         if (action == "brisi") {
             test = confirm('Ste prepričani, da želite izbrisati objavo?');
             if (test) {
                 $.get('/ajax/delForumMessage/'+id,
                     function(data) {
                         if (data == 'ok')
                             $('#row_'+id).hide();
                         else
                             alert("Napaka pri brisanju:\n" + data);
                     }
                 );
             }
         }
         else {
             $.get('/ajax/getForumMessage/'+id,
                 function(data) {
                     $('#topic_'+id).html(data);
                     
             		$('#topic_'+id+' .emoticons a').click(function(e) {
             			$('#message_'+id).replaceSelection($(this).attr("title"), true);
             			e.preventDefault();
             		});
                 }
             );
             
             $(this).parent().hide();
         }
         
         ev.preventDefault();
     });

     // collapse message controls
     $('.message_control').css('width', '12px');     
     $('.rollout').click(function(ev) {
         if ($(this).parent().width() < 100) {
             $(this).parent().animate({
                 width: '120px'
             }, 400);
         }
         else {
             $(this).parent().animate({
                 width: '12px'
             }, 400);
         }
         
         ev.preventDefault();
     });
 });
