/**
 * Common Toaster & Toast settings
 */
var toastSettings = {
	'nudge_amount' : 4,
	'xOffset' : 64,
	'yOffset' : 8
};

function init_toaster() 
{
	new Toaster();
}
init_last.push(init_toaster); 

/**
 * Class to toast bread, aka pop up toaster messages to the lower right corner
 * of Umbrella screen.
 */
var Toaster = Class.create({
	initialize: function()
	{
		this.bread = {};
		this.url = './styles/js/list.php';
		
		var params = {
			'mode':'toast',
			'q_id': 1,
	  		'keyword': 'toast'
		};
		
		new Ajax.Request(this.url, {
	  		method: 'post',
	  		encoding: 'UTF-8',
	  		parameters: params,
	  		onFailure: function() { ajax_failure(ajax_failure_text) },
	  		onSuccess: this.toastBread.bind(this)});	
	},
	
	/**
	 * Our magnificent ajax handler! 
	 */
	toastBread: function(r)
	{
		var text = r.responseText;
		try
		{
			var result = eval('(' + text + ')');
		} 
		catch(e) { var result = []; }
		
		var size = result.size();
		
		for(var i = 0; i < size; i++)
		{
			this.bread[result[i]['toast_id']] = new Toast(this, i, result[i]);
		}
		this.nudgeAll();
	},
	
	removeToast: function(index)
	{
		delete this.bread[index];
		this.nudgeAll();
	},
	
	nudgeAll: function()
	{
		var keys = Object.keys(this.bread);
		var size = keys.size();
		
		for(var i = 0; i < size; i++)
		{
			try 
			{
				this.bread[keys[i]].nudgeRight(i);
			}
			catch(e) {
			}
		}
	}
});

/**
 * Bread class to shoot up from Toaster, ie, this is the tiny popup
 */
var Toast = Class.create({
	
	initialize: function(toaster, offset, data)
	{
		this.myToaster = toaster;
		this.offset = offset;
		this.data = data;
		
		this.url = './styles/js/save.php';
		
		this.params = {
			'mode':'toast',
			'toast': this.data['toast_id']
		};
		
		this.myObject = new Element('div', {
			'id'	:	'toast_' + this.data['toast_id']
		}).addClassName('toast');
		
		var height = this.calcHeight(this.offset);
		
		this.myObject.addClassName('toast').setStyle({
			'display'	: 'none',
			'height'	: height + 'px',
			'right'		: this.calcRight(this.offset) + 'px',
			'zIndex'	: 100000000 - this.offset
		});
		
		this.myTopic = new Element('div', {
		}).addClassName('toasttopic').insert(this.data['toast_topic']);
		
		this.myClose = new Element('a', {
		}).addClassName('topicclose').insert('&times;');

		this.myTopic.insert(this.myClose);
		
		if(this.data['toast_body'] == "") this.data['toast_body'] = "...";
		
		this.myBody = new Element('div', {
		}).addClassName('toastbody').insert(new Element('a').insert(this.data['toast_body']));
		
		Event.observe(this.myBody, 'click', this.openLink.bind(this));
		Event.observe(this.myClose, 'click', this.closeToast.bind(this));
		
		this.myObject.insert(this.myTopic).insert(this.myBody);
		
		//var host = 
		if(height > 0) 
		{
			$$('body').first().insert(this.myObject);
			this.eject();
		}
	},
	
	/**
	 * Fire the toast out! show message to user!
	 */
	eject: function()
	{
		jQuery('#' + this.myObject.identify()).slideDown(animate_slow);
	},
	
	nudgeRight: function(offset)
	{
		this.offset = offset;
		
		if(this.myObject)
		{
			var height = this.calcHeight(offset);
			var body = $$('body').first();
			
			if(height > 0 && !this.myObject.descendantOf(body))
			{
				body.insert(this.myObject);
			}
			
			jQuery('#' + this.myObject.identify()).animate({
				'right'		: this.calcRight(offset) + 'px',
				'height'	: height + 'px'
			}, animate_fast);
			
			if(height > 0) this.eject();
		}
		else return;
	},
	
	calcRight: function(offset)
	{
		var x =  Math.round(
			toastSettings['xOffset'] - (offset * toastSettings['nudge_amount'])
		);
		return x;
	},
	
	calcHeight: function(offset)
	{
		if(offset > 9) return 0;
	
		return Math.floor(120 * Math.cos(offset / (1.5 * Math.PI)));
	},
	

	/**
	 * function to dismiss toast.
	 */
	markRead: function(onSuccess)
	{
		new Ajax.Request(this.url, {
	  		'method'		: 'post',
	  		'encoding'		: 'UTF-8',
	  		'parameters'	: this.params,
	  		'onFailure'		: function() { ajax_failure(ajax_failure_text) },
	  		'onSuccess'		: onSuccess
	  	});
	},
	
	openLink: function(event)
	{
		//only allow clicking of first toast
		if(this.offset != 0) return;
		
		this.markRead(this.redirect.bind(this));
	},
	
	closeToast: function(event)
	{
		if(this.offset !== 0) return;
		if(event) 
		{
			Event.stop(event);
		}
		
		this.markRead(undefined);
		
		jQuery('#' + this.myObject.identify()).slideUp(animate_fast, this.destroy.bind(this));
	},
	
	redirect: function()
	{
		window.location = this.data['toast_url'];
		jQuery('#' + this.myObject.identify()).slideUp(animate_fast, this.destroy.bind(this));
	},
	
	destroy: function()
	{
		this.myObject.remove();
		delete(this.myObject);
		this.myToaster.removeToast(this.data['toast_id']);
	}
});


