/**
 * SwiftPanda Script
 * Version 1.0
 */

// Cufon Font Replace
Cufon.replace("h1", "Futura Md BT");
Cufon.replace("h2", "Futura Md BT");

// Preload Anime Series images
var animeSeries = new Array();
animeSeries[0] = new Image();
animeSeries[1] = new Image();
animeSeries[0].src = "http://static.swiftpanda.com/images/animeseries.png";
animeSeries[1].src = "http://static.swiftpanda.com/images/animeseries_hover.png";

// Preload Recently Added images
var recentlyAdded = new Array();
recentlyAdded[0] = new Image();
recentlyAdded[1] = new Image();
recentlyAdded[0].src = "http://static.swiftpanda.com/images/recentlyadded.png";
recentlyAdded[1].src = "http://static.swiftpanda.com/images/recentlyadded_hover.png";

// Preload PandaLife images
var pandaLife = new Array();
pandaLife[0] = new Image();
pandaLife[1] = new Image();
pandaLife[0].src = "http://static.swiftpanda.com/images/pandalife.png";
pandaLife[1].src = "http://static.swiftpanda.com/images/pandalife_hover.png";

// Preload Spotlight images
var spotlight = new Array();
spotlight[0] = new Image();
spotlight[1] = new Image();
spotlight[0].src = "http://static.swiftpanda.com/images/spotlight.png";
spotlight[1].src = "http://static.swiftpanda.com/images/spotlight_hover.png";

// Preload Forum images
var forum = new Array();
forum[0] = new Image();
forum[1] = new Image();
forum[0].src = "http://static.swiftpanda.com/images/forum.png";
forum[1].src = "http://static.swiftpanda.com/images/forum_hover.png";

// Preload Watch Now images
var watchNow = new Array();
watchNow[0] = new Image();
watchNow[1] = new Image();
watchNow[0].src = "http://static.swiftpanda.com/images/spotlight_box_watch_now.png";
watchNow[1].src = "http://static.swiftpanda.com/images/spotlight_box_watch_now_hover.png";

// Preload Episodes images
var episodes = new Array();
episodes[0] = new Image();
episodes[1] = new Image();
episodes[2] = new Image();
episodes[0].src = "http://static.swiftpanda.com/images/tab_episodes.png";
episodes[1].src = "http://static.swiftpanda.com/images/tab_episodes_hover.png";
episodes[2].src = "http://static.swiftpanda.com/images/tab_episodes_active.png";

// Preload OVAs images
var ovas = new Array();
ovas[0] = new Image();
ovas[1] = new Image();
ovas[2] = new Image();
ovas[0].src = "http://static.swiftpanda.com/images/tab_ovas.png";
ovas[1].src = "http://static.swiftpanda.com/images/tab_ovas_hover.png";
ovas[2].src = "http://static.swiftpanda.com/images/tab_ovas_active.png";

// Preload Movies images
var movies = new Array();
movies[0] = new Image();
movies[1] = new Image();
movies[2] = new Image();
movies[0].src = "http://static.swiftpanda.com/images/tab_movies.png";
movies[1].src = "http://static.swiftpanda.com/images/tab_movies_hover.png";
ovas[2].src = "http://static.swiftpanda.com/images/tab_movies_active.png";

// Hover
function rollOver(image)
{
	document.getElementById(image).src = eval(image + "[1]").src;
}

// Normal
function rollOut(image)
{
	document.getElementById(image).src = eval(image + "[0]").src;
}

// Toggle Table
function toggleTable(cell, season)
{
	var id = "season" + season;
	if(cell.innerHTML.indexOf('+') >= 0)
	{
		document.getElementById(id).style.display = "block";
		cell.innerHTML = "- Season " + season + " -";
	}
	else
	{
		document.getElementById(id).style.display = "none";
		cell.innerHTML = "+ Season " + season + " +";
	}
}

// Set Tab
function setTab(id, tab)
{
	var xmlhttp;
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		alert("Your browser does not support XML HTTP!");
	}
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 1)
		{
			var tabs = new Array();
			tabs[2] = "episodes";
			tabs[1] = "ovas";
			tabs[0] = "movies";
			
			var database = new Array();
			for(var i = 2; i>= 0; i--)
			{
				database[i] = false;
				if(document.getElementById("tabs").innerHTML.indexOf(tabs[i] + "_inactive") >= 0)
				{
					database[i] = true;
				}
			}
			
			var html = "";
			var beginAnchor;
			var endAnchor;
			for(var i = 2; i >= 0; i--)
			{
				beginAnchor = "";
				endAnchor = "";
				
				if(i != tab && database[i])
				{
					beginAnchor = "<a href=\"#\" onmouseover=\"rollOver('" + tabs[i] +
						"')\" onmouseout=\"rollOut('" + tabs[i] + "')\" onclick=\"return setTab('" +
						id + "', " + i + ")\">";
					endAnchor = "</a>";
				}
				html += beginAnchor + "<img id=\"" + tabs[i] +
					"\" src=\"http://static.swiftpanda.com/images/tab_" + tabs[i];
				if(i == tab)
				{
					html += "_active";
				}
				else if(!database[i])
				{
					html += "_inactive";
				}
				html += ".png\" alt=\"" + tabs[i] + "\" />" + endAnchor + "\n";
			}
			
			document.getElementById("tabs").innerHTML = html;
			document.getElementById("tabContent").innerHTML = "<div style=\"height: 100px; line-height: 100px; text-align: center\"><img src=\"http://static.swiftpanda.com/images/loader.gif\" \"/></div>";
		}
		else if(xmlhttp.readyState == 4)
		{
			document.getElementById("tabContent").innerHTML = xmlhttp.responseText;
			Cufon.replace("h2", "Futura Md BT");
		}
	}
	
	xmlhttp.open("GET", "/tab_content.php?id=" + id + "&tab=" + (tab - 1), true);
	xmlhttp.send(null);
	
	return false;
}

function lookUpStyle(focus)
{
	var input = document.getElementById("lookUpQuery");
	if(focus)
	{
		if(input.value == "Enter an Anime")
		{
			input.value = "";
			input.style.color = "#000000";
		}
	}
	else
	{
		if(input.value.length == 0)
		{
			input.style.color = "#a5a5a5";
			input.value = "Enter an Anime";
		}
		var results = document.getElementById("lookUpResults");
		results.style.visibility = "hidden";
		results.innerHTML = "";
		results.style.height = "0px";
	}
}

function lookUp(query)
{
	var results = document.getElementById("lookUpResults");
	var xmlhttp;
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		alert("Your browser does not support XML HTTP!");
	}
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{
			if(xmlhttp.responseText.length)
			{
				results.style.height = (xmlhttp.responseText.split("\n").length - 1) * 20 + "px";
				results.innerHTML = xmlhttp.responseText;
			}
			else
			{
				results.style.height = "20px";
				results.innerHTML = "Anime not found.";
			}
		}
	}
	
	if(query.length > 0)
	{
		results.style.visibility = "visible";
		
		xmlhttp.open("GET", "/lookup.php?query=" + query, true);
		xmlhttp.send(null);
	}
	else
	{
		results.style.visibility = "hidden";
		results.innerHTML = "";
		results.style.height = "0px";
	}
}