Configurator=new Class({
	queue: null,
	busy: null,

	currentButton: null,

	clientId: null,
	sourceId: null,

	cookie: null,
	defaultPosition: null,
	xmlBaseUrl: null,
	version: null,

	logUri: null,

	initialize: function() {
		this.queue=[];
		this.busy=false;

		if (sitepass.src_id) this.sourceId=sitepass.src_id;
		if (sitepass.client_id) this.clientId=sitepass.client_id;

		this.cookie=0;
		this.defaultPosition=1;
		this.xmlBaseUrl='update.alot.com/1/update/button_configs/button_config_';

		// Version should always be the current date/time.
		var datetime=new Date();
		var year=datetime.getUTCFullYear();
		var month=datetime.getUTCMonth()+1;
		if (month<10) month='0'+month;
		var day=datetime.getUTCDay();
		if (day<10) day='0'+day;

		var hour=datetime.getUTCHours();
		var minute=datetime.getUTCMinutes();
		var second=datetime.getUTCSeconds();

		this.version=year+'-'+month+'-'+day+'T'+hour+':'+minute+':'+second+'+00:00';

		this.logUri='http://www.alot.com/api/addDelLog';

		this.addEventHandlers();
	},

	addEventHandlers: function() {
		return false;
	},

	addToQueue: function(form) {
		var data={};

		var idEl=$E('input[name=id]', form);
		if (!idEl) return false;
		data.id=idEl.getValue().toInt();

		var captionEl=$E('input[name=caption]', form);
		if (!captionEl) return false;
		data.caption=captionEl.getValue();

		var minVersionEl=$E('input[name=minVersion]', form);
		if (!minVersionEl) return false;
		var minVersion=minVersionEl.getValue();

		if ('all'!=minVersion) {
			if ('none'==minVersion) {
				alert("This button is not currently available.");
				return false;
			} else if (sitepass.version<minVersion) {
				alert("This button is not compatible with your existing version.");
				return false;
			}
		}

		this.queue.push(data);

		return true;
	},

	logAdd: function(buttonId) {
		if ('number'!=$type(buttonId) || buttonId<1) return false;

		new Json.Remote(this.logUri, {
			method: 'post'
		}).send({
			'addDel': 1,
			'button': buttonId,
			'client': this.clientId,
			'source': this.sourceId
		});

	},

	processQueue: function(fromEvent) {
		if (!fromEvent && this.busy) return true;
		this.busy=false;

		if (0==this.queue.length) return true;

		// Pop the first item off of the queue.
		this.currentButton=this.queue.splice(0, 1)[0];
		this.busy=true;

		return this.addButton();
	}
});

var ConfiguratorIe=Configurator.extend({
	initialize: function() {
		this.parent();

		this.xmlBaseUrl='http://'+this.xmlBaseUrl;
	},

	addButton: function() {
		var id=this.currentButton.id;

		try {
			SHOT$$.add(
				id,
				this.version,
				this.xmlBaseUrl+id+'_1.xml',
				this.defaultPosition,
				this.cookie++
			);

			this.logAdd(id);

			return true;
		} catch (e) {
			this.handleAddEvent(false);
			return false;
		}

		return true;
	},

	addEventHandlers: function() {
		SHOT$$.OnDownloadComplete=this.processQueue.bindAsEventListener(this);

		return true;
	}
});

var ConfiguratorFf=Configurator.extend({
	initialize: function() {
		this.parent();

		this.xmlBaseUrl='https://'+this.xmlBaseUrl;
	},

	addButton: function() {
		var id=this.currentButton.id;

		try {
			sitepass.api.Custom.add(
				id,
				false,
				this.xmlBaseUrl+id+'_1_plaintext.xml',
				this.defaultPosition,
				this.cookie++
			);

			this.logAdd(id);

			return true;
		} catch (e) {
			this.handleAddEvent(false);
			return false;
		}

		return true;
	},

	addEventHandlers: function() {
		window.addEventListener(
			'AlotButtonAdd', this.processQueue.bindAsEventListener(this), false
		);

		return true;
	}
});


var gConfigurator;

window.addEvent('domready', function() {
	if ('undefined'==typeof sitepass || !sitepass.passed) return false;

	if ('ff'==sitepass.platform) {
		gConfigurator=new ConfiguratorFf();
	} else {
		gConfigurator=new ConfiguratorIe();
	}
});

document.addEvent('click', function(e) {
	e=new Event(e);
	var target=$(e.target);

	if (!target.hasClass('addToToolbar')) return true;

	e.preventDefault();

	if (!gConfigurator) return false;

	gConfigurator.addToQueue(target.getParent());
	gConfigurator.processQueue();
});


