var thiswrapper;
var chgclassName;
var mainclassName;

var MooTips = new Class({

	options: { // modded for X3
		onShow: function(tip){
			tip.setStyle('visibility', 'visible');
		},
		onHide: function(tip){
			tip.setStyle('visibility', 'hidden');
		},
		showOnClick: false,
		showOnMouseEnter: true,
		maxTitleChars: 30,
		showDelay: 100,
		hideDelay: 100,
		className: 'tool',
		offsets: {'x': 16, 'y': 16},
		fixed: false,
		openfixed: true,
		loadingText: 'Loading...',
		errTitle: 'Error...',
		errText: 'There was a problem retrieving the contents of this tooltip.',
		evalAlways: false
	},

	initialize: function(elements, options){
		this.setOptions(options);
mainclassName = this.options.className;
chgclassName = mainclassName;
		this.toolTip = new Element('div', {
			'class': this.options.className + '-tip',
			'styles': {
				'position': 'absolute',
				'top': '0',
				'left': '0',
				'visibility': 'hidden'
			},
			'events': {
					'mouseenter': function(event){
						//setting state property, needed on end function
						this.setProperty('state','mouseenter');
					},
					'mouseleave': function(event){
						//setting state property, needed on end function
						this.setProperty('state','mouseleave');
						this.pather.end(event);
					}
			}
		}).inject(document.body);
		
		//didn't find other way to get owner of toolTip inside toolTip
		this.toolTip.pather = this;
			
		this.wrapper = new Element('div').inject(this.toolTip);
		$$(elements).each(this.buildEvents, this);
		$$(elements).each(this.build, this);
		if (this.options.initialize) this.options.initialize.call(this);
	},
	
	buildEvents: function(el) {
		//code with errors but works
		//that's why showOnClick option is false by default
		if (this.options.showOnClick) {
			el.addEvent('click', function(event){
				this.start(el);
				if (!this.options.fixed) this.locate(event);
				else this.position(el);
			}.bindWithEvent(this));
		}
		
		if (this.options.showOnMouseEnter) {
			el.addEvent('mouseenter', function(event){
				this.start(el);
				if (!this.options.fixed) this.locate(event);
				else this.position(el);
			}.bind(this));
		}
		
		if (!this.options.fixed && !this.options.openfixed) el.addEvent('mousemove', this.locate.bindWithEvent(this));
		var end = this.end.bind(this);
		el.addEvent('mouseleave', end);
		el.addEvent('trash', end);
	},

	build: function(el){ // modded for X3
		el.myTitle = (el.href && el.match('a')) ? el.href.replace('http://', '') : (el.rel || false);
		if (el.title){
			
				el.title = this.options.loadingText + '::' + el.title;
								
			var dual = el.title.split('::');
			if (dual.length > 1) {
				el.myTitle = dual[0].trim();
				el.myText = dual[1].trim();
			} else {
				el.myTitle = false;
				el.myText = el.title;
			}					
			el.removeAttribute('title');
		} else {
			el.myText = false;
		}
		if (el.myTitle && el.myTitle.length > this.options.maxTitleChars) el.myTitle = el.myTitle.substr(0, this.options.maxTitleChars - 1) + "&hellip;";
	},

	start: function(el){ // modded for X3
	thiswrapper = this.wrapper;
		thiswrapper.empty();
	

		// check if we have an AJAX request - if so, show a loading animation and launch the request		
		if (el.myText && el.myText.test('^AJAX:', 'i')) {
			//if (this.ajax) this.ajax.cancel();
			if(this.options.evalAlways) {
				// save original text
				el.myEvalAlwaysText = el.myText;
			}
function sendtips(el, html) {
	if(!html)
		return;
	thiswrapper.empty();
	el.title = html;
				var dual = el.title.split('::');
				myTitle = dual[0].trim();
				myText = dual[1].trim();
el.removeAttribute('title');
		if (myTitle){
			this.title = new Element('span').inject(
				new Element('div', {'class': chgclassName + '-title'}).inject(thiswrapper)
			).set('html', myTitle);
		}
		if (myText){
			this.text = new Element('span').inject(
				new Element('div', {'class': chgclassName + '-text'}).inject(thiswrapper)
			).set('html', myText);
		}

chgclassName = mainclassName;
}

			var gourl = el.myText.replace(/AJAX:/i,'');
			arr = gourl.split(':req:');
			var mylink = arr[0].trim();
			var myreq = arr[1].trim();
			
			var tipajax = new Request({url:mylink,
		 method: 'post',
         onSuccess: function(responseText){
				sendtips(el, responseText);
         }
 });	
			tipajax.send(myreq);			
			el.myText = '<div class="' + this.options.className + '-loading">&nbsp;</div>';			
		} 

		if (el.myTitle){
			this.title = new Element('span').inject(
				new Element('div', {'class': mainclassName + '-title'}).inject(this.wrapper)
			).set('html', el.myTitle);
		}
		if (el.myText){
			this.text = new Element('span').inject(
				new Element('div', {'class': mainclassName + '0-text'}).inject(this.wrapper)
			).set('html', el.myText);
			
			if((this.options.evalAlways) && (el.myEvalAlwaysText)) {
				// reset text so that it will evaluate again
				el.myText = el.myEvalAlwaysText;
			}
		}
		$clear(this.timer);
		
		// setting initial state of tip
		this.toolTip.setProperty('state','mouseleave');

		this.timer = this.show.delay(this.options.showDelay, this);
	},

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

	position: function(element){
		var pos = element.getPosition();
		this.toolTip.setStyles({
			'left': pos.x + this.options.offsets.x,
			'top': pos.y + this.options.offsets.y
		});
	},

	locate: function(event){
		var win = {'x': window.getWidth(), 'y': window.getHeight()};
		var scroll = {'x': window.getScrollLeft(), 'y': window.getScrollTop()};
		var tip = {'x': this.toolTip.offsetWidth, 'y': this.toolTip.offsetHeight};
		var prop = {'x': 'left', 'y': 'top'};

		for (var z in prop){
			var pos = event.page[z] + this.options.offsets[z];
			if ((pos + tip[z] - scroll[z]) > win[z]) {
				pos = event.page[z] - this.options.offsets[z] - tip[z];
				chgclassName = 'toolR';	
			}
			this.toolTip.setStyle(prop[z], pos);
		};
	},

	show: function(){
		if (this.options.timeout) this.timer = this.hide.delay(this.options.timeout, this);
		this.fireEvent('onShow', [this.toolTip]);
	},

	hide: function(){
		// if "fixed", tooltip is only hidden when mouse leaves the tooltip (itself)
		if ((this.toolTip.getProperty('state') == 'mouseleave') || (!this.options.fixed && !this.options.openfixed))
			this.fireEvent('onHide', [this.toolTip]);
	}
});

MooTips.implement(new Events, new Options);