var stuff = Array();
var formSubmit = null;

var kbPath = "../";
var scriptVersion = "6";

function image(id, mode, title)
{
	return "<a class=\"lightbox-enabled\" href=\"" + kbPath + "images/full/" + id + ".png\" title=\"" + title + "\"><img src=\"" + kbPath + "images/" + mode + "/" + id + ".png\" title=\"" + title + "\" alt=\"" + title + "\" /></a>";
}
function isInt(x)
{
	var y = parseInt(x);
	if (isNaN(y)) 
		return false;
	return x == y && x.toString() == y.toString();
} 
function getId()
{
	return $("#id").html();	
}

$(document).ready(function()
{
	if (latestVersion > scriptVersion)
	{
		alert("You are using an outdated version of the encyclopedia script. This can be fixed by refreshing the page or clearing your browser cache, either of which should force your browser to download to the newest version.");
	}
	$(".search-site").click(function(){
		$("#search-site").animate({
			opacity: 'toggle',
			//height: 'toggle'
		}, "fast", "swing");
		return false;
	});
	
	$("a.spoiler").click(function(){
		if ($(this).html() == "Hide Spoiler")
			$(this).html("Show Spoiler");
		else
			$(this).html("Hide Spoiler");
		$(this).parent().children(".spoiler-content").animate({
			opacity: 'toggle',
			height: 'toggle'
		}, "fast");
		return false;
	});
	
	$(".starForm")[0].reset();
	$(".starForm").submit(function(){
		return false;
	});
	$(".starForm select").change(function(){
		var id = $(this).parent().parent().attr("id").substr(4);
		var type = $(this).attr("id");
		var value = $(this).val();
		if (type == "status")
		{
			if (value == "" || value == "want" || value == "wont" || value == "watch")
			{
				$(this).parent().children("#progress").attr("disabled", "disabled");
				var newValue = "";
				if (value == "watch")
				{
					newValue = $(this).parent().parent().attr("name");
				}
				$(this).parent().children("#progress").val(newValue);
				if (newValue == "")
				{
					newValue = 0;	
				}
				$.post("ajax/rating.php", { 
					value: newValue, 
					article_id: id,
					type: "progress"
				});
			}
			else
			{
				$(this).parent().children("#progress").removeAttr("disabled");
			}
		}
		$.post("ajax/rating.php", { 
			value: value, 
			article_id: id,
			type: type
		});
	});

	$(".stars").hover(function(){ return; },function(){
		for (k = 150; k >= 105; k = k - 5)
		{
			if (k % 10 == 5)
			{
				$(this).children("#star" + k).removeClass("star-left star-left-click star-left-on");
				if ($(this).children("#star" + k).attr("name") == "clicked")
				{
					$(this).children("#star" + k).addClass("star-left-click");
				}
				else
				{
					$(this).children("#star" + k).addClass("star-left");
				}
			}
			else
			{
				$(this).children("#star" + k).removeClass("star-right star-right-click star-right-on");
				if ($(this).children("#star" + k).attr("name") == "clicked")
				{
					$(this).children("#star" + k).addClass("star-right-click");
				}
				else
				{
					$(this).children("#star" + k).addClass("star-right");
				}
			}
		}
	});
	$(".star").hover(function(){
		//over
		var num = parseInt($(this).attr("id").substr(4, 7));
		var parent = $(this).parent();
		var odd = (num % 10 == 5);
		for (k = 150; k >= 105; k = k - 5)
		{
			if (k % 10 == 5)
			{
				parent.children("#star" + k).removeClass("star-left star-left-click star-left-on");
				parent.children("#star" + k).addClass("star-left");
			}
			else
			{
				parent.children("#star" + k).removeClass("star-right star-right-click star-right-on");
				parent.children("#star" + k).addClass("star-right");
			}
		}
		for (k = num; k >= 105; k = k - 5)
		{
			if (k % 10 == 5)
			{
				parent.children("#star" + k).removeClass("star-left star-left-click");
				parent.children("#star" + k).addClass("star-left-on");
			}
			else
			{
				parent.children("#star" + k).removeClass("star-right star-right-click");
				parent.children("#star" + k).addClass("star-right-on");
			}
		}
	},function(){ return; });
	$(".cancel").hover(function(){
		var parent = $(this).parent();
		for (k = 150; k >= 105; k = k - 5)
		{
			if (k % 10 == 5)
			{
				parent.children("#star" + k).removeClass("star-left star-left-click star-left-on");
				parent.children("#star" + k).addClass("star-left");
			}
			else
			{
				parent.children("#star" + k).removeClass("star-right star-right-click star-right-on");
				parent.children("#star" + k).addClass("star-right");
			}
		}
		parent.children(".cancel").removeClass("cancel-off");
		parent.children(".cancel").addClass("cancel-on");
	},function(){
		var parent = $(this).parent();
		parent.children(".cancel").removeClass("cancel-on");
		parent.children(".cancel").addClass("cancel-off");
	});
	$(".star").click(function(){
		//over
		var num = parseInt($(this).attr("id").substr(4, 7));
		var parent = $(this).parent();
		var odd = (num % 10 == 5);
		for (k = 150; k >= 105; k = k - 5)
		{
			parent.children("#star" + k).attr("name", "");
			if (k % 10 == 5)
			{
				parent.children("#star" + k).removeClass("star-left star-left-click star-left-on");
				parent.children("#star" + k).addClass("star-left");
			}
			else
			{
				parent.children("#star" + k).removeClass("star-right star-right-click star-right-on");
				parent.children("#star" + k).addClass("star-right");
			}
		}
		for (k = num; k >= 105; k = k - 5)
		{
			parent.children("#star" + k).attr("name", "clicked");
			if (k % 10 == 5)
			{
				parent.children("#star" + k).removeClass("star-left star-left-click star-left-on");
				parent.children("#star" + k).addClass("star-left-click");
			}
			else
			{
				parent.children("#star" + k).removeClass("star-right star-right-click star-right-on");
				parent.children("#star" + k).addClass("star-right-click");
			}
		}
		var id = parent.parent().parent().attr("id").substr(4);
		$.post("ajax/rating.php", { number: num, article_id: id });
	});
	$(".cancel").click(function(){
		var parent = $(this).parent();
		for (k = 150; k >= 105; k = k - 5)
		{
			parent.children("#star" + k).attr("name", "");
			if (k % 10 == 5)
			{
				parent.children("#star" + k).removeClass("star-left star-left-click star-left-on");
				parent.children("#star" + k).addClass("star-left");
			}
			else
			{
				parent.children("#star" + k).removeClass("star-right star-right-click star-right-on");
				parent.children("#star" + k).addClass("star-right");
			}
		}
		var id = parent.parent().parent().attr("id").substr(4);
		$.post("ajax/rating.php", { number: 0, article_id: id });
	});

	if (article === undefined)
		return;
	
	$("#root .lightbox-enabled").lightbox();
	
	$("a.additional").click(function(){
		editSwap($(this).parent().parent().parent());
		return false;
	});
	
	$(".lyrics_reveal").click(function(){
		var type = $(this).attr("id");
		var otherType = (type == "lyrics-jp") ? "lyrics-en" : "lyrics-jp";
		if (!$(this).parent().children("." + otherType).is(":hidden"))
		{
			$(this).parent().children("." + otherType).animate({
				opacity: 'hide',
				height: 'hide'
			}, "normal");
		}
		$(this).parent().children("." + type).animate({
			opacity: 'toggle',
			height: 'toggle'
		}, "normal");
		return false;
	});
	
	$(".show-player").click(function(){
		$(this).parent().parent().children("div.player").animate({
			opacity: 'toggle',
			height: 'toggle'
		}, "normal");
		return false;
	});

	$(".calendar").datepicker({ 
		showOn: "both", 
		buttonImage: "../../styles/ar_theme/theme/script/calendar.gif", 
		buttonImageOnly: true 
	}).attr("readonly", "readonly");
	
	$("#root div div div ul li").click(function(){
		goToMini($(this).attr("class"));
		return false;
	});
	
	$(".encytabs ul li").click(function(){
		goTo($(this).attr("id"));
		return false;
	});
	
	$(".hidenext").click(function(){
		$(this).next().toggle();
	});
	
	$('#root form').resetForm().ajaxForm({ 
        //target:			'.output', 
        success:		showResponse,
		beforeSubmit: 	updateOutput,
		dataType:		'json',
        type:			'post', 
    });
	
	$(".close").click(function(){
		$(".opwindow").animate({
			opacity: 'hide',
			height: 'hide'
		}, "normal");
		return false;
	});
	
	for (gallery in stuff)
	{
		galleryPage(gallery, 1);
	}
	$(".gallery-next").click(function(){
		var gallery = $(this).parent().parent().parent().attr("id");
		var page = stuff[gallery]["page"] + 1;
		if (stuff[gallery]["count"] < (page - 1) * 6 + 1)
			return false;
		galleryPage(gallery, page);
		return false;
	});
	$(".gallery-prev").click(function(){
		var gallery = $(this).parent().parent().parent().attr("id");
		var page = stuff[gallery]["page"] - 1;
		if (page <= 0)
			return false;
		galleryPage(gallery, page);
		return false;
	});
		
});

function goTo(tab)
{
	var previous = $(".activetab").attr("id");
	if (previous == tab)
		return false;
	$(".activetab").removeClass("activetab");
	$("." + previous + "div").animate({
		height: 'hide', 
		opacity: 'hide'
	}, "slow");
	$("#" + tab).addClass("activetab");
	$("." + tab + "div").animate({
		height: 'show', 
		opacity: 'show'
	}, "slow");
	//$("#root").show();
}
function goToMini(tab)
{
	var maintab = $(".activetab").attr("id");
	var previous = $("." + maintab + "div #active-subsection").attr("class");
	if (previous == tab)
		return false;
	$("." + maintab + "div ." + previous).attr("id", "");
	$("." + maintab + "div ." + previous + "_div").animate({
		height: 'hide', 
		opacity: 'hide',
	}, "slow");
	$("." + maintab + "div ." + tab).attr("id", "active-subsection");
	$("." + maintab + "div ." + tab + "_div").animate({
		height: 'show', 
		opacity: 'show',
	}, "slow");
}
function updateOutput(formData, jqForm, options)
{
	formSubmit = jqForm;
	return true;
}
function showResponse(json, status)
{
	if (!(status == 'success' && json['success']))
		$(".opwindow").removeClass("ruleless");
	else
		$(".opwindow").addClass("ruleless");
	if (formSubmit != undefined)
	{
		if (json['reset'])
			formSubmit.resetForm();
		if (json['close'])
			editSwap(formSubmit.parent().parent());
	}
	$(".output").html(json["return"]);
	
	formSumit = null;
	
	if ($(".opwindow").is(":hidden"))
	{
		$(".opwindow").animate({
			opacity: 'show',
			height: 'show'
		}, "normal");
	}
} 
function editSwap(parent)
{
	var swapLink = parent.children("a.additional");
	swapLink.animate({
		opacity: 'hide'
	}, "fast", function(){
		if (swapLink.html() == "Done Editing")
			swapLink.html("Edit");
		else
			swapLink.html("Done Editing");
		swapLink.animate({
			opacity: 'show'
		}, "fast");
	});
	parent.children(".catdata").animate({
		opacity: 'toggle',
		height: 'toggle'
	}, "normal");
	parent.children(".catedit").animate({
		opacity: 'toggle',
		height: 'toggle'
	}, "normal");
}
function galleryPage(gallery, pageNum)
{
	$("#" + gallery).animate({
		opacity: 'hide',
	}, "fast", function(){
		stuff[gallery]["page"] = pageNum;
		if (pageNum == 1)
			$("div#" + gallery + " a.gallery-prev").hide();
		if (pageNum == 2)
			$("div#" + gallery + " a.gallery-prev").show();
		var k = 1;
		var x = 1;
		for (var image in stuff[gallery])
		{
			if (image == "page" || image == "count")
			{
				break;
			}
			if (k <= (pageNum - 1) * 6 + 6 && k >= (pageNum - 1) * 6 + 1)
			{
				$("dl#" + gallery + (x++)).html("<dt><a class=\"lightbox-enabled\" href=\"../images/full/" + image + ".png\" rel=\"lightbox-" + gallery + "\" title=\"" + stuff[gallery][image] + "\"><img src=\"../images/thumb/" + image + ".png\" alt=\"" + stuff[gallery][image] + "\" title=\"" + stuff[gallery][image] + "\" /></a></dt><dd>" + stuff[gallery][image] + "</dd>");
				//$(".output").html($(".output").html() + "++");
			}
			//$(".ruleless").show();
			//$(".output").html($(".output").html() + image + "-<br />");
			k++;
		}
		for (var j = x; j <= 6; j++)
		{
			$("dl#" + gallery + j).html("");
		}
		stuff[gallery]["count"] = k - 1;
		//alert(stuff[gallery]["count"] + " " + ((pageNum - 1) * 6 + 6));
		if (stuff[gallery]["count"] <= (pageNum - 1) * 6 + 6)
			$("div#" + gallery + " a.gallery-next").hide();
		if (stuff[gallery]["count"] > (pageNum - 1) * 6 + 6)
			$("div#" + gallery + " a.gallery-next").show();

		//$(".output").html($(".output").html() + "-" + k + "-<br /><br />");
		
		$("#" + gallery).animate({
			opacity: 'show',
		}, "fast", function(){
			$("#" + gallery + " .lightbox-enabled").lightbox();
		});
	});
	
}
