/*
 *@author Carlos Barrantes
 *
*/

(function($){
$(function(){
	
	//image post fix
	function image(){
		$('div.newsContent').find('img:first').addClass('thumbPost');	
	}
	image();
	
	//------news_expander
	//jQuery object with target element
	var $target;

	function _loadText(response){

		$target.parents('.post').find('div.newsContent').html(response);

	}
    
    function _error(){
        alert('An error ocurred, please try again');
    }

	$('a.readon').click(function(e){

		$target = $(this);

		var art = $target.attr('name').replace('expander','');
        
		var opts = {

			async:		false,
			url: 		'includes/news_expander.php',
			type: 		'GET',
			dataType: 	"html",
			data: 		{id:art,full:1},
			beforeSend: function(){
				//$target.siblings('div.newsContent img:first').hide();
			},
			error: 		function(){_error();},
			success: 	_loadText,
			complete: 	function(){
				//$target.parents('.post').find('div.newsContent img:first').hide();
				$target.hide();
				$target.next().show();
                $target.parents('.post').find('.newsImages img').css('float','none');			
			}
		
		};
		
		e.preventDefault();
		$.ajax(opts);

	});
	
	$('a.collapse').click(function(e){

		$target = $(this);

		var art = $target.attr('name').replace('collapse','');
		
		var opts = {

			async:		false,
			url: 		'includes/news_expander.php',
			type: 		'GET',
			dataType: 	"html",
			data: 		{id:art,full:0},
			beforeSend: function(){
				//$target.siblings('p.iteminfo').next().fadeOut();
			},
			error: 		function(){_error();},
			success: 	_loadText,
			complete: 	function(){
				image();
				$target.hide();
				$target.prev().show();
                $target.parents('.post').find('.newsImages img').css('float','right');							
			}
		
		};
		
		e.preventDefault();
		$.ajax(opts);

	});
    
    $('a.comments-list').click(function(e){
        e.preventDefault();
        $(this).parents('.post').find('.comments,.comment-new,.comments-collapse').fadeIn().end().end().hide();        
    });
    $('.comments-collapse').click(function(e){
        e.preventDefault();
        $(this).parents('.post').find('.comments,.comment-new,.comments-collapse').fadeOut().end().end().hide();
        $('a.comments-list').show();        
    });
    
    $('button[id^="comment-buttoon-"]').click(function(e){
        var $this = $(this),
            art = this.id.replace('comment-buttoon-',''),
            user = this.name,
            text = $this.prev().val(),
            $container = $this.parents('.post');
            
            $this.prev().val('');
        
        if(!text){return false;} 
        
           
            
        var opts = {

			async:		false,
			url: 		'includes/comment_new.php',
			type: 		'POST',
			dataType: 	"html",
			data: 		{id:art,userid:user,comment:text,token:$('#token').val()},
			beforeSend: function(){
				//$target.siblings('p.iteminfo').next().fadeOut();
			},
			error: 		function(){_error();},
			success: 	function(response){
			 //alert(response);//debug
			     if(!response) return;
		          $container.find('.comments').append(response); 
			},
			complete: 	function(){
			     $container.find('.counter').text(parseInt($container.find('.counter').text())+1);  
			}
		
		};
        
        e.preventDefault();
		$.ajax(opts);
    });  
    
});
})(jQuery);