var MindTip = new Class({
	
	properties: {
		title: null,
		message: null,
		icon: null,
		target: null,
		duration: 150,
		align: 'tl',
		offset_y: 0,
		offset_x: 60,
		mouse: false
	},
	
	mousePoint: null,
	
	ie6: null,
		
	tip: null,
	tipcontent: null,
	tiptitle: null,
	tipmessage: null,
	tipbottom: null,
	
	initialize: function(b) {
		for(var a in b)this.properties[a] = b[a];
		this.create();
	},
	
	create: function() {
		this.tip = new Element('div').setProperty('class','tip-container').setStyles({opacity:0});
		this.disableSelection(this.tip);
		this.tip.fx = {up:new Fx.Morph(this.tip,{duration:this.properties.duration}),down:new Fx.Morph(this.tip,{duration:this.properties.duration})};
		this.tipcontent = new Element('div').setProperty('class','tip-top').injectInside(this.tip);
		if(this.properties.title)this.tiptitle = new Element('div').setProperty('class','tip-title').injectInside(this.tipcontent);
		if(this.properties.message)this.tipmessage = new Element('div').setProperty('class','tip-message').injectInside(this.tipcontent);
		this.tipbottom = new Element('div').injectInside(this.tip);
		if(Browser.Engine.trident) {
			this.tipcontent.setProperty('class','tip-top-gif');
			this.tipbottom.setProperty('class','tip-bottom-gif');
		} else {
			this.tipcontent.setProperty('class','tip-top-png');
			this.tipbottom.setProperty('class','tip-bottom-png');
		}
		this.container().addEvent('mouseenter',function(e){
			this.mousePoint = e.client;
		}.bind(this));
		if(this.properties.title)this.createTitle();
		if(this.properties.message)this.createMessage();
		if(this.properties.target)
			this.move();
		this.tip.injectInside(document.body);
		window.addEvent('resize',this.move.bind(this));
	},
	
	move: function() {
		var z = this.tip.getSize();
		var e = {x:0,y:0};
		if(this.properties.mouse&&this.mousePoint) {
			e.x = this.mousePoint.x-35;
			e.y = this.mousePoint.y-z.y-10;	
		} else {
			var e = this.properties.target.getPosition();
			var s = this.properties.target.getSize();
			switch(this.properties.align) {
				case 'tl':e.x=e.x-s.x-50;e.y=(e.y-z.y+this.properties.offset_y);break;
				case 'tr':e.x=e.x+s.x-35;e.y=(e.y-z.y+this.properties.offset_y);break;
				case 'tc':e.x=(e.x+s.x*0.5)-100;e.y=(e.y-z.y+this.properties.offset_y);break;
			}
		}
		this.tip.setStyles({left:e.x,top:e.y});
	},
	
	createTitle: function() {
		this.tiptitle.setProperty('html',this.properties.title);
	},
	
	createMessage: function() {
		this.tipmessage.setProperty('html',this.properties.message);
	},
	
	show: function() {
		this.move();
		if(this.tip.fx.down)this.tip.fx.down.cancel();
		this.tip.fx.up.start({opacity:1});
	},
	
	hide: function() {
		if(this.tip.fx.up)this.tip.fx.up.cancel();
		this.tip.fx.down.start({opacity:0});
	},
	
	container: function() {
		return this.tip;
	},
	
	disableSelection: function(item) {
		item.onselectstart = function() {
			return false;
		};
		item.unselectable = "on";
		item.style.MozUserSelect = "none";
		item.style.cursor = "default";
	}
	
});

var aTags = new Class({
	initialize: function() {
		$$('.tipz').each(function(item,index){
			var title = item.getProperty('title');
			if (item.getProperty('rel')) {
				var message = item.getProperty('rel');
			}
			if (title) {
				var link = '';
				item.setProperty('title','');
				item.tip = new MindTip({align:'tl','title':title,'message':message,target:item});
				item.addEvent('mouseenter',function(){
					this.show(item.tip)
				}.bind(this));
				item.addEvent('mouseleave',function(){
					this.hide(item.tip)
				}.bind(this));
			}
		}.bind(this));
		$$('.tipzc').each(function(item,index){
			var title = item.getProperty('title');
			if (title) {
				var link = '';
				item.setProperty('title','');
				item.tip = new MindTip({align:'tc','title':title,message:'',target:item});
				item.addEvent('mouseenter',function(){
					this.show(item.tip)
				}.bind(this));
				item.addEvent('mouseleave',function(){
					this.hide(item.tip)
				}.bind(this));
			}
		}.bind(this));	
	},
	show: function(item) {
		item.show();
	},
	hide: function(item) {
		item.hide();
	},
	hideAll: function(item) {
		// close all balloons
		tipArray = $$('.tip-container');
		for (i=0;i<tipArray.length;i++) {
			tipArray[i].setStyle('visibility','hidden');
		}	
	}
});
window.addEvent('domready',function(){
	mindTips = new aTags();
});
