var ticks,clockDate,tooltipping,itemID,userID;
itemID = 0;
var itemArray = [];
userID = 0;
var userArray = [];

var mouseX = mouseY = 0; 
var clonedAttacks;

function disableElement(obj) {
	obj.disabled = true;
	obj.form.submit();
}

function popupConfirm(form) {
	return confirm('Are you sure you wish to do this?');
}

$(document).ready(function() {
	$(document).mousemove(function(e) { mouseX = e.pageX; mouseY = e.pageY; });

	setTimeout("newEvents()", 10000);
	
	$('[id^=js]').show();
	$('#klaspan').html('<a href="#">Spin</a>').find('a').click(function(){
		$('#cheat').parent().parent().submit();
	});
	$('[id^=js][id$=SelectAll]').html('Select All');
	$('[id^=js][id$=DelSelect]').html('Delete Selected');
});

function newEvents() {
	if ($('#loggedinText').length) {	
		getControlAjax('gameplays', 'gameplayCon');
		getControlAjax('login', 'loginText');
		getControlAjax('buddy', 'buddyCon');
		setTimeout("newEvents()", 10000);
	}
}

function showHideMoves() {
	$('input[name=\'attack\']').each(function(i) { $(this).removeAttr('disabled'); });	
	$('input[name=\'attack\']').not('.' + $('select#enemy option:selected').attr('class')).each(function(i) { $(this).attr('disabled', 'disabled'); });
}

function submitter(obj) {
	obj.onclick = null;
	getObj('cheat').form.submit();
}

function init(t,bst) {
	ticks = ((t + (bst * 3600)) * 1000);
	oldT = t;
	clockDate = new Date(ticks);
	clockTick();
	$('#cheat').val(t);
}

function referLink(id,usr) {
	$('#jsReferLink').html(
			'&lt;a href=&quot;http://anatheria.com/reg.ath?ref=' + usr + '&quot;&gt;&lt;img src=&quot;http://images.anatheria.com/referral/' + id + '.png&quot; alt=&quot;Join Anatheria&quot; title=&quot;Join Anatheria&quot; /&gt;&lt;/a&gt;'
	).show();
}

function selAllMoveItem() {
	$('select[name*=sel]').find('option:selected').removeAttr("selected").end().find('option:last').attr("selected","selected");
}

function shopSearch(itemName) {
	$('#shopStock').find('div[class*=item-]:not([class*=' + itemName.toLowerCase().replace(' ','-') + '])').fadeTo('fast','0.1').end().find('div[class*=item-][class*=' + itemName.toLowerCase().replace(' ','-') + ']').fadeTo('fast',1);
	return false;
}

function trainPet(id,stat) {
	$.ajax({
		url: "/ajax/train.php?pet="+id+"&stat="+stat,
		cache: false,
		success: function(html){
			$('#petdetails'+id).html(html);
		}
	});
	return false;
}

function otherBuddyToggle() {
	$('#otherBuddy').toggle();
	$.ajax({
		url: "/ajaxbuddy.ath?type=3",
		cache: false,
		success: function() { return false; }
	});
	return false;
}

function offBuddyToggle() {
	$('#offBuddy').toggle();
	$.ajax({
		url: "/ajaxbuddy.ath?type=2",
		cache: false,
		success: function() { return false; }
	});
	return false;
}

function onBuddyToggle() {
	$('#onBuddy').toggle();
	$.ajax({
		url: "/ajaxbuddy.ath?type=1",
		cache: false,
		success: function() { return false; }
	});
	return false;
}

function ajaxDeletePost(id) {
	if (confirm('Are you sure you wish to delete this post?')) {
		$('#post'+id).slideUp('slow');
		$.ajax({
			url: '/ajax/forumdel.php?del='+id,
			cache: false
		});
	}
}

function ajaxPost() {
	$.ajax({
		url: '/ajax/forumpost.php',
		type: 'post',
		data: 'id=' + $('#id').val() + '&message=' + encodeURIComponent($('#message').val()),
		cache: false,
		success: function(html){
			$('#forumposts').html(html);
		}
	});
	$('#message').text('');
}

function clockTick() {
	ticks = ticks + 1000;
	clockDate = new Date(ticks);
	clockUpd();
	setTimeout("clockTick()", 1000);
}

function clockUpd() {
	h = clockDate.getUTCHours();
	ampm = (h >= 12 ? 'PM' : 'AM');	
	h = (h % 12);	
	if (h == 0) h = 12;
	
	$('#datetimeCon').html(clockDate.getUTCDate()+ ' ' +monthName(clockDate.getUTCMonth())+ ' ' +clockDate.getUTCFullYear()+ ' ' +h + ':' + pre(clockDate.getUTCMinutes()) + ':' + pre(clockDate.getUTCSeconds())+ ' ' +ampm);
}

function getControlAjax(control, container)
{
	$.ajax({
		url: '/ajax/viewcontrol.php?control=' + control,
		type: 'get',
		cache: false,
		success: function(html){
			$('#' + container).html(html);
		}
	});
}

function switchActivePet(petID) {
	$.ajax({
		url: '/ajax/setactivepet.php?act=' + petID,
		type: 'get',
		cache: false,
		success: function(html){
			getControlAjax('pet', 'activePet');
		}
	});
}

function pre(t) {
	return (t <= 9) ? ('0' + t) : t;
}

function monthName(id) {
	switch (id) {
		case 0:{return 'January';break;}
		case 1:{return 'February';break;}
		case 2:{return 'March';break;}
		case 3:{return 'April';break;}
		case 4:{return 'May';break;}
		case 5:{return 'June';break;}
		case 6:{return 'July';break;}
		case 7:{return 'August';break;}
		case 8:{return 'September';break;}
		case 9:{return 'October';break;}
		case 10:{return 'November';break;}
		case 11:{return 'December';break;}
	}
}

function avChange(fname) {
	if (getObj('newAv')) {
		getObj('newAv').src = fname;
	}
}

function getObj(name) {
	j = $('#'+name);
	if (j.size() > 0)
		return j[0];
	return null;
}

function charLimit(total,used,counter) {
	if (counter == null)
		counter = '#count';
	$(counter).html(used + ' / ' + total + ' characters used');
}

function profPrev(v){
	charLimit(15000,v.length);
	$('#profPreview').html(v);
}

function tooltip(text,e) {
	var posX = (mouseX + 15);
	if (posX >= (getDocX() - 360)) 
		posX = (mouseX - 370);
		
	$('#tooltip').css('left',posX + 'px').css('top',mouseY + 'px');
	
	if (text.substring(0,5) == 'item:') {
		if (itemID != text.replace("item:","")) {		
			$('#tooltip').show();
			
			itemID = text.replace("item:","");
			
			var tmpItemID = itemID;
			
			if (itemArray[tmpItemID] && itemArray[tmpItemID] != '') {
				$('#tooltip').html(itemArray[tmpItemID]);
			}
			else {
				$('#tooltip').html('<div style="height:128px;"></div>');
				$.ajax({
					url: '/ajax/itemtip.php?id=' + tmpItemID,
					cache: true,
					success: function(html) {
						$('#tooltip').html(html);
						itemArray[tmpItemID] = html;
						itemID = 0;
					}
				});
			}
		}
		else if (!$('#tooltip').is(':visible')) {
			$('#tooltip').show();
		}
	}
	else if (text.substring(0,5) == 'user:') {
		if (userID != text.replace("user:","")) {		
			$('#tooltip').show();
			
			userID = text.replace("user:","");
			
			var tmpUserID = userID;
			
			if (userArray[tmpUserID] && userArray[tmpUserID] != '') {
				$('#tooltip').html(userArray[tmpUserID]);
			}
			else {
				$('#tooltip').html('<div style="height:128px;"></div>');
				$.ajax({
					url: '/ajax/usertip.php?id=' + tmpUserID,
					cache: true,
					success: function(html) {
						$('#tooltip').html(html);
						userArray[tmpUserID] = html;
						userID = 0;
					}
				});
			}
		}
		else if (!$('#tooltip').is(':visible')) {
			$('#tooltip').show();
		}
	}
	else {
		$('#tooltip').html(text);
		var posX = (mouseX + 15);
		if (posX >= (getDocX() - $('#tooltip').width())) 
			posX = (mouseX - ($('#tooltip').width() + 10));
		
		$('#tooltip').css('left',posX + 'px').css('top',mouseY + 'px').show();
		itemID = 0;
		userID = 0;
	}
}

function tooltipRem() {
	$('#tooltip').hide();
}

function getMouseX(e) {
	return mouseX;
}

function getMouseY(e) {
	return mouseY;
}

function getDocX() {
	return $(window).width();
}

function getDocY() {
	return $(window).height();
}

function sigPrev(text) {
	$('#sigPrev').html(text);
}

function selectAll(formObj) {
	$(formObj).find(':checkbox').each(function () {
		if ($(this).is(":checked"))
			$(this).removeAttr("checked");
		else
			$(this).attr("checked","checked");
	});
}

