//MooTools More, <http://mootools.net/more>. Copyright (c) 2006-2009 Aaron Newton <http://clientcide.com/>, Valerio Proietti <http://mad4milk.net> & the MooTools team <http://mootools.net/developers>, MIT Style License.

/*
---

script: More.js

description: MooTools More

license: MIT-style license

authors:
- Guillermo Rauch
- Thomas Aylott
- Scott Kyle

requires:
- core:1.2.4/MooTools

provides: [MooTools.More]

...
*/

MooTools.More = {
	'version': '1.2.4.4',
	'build': '6f6057dc645fdb7547689183b2311063bd653ddf'
};

/*
---

script: Tips.js

description: Class for creating nice tips that follow the mouse cursor when hovering an element.

license: MIT-style license

authors:
- Valerio Proietti
- Christoph Pojer

requires:
- core:1.2.4/Options
- core:1.2.4/Events
- core:1.2.4/Element.Event
- core:1.2.4/Element.Style
- core:1.2.4/Element.Dimensions
- /MooTools.More

provides: [Tips]

...
*/

(function(){

var read = function(option, element){
	return (option) ? ($type(option) == 'function' ? option(element) : element.get(option)) : '';
};

this.Tips = new Class({

	Implements: [Events, Options],

	options: {
		/*
		onAttach: $empty(element),
		onDetach: $empty(element),
		*/
		onShow: function(){
			this.tip.setStyle('display', 'block');
		},
		onHide: function(){
			this.tip.setStyle('display', 'none');
		},
		title: 'title',
		text: '',
		showDelay: 100,
		hideDelay: 100,
		className: 'ne-tip-wrap ne-fixbugie',
        position: 'center-bottom', // or 'middle-right'
		offset: {x: 16, y: 16},
		windowPadding: {x:0, y:0},
		fixed: true
	},

	initialize: function(){
		var params = Array.link(arguments, {options: Object.type, elements: $defined});
		this.setOptions(params.options);
		if (params.elements) this.attach(params.elements);
		this.container = new Element('div', {'class': 'ne-tip'});
	},
    
    buildOffset: function(element) {
        var el_coord = element.getCoordinates();
        var tip_coord = this.tip.getCoordinates();
        if (this.options.position=='center-bottom') {
            this.options.offset.x = parseInt((el_coord.width-tip_coord.width)/2);
            this.options.offset.y = el_coord.height + 4;
        } else {
        
        }
    },
    
	toElement: function(){
		if (this.tip) return this.tip;

		return this.tip = new Element('div', {
			'class': this.options.className,
			styles: {top: 0, left: 0}
		}).adopt(
            new Element('div', {'class': 'ne-tip-top ne-center'}),
            this.container
        ).inject(document.body);
	},

	attach: function(elements){
		$$(elements).each(function(element){
			var title = read(this.options.title, element),
				text = read(this.options.text, element);
			
			element.erase('title').store('tip:native', title).retrieve('tip:title', title);
			element.retrieve('tip:text', text);
			this.fireEvent('attach', [element]);
			
			var events = ['enter', 'leave'];
			if (!this.options.fixed) events.push('move');
			
			events.each(function(value){
				var event = element.retrieve('tip:' + value);
				if (!event) event = this['element' + value.capitalize()].bindWithEvent(this, element);				
				element.store('tip:' + value, event).addEvent('mouse' + value, event);
			}, this);
		}, this);
		
		return this;
	},

	detach: function(elements){
		$$(elements).each(function(element){
			['enter', 'leave', 'move'].each(function(value){
				element.removeEvent('mouse' + value, element.retrieve('tip:' + value)).eliminate('tip:' + value);
			});
			
			this.fireEvent('detach', [element]);
			
			if (this.options.title == 'title'){ // This is necessary to check if we can revert the title
				var original = element.retrieve('tip:native');
				if (original) element.set('title', original);
			}
		}, this);
		
		return this;
	},

	elementEnter: function(event, element){
		this.container.empty();
		
		['title', 'text'].each(function(value){
			var content = element.retrieve('tip:' + value);
			if (content) this.fill(new Element('div', {'class': 'ne-tip-' + value}).inject(this.container), content);
		}, this);
		
		$clear(this.timer);
		this.timer = (function(){
			this.show(this, element);
            this.buildOffset(element);
			this.position((this.options.fixed) ? {page: element.getPosition()} : event);
		}).delay(this.options.showDelay, this);
	},

	elementLeave: function(event, element){
		$clear(this.timer);
		this.timer = this.hide.delay(this.options.hideDelay, this, element);
		this.fireForParent(event, element);
	},

	fireForParent: function(event, element){
		element = element.getParent();
		if (!element || element == document.body) return;
		if (element.retrieve('tip:enter')) element.fireEvent('mouseenter', event);
		else this.fireForParent(event, element);
	},

	elementMove: function(event, element){
		this.position(event);
	},

	position: function(event){
		if (!this.tip) document.id(this);

		var size = window.getSize(), scroll = window.getScroll(),
			tip = {x: this.tip.offsetWidth, y: this.tip.offsetHeight},
			props = {x: 'left', y: 'top'},
			obj = {};
		
		for (var z in props){
			obj[props[z]] = event.page[z] + this.options.offset[z];
			if ((obj[props[z]] + tip[z] - scroll[z]) > size[z] - this.options.windowPadding[z]) obj[props[z]] = event.page[z] - this.options.offset[z] - tip[z];
		}
		
		this.tip.setStyles(obj);
	},

	fill: function(element, contents){
		if(typeof contents == 'string') element.set('html', contents);
		else element.adopt(contents);
	},

	show: function(element){
		if (!this.tip) document.id(this);
		this.fireEvent('show', [this.tip, element]);
	},

	hide: function(element){
		if (!this.tip) document.id(this);
		this.fireEvent('hide', [this.tip, element]);
	}

});

})();

Modal = new Class({
	Implements: [Options],

	options: {
		id: 'ne-modal'
	},

	initialize: function(options) {
		this.setOptions(options);
		this.overlay = new Overlay();
		this.modal = new Element('div', {
			id: this.options.id,
			'class': this.options.id
		}).inject(document.body);
        document.addEvent((Browser.Engine.trident || Browser.Engine.webkit) ? 'keydown' : 'keypress', this.onCommand.bind(document));
	},

    attachCloseEvent: function() {
        var close = this.modal.getElement('.ne-modal-close');
        if (close) {
            close.addEvent('click', function(e) {
                e.stop();
                this.close();
            }.bind(this));
        }
    },

	open: function(content) {
		this.set(content);
	},

	set: function(content) {
		if ($type(content) == 'string')
			content = new Element('DIV', {'html': content});
		this.modal.setStyles({
            top: document.id(document.body).getScroll().y + 150
        });
        this.modal.innerHTML = '';
		this.modal.style.display = '';
		this.modal.appendChild(content);
        this.attachCloseEvent();
		this.overlay.open();
	},

	close: function() {
		this.modal.style.display = 'none';
		this.overlay.close();
	},

    onCommand: function(e) {
        if (e && e.key && !e.shift) {
            switch (e.key) {
                case 'esc':
                    e.stop();
                    ne.Utils.modal.close();
                break;
            }
        }
    }
});
Overlay = new Class({

	Implements: [Options,Events],

	options:  {
		id: 'ne-overlay',
		duration: 500,
		opacity: 0.5
	},

	initialize: function(options) {
		this.setOptions(options);
		this.container = document.id(document.body);
		this.overlay = new Element('div',{
            'class': this.options.id,
			id: this.options.id,
			opacity: 0,
			events: {
				click: function() {
					this.fireEvent('click');
				}.bind(this)
			}
		}).inject(this.container);
		this.tween = new Fx.Tween(this.overlay,{
			duration: this.options.duration,
			link: 'cancel',
			property: 'opacity',
			onStart: function() {
				var size = this.container.getScrollSize();
				this.overlay.setStyles({
					width: size.x,
					height: size.y
				});
			}.bind(this),
			onComplete: function() {
				this.fireEvent(this.overlay.get('opacity') == this.options.opacity ? 'show' : 'hide');
			}.bind(this)
		});
	},

    dim: function() {
        this.overlay.style.display = '';
        var doc = $(document.documentElement);
        if (!doc.hasClass('ne-dim')) {
            doc.addClass('ne-dim');
            doc.setStyles({'overflow': 'hidden', 'width': doc.scrollWidth, 'position': 'relative'});
        }
    },

    un_dim: function() {
        this.overlay.style.display = 'none';
        var doc = $(document.documentElement);
        doc.removeClass('ne-dim');
        doc.removeProperty('style');
    },

	open: function() {
		this.fireEvent('open');
        this.dim();
		this.tween.start(this.options.opacity);
	},
	close: function() {
		this.fireEvent('close');
		this.tween.start(0).chain(this.un_dim.bind(this));
	}
});

DropDown = new Class({
    Implements: [Options],
    options: {
        input: null,
        content: null,
        action: null
    },
    initialize: function(options) {
        this.setOptions(options);
        this.attachEvent();
    },
    toggle: function() {
        if (this.options.content.style.display=='')
            this.hide();
        else
            this.options.content.style.display='';
    },
    attachEvent: function() {
        this.options.input.addEvents({
            'dblclick': function(e) {
                this.toggle();
            }.bind(this),
            'click': function(e) {
                e.stop();
            }.bind(this)
        });
        this.options.action.addEvent('click', function(e) {
            e.stop();
            this.toggle();
        }.bind(this));
        this.options.content.getElements('a').each(function(a) {
            a.addEvent('click', function(e) {
                e.stop();
                this.options.input.value = a.getElement('div').getProperty('val');
                this.hide();
            }.bind(this));
        }.bind(this));
        document.addEvent('click', function() {
            this.hide();
        }.bind(this));
    },

    hide: function() {
        this.options.content.style.display='none';
    }
});

Fx.Elements=new Class({Extends:Fx.CSS,initialize:function(b,a){this.elements=this.subject=$$(b);
this.parent(a);},compute:function(g,h,j){var c={};for(var d in g){var a=g[d],e=h[d],f=c[d]={};for(var b in a){f[b]=this.parent(a[b],e[b],j);}}return c;
},set:function(b){for(var c in b){var a=b[c];for(var d in a){this.render(this.elements[c],d,a[d],this.options.unit);}}return this;},start:function(c){if(!this.check(c)){return this;
}var h={},j={};for(var d in c){var f=c[d],a=h[d]={},g=j[d]={};for(var b in f){var e=this.prepare(this.elements[d],b,f[b]);a[b]=e.from;g[b]=e.to;}}return this.parent(h,j);
}});
