RISE.MMessages = SZN.ClassMaker.makeClass({
	NAME: "MMessages",
	VERSION: "1.0",
	CLASS: "class",
	EXTEND: RISE.Module
});
RISE.MMessages.prototype.$constructor = function(mid,mn,mp,endtime) {
	this.Module(mid,mn,mp);
	var self = this;
	
	self.timeout = false;
	self.endtime = endtime;
	self.lifetime = 0;
	self.timestepsize = 100;
	self.paused = false;
	self.msg = $('#'+self.mid);
	self.msgcont = $('#'+self.mid+' div.cont');
	
	self.fetchMessagesSuccessCallback = function(response,textStatus){ self.fetchMessagesSuccess(this,response,textStatus); }
	
	self.clocks = $('#'+self.mid+' div.clocks');
	
	self.hideEvent = function(event){ return self.hide(this,event); }
	self.pauseTimeoutEvent = function(event){ return self.pauseTimeout(this,event); }
	self.stopTimeoutEvent = function(event){ return self.stopTimeout(this,event); }
	self.focusEvent = function(event){ return self.focus(this,event); }
	
	self.msg.live('mouseover',self.pauseTimeoutEvent);
	self.msg.live('mouseout',self.pauseTimeoutEvent);
	self.msg.live('click',self.stopTimeoutEvent);
	$('#'+self.mid+' div.close').live('click',self.hideEvent);
	$(self.mid+ ' div[rel]').live('click',self.focus);
		
	self.show();
	
	
}

RISE.MMessages.prototype.focus = function(elm,event) {
	var source = $(elm).attr('rel');
	if(source){
		source.get(0).focus();
		this.hide();
	}
	
}

RISE.MMessages.prototype.timestep = function() {
	var self = this;
	
	self.lifetime = self.lifetime + self.timestepsize;
	if(self.lifetime<self.endtime && self.lenght>0){
		clearTimeout(self.timeout);
		self.timeout = setTimeout(function(){ self.timestep() }, self.timestepsize);
	}else{
		self.hide();
		return false;
	}	
	
	self.clocks.html( ((this.endtime - this.lifetime)/1000).toFixed(1)+' s');
	if($.support.opacity){
		var opacity = Math.round((1-Math.pow(this.lifetime/this.endtime,6))*100)/100;
		if(this.msg.css('opacity') != opacity) this.msg.css('opacity',opacity);
	}
	
	return true;
}
RISE.MMessages.prototype.pauseTimeout = function() {
	if(this.timeout){
		clearTimeout(this.timeout);
		this.timeout = false;
		this.paused = true;
		if($.support.opacity) this.msg.css('opacity',1);
	}else if(this.paused) this.timestep();
		
}
RISE.MMessages.prototype.stopTimeout = function() {
	if(this.timeout){
		clearTimeout(this.timeout);
	}
	if(this.clocks) this.clocks.html(' ');
	if(!this.timeout && !this.paused) this.hide();
	this.paused = false;
	this.timeout = false;
}
RISE.MMessages.prototype.hide = function() {
	clearTimeout(this.timeout);
	this.msg.css('display','none');
}
RISE.MMessages.prototype.show = function() {
	this.lenght = $('#'+this.mid+' div.messages div').length;
	this.msg.css('display','block');
	if($.support.opacity) this.msg.css('opacity',1);
	this.lifetime = 0;
	clearTimeout(this.timeout);
	this.timestep();
}

RISE.MMessages.prototype.fetchMessages = function(){
	
	var data = { action: "fetchMessages",location: 0 };
	var custom = { errorMsg: this.t.fetchMessagesKO };
	var options = { dataType: 'html',success: this.fetchMessagesSuccessCallback };
	this.ajax(options,data,custom);
	
}
RISE.MMessages.prototype.fetchMessagesSuccess = function(options,response,textStatus){
	this.msgcont.html(response);
	this.show();
}

