$(function()
{
	toplist_tabs();
	box_in_table();
	screenshots();
	IconsList.init();
});

function toplist_tabs()
{
	var $container = $( "#toplist" );
	
	var $tables = $container.find( "table" );
	var $tabs 	= $container.find( "ul" );
	
	$tables.not( ":first" ).hide();		// Hide all toplist tables but the first
	
	$tabs.find( 'a' ).click(function()	// Assign clicks to tabs
	{
		var $this = $(this);
						
		var $table_to_show = $($this.attr( 'href' ));	// Get the table to show from the href attr of the clicked tab 
		if (!$table_to_show.size())				// Check if table exists - else, stop here
		{
			return false;
		}
		// Table is found..
		
		// Highlight the current tab
		$tabs.find( 'li' ).removeClass( 'current' );
		$this.parent().addClass( 'current' );
		
		// Show the wanted table and hide the others
		$tables.hide();
		$table_to_show.show();
		
		if($("#toplist-page").size()) 
		{
			box_in_table();
		}
		
		return false;
	});
}

// Create a box around the three first rows on toplist table
function box_in_table()
{
	if(!$("#toplist-page").size()) return false; // Check if we're on the right page
	
	var $table = $("#toplist table:visible");
		
	if ($table.find('td:first.rank').text() != 1) return false;	// Only apply this on the first table page (num 1-10)
		
	var $three_first_rows 	= $table.find('tr:lt(4)');	// Get three first rows
	// Add borders..
	$three_first_rows.eq(1).find('td').addClass('toplist-border-top');		// top
	$three_first_rows.find('td:last').addClass('toplist-border-right');		// right
	$three_first_rows.eq(3).find('td').addClass('toplist-border-bottom');	// bottom
	$three_first_rows.find('td:first').addClass('toplist-border-left');		// left
		
	// I love u Jquery.
	
}

// Opens screenshots in new window
function screenshots()
{
	var specs = 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=300,height=300';
	$("#screenshots a").click(function()
	{
		var url = $(this).attr('href');
		window.open(url,'pop',specs);
		return false;
	});
}

$(document).ready(function() {
	$('#categories input[@type="submit"]').remove();

	$('#categories select').change(function() {
		window.location = $(this).val();
	});
});

var IconsList =
{
	init:function()
	{
		if (!$('body#iconpage-page').size())
		{
			return false;
		}
		
		var $icons = $('#icons ul img');
		
		$icons.each(function()
		{
			var $this = $(this);
			
			$this.wrap('<a href="#" title="Get HTML for this one"></a>');
			
			$this.parent('a').click(function(e)
			{
				IconsList.show_markup($(this),e.pageX, e.pageY);
				return false;
			})
		});
	},
	
	show_markup:function(obj, mouse_x, mouse_y)
	{
		var $container = obj.parent('li');
		var $img = obj.find('img');
		var img_src = $img.attr('src');
		var img_alt = $img.attr('alt');
				
		var site_url = 'http://www.affiliatetips.com';
		
		var box_y = mouse_y - 100;
		var box_x = mouse_x - 400;
		
		$('div.markup').remove();
		
		var markup = '<a href="' + site_url + '/">\n<img src="' + site_url + img_src + '" alt="' + img_alt + '" />\n</a>';
		
		$('#icons').append('<div class="markup" style="top:' + box_y + 'px;left:' + box_x + 'px;"><textarea>' + markup + '</textarea><br /><a href="#" class="close">Close</a></div>');
		
		$('div.markup a.close').click(function()
		{
			$('div.markup').remove();
			return false;
		});
	}
};