var video_first = true;
var VIDEO_TYPING_WARNING = 'Vui lòng bình luận bằng tiếng việt có dấu.';
var post_id;
$(document).ready(function() {
   $('#mycarousel').jcarousel({scroll:1});
   
   $('.trochuyen_next').click(function(){
   	post_id = $(this).attr('id');
   	post_id = post_id.split('_');
   	id = post_id[1];	
   	
   	if(current_pages[id] < total_pages[id]){
   		$('.ajax_separator').children('#trochuyen_separator_'+id).hide();
   	   	$('.ajax_separator').children('#Loader_'+id).show(); 
   	   	
	   	$.get(webroot+'trochuyen/getMoreReplies2Items',{id:id,offset:(current_pages[id]+1)*2},function(data){
	   		if(data){
	   			$('.changethis_'+id).hide('fast',function(){
		   			$('.ajax_separator').children('#trochuyen_separator_'+id).show();
		   		   	$('.ajax_separator').children('#Loader_'+id).hide();
		   			$('.changethis_'+id).html(data);
		   			$('.changethis_'+id).show();
		   			current_pages[id] += 1;
	   			});
	   		}	
	   		return false;
	   	});
   	}   	
   	return false;
   });
   
   $('.trochuyen_prev').click(function(){
	   	post_id = $(this).attr('id');
	   	post_id = post_id.split('_');
	   	id = post_id[1]; 
	   	
	   	if(current_pages[id] > 0){
	   		
	   		$('.ajax_separator').children('#trochuyen_separator_'+id).hide();
		   	$('.ajax_separator').children('#Loader_'+id).show();
		   	
		   	$.get(webroot+'trochuyen/getMoreReplies2Items',{id:id,offset:(current_pages[id]-1)*2},function(data){
		   		if(data){
		   			$('.changethis_'+id).fadeOut('fast',function(){
			   			$('.ajax_separator').children('#trochuyen_separator_'+id).show();
			   		   	$('.ajax_separator').children('#Loader_'+id).hide();
			   			$('.changethis_'+id).html(data);
			   			$('.changethis_'+id).fadeIn('fast');
		   			current_pages[id] -= 1;
		   			});
		   		}
		   		return false;
		   	});
	   	}
	   	
	   	return false;
	   });
   
	$('#video_moduleAddForm').submit(function(){
										if(video_first || $.trim($('#PostContent1').val()) == ""){
											message = "<div>Vui lòng cung cấp nội dung!</div>";
											$('#dialog p').html(message)
											$('#dialog').dialog('open');
											return false;
										}
										return true;
									}
							);
	
	/*
     * Begin: Ajax Chu de hot
     */

    $('.post_prev').click(function(){
		return ajax_previous();
    });
	
	$('.round_separator').click(function(){
		return ajax_next();
    });
	
    $('.post_next').click(function(){
		return ajax_next();
    });
    
    
    
    /*
     * End: Ajax Chu de hot
     */
	 
	
	
	/* Validate form cua Video module */
	$('#PostContent1').click(function(){
		 $('#PostContent1').removeClass('color1');
		 $('#PostContent1').addClass('color2');
		if(video_first){
            $('#PostContent1').val('');
		}
		video_first = false;
	});
	$('#PostContent1').blur(function(){
		if($.trim($('#PostContent1').val())==''){
            $('#PostContent1').val(VIDEO_TYPING_WARNING);
            $('#PostContent1').removeClass('color2');
            $('#PostContent1').addClass('color1');
			video_first = true;
		}
	});
});

/* Begin: Ajax Chu de hot */
function ajax_previous(){
	if(current_page > 0) {
		$('.video_place_holder').stop();
		current_page -= 1;
		$('.video_place_holder').fadeOut('slow',function(){
			$('.loader').show();
			$('.round_separator').hide();
			//$('.video_place_holder').html('<img src="/NesCafe/img/ajax-loader.gif" />');
			$.get(webroot+'trochuyen/getRepliesOfVideo/'+newest_post_id+'/'+current_page*video_replies_limit,{},function(data){
				$('.video_place_holder').html(data);
				$('.loader').hide();
				$('.round_separator').show();
				$('.video_place_holder').fadeIn('slow');
			});
		});
	}
	return false;
}

function ajax_next(){
	if(current_page < (page - 1)){
		$('.video_place_holder').stop();
		current_page += 1;
		$('.video_place_holder').fadeOut('slow',function(){
			$('.loader').show();
			$('.round_separator').hide();
			$.get(webroot+'trochuyen/getRepliesOfVideo/'+newest_post_id+'/'+current_page*video_replies_limit,{},function(data){
					$('.video_place_holder').html(data);
					$('.loader').hide();
					$('.round_separator').show();
					$('.video_place_holder').fadeIn('slow');
			});
		});
	}
	return false;
}
/* End: Ajax Chu de hot */

/*function video_module_textbox_click(){
	if(video_first){
		$('#PostContent1').attr({"value": ''});
		video_first = false;
	}
}*/

function goAdvanceSubmit(url){
	$('#createNewPost').attr({'action' : url});
}
