$(document).ready( function( ) {
							
	$('a.load-local').mouseover( function( ) {
		showInfo( $(this).attr( 'id' ) );
	});
	
	//setClicks( );
	//resetTierRanks( );
	
	$('#zoneSelect').change( function( ) {
		var newZone = $(this).val( );
		$.ajax({
			url: "../scripts/fetchZoneListing.php",
			type: "POST",
			data: ({zone: newZone}),
			beforeSend: function( ) {
				$("#petopiaBox").html( '<img src="../images/ajax-loader.gif" />' );
			},
			success: function( msg ) {
				$("#petopiaBox").html( msg );
			}
		});
	});

});

function setClicks( ) {
	
	$('a.activeTalent').click( function( ) {
		increaseRank( $(this).attr( 'id' ) );								
	});
	
	$('a.activeTalent').rightClick( function( el ) {
		decreaseRank( $(el).attr( 'id' ) );								
	});
	
}

function resetTierRanks( ) {
	$("#tier1-rank").val( "0" );
	$("#tier2-rank").val( "0" );
	$("#tier3-rank").val( "0" );
	$("#tier4-rank").val( "0" );
	$("#tier5-rank").val( "0" );
}

function showInfo( stub ) {

	$("#talent-info").show( );
	$('.talent-desc').hide( );
	$('#' + stub + "-desc").show( );

}

function lightTier( tier ) {
	
		var reqTalent = "";
		var reqRank = "";
		var stub = "";
	
		$('a.' + tier + '-grey').each( function( ) {									
			
			stub = $(this).attr( "id" );
			reqTalent = $("#" + stub + "-req-talent").val( );
			reqRank = parseInt( $("#" + stub + "-req-rank" ).val( ) );
			
			if( reqTalent == undefined ) {
				$(this).hide( );
			} else {
				var checkRank = parseInt( $("#" + reqTalent + "-rank").html( ) );
				if( reqRank == checkRank ) {
					$(this).hide( );
				} else {
					$(this).show( );
				}
			}
			
		});
		
		$('a.' + tier).each( function( ) {
									  
			stub = $(this).attr( "id" );
			reqTalent = $("#" + stub + "-req-talent").val( );
			reqRank = parseInt( $("#" + stub + "-req-rank" ).val( ) );
			
			if( reqTalent == undefined ) {
				$(this).show( );
			} else {
				var checkRank = parseInt( $("#" + reqTalent + "-rank").html( ) );
				if( reqRank == checkRank ) {
					$("#" + stub + "-arrow-lite").show( );
					$("#" + stub + "-arrow").hide( );
					$(this).show( );
				} else {
					$("#" + stub + "-arrow-lite").hide( );
					$("#" + stub + "-arrow").show( );
					$(this).hide( );
				}
			}
			
		});
	
}

function greyTier( tier ) {
	
		var totalPoints = parseInt( $("#tc-talentpoints").html( ) );
		var totalSpent = parseInt( $("#tc-talentspent").html( ) );
	
		$('a.' + tier).each( function( ) {	
			stub = $(this).attr( "id" );
			reqTalent = $("#" + stub + "-req-talent").val( );
			
			if( reqTalent != undefined ) {
				$("#" + stub + "-arrow-lite").hide( );
				$("#" + stub + "-arrow").show( );
			}
			

			
			$(this).hide( );
		});
	
		$('a.' + tier + '-grey').each( function( ) {
				
			stub = $(this).attr( "id" );
												
			var checkRank = parseInt( $("#" + stub + "-rank").html( ) );
			if( checkRank > 0 ) {
				totalPoints += checkRank;
				totalSpent -= checkRank;
				$("#" + stub + "-rank").html( "0" );
			}
												
			$(this).show( );
		});
		
		$("#tc-talentpoints").html( '' + totalPoints );
		$("#tc-talentspent").html( '' + totalSpent );
		$("#" + tier + "-rank").val( "0" );
	
}

function checkTiers( spent ) {
	
	var tier1Points = parseInt( $("#tier1-rank").val( ) );
	var tier2Points = parseInt( $("#tier2-rank").val( ) );
	var tier3Points = parseInt( $("#tier3-rank").val( ) );
	var tier4Points = parseInt( $("#tier4-rank").val( ) );
	var tier5Points = parseInt( $("#tier5-rank").val( ) );
	
	if( tier1Points >= 3 ) {
		lightTier( 'tier2' );
	} else {
		greyTier( 'tier2' );
	}
	
	if( (tier1Points+tier2Points) >= 6 ) {
		lightTier( 'tier3' );
	} else {
		greyTier( 'tier3' );
	}
	
	if( (tier1Points+tier2Points+tier3Points) >= 9 ) {
		lightTier( 'tier4' );
	} else {
		greyTier( 'tier4' );
	}
	
	if( (tier1Points+tier2Points+tier3Points+tier4Points) >= 12 ) {
		lightTier( 'tier5' );
	} else {
		greyTier( 'tier5' );
	}
	
}

function decreaseRank( stub ) {
	
	var rankValue = parseInt( $("#" + stub + "-rank").html( ) );
	var totalPoints = parseInt( $("#tc-talentpoints").html( ) );
	var totalSpent = parseInt( $("#tc-talentspent").html( ) );
	var tierRank = parseInt( $("#" + stub + "-tier").val( ) );
	var tierPoints = parseInt( $("#tier" + tierRank + "-rank").val( ) );
	var newRank = 0;
	
	if( rankValue > 0 ) {
		newRank = (rankValue - 1);
		totalPoints = (totalPoints + 1);
		totalSpent = (totalSpent - 1);
		$("#tier" + tierRank + "-rank").val( '' + (tierPoints - 1) )
	} else {
		newRank = 0;
	}
	
	$("#" + stub + "-rank").html( '' + newRank );
	$("#tc-talentpoints").html( '' + totalPoints );
	$("#tc-talentspent").html( '' + totalSpent );

	
	checkTiers( totalSpent );
	
}

function increaseRank( stub ) {
	
	var maxValue = parseInt( $("#" + stub + "-max").val( ) );
	var rankValue = parseInt( $("#" + stub + "-rank").html( ) );
	var tierRank = parseInt( $("#" + stub + "-tier").val( ) );
	var totalPoints = parseInt( $("#tc-talentpoints").html( ) );
	var totalSpent = parseInt( $("#tc-talentspent").html( ) );
	var tierPoints = parseInt( $("#tier" + tierRank + "-rank").val( ) );
	
	var newRank = 0;
	
	if( totalPoints > 0 ) {
		if( rankValue < maxValue ) {
			newRank = (rankValue + 1);
			totalPoints = (totalPoints - 1);
			totalSpent = (totalSpent + 1);
			$("#tier" + tierRank + "-rank").val( '' + (tierPoints + 1) );
		} else {
			newRank = rankValue;
		}
	} else {
		newRank = rankValue;
	}
	
	$("#" + stub + "-rank").html( '' + newRank );
	$("#tc-talentpoints").html( '' + totalPoints );
	$("#tc-talentspent").html( '' + totalSpent );
	
	checkTiers( totalSpent );

}

function show3D( ) {

	$("#3D-tab")
		.removeClass('pet-profile-tab')
		.addClass('pet-profile-tab-active');
	
	$("#image-tab")
		.removeClass('pet-profile-tab-active')
		.addClass('pet-profile-tab');
	
	$("#video-tab")
		.removeClass('pet-profile-tab-active')
		.addClass('pet-profile-tab');
		
	$("#pet-profile-image").hide( );
	$("#pet-profile-video").hide( );
	$("#pet-profile-embed").show( );

}

function showImage( ) {

	$("#3D-tab")
		.removeClass('pet-profile-tab-active')
		.addClass('pet-profile-tab');
	
	$("#image-tab")
		.removeClass('pet-profile-tab')
		.addClass('pet-profile-tab-active');
	
	$("#video-tab")
		.removeClass('pet-profile-tab-active')
		.addClass('pet-profile-tab');
		
	$("#pet-profile-video").hide( );
	$("#pet-profile-embed").hide( );
	$("#pet-profile-image").show( );

}

function showVideo( ) {

	$("#3D-tab")
		.removeClass('pet-profile-tab-active')
		.addClass('pet-profile-tab');
	
	$("#image-tab")
		.removeClass('pet-profile-tab-active')
		.addClass('pet-profile-tab');
	
	$("#video-tab")
		.removeClass('pet-profile-tab')
		.addClass('pet-profile-tab-active');
		
	$("#pet-profile-image").hide( );
	$("#pet-profile-embed").hide( );
	$("#pet-profile-video").show( );

}

function showSubNav( ) {
	$("#subnavset").fadeIn( 200 );	
}
