$(document).ready(function() {
	show_input_warning = true;
    $('#chude_cua_tuan').jcarousel();
	
	/* Begin: AJAX Navigation */
	$('.post_prev').click(function(){
		return ajax_previous();
    });
	
    $('.round_separator').click(function(){
		return ajax_next();
    });

    $('.post_next').click(function(){
		return ajax_next();
    });
	/* End: AJAX Navigation */
	
	$('#tranhLuanNhanh_submit').click(function(){
										if(show_input_warning || $.trim($('#PostContent').val()) == ""){
											message = "<div>Nội dung reply không được rỗng!</div>";
											$('#dialog p').html(message)
											$('#dialog').dialog('open');
											return false;
										}
										return true;
									}
							);
	/* AJAX POST */
    var options = {
        success:  showResponse  // post-submit callback 
    }; 
	$('#tranhLuanNhanhAddForm').ajaxForm(options); 
})

/* Begin: AJAX Navigation */
function ajax_previous(){
	if(current_page > 0) {
		$('.place-holder').stop();
		current_page -= 1;
		$('.place-holder').fadeOut('slow',function(){
			$('.loader').show();
			$('.round_separator').hide();
			$('.place-holder').html('<img src="/NesCafe/img/ajax-loader.gif" />');
			$.get(webroot+'pages/get_replies_of_weekpost/'+newest_post_id+'/'+current_page*4,{},function(data){
				$('.place-holder').html(data);
				$('.loader').hide();
				$('.round_separator').show();
				$('.place-holder').fadeIn('slow');
			});
		});
	}
	return false;
}

function ajax_next(){
	if(current_page < (page - 1)){
		$('.place-holder').stop();
		current_page += 1;
		$('.place-holder').fadeOut('slow',function(){
			$('.loader').show();
			$('.round_separator').hide();
			$.get(webroot+'pages/get_replies_of_weekpost/'+newest_post_id+'/'+current_page*4,{},function(data){
					$('.place-holder').html(data);
					$('.loader').hide();
					$('.round_separator').show();
					$('.place-holder').fadeIn('slow');
			});
		});
	}
	return false;
}
/* End: AJAX Navigation */

function showResponse(responseText, statusText)  {
	$('#dialog p').html(responseText)
	$('#dialog').dialog('open');
}

function post_toggle(link, post_id, topic_id){
	// Giu lai trang thai
	var class_name = $('#icon'+post_id).attr('class');
	
	// Truoc het:  reset (an di) tat ca cac chu de con lai
	$('.topic_collapse' + topic_id).show();
	$('.topic_expand' + topic_id).hide();
	var topic_icon_class_name = 'topic_icon' + topic_id;
	$('.' + topic_icon_class_name).attr( { "class": "icon_expand " + topic_icon_class_name } );
	
	if(class_name.indexOf("icon_expand") != -1){
		$('#icon'+post_id).attr( { "class": "icon_collapse " + topic_icon_class_name} );
		$('#expand'+post_id).load(link);// load ajax
		$('#collapse'+post_id).hide();
		$('#expand'+post_id).show();
	} else {
		$('#icon'+post_id).attr( { "class": "icon_expand " + topic_icon_class_name} );
		$('#expand'+post_id).show();
		$('#expand'+post_id).hide();
	}
}
