/**
 * Adlift
 * AJAX actions
 * (c) Alex Macmillan 2008
 * USE OF THIS CODE IS PERMITTED UNDER LICENSE ONLY.
 * @package com.x13n.alex.adlift
 */
 
AdliftActions = {
	deleteImage: function (imageid) {
		this.command = "deleteimage";
		
		this.parameters = [
			'imageid', imageid
		];
		
		this.bottom = function (response) {
			// Check that AJAX request succeeded.
            Assert.responseNotError(response);
            Assert.responseIs("ok", response);
			
			// Reflect that the image has been deleted on the webpage
			get("photos").removeChild(get("thumbholder"+imageid));
		};
	
	},
	
	toggleImageFeatured: function(imageid) {
		this.command = "toggleimagefeatured";
		
		this.parameters = [
			'imageid', imageid
		];
		
		this.bottom = function (response) {
			Assert.responseNotError(response);
			Assert.responseIs("toggle", response);
			
			if (response.attributeAsInt('featured') == 1)
				get("featured"+imageid).src = "admin-img/featured.png";
			else
				get("featured"+imageid).src = "admin-img/not-featured.png";
		}
	
	},
	
	setFavouriteImage: function(imageid) {
		this.command = "favouriteimage";
		
		this.parameters = [
			'imageid', imageid
		];
		
		this.bottom = function (response) {
			Assert.responseNotError(response);
			Assert.responseIs("ok", response);
			
			old = response.attribute('old');
			
			get("featured"+imageid).src = "admin-img/featured.png";
			if (old > "") {
				olde = get("featured"+old);
				if (olde)
					olde.src = "admin-img/not-featured.png";
			}
		}
	},
	
	publishImage: function(imageid) {
		this.command = "publishimage";
		
		this.parameters = [
			'imageid', imageid
		];
		
		this.bottom = function (response) {
			Assert.responseNotError(response);
			Assert.responseIs("ok", response);
			
			// Reflect that the image has been moved to the live page.
			get("photos").removeChild(get("thumbholder"+imageid));
		}
	},
	
	unpublishImage: function(imageid) {
		this.command = "unpublishimage";
		
		this.parameters = [
			'imageid', imageid
		];
		
		this.bottom = function (response) {
			Assert.responseNotError(response);
			Assert.responseIs("ok", response);
			
			// Reflect that the image has been moved to the preview page.
			get("photos").removeChild(get("thumbholder"+imageid));
		}
	},
	
	getImageHash: function(imageid) {
		this.command = "getimagehash";
		
		this.parameters = [
			'imageid', imageid
		];
		
		this.bottom = function (response) {
			Assert.responseNotError(response);
			Assert.responseIs("hash", response);
		
			hash = response.text();
			
			backgroundloader = new Image();
			backgroundloader.src = "image.php?type=big&id=" + hash + "&uniq=" + (new Date()).getTime();
			loading = true;
		}
	},
	
	loadImage: function(imageid) {
		this.command = "getimagemetadata";
		
		this.parameters = [
			'imageid', imageid
		];
		
		this.bottom = function (response) {
			Assert.responseNotError(response);
			Assert.responseIs("metadata", response);
			
			hash = response.child("hash").text();
			
			backgroundloader = new Image();
			backgroundloader.src = "image.php?type=big&id=" + hash + "&uniq=" + (new Date()).getTime();
			loading = true;
			
			get('location').innerHTML = response.child("location").text();
			get('mall').innerHTML = response.child("mall").text();
			get('date').innerHTML = response.child("date").text();
			get('advertiser').innerHTML = response.child('advertiser').text();
			get('categories').innerHTML = response.child('categories').text();
            get('format').innerHTML = response.child('format').text();
		}
	}
}