/*======================================================================*\
|| #################################################################### ||
|| # PMNet Version 3.0												  #	||
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2003-2008 John Slone. All Rights Reserved. 			  #	||
|| # This file may not be redistributed in whole or significant part. # ||
|| #  																  # ||
|| #################################################################### ||
\*======================================================================*/

/* dex.js */


var Dex = {
	
	showPokesForChar : function(el, cid)
	{
		if ( $("#pokes_char_"+cid).length > 0 )
		{
			// They already loaded it
			return false;
		}
		$.ajax({
			url : '/ajax_dex.php',
			data : { 'action' : 'getabilitypokes', 'cid' : cid },
			success : function(d)
			{
				$(el).parents('tr.rr').after(d);
				m = $("#pokes_char_"+cid);
				m.children("td").each(function(){
					var mm = $(this);
					$(this).wrapInner('<div></div>').children('div').hide().slideDown(200,function(){ });
				});
				m.slideDown('slow');
				$(".pokeSprite",m).hover(function(){
					if ( $("#namedisplaybox").length < 1 )
					{
						$("#pmBody").append('<div id="namedisplaybox" style="display:none;"><div class="llll"></div><div class="pokeNameArrow"></div></div>');
					}
					a = $("#namedisplaybox");
					a.find('div.llll').html($(this).attr('alt'));
					a.css('top',$(this).offset()['top']-a.height()-10);
					a.css('left',$(this).offset()['left']+$(this).width()/2-a.width()/2-10);
					a.css('display','block');
				},
				function(){
					a = $("#namedisplaybox").css('display','none');
				});
			}
		});
		return false;
	}	
}

var viewPoke = {
	
	init : function()
	{
		$("#attackListTable").tablesorter({'cssHeader':'headerSortable'});
		$("#tmhmTable").tablesorter({'cssHeader':'headerSortable',debug:false,'headers':{5:{sorter:"digit"},6:{sorter:"digit"},7:{sorter:"digit"}}});

		$("#atkGameLink"+activeAtkGame).css('opacity',1.0);
		$("#atkGameCellRow td").hover(
			function(){
				$(this).stop().fadeTo(400, 1);
			},
			function(){
				if ( this.id == 'atkGameLink' + activeAtkGame )
				{
					return;
				}
				else
				{
					$(this).stop().fadeTo(400, 0.3);
				}
		})
		.click(function()
		{
			//alert('hi');
			var gameID = $(this).attr('id').substring(11);
			$("#attackListTable tbody").addClass('nodisplay');
			$("#atkGameCellRow td").css('opacity',0.3);
			$("#atkGameLink"+gameID).css('opacity',1);
			$("#atkGroup"+gameID).removeClass('nodisplay');
			activeAtkGame = gameID;
		});
		
		// Format the pokemon selects so that they automatically submit
		$(".pokejump").change(function(){
			num = $(this).val();
			window.location = '/dex/' + num;
		});
		
	},

	showTMHMGen : function(gen_num)
	{
		$("#tmhmTable tr[class*=gen]").addClass('nodisplay');
		$("#tmhmTable tr.gen"+gen_num).removeClass('nodisplay');
		return false;
	}	
}

var viewAttack = {
	
	toggleBlock : function(el)
	{
		$(el).parents('h2').next('div').toggleClass('nodisplay');
		$(el).parents('h2').find('span').toggleClass('nodisplay');
		return false;
	},
	
	init : function()
	{
		$("table.atktable").floatHeader();
	}
}

var TypeChart = {
	
	types : new Array('','Bug','Dark','Dragon','Electric','Fighting','Fire','Flying','Ghost','Grass','Ground','Ice','Normal','Poison','Psychic','Rock','Steel','Water'),
	
	calculate : function(f)
	{
		var t1 = $(f).find("select[name='typeone']").val();
		var t2 = $(f).find("select[name='typetwo']").val();
		if ( t2 == 0 )
		{
			t2 = t1;
		}
		
		this._makeRow(t1, t2, 0);
	},
	
	fromPokemon : function(f)
	{
		pokeid = $(f).find("select[name='pokeid']").val();
		if (pokeid > 0 )
		{
			t = pokeTypes[pokeid];
			this._makeRow(t[0], t[1], pokeid);
		}
	},
	
	clearTable : function()
	{
		$("#testRows").empty();
	},
	
	_allCombos : function()
	{
		// Test case - make a table of ALL possible type combinations
		this.enmasse = true;
		for ( ii=1; ii<=17; ii++ )
		{
			for (j = ii; j<=17; j++)
			{
				this._makeRow(ii,j,0);
			}
		}
		this.enmasse = false;
		$("#attackTable").floatHeader();
		return false;
	},
	
	_makeRow : function(typeone, typetwo, pokemonid)
	{
		t1 = Efficacy[typeone];
		t2 = Efficacy[typetwo];
		combined = new Array();
		
		namestr = '';
		if ( pokemonid > 0 )
		{
			namestr = pokeTypes[pokemonid][2];
		}
		// Now, create the row
		if ( typeone == typetwo)
		{
			// Create combined damage array
			for (i=1; i<=17; i++)
			{
				combined[combined.length] = (t1[i])/100;
			}
			str = '<tr><td class="r">'+namestr+'<img src="/images/icons/type_'+this.types[typeone].toLowerCase()+'.png" alt="'+this.types[typeone].toLowerCase()+'" /></td>';
		}
		else
		{
			// Create combined damage array
			for (i=1; i<=17; i++)
			{
				combined[combined.length] = (t1[i] * t2[i])/10000;
			}
			str = '<tr><td class="r">'+namestr+'<img src="/images/icons/type_'+this.types[typeone].toLowerCase()+'.png" alt="'+this.types[typeone].toLowerCase()+'" /> <img src="/images/icons/type_'+this.types[typetwo].toLowerCase()+'.png" alt="'+this.types[typetwo].toLowerCase()+'" /></td>';
		}
		for(i=0; i<combined.length;i++)
		{
			if ( combined[i] == 1 )
			{
				str += '<td></td>';
			}
			else
			{
				str += '<td>'+combined[i]+'x</td>';
			}
		}
		//$("#testRows").append(str);
		$("#testRows").prepend(str);
		if ( !this.enmasse )
		{
			$("#attackTable").floatHeader();
		}
	}
	
}

/* Do this for every page in the dex */
$(document).ready(function(){
	$(".pokeSprite").hover(function(){
		if ( $("#namedisplaybox").length < 1 )
		{
			$("#pmBody").append('<div id="namedisplaybox" style="display:none;"><div class="llll"></div><div class="pokeNameArrow"></div></div>');
		}
		a = $("#namedisplaybox");
		a.find('div.llll').html($(this).attr('alt'));
		a.css('top',$(this).offset()['top']-a.height()-10);
		a.css('left',$(this).offset()['left']+$(this).width()/2-a.width()/2-10);
		a.css('display','block');
	},
	function(){
		a = $("#namedisplaybox").css('display','none');
	});
});

/*======================================================================*\
|| ####################################################################
|| # File Revision:    $Revision: 1254 $
|| # Last Change:      $LastChangedDate: 2011-06-13 11:59:01 -0700 (Mon, 13 Jun 2011) $
|| # Last Commit By:   $Author: JDS $
|| ####################################################################
\*======================================================================*/
