$(document).ready(function(){
	// Select all checkboxes in parent form
	$('a[@name=select]').click(function(){
		$(this).parents('form').find('input[@type=checkbox]').attr('checked', 'checked');
	});
	// Deselect all checkboxes in parent form
	$('a[@name=deselect]').click(function(){
		$(this).parents('form').find('input[@type=checkbox]').removeAttr('checked');
	});
	// Toggle all checkboxes in parent form
	$('a[@name=toggle]').toggle(
		function() {
			$(this).parents('form').find('input[@type=checkbox]').attr('checked', 'checked');
		},
		function() {
			$(this).parents('form').find('input[@type=checkbox]').removeAttr('checked');
		}
	);
	// Links that require confirmation
	$('a[@name=confirm]').click(function() {
		ans = confirm('Are you sure?');
		if(!ans) return false;
		return true;
	});
	// Links that require confirmation
	$('input[@name=confirm]').click(function() {
		ans = confirm('Are you sure?');
		if(!ans) return false;
		return true;
	});
	// Bookmark	
	$('a[@name=bookmark]').click(function() {
		var url = 'http://www.rickrolled.com';
		var title = 'RickRolled';
		if($.browser.mozilla) window.sidebar.addPanel(title, url, "");
		if($.browser.msie) window.external.AddFavorite(url, title);
	});
	
	$('a[@name=abuse]').click(function() {
		$(this).parent().html('Thank you!');
	});
	
	notice();
	
});

// Count clicks on link
function count_click(entity, id) {
	url = base_url+'ajax/click/'+entity+'/'+id;
	return $.get(url);
}

// Notice
function notice() {
	$('a[@name=notice]').click(function(e) {
		$('#notice').remove();
		xcord = e.pageX;
		ycord = e.pageY;
		$.get(base_url+'ajax/notice', function(data) {
			$(data).appendTo('body')
				.css({left: xcord, top: ycord});
			$('a[@name=close]').click(function() {
				$('#notice').remove();
			});
		});
	});
	$('input[@name=notice]').click(function(e) {
		$('#notice').remove();
		$(this).attr('checked', false);
		xcord = e.pageX;
		ycord = e.pageY;
		$.get(base_url+'ajax/notice', function(data) {
			$(data).appendTo('body')
				.css({left: xcord, top: ycord});
			$('a[@name=close]').click(function() {
				$('#notice').remove();
			});
		});
	});
}

function report_abuse(usr_id, section) {
	$.post(base_url+'ajax/report_abuse', {usr_id: usr_id, section: section});
}

function color_picker(link, e) {
	id = $(link).siblings('input').attr('id');
	$('#colorpicker').remove();
	xcord = e.pageX;
	ycord = e.pageY;
	$('<div id="colorpicker"></div>').appendTo('body').css({left: xcord, top: ycord});
	$('#colorpicker').farbtastic('#'+id);
	$('<p><a href="javascript:void(0);" name="confirm" id="'+id+'">Confirm</a> | <a href="javascript:void(0);" name="cancel">Cancel</a></p>').appendTo('#colorpicker');
	$('.wheel').focus();
}