/*
Toni Rantanen
v1.0 (mootools 1.2b) - 2008.01.28
v1.1 (mootools 1.2) - 2008.06.23
v1.1.1 tween duration - 2008.07.24
www.kotimaa-yhtiot.fi/verkkopalvelut
*/
var TipsHtml = new Class({
	Extends: Tips,
	
	options: {
		onInit: function(tip){
			tip.fade('hide');
			tip.set('tween', {duration: 250});
		},
		onShow: function(tip){
			tip.fade('in');
		},
		onHide: function(tip){
			tip.fade('out');
		},
		showDelay: 500,
		hideDelay: 0,
		className: null,
		offsets: {x: 16, y: 16},
		fixed: false
	},
	
	template: {},
	dataUrl: null,
	dataList: {},
	cmdList: null,
	allowShow: true,

	initialize: function(elements, templateUrl, dataUrl, options)
	{
		this.cmdList = new Chain();
		// orig stuff
		this.setOptions(options || null);
		this.document = (elements.length) ? elements[0].ownerDocument : document;
		this.tip = new Element('div', {
			'class': this.options.className + '-tip',
			'styles': {
				'position': 'absolute',
				'top': '0',
				'left': '0',
				'visibility': 'hidden'
			}
		}, this.document).inject(this.document.body);
		this.wrapper = new Element('div').inject(this.tip);
		this.fireEvent('init', this.tip);
		
		this.dataUrl = dataUrl;
		this.template.thisobj = this;
		this.templateLoad(templateUrl, 'TipsHtml', this.template, {
			obj: this,
			func: this.add,
			args: [elements]
		});
	},
	
	add: function(elements)
	{
		elements.each(this.build, this);
	},

	build: function(element)
	{
		element.store('tipTitle', element.get('title'));
		var enter = this.elementEnter.bindWithEvent(this, element);
		var leave = this.elementLeave.bindWithEvent(this, element);
		element.addEvents({mouseenter: enter, mouseleave: leave});
		if (!this.options.fixed){
			var move = this.elementMove.bindWithEvent(this, element);
			element.addEvent('mousemove', move);
		}
		element.erase('title');
	},

	elementEnter: function(event, el)
	{
		this.allowShow = true;
		this.dataLoad(this.dataUrl, el.retrieve('tipTitle'), {
			obj: this,
			func: this.templateDraw,
			args: [el]
		});
	},

	elementLeave: function(event, el){
		this.allowShow = false;
		$clear(this.timer);
		this.timer = this.hide.delay(this.options.hideDelay, this);
	},
	
	show: function(){
		if(!this.allowShow)
			return;
		this.fireEvent('show', this.tip);
	},
	
	dataLoad: function(url, id, rel)
	{
		var result = this.dataList[id];
		if(result == undefined)
		{
			var request = new Request.JSON({
				url: url + id,
				onComplete: function(content)
				{
					rel.obj.dataList[id] = content;
					rel.args.push(content);
					rel.func.attempt(rel.args, rel.obj);
				}
			}).send();
		}
		else
		{
			rel.args.push(result);
			var result = rel.func.attempt(rel.args, rel.obj);
		}
	},

	templateDraw: function(el, content)
	{
		this.wrapper.empty();
		if(content.result == false)
			this.wrapper.set('html', "Error, title/data request failed.");
		else
			this.wrapper.set('html', this.templateProcess(this.template, content.result));
		
		this.timer = $clear(this.timer);
		this.timer = this.show.delay(this.options.showDelay, this);

		this.position((!this.options.fixed) ? event : {page: element.getPosition()});
	},
	
	templateProcess: function(obj, data, flags)
	{
		return obj.parsed.process(data, flags);
	},

	templateLoad: function(url, name, tmpl, rel)
	{
		var request = new Request({
			url: url,
			method: 'get',
			onComplete: function(content)
			{
				tmpl.name = name;
				tmpl.html = content;
				tmpl.parsed = TrimPath.parseTemplate(tmpl.html, tmpl.name);
				rel.func.attempt(rel.args, rel.obj);
			}
		}).send();
	}
});