// двигающаяся подсказка
var mnoticeClass = Class.create({
	initialize: function(e, bubble) {
		this.e = e;
		this.bubble = bubble.cloneNode(true);
		
		this.prepare();
		this.init();
	},
	
	prepare: function() {
		var attr = '';
		
		this.text = '';
		this.effect = null;
		
		this.bubble.removeAttribute('id');
		//alert(document.body.appendChild)
		document.body.appendChild(this.bubble);

		if (this.e.hasAttribute('title')) {
			attr = 'title';
		}
		else if (this.e.hasAttribute('alt')) {
			attr = 'alt';
		}

		if (attr) {
			this.text = this.e.getAttribute(attr);
		}
		
		if (attr == 'title') {
			this.e.setAttribute(attr, '');
		}
	},
	
	init: function() {
		this.observeMouseover();
		this.observeMouseout();
		this.observeMousemove();
	},
	
	setCoords: function(event) {
		var x = Event.pointerX(event) - 7, y = Event.pointerY(event) - 22;
		this.bubble.setStyle({'left': x + 'px', 'top': y + 'px'});
	},
	
	observeMouseover: function() {
		this.e.observe('mouseover', function(event) {
			this.stopEffect();
			//with (this.bubble) {
				this.setCoords(event);
				this.bubble.getElementsByClassName('green_popup_text')[0].update(this.text);
				//$(this.bubble).down('#green_popup_text').update(this.text);
				this.bubble.hide();
				this.effect = new Effect.Appear(this.bubble, {
					duration: 0.5,
					queue: 'parrallel',
					afterFinish: function() {
						this.effect = null;
					}.bind(this)
				});
			//}
		}.bind(this));
	},
	
	observeMouseout: function() {
		this.e.observe('mouseout', function() {
			this.stopEffect();
			//with (this.bubble) {
				/*show();
				this.effect = new Effect.Fade(this.bubble, {
					duration: 0.3,
					queue: 'parrallel',
					afterFinish: function() {
						this.effect = null;
					}.bind(this)
				});*/
				this.bubble.hide();
			//}
		}.bind(this));
	},
	
	observeMousemove: function() {
		this.e.observe('mousemove', function(event) {
			this.setCoords(event);
		}.bind(this));
	},
	
	stopEffect: function() {
		//with (this) {
			if (this.effect) {
				this.effect.cancel();
				this.effect = null;
			}
		//}
	}
});

// недвигающаяся подсказка, прилипающая к элементу
var bnoticeClass = Class.create(mnoticeClass, {
	init: function() {
		this.observeMouseover();
		this.observeMouseout();
	},
	setCoords: function() {
		var offset = this.offset = this.e.cumulativeOffset();
		//with (this) {
			this.bubble.setStyle({'left': offset[0] + 2 + 'px', 'top': offset[1] - 20 + 'px'});
		//}
	}
});

var initPopupBubbles = function(root) {
	// стандартная подсказка bnotice (basic notice)
	bnotice = $('green_popup');
	root = root || document;
	if (bnotice) {
		$A(root.getElementsByClassName('bnotice')).each(function(e) {
			new bnoticeClass(e, bnotice);
		});
		
		$A(root.getElementsByClassName('mnotice')).each(function(e) {
			new mnoticeClass(e, bnotice);
		});
	}	
};

// показ подсказок над элементами + затирание стандартных тайтлов, альтов и т.п.
Event.observe(document, 'dom:loaded', function() {
	initPopupBubbles();
});
