$(function()
{
	$('.featured_resources dd a').live('click' ,function()
	{
		$.get('/actions/download_resouce/'+this.rel+'/');
	});
	
	$('#sidebar .categories .header h3').click(function()
	{
		$('#sidebar .categories .content').hide().parents('.categories').addClass('closed');
		$(this).parents('.categories').removeClass('closed').find('.content').show();
	});
	
	$('.add_to_tracker').live('click', function()
	{
		var $element 	= $(this);
		var a_title		= $element.parent().siblings('.title_wrapper').find('a').attr('title');
		var a_href		= $element.parent().siblings('.title_wrapper').find('a').attr('href');
		var id			= $element.attr('id').substr(14);
		var image		= $element.siblings('a.image_link').html().replace(/height:(\s*)(\d+)px/, 'height: 26px').replace(/width:(\s*)(\d+)px/, 'width: 26px');
		
		$.get($element.attr('href') + '?hide_loader', function(data)
		{
			$('#ajax_like_' + id).parents('li').remove();
			$('<li><a id="ajax_like_' + id + '" href="' + a_href + '" title="' + a_title + '">' + a_title + '</a></li>').prependTo($('.sidebar_my_actions_list ul')).hide().fadeIn();
			
			var len = $('.sidebar_my_actions_list ul').children().length;
			if(len == 1){ $('.sidebar_my_actions_list, .sidebar_actions_button, .sidebar_actions_no_button').show(); }
			if(len > 5)
			{
				$('.sidebar_my_actions_list ul li:last-child').remove();
				$('.sidebar_my_actions_list .see_all').css({'display': 'block'});
			}
			
			if($('#header_actions').children('li').length == 6){ $('#header_actions').children('li:last').fadeOut().remove(); }
			$('<li>'
			+	'<a href="' + a_href +'" title="' + a_title +'">' + image +	'</a>'
			+'</li>').prependTo($('#header_actions')).hide().fadeIn();
			
			$('#no-tracker-text').slideUp();
			
			var $update_all = $('.sidebar_start_tracking #tracking_total');
			if($update_all.length>0)
			{
				var all_done = parseInt($update_all.html())>0 ? parseInt($update_all.html()) + 1 : 1;
				$update_all.html(all_done);
				if (all_done == 2){ $('.sidebar_start_tracking #tracking_multiple').html('s'); }
			}
			
			var $header_update_all = $('#id_header_all_track');
			if($header_update_all.length>0)
			{
				var header_all_done = parseInt($header_update_all.html()) + 1;
				$header_update_all.html(header_all_done);
				if (header_all_done == 2){ $('#id_header_all_track_multiple').html('s'); }
				header_pulsate($header_update_all);
			}
			
//			$element.css('visibility', 'hidden');
			$('#add_to_tracker'+id).css('visibility', 'hidden');
			$('#empty_tracker_li').remove();
			
			$('.sidebar_start_tracking').show();
			
			header_bounce();
		});
		
		return false;
	});
	
	$('.dialog_challenge_someone').dialog(
	{
		autoOpen		: false,
		bgiframe		: false,
		width			: 500,
		modal			: true,
		closeOnEscape	: true,
		draggable		: false,
		resizable		: false,
		buttons			:
		{	
			'Cancel'		: function(){ $(this).dialog('close'); }
		}
	});
	
	$('#challenge_someone').click(function()
	{
		$('.dialog_challenge_someone').dialog('open');
		return false;
	});
	
	$('.multiplier_description').dialog(
	{
		autoOpen		: false,
		bgiframe		: false,
		width			: 500,
		modal			: true,
		closeOnEscape	: true,
		draggable		: false,
		resizable		: false,
		buttons			:
		{	
			'Ok'		: function(){ $(this).dialog('close'); }
		}
	});
	
	$('#multiplier_description').click(function()
	{
		$('.multiplier_description').dialog('open');
		return false;
	});
	
	$('.dioalog_copyright_alert').dialog(
	{
		autoOpen		: false,
		bgiframe		: false,
		width			: 500,
		modal			: true,
		closeOnEscape	: true,
		draggable		: false,
		resizable		: false,
		buttons			:
		{	
		'Ok'		: function(){ $(this).dialog('close'); }
		}
	});
	
	if($('.copyright').length)
	{
		$('.copyright')[0].oncontextmenu = function(e)
		{
			$('#copyright_alert').dialog('open');
			return false;
		};
	}
	
});