function watched(id, ep, lastEp, year, month, day)
{
	$.get("/ajax/episode/watched/"+year+"/"+month+"/"+day+"/"+id, function(data){
		if(data == '0')
		{
			if (lastEp == 1)
				$('#ep_img_'+id+', #ep_img_'+id).attr("src","/images/good4.gif");
			else if (ep == 1)
				$('#ep_img_'+id+', #ep_img_'+id).attr("src","/images/good2.gif");
			else
				$('#ep_img_'+id+', #ep_img_'+id).attr("src","/images/good.gif");
				
			$('#ep_box_'+id+', #ep_box_'+id).removeClass('watched');
		}
		else if (data == '1')
		{
			$('#ep_img_'+id+', #ep_img_'+id).attr("src","/images/good3.gif"); 
			$('#ep_box_'+id+', #ep_box_'+id).addClass('watched');
		}
	});
}

function getEpisodes(showId, limit, offset, epCount)
{
	$('#showInfoEps #next').unbind('click');
	$('#showInfoEps #back').unbind('click');
	$('#showInfoEps #first').unbind('click');
	$('#showInfoEps #last').unbind('click');
	
	$('#showInfoEps #loading').show();
	$.get('/ajax/episodes/'+showId+'/'+limit+'/'+offset, function(data) {
		$('#showInfoEps #overflow.allEps').html(data);
		$('#showInfoEps #loading').hide();
		
		$('#showInfoEps #count').html((offset+1)+' / '+Math.ceil((epCount/limit)));
		
		if (limit < limit*(offset+1) && (offset-1) >= 0)
		{
			$('#showInfoEps #back').removeClass('disable');
			$('#showInfoEps #first').removeClass('disable');
			$('#showInfoEps #back').click(function() {
			  getEpisodes(showId, limit, offset-1, epCount);
			});
			$('#showInfoEps #first').click(function() {
			  getEpisodes(showId, limit, 0, epCount);
			});
		}
		else
		{
			$('#showInfoEps #back').addClass('disable');
			$('#showInfoEps #first').addClass('disable');
		}
		
		if (epCount > limit*(offset+1))
		{
			$('#showInfoEps #next').removeClass('disable');
			$('#showInfoEps #last').removeClass('disable');
			$('#showInfoEps #next').click(function() {
			  getEpisodes(showId, limit, offset+1, epCount);
			});
			$('#showInfoEps #last').click(function() {
			  getEpisodes(showId, limit, Math.ceil((epCount/limit))-1, epCount);
			});
		}
		else
		{
			$('#showInfoEps #next').addClass('disable');
			$('#showInfoEps #last').addClass('disable');
		}
	});
	
}

function getNews(limit, offset, newsCount)
{
	$('#newsBox #next').unbind('click');
	$('#newsBox #prev').unbind('click');

	$.get('/ajax/news/'+limit+'/'+offset, function(data) {
		$('#newsBox').show();
		$('#newsBox .notice').html(data);

		if (limit < limit*(offset+1) && (offset-1) >= 0)
		{
			$('#newsBox #prev').removeClass('disable');
			$('#newsBox #prev').click(function() {
			  getNews(limit, offset-1, newsCount);
			});
		}
		else
		{
			$('#newsBox #prev').addClass('disable');
		}

		if (newsCount > limit*(offset+1))
		{
			$('#newsBox #next').removeClass('disable');
			$('#newsBox #next').click(function() {
			  getNews(limit, offset+1, newsCount);
			});
		}
		else
		{
			$('#newsBox #next').addClass('disable');
		}
	});
}

function runSearchBox()
{
	
	$.get('/ajax/search/?searchText='+$("#searchBox #searchText").val(), function(data) {
		$("#searchBox #searchResult").html(data);
	});
}