
$(document).ready(function() {
  // do nothing
  });

function changeep_loadurl(list_entry_id, totalEpisodes, animeStatus, uStatus, inepVal, animeid) 
{ 
	var episode_val = document.getElementById('epID'+list_entry_id);
	var myVal = episode_val.value;
	var epNum;
	
	if (inepVal == null || inepVal == 0)
		epNum = myVal;
	else
		epNum = inepVal;

	epNum = Number(epNum);
	
	if ( isNaN(epNum) || ( (epNum>totalEpisodes) && (totalEpisodes != 0) ) || (epNum<0) )
		{
		alert('Invalid number supplied, try again.');
		return false;
		}
	else
		{
		// allow update		
		//actualurl = '../includes/change_ep.php?series_id='+list_entry_id+'&ep_val='+epNum;
		$.post("/includes/ajax.inc.php?t=79", {anime_id:animeid, ep_val:epNum}, function(data)
			{
			document.getElementById("output"+list_entry_id).innerHTML = epNum;
			var dynamicEpNum = document.getElementById("holdDynamicEpNum"+list_entry_id);
			dynamicEpNum.innerHTML = epNum+1;
			}
		);	
		
		if ( (uStatus == 2) && (totalEpisodes == epNum) )
			{
			// if you've already watched this, we're gonna ask if you want to set it as done rewatching
			document.getElementById("inlineContent").innerHTML = '<div style="font-family: verdana, arial; font-size: 11px; text-align: center;">Do you want to set this show as finished rewatching?<div style="margin-top: 5px;"><input type="button" onclick="doneRewatching('+animeid+');" value="Yes, I am done Rewatching">&nbsp;&nbsp;<input type="button" value="Not Finished Rewatching" onclick="notdoneRewatching();"></div>';
			tb_show('Finished Rewatching?', '#TB_inline#TB_hideclose?height=50&width=500&inlineId=inlineContent', null); 
			}
		else if ( (totalEpisodes == epNum) && (animeStatus == 2) ) // ask to set to completed, anime series status has to be completed (finished airing
			{
			// prompt question
			document.getElementById("inlineContent").innerHTML = '<div style="font-family: verdana, arial; font-size: 11px; text-align: center;">Do you want to set this entry as completed?<div style="margin-top: 5px;"><input type="button" onclick="markComplete('+list_entry_id+','+epNum+','+animeid+');" value="Set as Completed">&nbsp;&nbsp;<input type="button" value="Do not set as Completed" onclick="doNotMove('+epNum+','+animeid+','+list_entry_id+');"></div>';
			tb_show('Mark Entry as Completed?', '#TB_inline#TB_hideclose?height=50&width=375&inlineId=inlineContent', null); 
			}
		else if ( (uStatus == 3) || (uStatus == 6) ) // if userStatus is on-hold or plan to watch
			{
			document.getElementById("inlineContent").innerHTML = '<div style="font-family: verdana, arial; font-size: 11px; text-align: center;">Move entry to watching?<div style="margin-top: 5px;"><input type="button" onclick="markWatching('+list_entry_id+','+epNum+','+animeid+');" value="Set as Watching">&nbsp;&nbsp;<input type="button" value="Do Not Move" onclick="doNotMove('+epNum+','+animeid+');"></div>';
			tb_show('Move Entry to Watching?', '#TB_inline#TB_hideclose?height=50&width=375&inlineId=inlineContent', null); 
			}
		else
			setTimeout("askToDiscuss("+epNum+","+animeid+","+list_entry_id+")",500);
			
		determineEpVisibility(list_entry_id);
		episode_val.value = '';
		return false;
		}
}

function doneRewatching(anime_id)
{
	$.post("/includes/ajax.inc.php?t=59", {aid:anime_id}, function(data)
			{
			// do nothing
			self.parent.tb_remove();
			}
		);
}

function doneRereading(manga_id)
{
	$.post("/includes/ajax.inc.php?t=81", {mid:manga_id}, function(data)
			{
			// remove thickbox
			self.parent.tb_remove();
			}
		);
}

function notdoneRewatching()
{
	self.parent.tb_remove();
}

function askToDiscuss(ep,animeid,list_id)
{
	//var theurl = "../includes/ajax.inc.php?t=50&epNum="+epNum+"&aid="+animeid+"&id="+list_id;
	$.post("/includes/ajax.inc.php?t=50", {epNum:ep, aid:animeid, id:list_id}, function(data)
		{
		var myRegExp = /trueEp/;
		var matchPos = data.search(myRegExp);
		
		if (matchPos != -1) // found it, so display TB
			{
			//alert('test2');
			document.getElementById("inlineContent").innerHTML = '<div style="font-family: verdana, arial; font-size: 11px; text-align: center;">'+data+'</div>';
			tb_show('Discuss this Episode?', '#TB_inline#TB_hideclose?height=50&width=650&inlineId=inlineContent', null); 	
			}
		}
	);
}

function notthisseries(list_id)
{
	$.post("/includes/ajax.inc.php?t=53", {id:list_id}, function(data)
			{
			self.parent.tb_remove();
			}
		);	
}

function dontAsk()
{
	$.post("../includes/ajax.inc.php?t=51", {y:1}, function(data)
			{
			// do nothing
			self.parent.tb_remove();
			}
		);
}

function doNotMove(epNum,animeid,list_id)
{
	self.parent.tb_remove();
	setTimeout("askToDiscuss("+epNum+","+animeid+","+list_id+")",500);
}

function markWatching(list_entry_id,epNum,animeid)
{
	$.post("/includes/ajax.inc.php?t=21", {eid:list_entry_id}, function(data)
			{
			self.parent.tb_remove();
			setTimeout("askToDiscuss("+epNum+","+animeid+","+list_entry_id+")",500);
			}
		);
}

function markComplete(list_entry_id,epNum,animeid)
{
	$.post("/includes/ajax.inc.php?t=18", {aid:animeid}, function(data)
			{
			self.parent.tb_remove();
			setTimeout("askToDiscuss("+epNum+","+animeid+","+list_entry_id+")",500);
			}
		);
}

function updateEpNumByOne(list_entry_id, totalEpisodes, animeStatus, uStatus, animeid)
{
	var dynamicEpNum = document.getElementById("holdDynamicEpNum"+list_entry_id);
	var dynamicEpNumVal = Number(dynamicEpNum.innerHTML);
	
	changeep_loadurl(list_entry_id,totalEpisodes,animeStatus,uStatus,dynamicEpNumVal,animeid);
}

function determineEpVisibility(layer_id)
{
    
    var myLayer = document.getElementById('epLayer'+layer_id);
    var visibleVal = (myLayer.style.display != "none");
    var myText = document.getElementById('epText'+layer_id);
    
    
    if (visibleVal)
			{
			//key.innerHTML = "+";
            // hide 
			myLayer.style.display = "none"; 
	 		myText.style.fontWeight = "normal";
			}
		else
			{
			//key.innerHTML = "-";
            //show 
            
			myLayer.style.display = "block";
			myText.style.fontWeight = "bold";
            document.getElementById('epID'+layer_id).focus();
			}
    
}    

function tag_add(aid,type)
{
	var url;
	var tagString = document.getElementById("tagInfo"+aid).value;
	document.getElementById("tagRowEdit"+aid).style.display = 'none';
	document.getElementById("tagLinks"+aid).style.display = 'block';
	document.getElementById("tagLinks"+aid).innerHTML = 'Saving...';
	document.getElementById("tagRow"+aid).innerHTML = tagString;
	
	if (type == 1)
		{
		url = "/includes/ajax.inc.php?t=22&aid="+aid+"&tags="+tagString;
		}
	else
		url = "/includes/ajax.inc.php?t=30&mid="+aid+"&tags="+tagString;
	
	//alert(url);
	
	$.get(url, function(data)
			{
			//alert(data);
   			document.getElementById("tagLinks"+aid).innerHTML = data;
			document.getElementById("tagLinks"+aid).style.display = 'block';
			document.getElementById("tagChangeRow"+aid).style.display = 'block';
	 		}
		);
}

function tag_showEdit(aid,type)
{
	var oldTags = document.getElementById("tagRow"+aid).innerHTML;
	document.getElementById("tagLinks"+aid).style.display = 'none';
	document.getElementById("tagChangeRow"+aid).style.display = 'none';
	
	var tagRowEditObj = document.getElementById("tagRowEdit"+aid);
	tagRowEditObj.style.display = 'block';
	tagRowEditObj.innerHTML = '<textarea id="tagInfo'+aid+'" onkeydown="tag_checkEnter(event,'+aid+','+type+');"  class="inputtext" cols="12" rows="3">'+oldTags+'</textarea><div style="margin-top: 3px;"><span style="float: right;"><input type="button" value="Save" onclick="tag_add('+aid+','+type+')"></span><span style="float: left;"><input type="button" value="Cancel" onclick="tag_cancelAdd('+aid+')"></span></div>';
	document.getElementById("tagInfo"+aid).focus();
}

function tag_cancelAdd(aid)
{
	//alert('test');
	var oldTags = document.getElementById("tagRow"+aid).value;
	document.getElementById("tagLinks"+aid).style.display = 'block';
	document.getElementById("tagChangeRow"+aid).style.display = 'block';
	document.getElementById("tagRowEdit"+aid).style.display = 'none';
}

function tag_checkEnter(e,aid,type)
{
	var key;

	 if(window.event)
		  key = window.event.keyCode;     //IE
	 else
		  key = e.which;     //firefox

	 if (key == 13)
		{
		tag_add(aid,type);
		return false;
		}
	 else
		return true;
}

function manga_updateVol(id,type,manga_id)
{
	var volObj = document.getElementById("vol"+id);
	var curVol = volObj.innerHTML;
	var volNum = 0;
	
	if (type == 1) // update normally
		{
		volNum = document.getElementById("voltext"+id).value;
		}
	else // update through "+"
		{
		if (isNaN(curVol))
			curVol = 0;
		
		volNum = Number(curVol) + 1;
		}
	
	volNum = Number(volNum);
	
	if ( isNaN(volNum) || (volNum<0) )
		{
		alert('Invalid number supplied, try again.');
		return false;
		}
	else
		{
		document.getElementById("voldiv"+id).style.display = 'none';
		$.post("/includes/ajax.inc.php?t=32", {mid:manga_id,vol:volNum}, function(data)
				{
				volObj.innerHTML = volNum;
				document.getElementById("voltext"+id).value = '';
				}
			);
		}
}

function manga_updateChap(id,type,manga_id,total_chapters,curStatus)
{
	var chapObj = document.getElementById("chap"+id);
	var curChap = chapObj.innerHTML;
	var chapNum = 0;
	
	if (type == 1) // update normally
		{
		chapNum = document.getElementById("chaptext"+id).value;
		}
	else // update through "+"
		{
		if (isNaN(curChap))
			curChap = 0;
		
		chapNum = Number(curChap) + 1;
		}
	
	chapNum = Number(chapNum);
	
	
	//alert(chapNum);
	if ( isNaN(chapNum) || (chapNum<0) )
		{
		alert('Invalid number supplied, try again.');
		return false;
		}
	else
		{
		document.getElementById("chapdiv"+id).style.display = 'none';
		$.post("/includes/ajax.inc.php?t=31", {eid:id,mid:manga_id,chap:chapNum}, function(data)
				{
				chapObj.innerHTML = chapNum;
				document.getElementById("chaptext"+id).value = '';
				
				if ( (curStatus == 2) && (total_chapters == chapNum) )
					{
					// if you've already read this, we're gonna ask if you want to set it as done rereading
					document.getElementById("inlineContent").innerHTML = '<div style="font-family: verdana, arial; font-size: 11px; text-align: center;">Set this series as finished rereading?<div style="margin-top: 5px;"><input type="button" onclick="doneRereading('+manga_id+');" value="Yes, I am done Rereading">&nbsp;&nbsp;<input type="button" value="Not Finished" onclick="notdoneRewatching();"></div>';
					tb_show('Finished Rereading?', '#TB_inline#TB_hideclose?height=50&width=500&inlineId=inlineContent', null); 
					}
				else if (total_chapters == chapNum) // ask to set as completed
					{
					document.getElementById("inlineContent").innerHTML = '<div style="font-family: verdana, arial; font-size: 11px; text-align: center;">Set this entry as completed?<div style="margin-top: 5px;"><input type="button" onclick="manga_markComplete('+id+','+chapNum+','+manga_id+');" value="Set as Completed">&nbsp;&nbsp;<input type="button" value="Do not set as Completed" onclick="manga_doNotMove('+chapNum+','+manga_id+','+id+');"></div>';
					tb_show('Set Manga as Completed?', '#TB_inline#TB_hideclose?height=50&width=400&inlineId=inlineContent', null); 
					}
				else if ( (curStatus == 3) || (curStatus == 6) ) // if userStatus is on-hold or plan to watch
					{
					document.getElementById("inlineContent").innerHTML = '<div style="font-family: verdana, arial; font-size: 11px; text-align: center;">Move entry to reading?<div style="margin-top: 5px;"><input type="button" onclick="manga_markReading('+id+','+chapNum+','+manga_id+');" value="Set as Reading">&nbsp;&nbsp;<input type="button" value="Do Not Move" onclick="manga_doNotMove('+chapNum+','+manga_id+');"></div>';
					tb_show('Set Manga as Reading?', '#TB_inline#TB_hideclose?height=50&width=375&inlineId=inlineContent', null); 
					}
				else
					setTimeout("manga_askToDiscuss("+chapNum+","+manga_id+","+id+")",500);
				
				}
			);
		
		}
}

function manga_markReading(list_entry_id,chap,manga_id)
{
	$.post("/includes/ajax.inc.php?t=57", {id:list_entry_id}, function(data)
			{
			self.parent.tb_remove();
			setTimeout("manga_askToDiscuss("+chap+","+manga_id+","+list_entry_id+")",500);
			}
		);
}

function manga_askToDiscuss(chapter,manga_id,list_id)
{
	//var theurl = &chap="+chap+"&mid="+manga_id+"&id="+list_id;
	//alert(theurl);
	$.post("/includes/ajax.inc.php?t=55", {chap:chapter,mid:manga_id,id:list_id}, function(data)
		{
		var myRegExp = /trueChap/;
		var matchPos = data.search(myRegExp);
		//alert('test');
		
		if (matchPos != -1) // found it, so display TB
			{
			//alert(data);
			document.getElementById("inlineContent").innerHTML = '<div style="font-family: verdana, arial; font-size: 11px; text-align: center;">'+data+'</div>';
			tb_show('Discuss this Chapter?', '#TB_inline#TB_hideclose?height=50&width=650&inlineId=inlineContent', null); 	
			}
		}
	);
}

function manga_notthisseries(list_id)
{
	$.post("/includes/ajax.inc.php?t=56", {id:list_id}, function(data)
			{
			self.parent.tb_remove();
			}
		);	
}

function manga_dontAsk()
{
	$.post("/includes/ajax.inc.php?t=71", {y:1}, function(data)
			{
			self.parent.tb_remove();
			}
		);
}

function manga_markComplete(entry_id,chap,manga_id)
{
	$.post("/includes/ajax.inc.php?t=54", {mid:manga_id}, function(data)
			{
			self.parent.tb_remove();
			self.parent.location.reload(true);
			setTimeout("manga_askToDiscuss("+chap+","+manga_id+","+entry_id+")",500);
			}
		);
}


function manga_doNotMove(chap,manga_id,id)
{
	self.parent.tb_remove();
	setTimeout("manga_askToDiscuss("+chap+","+manga_id+","+id+")",500);
}


function manga_showTextVol(id)
{
	var obj = document.getElementById("voldiv"+id);
	$(obj).toggle();
	document.getElementById("voltext"+id).focus();
}

function manga_showTextChap(id)
{
	var obj = document.getElementById("chapdiv"+id);
	$(obj).toggle();
	document.getElementById("chaptext"+id).focus();
}

function manga_checkEnter(e,type,id,manga_id,total_chapters,user_status)
{
	var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

	if (key == 13)
	 	{
	 	if (type == 1) // chapters
			manga_updateChap(id,1,manga_id,total_chapters,user_status);
		else if (type == 2) // volumes
			manga_updateVol(id,1,manga_id,total_chapters,user_status);
		else if (type == 3)
			manga_updateScore(id,manga_id);
		}
     else
          return true;
}

function manga_toggleScore(id)
{
	$('#scorediv'+id).toggle();
	document.getElementById("scoretext"+id).focus();	
}

function manga_updateScore(id,manga_id)
{
	var newScore = document.getElementById("scoretext"+id).value;
	
	newScore = Number(newScore);
	
	if ( isNaN(newScore) || (newScore>10) || (newScore<0) )
		{
		alert('Invalid score value, must be between 1 and 10');	
		}
	else
		{
		document.getElementById("scorediv"+id).style.display = 'none';
		$.post("/includes/ajax.inc.php?t=33", {mid:manga_id,score:newScore}, function(data)
				{
				document.getElementById("score"+id).innerHTML = newScore;
				document.getElementById("scoretext"+id).value = '';
				}
			);
		}
}

function anime_updateScore(entry_id)
{
	var newScore = document.getElementById("scoretext"+entry_id).value;
	$.post("/includes/ajax.inc.php?t=63", {id:entry_id,score:newScore}, function(data)
			{
			///joinalert(data);
			document.getElementById("scoreval"+entry_id).innerHTML = newScore;
			document.getElementById("scoretext"+entry_id).value = '';
			document.getElementById("scorediv"+entry_id).style.display = 'none';
			}
		);
}

function anime_scoreDisplay(id)
{
	var obj = document.getElementById("scorediv"+id);
	$(obj).toggle();
	document.getElementById("scoretext"+id).focus();
}

function anime_checkScoreEnter(e,id)
{
	var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

	if (key == 13)
	 	anime_updateScore(id);
     else
          return true;
}