AlertSet =
{
	_container: document.createElement('div'),
	_modalOverlay: document.createElement('div'),
	_alerts: {},
	_buttons: [],
	
	_clear: function(clearButtons)
	{
		this._alerts =  
		{
			error:[],
			warning:[],
			validation:[],
			info:[],
			success:[],
			question:[],
			debug:[],
			mysql_debug:[]
		};
		
		if(clearButtons!==false)
			this._buttons = [];
		
		this._update();
		
	},
	
	_update: function()
	{
		var html, prop, i;
		
		
		
		html='';
		for(prop in this._alerts)
		{
			if(this._alerts[prop].length != 0)
			{
				html += '<div class="'+prop+'">';
				
				if(this._alerts[prop].length > 1)
				{
					html += '<ul>';
				
					for(i=0; i < this._alerts[prop].length; i++)
					{
						html += '<li>';
						html += this._alerts[prop][i];
						html += '</li>';
					}
					html += '</ul>';
				}
				else
					html += this._alerts[prop][0];	
				
				html += '</div>';
			}
		}
		
		if(this._buttons.length==0)
			html += '<div class="AlertSet_button" onclick="AlertSet.hide()">Close</div>';
		else
		{
			var i=this._buttons.length
			while(i--)
			{
				html += '<div class="AlertSet_button" onclick="AlertSet.callback('+i+')">'+this._buttons[i].text+'</div>';
			}
		}
		
		
		this._container.innerHTML = html;	
	}, 
	
	callback: function(index)
	{
		if(isNaN(index))
			return false;
		
		if(!!this._buttons[index].callback)
			this._buttons[index].callback();
			
		return this;
	},
	
	show: function()
	{
		this._container.style.display = '';
		this._container.style.left = (screen.availWidth/2 - this._container.scrollWidth/2)+'px';
		this._modalOverlay.style.display = '';
	},
	
	hide: function(clear)
	{
		if(clear!==false)
			this._clear();
	
		this._container.style.display = 'none';
		this._modalOverlay.style.display = 'none';
	},
	
	addJSON: function(jsonObj, append)
	{
		var tmpArray=[], tmpJSON={};
		
		if(!append)
			append = false;
	
		try
		{
			if(typeof(jsonObj)=='string')
				jsonObj = JSON.parse(jsonObj);
			tmpArray=jsonObj['response']['infoArray'];
		}
		catch(err)
		{
			return false;
		}
		
		if(!append)
			this._clear(false);
		
		for(prop in tmpArray)
		{
			for(var i=0; i<tmpArray[prop].length;i++)
			{
				this._alerts[prop].push(tmpArray[prop][i]);
			}
		}
		
		this._update();
		this.show();
		return this;
	},
	
	addError: function(text)
	{
		this._alerts.error.push(text);
		this._update();
		return this;
	},
	
	addSuccess: function(text)
	{
		this._alerts.success.push(text);	
		this._update();
		return this;
	},
	
	addWarning: function(text)
	{
		this._alerts.warning.push(text);
		this._update();
		return this;
	},
	
	addValidation: function(text)
	{
		this._alerts.validation.push(text);
		this._update();
		return this;
	},
	
	addQuestion: function(text)
	{
		this._alerts.question.push(text);
		this._update();
		return this;
	},
	
	addDebug: function(text)
	{
		this._alerts.debug.push(text);
		this._update();
		return this;
	},
	
	addInfo: function(text)
	{
		this._alerts.info.push(text);
		this._update();
		return this;
	},
	
	addButton: function(text, callback)
	{
		this._buttons.push({
						   text: text,
						   callback: callback
						   });
		
		AlertSet._modalOverlay.onclick=null;
	
		return this;
	},
	
	addEvent: function(obj, evt, fn)
	{
		if (obj.addEventListener)
			obj.addEventListener(evt, fn, false);
		else if (obj.attachEvent)
			obj.attachEvent('on'+evt, fn);
		else
			obj['on'+evt] = fn;
	}
};

AlertSet._clear();
AlertSet._container.style.display='none';
AlertSet._container.className='responseContainer';

AlertSet._modalOverlay.style.display='none';
AlertSet._modalOverlay.className='responseModalOverlay';

AlertSet._modalOverlay.onmousedown = function() {return false};
AlertSet._modalOverlay.onclick = function() {AlertSet.hide()};

AlertSet.addEvent(window, 'load', function()
{
	document.body.appendChild(AlertSet._modalOverlay);
	document.body.appendChild(AlertSet._container);
});

