// Track info
var the_track_info = [];
function display_track_info()
{
  //var track_id = this.getAttribute('track');
  var track_id = $(this).attr('track');

	if(!the_track_info['elmt']) // There's no tool tip, create one.
	{
		the_track_info['elmt'] = $("#track_info");
		the_track_info['text'] = $("#track_info_mid");
	} 
	the_track_info['y'] = findPosY(this);
	the_track_info['x'] = findPosX(this);
	the_track_info['elmt'].css('top', the_track_info['y']+"px");
	the_track_info['elmt'].css('left', the_track_info['x']+"px");
	the_track_info['text'].html('<div style="text-align: center; font-weight: bold;"><img src="/_i/loading.gif" /> <span style="position: relative; top: -12px;">Loading...</span></div>'); 
	the_track_info['elmt'].fadeIn(1000);
	
	$.getJSON('/track/a_info.php?track_id='+track_id, load_track_info_done)
}
function load_track_info_done(data) 
{ 
	the_track_info['text'].html(data);
	//init_track_list('info');
	init_track_list('info');
}

// Modify Treasure
var the_modify_treasure;
function modify_treasure(el, track_id)
{
  the_modify_treasure = el;
  $.getJSON('/track/a_modify_treasure.php?track_id='+track_id, modify_treasure_done);
}
function modify_treasure_done(data)
{
	the_modify_treasure.innerHTML = data.html;
	if(typeof(in_treasures) != "undefined"  && !data.error) // We're in the treasures page
	{
		if(the_modify_treasure.tagName != "TD")
		{
			the_modify_treasure = document.getElementById('track_id_'+data.id);
		}
		the_modify_treasure.parentNode.parentNode.removeChild(the_modify_treasure.parentNode);
	}
	// Alters the td's to show the background.
	if(!data.error)
	{
		//var track_list = document.getElementById('track_list');
		//var tds = track_list.getElementsByTagName('td');
		tds = $('#track_list td');
		for(var i = 0, len = tds.length; i < len; i++)
		{
			if($(tds[i]).attr('track') == data.id)
			{
				highlight($(tds[i]), data.state, 'treasure_bg');
			}
		}
	}
}
function display_user_info()
{
  window.location='/user/info.php?id='+this.getAttribute('user');
}
function display_comment()
{
  window.location='/track/comments.php?track_id='+this.getAttribute('comment');
}

// Handles track requesting.. duh
var request_hit = [];
function request_track(el)
{
	if(!request_hit['in_progress'])
	{
		request_hit['in_progress'] = 1;
		request_hit['elmt'] = el;
		var track_id = request_hit['elmt'].attr('request');
		var pass = '/station/a_request.php?track_id='+track_id;

		// Need to pass el[0] for the original element
		tool_tip['elmt'].css('top', findPosY(el[0]) + 10 + "px");
		tool_tip['elmt'].css('left', findPosX(el[0]) + 10 + "px");
		tool_tip['text'].html('<div style="text-align: center;"><img src="/_i/loading.gif" /> Requesting...</div>');
		//tool_tip['text'].append('<br />' + pass); 
		tool_tip['elmt'].fadeIn(500);

		
		request_hit['elmt'].html('...');
		request_hit['elmt'].removeClass();
		request_hit['elmt'].unbind();
		$.getJSON(pass, request_track_done);
	}
}

// Handles track tooltip and all elements with that requested id
function request_track_done(data)
{
	tool_tip['text'].html(data.html);
	
	//foo.concat(foo);
	if(!data.error)
	{
		init_user_regen();
		var request_buttons = $('#track_list td, #track_info td');
		
		for(var i = 0, len = request_buttons.length; i < len; i++)
		{
			// If it's not set up to be a request and the cost is greater than what the user has then disable it
			if(
				request_buttons[i].getAttribute('request') != request_hit['elmt'].attr('request') &&
				request_buttons[i].getAttribute('request') &&
				(
					data.user_points != 'unlimted' &&
					request_buttons[i].getAttribute('cost') > data.user_points
				)
			)
			{
				if(request_buttons[i].innerHTML != "In Queue")
				{
					request_buttons[i].className = 'request_lack_points';
					$(request_buttons[i]).unbind();
				}
			}
			else if(request_buttons[i].getAttribute('request') == request_hit['elmt'].attr('request'))
			{
				$(request_buttons[i]).unbind();
				$(request_buttons[i]).removeClass();
				$(request_buttons[i]).html('In Queue');
			}
		}
	} else {
		request_hit['elmt'].html('Now');
		request_hit['elmt'].addClass('request_allowed');
		request_hit['elmt'].hover( function(){ highlight($(this), 1, 'request_allowed_on') }, function(){ highlight($(this), 0, 'request_allowed_on') });
		request_hit['elmt'].click(function() { request_track($(this)) });
	}  
	window.setTimeout(function() { $(tool_tip['elmt']).fadeOut(1000); }, 1500);
	window.setTimeout(request_hit_timeout, 1500);
}
function request_hit_timeout()
{
  request_hit['in_progress'] = 0;
}

function init_track_list(list)
{
	var tds;
	switch(list)
	{
		case 'info':
			tds = $('#track_info td');
			break;
		default:
			tds = $('#track_list td, #track_info td');
			break;
	}	

	for(var i = 0, len = tds.length; i < len; i++)
	{
		if(tds[i].getAttribute('track'))
		{
			$(tds[i]).hover( function(){ highlight($(this), 1) }, function(){ highlight($(this), 0) });
			$(tds[i]).click(display_track_info);
		}
		if(tds[i].getAttribute('request'))
		{
			$(tds[i]).hover( function(){ highlight($(this), 1, 'request_allowed_on') }, function(){ highlight($(this), 0, 'request_allowed_on') });
			$(tds[i]).click(function() { request_track($(this)) });
		}
		if(tds[i].getAttribute('user'))
		{
			$(tds[i]).hover( function(){ highlight($(this), 1) }, function(){ highlight($(this), 0) });
			$(tds[i]).click(display_user_info);
		}
		if(tds[i].getAttribute('comment'))
		{
			$(tds[i]).hover( function(){ highlight($(this), 1) }, function(){ highlight($(this), 0) });
			$(tds[i]).click(display_comment);
		}
	}
}




//function collection2Array(collection){ var rv=[]; for(var i=0; i < collection.length; i++ ) rv.push(collection[i]); return rv }
//var list = collection2Array( collection1 ).concat( collection2Array( collection2 ) ); 
