/**
 * Atom updater
 * @author Martin Vlach aka Pika (thx!)
 * @author Ondrej Hatala
 */
/*----------------------------------------------------------------------------------------------------------------------------
 * Mouse
 */
var Mouse = {
	store: {},
	save: function() {
	this.store = {
		x: Mouse.x,
		y: Mouse.y };
	},
	clear: function() {
		Mouse.store = {};
	}
}; 
/*----------------------------------------------------------------------------------------------------------------------------
 * Message
 */
var Message = Class.create();
Message.prototype = {

	/** init message */
	initialize: function(text, options) {
		this._options = {
			text: text,
			timer: null,
			className: '',
			position: 'top left',
			ieHack: false,
			x: Mouse.x, y: Mouse.y
		}
		Object.extend(this._options, options || {});
		
		if (typeof this._options.x == 'undefined') {
			var b = Element.getDimensions(document.body);
			this._options.x = (b.width/2)-65;
			this._options.y = 300;
		}
		
		this.obj = null;
		this.display();
		return this;
	},
	
	/** display message */
	display: function() {
		this.obj = document.createElement('div');
		this.obj.messageObj = this;
		
		$(this.obj).addClassName('js_message');
		$(this.obj).addClassName(this._options.className);
		$(this.obj).setStyle({'top':'-1000px', 'left':'-1000px'});
		
		this.obj.innerHTML = this._options.text;
		document.body.appendChild(this.obj);
		this.calibratePosition();
		
		if (this._options.ieHack) {
			this.ieHack();
		}
		
		// automatic hide
		if (this._options.timer) {
			window.setTimeout(this.hide.bind(this), this._options.timer);
		}
		return this;
	},
	
	/** window overflow issue */
	calibratePosition: function() {
		var cl = Element.getDimensions(document.body);
		var msg = Element.getDimensions(this.obj);
		var left = (this._options.x+msg.width > cl.width) ? cl.width-msg.width-10 : this._options.x;
		var top = (this._options.y+msg.height > cl.height) ? cl.height-msg.height-10 : this._options.y;
		if (this._options.position.match('bottom')) top = top-msg.height-3;
		if (this._options.position.match('right')) left = left-msg.width-3;
//		var pos = { 'top':top+'px', 'left':left+'px' }
		var pos = { 'top':document.documentElement.scrollTop+200+'px', 'left':left+'px' }
		Element.setStyle(this.obj, pos);
		return pos;
	},
	
	ieHack: function() {
		if (document.all) {
			var hack = document.createElement('iframe');
			hack.src = "empty_page.html";
			Element.addClassName(hack, 'nonie_hide');
			var dm = $(this.obj).getDimensions();
			Element.setStyle(hack, {
				'position': 'absolute',
				'top': this.obj.style.top, 'left': this.obj.style.left,
				'width': dm.width+'px', 'height': dm.height+'px'
			});
			new Insertion.Bottom(document.body, hack.outerHTML);
			this.iehack = document.body.lastChild;
		}
	},
	
	/** hide message */
	hide: function() {
		try {
			if (this.obj) {
				document.body.removeChild(this.obj);
				this.obj = null;
				delete this.obj;

				if (this._options.ieHack && this.iehack) {
					document.body.removeChild(this.iehack);
					this.iehack = null;
					delete this.iehack;
				}
				return true;
			}
		} catch (ex) { if(_debug) throw ex; return false; };
	}
}; 
var BoxUpdater = {
	/**
	 * update box containers
	 * @param {String} url
	 * @param {String|Object} form
	 */
	update: function(url, forms) {
		var params = [];
		if (typeof forms != 'undefined') {
			params.push(Form.serialize(forms));
		}
		new Ajax.Request(url, {
				method: 'post',
				parameters: params.join('&'),
				onSuccess: this.onUpdate.bind(this)
			});
		return true;
	},

	/**
	 * parse xml data and update containers
	 * @param {XMLHttpRequest} x
	 * @param {Object} json
	 */
	onUpdate: function(x, json) {
		var response = x.responseXML;
		if (response != null) {
			var atoms = $A(response.getElementsByTagName('atom'));
			//console.log(atoms);
			atoms.each( function(atom) {
				var id = atom.getAttribute('id');
				var container = $(id);
				if (container) {
					var text = [];
					$A(atom.childNodes).each( function(node) { text.push(node.data) });
					container.update(text.join(''));
				}
			});

		}
		
		if(typeof(checkStates) == 'function') {
			checkStates();
		}
		
		if (typeof(postLoadEvents)== 'function')
		{
			postLoadEvents();
		}
		if (typeof(Map)== 'function')
		{
			Map();
		}
//		BoxUpdater.hideMessage();
//		Mouse.clear();		
	},

	hideMessage: function () {
	    if (typeof this.waitMsg != "undefined" && this.waitMsg != null) {
	        this.waitMsg.hide();
	        this.waitMsg = null;
	    }
	}
};
