// -- BEGIN LICENSE BLOCK -------------------------------
//
// This file is part of Guild Roster.
//
// Copyright © 2009 Simon Pottier and contributors
// Licensed under the GPL version 3.0 license
// http://www.gnu.org/licenses/gpl-3.0.html
//
// -- END LICENSE BLOCK ---------------------------------

$(document).ready(function($)
{
	////////////////////////////////////////
	//////////   DATA SWITCHERS   //////////
	////////////////////////////////////////
	
	// SELECTORS DISABLER
	$.fn.s_disabler = function(){$(this).attr("disabled", "disabled");}
	
	// SELECTORS ENABLER
	$.fn.s_enabler = function(){$(this).removeAttr("disabled");}
	
	// PROFESSIONS
	$("#profession_switcher").change(function()
	{
		$("#professions_header .pager *").s_disabler();
		$("#professions_header .pager").prepend("<span id='spinwheel'></span>");
		var pro = $("#profession_switcher option:selected").attr("value");
		$.get("index.php", {query: "professions", profession: pro}, function(data)
		{
			$("#professions_cont").html(data);
			$("#professions_header .pager #spinwheel").remove();
			nyro_init($("#professions_cont a.modal_char"));
			var sizeIs = $("#professions_header .pager .pagesize").attr("value");
			table_init($("#professions_table"), $("#professions_header .pager"), [[1,1]], sizeIs, {1:{sorter: 'digit'}});
			$("#professions_header .pager *").s_enabler();
		});
	});
	
	// ARENA
	$("#arena_switcher").change(function()
	{
		$("#arena_header .pager *").s_disabler();
		$("#arena_header .pager").prepend("<span id='spinwheel'></span>");
		var type = $("#arena_switcher option:selected").attr("value");
		$.get("index.php", {query: "arena", type: type}, function(data)
		{
			$("#arena_cont").html(data);
			$("#arena_header .pager #spinwheel").remove();
			nyro_init($("#arena_cont a.modal_char"));
			var sizeIs = $("#arena_header .pager .pagesize").attr("value");
			table_init($("#arena_table"), $("#arena_header .pager"), [[3,1]], sizeIs, {2:{sorter: 'digit'}, 3:{sorter: 'digit'}, 4:{sorter: 'digit'}});
			$("#arena_header .pager *").s_enabler();
		});
	});
	
	// SIMPLE STATS
	$("#simple_stats_lv_switcher").change(function()
	{
		$("#simple_stats_header .pager *").s_disabler();
		$("#simple_stats_header .pager").prepend("<span id='spinwheel'></span>");
		var stat = $("#simple_stats_switcher option:selected").attr("value");
		var from = $("#simple_stats_lv_switcher option:selected").attr("value");
		var to = 80;
		$.get("index.php", {query: "simple_stats", stat: stat, from: from, to: to, maxHeight: 95}, function(data)
		{
			$("#simple_stats_cont").hide();
			$("#simple_stats_cont").html(data);
			$("#simple_stats_header .pager #spinwheel").remove();
			$("#simple_stats_header .pager *").s_enabler();
			$("#simple_stats_cont").fadeIn('slow');
		});
	});
	
	$("#simple_stats_switcher").change(function()
	{
		$("#simple_stats_header .pager *").s_disabler();
		$("#simple_stats_header .pager").prepend("<span id='spinwheel'></span>");
		var stat = $("#simple_stats_switcher option:selected").attr("value");
		var from = $("#simple_stats_lv_switcher option:selected").attr("value");
		var to = 80;
		$.get("index.php", {query: "simple_stats", stat: stat, from: from, to: to, maxHeight: 95}, function(data)
		{
			$("#simple_stats_cont").hide();
			$("#simple_stats_cont").html(data);
			$("#simple_stats_header .pager #spinwheel").remove();
			$("#simple_stats_header .pager *").s_enabler();
			$("#simple_stats_cont").fadeIn('slow');
		});
	});
	
	////////////////////////////////////////
	////////        NYRO INIT       ////////
	////////////////////////////////////////
	function nyro_init(selector)
	{
		$(selector).nyroModal(
		{
			width: 700,
			height: 385,
			bgColor: '',
			showBackground: function (elts, settings, callback) {elts.bg.css({opacity:0}).fadeTo(300, 0.4, callback);},
			ajax: {cache:false, contentType:"application/x-www-form-urlencoded; charset=utf-8"},
			endShowContent: function()
			{
				$.get("index.php", {query: "3d_model", char: $("#td_3D strong").html()}, function(data)
				{
					$("#cont_3D").html(data);
				});
			}
		});
	}
	
	////////////////////////////////////////
	////////       TABLE INIT       ////////
	////////////////////////////////////////
	function table_init(selector, container, sort, sizeIs, headers)
	{
		$(selector)
		.tablesorter({sortList: sort, headers: headers, widgets: ['zebra']})
		.tablesorterPager({ container: container, size: sizeIs, positionFixed: false});
	}
	
	////////////////////////////////////////
	//////     FUNCTION LAUNCHER      //////
	////////////////////////////////////////
	var mod_posi;
	function f_launcher()
	{
		switch (mod_posi)
		{
			case 0 :
				roster();
				mod_posi += 1;
				break;
			case 1 :
				eval(left_content+"();");
				mod_posi += 1;
				break;
			case 2 :
				eval(right_content+"()");
				mod_posi += 1;
				break;
			case 3 :
				cache_inf();
				break;
			default :
				break;
		}
	}
	
	////////////////////////////////////////
	//////    CACHE INF AND UPDATE    //////
	////////////////////////////////////////
	function cache_inf()
	{
		// BACKGROUND UPDATE
		$.getJSON("index.php", {query: "roster_inf", step: 0}, function(data)
		{
			$("#footer .left").html(data.text);
			if (data.update == "true")
			{
				$.getJSON("index.php", {query: "roster_inf", step: 1}, function(data)
				{
					$("#footer .left").html(data.text);
					$("#refresh").click(function(){launch();return false;});
				});
			}
		});
	}
	
	////////////////////////////////////////
	//////////     PROFESSIONS    //////////
	////////////////////////////////////////
	function professions()
	{
		var pro = $("#profession_switcher option:selected").attr("value");
		$.get("index.php", {query: "professions", profession: pro}, function(data)
		{
			$("#professions_cont").html(data);
			nyro_init($("#professions_cont a.modal_char"));
			var sizeIs = $("#professions_header .pager .pagesize").attr("value");
			table_init($("#professions_table"), $("#professions_header .pager"), [[1,1]], sizeIs, {1:{sorter: 'digit'}});
			$("#professions_header .pager *").s_enabler();
			f_launcher();
		});
	}
	
	////////////////////////////////////////
	//////////        ARENA       //////////
	////////////////////////////////////////
	function arena()
	{
		var type = $("#arena_switcher option:selected").attr("value");
		$.get("index.php", {query: "arena", type: type}, function(data)
		{
			$("#arena_cont").html(data);
			nyro_init($("#arena_cont a.modal_char"));
			table_init($("#arena_table"), $("#arena_header .pager"), [[3,1]], 5, {2:{sorter: 'digit'}, 3:{sorter: 'digit'}, 4:{sorter: 'digit'}});
			$("#arena_header .pager *").s_enabler();
			f_launcher();
		});
	}
	
	////////////////////////////////////////
	/////////    SIMPLE STATS     //////////
	////////////////////////////////////////
	function simple_stats()
	{
		$("#simple_stats_header .pager *").s_disabler();
		var stat = $("#simple_stats_switcher option:selected").attr("value");
		var from = $("#simple_stats_lv_switcher option:selected").attr("value");
		var to = 80;
		$.get("index.php", {query: "simple_stats", stat: stat, from: from, to: to, maxHeight: 95}, function(data)
		{
			$("#simple_stats_cont").html(data);
			$("#simple_stats_header .pager *").s_enabler();
			f_launcher();
		});
	}
	
	////////////////////////////////////////
	//////////       ROSTER       //////////
	////////////////////////////////////////
	function roster()
	{
		$.get("index.php", {query: "roster"}, function(data)
		{
			$("#roster_cont").html(data);
			nyro_init($("#roster_cont a.modal_char"));
			table_init($("#roster_table"), $("#roster .pager"), [[1,1],[4,0]], 20, {1:{sorter: 'digit'},4:{sorter: 'digit'},5:{sorter: 'digit'}});
			$("#roster .pager *").s_enabler();
			f_launcher();
		});
	}
	
	////////////////////////////////////////
	//////////      LAUNCHING     //////////
	////////////////////////////////////////
	var left_content;
	var right_content;
	function launch()
	{
		mod_posi = 0;
		$(".pager *:text").attr("readonly", "true");
		$(".pager *").s_disabler();
		$("#roster_cont, #middle_left div:last, #middle_right div:last").html("<div id='loader'></div>");
		$.getJSON("index.php", {query: "js_conf"}, function(data){left_content = data.left_cont; right_content = data.right_cont; f_launcher();});
	}
	launch();
});