/**
 *  Chillat.net Client Side Library
 *
 *  (c) 2009, Chillat.net
 *
 *  This library is licensed under the GNU GPL v3. You may use the code in
 *  your own non-commercial projects as long as you license the end result
 *  under the GPL v3 license or similar, and the origin of the code is
 *  clearly indicated in your sourcecode.
 */

try { if (!net) net = {} } catch(e) { net = {} };
if (!net.chillat) net.chillat = {};

lepton.Image.preload('/assets/icons/loading.gif');

/**
 * Toast class, renders popups in the toastbar container.
 *
 * Pass text, icon, and action to the constructor. Self destructs in 7
 * seconds by default.
 */
net.chillat.Toast = Class.create({
	initialize:function(text,icon,action) {
		// Create the elements and make them hidden (transparent)
		this.el = new Element('div', { class:'toast' } );
		this.el.setStyle({
			opacity:0.0,
			backgroundImage:icon.getCssIcon()
		});
		var a = new Element('a', { href:action });
		a.update(text);
		this.el.appendChild(a);
		// Add it to the toastbar
		$('toastbar').appendChild(this.el);
		// Fade it in, and activate the timeout
		this.el.fade({ duration: 1.5, from: 0, to: 0.8 });
		setTimeout(this.hide.bindAsEventListener(this), 7000);
	},
	hide:function() {
		// Fade out, and then delete the toast
		this.el.fade({ duration: 1.5, from: 0.8, to: 0, after:function(){
			this.el.parentNode.removeChild(this.el);
			delete this;
		}});
	}
});

net.chillat.Auth = {
	showLogin:function() {
		net.chillat.overlay = new lepton.ui.Overlay({
			backgroundColor:'#405080'
		});
		var loginDlg = $('login_dialog');
		net.chillat.overlay.show();
		loginDlg.center();
		loginDlg.show();
	},
	hideLogin:function() {
		net.chillat.overlay.hide();
		$('login_dialog').hide();
	}
}

/**
 * Icon management class with subclasses; getIcon returns the full URL of the
 * icon, while getCssIcon returns a url() wrapped CSS string.
 */
net.chillat.icons = {}
net.chillat.icons.Base = {
	getIcon:function() {
		return this.iconurl;
	},
	getCssIcon:function() {
		return "url('" + this.iconurl + "')";
	}
};
net.chillat.icons.Icon = Class.create(
	net.chillat.icons.Base, {
	initialize:function(icon) {
		this.iconurl = '/assets/icons/toasts/' + icon + '.png';
	}
});
net.chillat.icons.Avatar = Class.create(
	net.chillat.icons.Base, {
	initialize:function(userid) {
		this.iconurl = 'http://1.gravatar.com/avatar/06005cd2700c136d09e71838645d36ff?s=48&d=wavatar';
	}
});

net.chillat.game = {
	setPlayer:function(id) {
		$('gameplayers').addClassName('updating');
		new Ajax.Request('/api/setplaying', {
			method:'post',
			parameters:{
				gameid:id,
				state:1
			},
			onSuccess:function(t) {
				$('gameplayers').update(t.responseText);
				$('gameplayers').removeClassName('updating');
			}
		});
	}
};

net.chillat.bluebox = {};
net.chillat.bluebox.Manager = Class.create({
	initialize:function(cn,opts) {
		var els = document.getElementsByClassName(cn);
		for (var i = 0; i < els.length; i++) {
			new net.chillat.bluebox.Image(els[i],opts);
		}
	}
});
net.chillat.bluebox.Image = Class.create({
	initialize:function(el,opts) {
		this._el = $(el);
		this._src = $(el).getAttribute('href');
		this._opts = {
			overlayColor:'#111144'
		};
		Object.extend(this._opts, opts);
		$(el).observe('click', this.showBox.bindAsEventListener(this));
	},
	showBox:function(e) {
		this._ol = new lepton.ui.Overlay({
			backgroundColor:this._opts.overlayColor
		});
		this._ol.show();
		this._pel = new Element('img', { src:this._src } );
		document.documentElement.appendChild(this._pel);
		$(this._pel).setStyle({
			zIndex:99999,
			border:'solid 3px #101010',
			position:'absolute',
			cursor:'pointer'
		});
		$(this._pel).center();
		$(this._pel).observe('click', this.hideBox.bindAsEventListener(this));
		Event.stop(e);
		return false;
	},
	hideBox:function() {
		this._ol.hide();
		delete this._ol;
		document.documentElement.removeChild(this._pel);
		this._pel.hide();
		delete this._pel;
	}
});

function toastTest() {
	new net.chillat.Toast('There is a new post in the forum by <strong>someone</strong>, the topic is <strong>mini fails at wesnoth</strong>', new net.chillat.icons.Avatar(1), 'none');
	new net.chillat.Toast('There is a new post in the forum by <strong>someone</strong>, the topic is <strong>mini fails at wesnoth</strong>', new net.chillat.icons.Icon('web'), 'none');
	new net.chillat.Toast('There is a new post in the forum by <strong>someone</strong>, the topic is <strong>mini fails at wesnoth</strong>', new net.chillat.icons.Icon('mail'), 'none');
}
