var Commercial = {

	id : 0,
	
	edit : function (site_id) {
		$('commContact_' + site_id).hide();
		$('commContactEdit_' + site_id).show();

		$('History_' + site_id).hide();
		$('HistoryEdit_' + site_id).show();
	},
	
	cancel : function (site_id) {
		$('commContactEdit_' + site_id).hide();
		$('commContact_' + site_id).show();

		$('HistoryEdit_' + site_id).hide();
		$('History_' + site_id).show();
	},

	save_info : function (site_id) {
		
		// get edit values
		var title = $('TitleEdit_' + site_id).value;
		var address = $('AddressEdit_' + site_id).value;
		var url = $('URLEdit_' + site_id).value;
		var desc = $('DescriptionEdit_' + site_id).value;

		new Ajax.Request('commercial_ajax.php?action=save_info', {
			  method: 'post',
			  parameters: {id : site_id, title : title, address : address, description : desc, url : url},
			  onSuccess: function(transport) {
				var json = transport.responseText.evalJSON();
				if (json.success == 1)
				{
					window.location.reload();
				}
			  },
			  onFailure: function(transport) {
				  alert("An error occurred while trying to save this information. Please try again later.");
			  }
			});

	},

	post_update : function(inform) {
		var form = $(inform);
		var update = 'Updates_' + form.htmlId.value;

		var req = new Ajax.Updater(update, 'commercial_ajax.php?action=post_update', {
			  method: 'post',
			  parameters: {id : form.id.value, htmlId: form.htmlId.value, date : form.date.value, comment : form.comment.value },
		      onComplete: function(transport) {
				  form.comment.value = '';
			  },
			  onFailure: function(transport) {
				  alert("An error occurred while trying to add an update. Please try again later.");
			  }
			});

	},
	
	remove_update : function (site_id, update_id) {
		var update = 'Updates_' + site_id;

		var req = new Ajax.Updater(update, 'commercial_ajax.php?action=remove_update', {
			  method: 'post',
			  parameters: {id : site_id, update_id : update_id },
			  onFailure: function(transport) {
				  alert("An error occurred while trying to remove this update. Please try again later.");
			  }
			});
	},

	set_featured_image : function (site_id, image_id) {

		var image = $('FeaturedImage_' + site_id);

		var req = new Ajax.Request('commercial_ajax.php?action=set_featured_image', {
			  method: 'post',
			  parameters: {id : site_id, image_id : image_id },
			  onSuccess: function(transport) {
					image.src = transport.responseText;
			  },
			  onFailure: function(transport) {
				  alert("An error occurred while trying to set the featured image. Please try again later.");
			  }
			});
		
	},

	load_image : function(site_id, image_id) {
		new Ajax.Request('commercial_ajax.php?action=add_image', {
			  method: 'post',
		      parameters: {'id' : site_id, 'image_id': image_id},
			  onSuccess: function(transport) {
				var html = transport.responseText;
				$('commThumbs_' + site_id).insert(html, { position: 'bottom' });
				$('imageForm' + site_id).reset();
			  },
			  onFailure: function(transport) {
				  alert("An error occurred while trying to load the new image. Please refresh the page.");
			  }
			});
	},

	remove_image : function(site_id, image_id) {

		if (confirm('Are you sure you want to delete this image?'))
		{
			new Ajax.Request('commercial_ajax.php?action=delete_image', {
				method: 'post',
				parameters: {id : site_id, image_id : image_id},
				onSuccess: function (transport) {
					if (transport.responseText == '1') {
						var idx = 'commImage'+image_id;
						$(idx).remove();
					} else {
						alert('Error deleting image');
					}
				}
			});
		}
	}

}
