function performSearch(type)
{
	$("srch_results").className = "hide";
	$("srch_loading").className = "show";
	
	var srch_txt = $("srch_txt").value;
	var srch_opt = $("srch_opt").value;
	
	var ajax = new XHConn();
	ajax.connect("/listas/" + srch_opt + "/busqueda/" + srch_txt, "POST", "", searchResults);
	ajax = null;
}

var searchResults = function (oXML)
{
	$("srch_loading").className = "hide";
	
	var srch_results = $("srch_results");
	srch_results.className = "show";
	srch_results.innerHTML = oXML.responseText;
}

function addTitle(title_id)
{
	var addBox = $("addBox" + title_id);
	
	if (addBox.className.indexOf("hide") != -1)
	{
		var boxClass = addBox.className.replace("hide", "show");
	}
	else
	{
		var boxClass = addBox.className.replace("show", "hide");
	}
	
	addBox.className = boxClass;
	
	return false;
}

function editTitle(type, title_id, action)
{
	openDashboard("lists");
	var w = ie ? document.documentElement.offsetWidth : self.innerWidth;
	
	var editTitle = $("editTitle");
	editTitle.className = "show";
	editTitle.style.left = ((w - 96) / 2) + "px";
	editTitle.style.top = ((dashboardHeight - 102) / 2) + "px";
	
	if (!action)
		action = "editar_ajax";
	
	var ajax = new XHConn();
	ajax.connect("/listas/" + type + "/" + action + "/" + title_id, "POST", "", editInfo);
	ajax = null;
	
	return false;
}

function favorite(type, title_id, faved, download)
{
	var ajax = new XHConn();
	
	if (download)
		faved = faved + "/download";
	
	ajax.connect("/listas/" + type + "/favorito/" + title_id + "/" + faved, "POST", "", confirmFavorite);
	ajax = null;
	
	return false;
}

var confirmFavorite = function (oXML)
{
	var msg = oXML.responseText.split("::");
	
	if (msg[0] != "error")
	{
		//alert(msg[2]);
		$("favorite" + msg[0]).innerHTML = msg[1];
	}
	else
	{
		alert(msg[1]);
	}
}

var editInfo = function (oXML)
{
	if (oXML.responseText.indexOf("error") == -1)
	{
		var w = ie ? document.documentElement.offsetWidth : self.innerWidth;
		
		var editTitle = $("editTitle");
		editTitle.style.width = 600 + "px";
		editTitle.style.height = (dashboardHeight - 108) + "px";
		editTitle.style.left = ((w - 648) / 2) + "px";
		editTitle.style.top = 30 + "px";
		
		$("editContent").innerHTML = oXML.responseText;
	}
	else
	{
		
	}
}

function confirmAdd(type, title_id, action, editing)
{
	var status = $("status" + title_id);
	status = status[status.selectedIndex].value;
	
	var rating = $("rating" + title_id);
	rating = rating[rating.selectedIndex].value;
	
	var favorite = $("favorite" + title_id);
	favorite = favorite.checked ? 1 : 0;
	
	var episodes_watched = $("episodes_watched").value;
	
	var recommend = 0;
	/*var recommend = $("recommend" + title_id);
	recommend = recommend.checked ? 1 : 0;*/
	
	if (!action)
		action = "add";
	
	var ajax = new XHConn();
	var post_info = "anime[anime_id]=" + title_id + "&anime[view_status]=" + status + "&anime[vote]=" + rating + "&anime[episodes_watched]=" + episodes_watched + "&anime[favorited]=" + favorite + "&anime[recommend]=" + recommend;
	if (editing)
	{
		if (editing != "addnew")
		{
			var times_watched = "&anime[times_watched]=" + $("times_watched").value + "&ajax=true";
			post_info = post_info + times_watched;
		}
		else
		{
			action = action + "/inlist";
		}
		ajax.connect("/listas/" + type + "/" + action, "POST", post_info, confirmEdit);
	}
	else
	{
		$("addBox" + title_id).innerHTML = "Enviando los datos...<br /><br /><img src=\"http://images.mcanime.net/images/loading.gif\" width=\"48\" height=\"54\" alt=\"Agregando...\" title=\"Enviando los datos...\" />";
		ajax.connect("/listas/" + type + "/" + action, "POST", post_info, updateList);
	}
	
	ajax = null;
	
	if (editing)
		return false;
}

function confirmUpdate(type, title_id)
{
	confirmAdd(type, title_id, "actualizar");
}

function confirmDelete(type, title_id, inList)
{
	var doDelete = confirm("�Estas seguro que quieres remover este " + type + " de tu lista?");
	
	if (doDelete)
	{
		var ajax = new XHConn();
		
		var listDelete = "";
		if (inList)
			listDelete = "/inlist";
		
		ajax.connect("/listas/" + type + "/remover/" + title_id + listDelete, "POST", "", deleteList);
	}
	
	return false
}

var updateList = function (oXML)
{
	if (oXML.responseText.indexOf("error") == -1)
	{
		var recentBox = $("recently_added");
		var rows = recentBox.getElementsByTagName("div");
		var total_rows = rows.length;
		
		var listRow = document.createElement("div");
		listRow.className = "added_row";
		listRow.innerHTML = oXML.responseText;
		
		if (total_rows == 0)
		{
			recentBox.appendChild(listRow);
		}
		else
		{
			if (total_rows > 4)
			{
				var lastRow = rows[total_rows - 1];
				recentBox.removeChild(lastRow);
			}
			
			recentBox.insertBefore(listRow, rows[0]);
		}
		
		var added = listRow.getElementsByTagName("input")[0];
		var addRow = $("addRow" + added.value);
		listRow.id = "arow" + added.value;
		
		addRow.parentNode.removeChild(addRow);
		added.parentNode.removeChild(added);
	}
	else
	{
		var error = oXML.responseText.split("::");
		$("addBox" + error[1]).innerHTML = error[2];
	}
}

var confirmEdit = function (oXML)
{
	var msg = oXML.responseText.split("::");
	if (msg[0] == "error")
	{
		var txt = "<div class=\"error\">" + msg[2] + "</div>";
	}
	else
	{
		var txt = "<div class=\"confirm\">" + msg[2] + "</div>";
	}
	
	if (msg[0] == "inlist")
	{
		var add = $("add" + msg[1]);
		add.parentNode.removeChild(add);
	}
	
	$("editContent").innerHTML = txt;
	
	var editTitle = $("editTitle");
	var w = ie ? document.documentElement.offsetWidth : self.innerWidth;
	
	editTitle.style.height = "auto";
	editTitle.style.top = ((dashboardHeight - editTitle.offsetHeight) / 2) + "px";
}

var deleteList = function (oXML)
{
	var txt = oXML.responseText;
	if (txt.indexOf("error") != -1)
	{
		alert("Ha ocurrido un error al tratar de remover el anime especificado. Porfavor intentelo de nuevo.");
	}
	else if (txt.indexOf("inlist") != -1)
	{
		txt = txt.split("::");
		var listRow = $("list" + txt[1]);
		
		//alert(txt[2]);
		
		listRow.parentNode.removeChild(listRow);
	}
	else
	{
		var title_id = oXML.responseText;
		var arow = $("arow" + title_id);
		
		arow.parentNode.removeChild(arow);
	}
}

function cancelAdd(title_id)
{
	addTitle(title_id);
}

function titleInfo(title_id)
{
	var infoRow = $("info" + title_id);
	var rowClass = infoRow.className;
	
	if (rowClass.indexOf("hide") != -1)
	{
		rowClass = rowClass.replace("hide", "show");
	}
	else
	{
		rowClass = rowClass.replace("show", "hide");
	}
	
	infoRow.className = rowClass;
	
	return false;
}

function rateSeries(type, title_id, vote, inList)
{
	if (inList)
		vote = vote + "&inList=inlist";
		
	var ajax = new XHConn();
	ajax.connect("/listas/" + type + "/rateSeries/", "POST", "anime_id=" + title_id + "&vote=" + vote, confirmRating);
	ajax = null;
	
	return false;
}

var confirmRating = function (oXML)
{
	var msg = oXML.responseText.split("::");
	
	if (msg[0] == "confirm")
	{
		var ratingBlock = $("s" + msg[1]);
		var ratingUl = ratingBlock.getElementsByTagName("ul")[0];
		
		var currentRating = ratingUl.childNodes[0];
		if (currentRating.nodeType == 3)
			currentRating = ratingUl.childNodes[1];
		
		var c_width = (msg[2] / 10) * 65;
		currentRating.style.width = c_width.toFixed(0) + "px";
		
		//alert("Tu voto ha sido guardado.");
	}
}

function checks(formObj)
{
	var checked = 0;
	var ins = formObj.getElementsByTagName("input");
	var len = ins.length;
	
	for (var i = 0; i < len; i++)
	{
		if (ins[i].type == "checkbox" && ins[i].checked)
		{
			checked++;
		}
	}
	
	if (checked == 0)
	{
		alert("No has seleccionado ning�n titulo para actualizar.");
		return false;
	}
	
	return true;
}

function checkViewStatus(obj)
{
	var status = obj[obj.selectedIndex].value;
	
	if (status == 'C')
	{
		var num = parseInt($('total_releases').childNodes[0].nodeValue);
		$('episodes_watched').value = num ? num : 0;
	}
	else
		$('episodes_watched').value = 0;
}

