// Requires jquery
function ilert(html, $overbox) {
	// Create the ilert
	var $box = $(document.createElement('span'));
	$box.addClass('ilertBox');
	$box.addClass('ui-corner-all');
	$box.html(html);
	$overbox.after($box);
	
	// Figure out where it should go
	var position = $overbox.position();
	position.left += $overbox.width() / 2 - $box.width() / 2;
	position.top += $overbox.height() / 2 - $box.height() / 2;
	$box.css({position: 'absolute', left: position.left + 'px', top: position.top + 'px'});
	$box.fadeIn(400).delay(800).fadeOut(400);
}

function addSpinner($overbox) {
	// Figure out where it should go
	var position = $overbox.position();
	position.left += $overbox.width() / 2 - 8;
	position.top += $overbox.height() / 2 - 8;

	var uid = 'spinner_' + Math.floor(Math.random() * 1000000);
	
	$overbox.after('<img id="' + uid + '" src="/salsa/images/spinner.gif" style="position: absolute; left: ' + position.left + 'px; top: ' + position.top + 'px" alt="waiting..." />');
	$overbox.css({opacity: .4, filter: 'alpha(opacity=40)'});

	return uid;
}

function removeSpinner($overbox, uid) {
	$overbox.css({opacity: '', filter: ''});
	$('#' + uid).remove();
}

function eventRSVP(orgkey, event_KEY, supporter_KEY, trigger_KEYS, feed_archetype, box) {
	if (supporter_KEY) {
		$.get('save', {
			table: 'supporter_event',
			event_KEY: event_KEY,
			supporter_KEY: supporter_KEY,
			email_trigger_KEYS: trigger_KEYS,
			organization_KEY: orgkey,
			_Type: 'Supporter',
			_Status: 'Signed Up',
			Additional_Attendees: 0,
			feed_archetype_custom_linked: feed_archetype,
			feed_key: event_KEY,
			simple: true
		}, function(data) {
			if (data > 0) {
				ilert("RSVP Received!", $(box));
				$(box).replaceWith('<a class="cancel" href="#" onclick="eventCancel('+orgkey+', '+event_KEY+', '+supporter_KEY+', \''+trigger_KEYS+'\', '+feed_archetype+', this); return false;">');
			} else
				ilert("RSVP Failed.", $(box));
		});
	} else {
		ilert("You must be signed in to RSVP.", $(box));
	}
}

function groupJoin(orgkey, groups_KEY, supporter_KEY, feed_archetype, box, handler) {
	if (supporter_KEY) {
		var params = {
			table: 'supporter_groups',
			groups_KEY: groups_KEY,
			supporter_KEY: supporter_KEY,
			organization_KEY: orgkey,
			simple: true
		};
		if (feed_archetype) {
			params.feed_archetype_custom_linked = feed_archetype;
			params.feed_key = groups_KEY;
		}
		$.get('/save', params,
		function(data) {
			if (handler)
				handler(data);
			else {
				if (data > 0)
					ilert("Group Joined!", $(box));
				else
					ilert("Group join failed.", $(box));
			}
		});
	} else {
		ilert("You must be signed in to join a group.", $(box));
	}
}

function groupLeave(orgkey, groups_KEY, supporter_KEY, box, handler) {
	if (supporter_KEY) {
		var params = {
			groups_KEY: groups_KEY,
			supporter_KEY: supporter_KEY
		};
		if (handler)
			params.successText = 'success';
		$.get('/o/' + orgkey + '/p/salsa/common/community/ajax/groupLeave.sjs', params,
		handler || function(data) {
			ilert(data, $(box));
		});
	} else {
		ilert("You must be signed in to leave a group.", $(box));
	}
}

function eventCancel(orgkey, event_KEY, supporter_KEY, trigger_KEYS, feed_archetype, box) {
	if (supporter_KEY) {
		$.get('/o/' + orgkey + '/p/salsa/common/community/ajax/eventCancel.sjs', {
			event_KEY: event_KEY,
			supporter_KEY: supporter_KEY
		}, function(data) {
			ilert(data, $(box));
			$(box).replaceWith('<a class="rsvp" href="#" onclick="eventRSVP('+orgkey+', '+event_KEY+', '+supporter_KEY+', \''+trigger_KEYS+'\', '+feed_archetype+', this); return false;">');
		});
	} else {
		ilert("You must be signed in to cancel an RSVP.", $(box));
	}
}

function requestFriend(orgkey, friendee, box) {
	var spinner = addSpinner($(box));
	$.get('/o/' + orgkey + '/p/salsa/common/community/ajax/friend.sjs', {
		friendee: friendee
	}, function(data) {
		removeSpinner($(box), spinner);
		ilert(data, $(box));
	});
}

// Adjust an image to a given size
function scaleImage($img, scale_width, scale_height) {
	var loader = new Image();
	loader.onload = function() {
		var width = this.width;
		var height = this.height;

		if (width > scale_width || height > scale_height) {
			// in this case, shrink it down small
			var wscale = scale_width / width;
			var hscale = scale_height / height;
			var scale = Math.min(wscale, hscale);
			width = Math.round(width * scale);
			height = Math.round(height * scale);

			$img.height(height);
			$img.width(width);
		}
	};
	loader.src = $img.attr('src');
}

// Adjust all images that request a size
function scaleAllImages($div) {
	$div.find('img[please_width][please_height]').each(function() {
		var $img = $(this);
		scaleImage($img, $img.attr('please_width'), $img.attr('please_height'));
		$img.removeAttr('please_width');
		$img.removeAttr('please_height');
	});
}

