/* ---------------------------------------------------------------------------------------------------- */
/* =mxm.js */
/* ---------------------------------------------------------------------------------------------------- */

if (!$defined(MXM)) var MXM = {};


/* -------------------------------------------------- */
/* =IFrameShim */

var IFrameShim = new Class({

	Implements: [Options, Events],
	
	options: {
		name: '',
		className: 'iframeShim',
		display: false,
		zindex: null,
		margin: 0,
		offset: {x: 0, y: 0},
		container: null,
		browsers: (Browser.Engine.trident4 || (Browser.Engine.gecko && !Browser.Engine.gecko19 && Browser.Platform.mac))
	},
	
	initialize: function (element, options) {
		this.setOptions(options);
		this.isActive = false;
		//if (!this.options.browsers) return false;
		this.isActive = true;
		this.element = $(element);
		this.build();
	},
	
	build: function () {
		this.id = this.options.name || new Date().getTime() + '_shim';
		
		/*if(this.element.getStyle('z-Index').toInt()<1 || isNaN(this.element.getStyle('z-Index').toInt())) {
			
			this.element.setStyle('z-index', 5);
		}
		var z = this.element.getStyle('z-index') - 1;
		
		if($chk(this.options.zindex) && this.element.getStyle('z-Index').toInt() > this.options.zindex) {
			 z = this.options.zindex;
		}*/
		
		var z = this.options.zindex;
		
		this.shim = new IFrame({
			'src': 'javascript:false;',
			'frameborder': '0',
			'scrolling': 'no',
			'id': this.id,
			'class': this.options.className,
			'styles': {
				'position': 'absolute',
				'zIndex': z,
				'border': 'none',
				'filter': 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)'
			}
		});
		
		this.shim.inject(this.element, 'after');
		(this.options.display) ? this.show() : this.hide();
	},
	
	adjust: function () {
		var before = this.element.getStyles('display', 'visibility', 'position');
		this.element.setStyles({
			display: 'block',
			position: 'absolute',
			visibility: 'hidden'
		});
		var size = this.element.getSize();
		this.element.setStyles(before);
		if($type(this.options.margin)){
			size.x = size.x-(this.options.margin*2);
			size.y = size.y-(this.options.margin*2);
			this.options.offset.x += this.options.margin; 
			this.options.offset.y += this.options.margin;
		}
		
		var pos = (this.options.container) ? this.element.getPosition(this.options.container) : this.element.getPosition();
		
 		this.shim.setStyles({
			'width': size.x,
			'height': size.y,
			'left': pos.x + this.options.offset.x,
			'top': pos.y + this.options.offset.y
		});
	},
	
	show: function(){
		this.adjust();
		this.shim.setStyle('display', 'block');
	},
	
	hide: function () {
		this.shim.setStyle('display', 'none');
	}	
	
});


/* -------------------------------------------------- */
/* =Dict */

var Dict = new Class({
	
	Implements: Events,
	
	initialize: function (dictJSON) {
		this.items = new Hash(dictJSON);
		this.fireEvent('complete');
	},
	
	get: function (key) {
		return (this.items.has(key)) ? this.items.get(key) : '[' + key + ']';
	}
	
});


Dict.Remote = new Class({
	
	Extends: Dict,
	
	initialize: function (serviceurl) {
		new Request({
			url: serviceurl,
			async: false,
			onSuccess: this.parse.bind(this)
		}).send();
	},
	
	parse: function (responseText, responseXML) {
		this.items = new Hash();
		var items = responseXML.getElementsByTagName('item');
		for (var i = 0, len = items.length; i < len; i++) {
			this.items.include(items[i].getAttribute('key'), this.getNodeValue(items[i]));
		}
	},
	
	getNodeValue: function (node) {
		if (node.textContent) return node.textContent;
		else if (node.nodeValue) return node.nodeValue;
		else if (node.text) return node.text;
	}
	
});



/* -------------------------------------------------- */
/* =FireDefaultButton */

var FireDefaultButton = new Class({
	
	initialize: function (element, button) {
		this.element = $(element);
		this.element.addEvent('keydown', this.check.bindWithEvent(this));
		this.button = $(button);
	},
	
	check: function (event) {
		if (event.key == 'enter' && !(event.target && (event.target.get('tag') == 'textarea'))) {
			if (this.button) {
				event.stop();
				this.button.click();
			}
		}
	}
	
});