/**
 * Project: Mootools Klasse
 * Author: Christian Boehnke & Paul Meyer Klickadeeler Mediendesign GbR <info [AT] klickadeeler [DOT] de>
 * Date: April 12th, 2009
 * File: Tip_yc.js
 * Version: 1.0
 */

// mootools version 1.2.2 wird minimal benoetigt
if (kd) {

	kd.Tip = new Class({
		
		// implementiere
		'Implements': [kd],
		
		// optionen
		options: {
			'offsetX': 0,
			'offsetY': 0
		},
		
		// internal
		_locked: false,
		_toId: null,
		_tipContainer: null,
		_swiffElement: null,
		
		/**
		 * ============================================================================
		 * klassen-konstruktor
		 * ============================================================================
		 * @param options@mixed		objekt mit allen einstellungen
		 * ============================================================================
		 * @return void
		 * ============================================================================
		 */
		'initialize': function(element, options) {
			// set options
			if (options) {
				for (var k in options) {
					if (this.options[k]) this.options[k] = options[k];
				}
			}
			
			this._tipContainer = new Element('DIV', { 
				'class': 'tooltipContainer',
				'html': '<div class="tooltipNW">&nbsp;</div>' + 
						'<div class="tooltipN"></div>' + 
						'<div class="tooltipNE">&nbsp;</div>' + 
						'<div class="tooltipWE"></div>' + 
						'<div class="tooltipSW">&nbsp;</div>' + 
						'<div class="tooltipS">&nbsp;</div>' + 
						'<div class="tooltipSE">&nbsp;</div>' + 
						'<br class="clear" />'
			});
			
			this._tipContainer.addEvent('mouseover', function(event) {
				this._locked = true;
				this._activated = true;
				this.keep();
			}.bindWithEvent(this));
			
			this._tipContainer.addEvent('mouseout', function(event) {
				this._locked = false;
				this._activated = false;
				this.close(300);
			}.bindWithEvent(this));
			
			$$('BODY').adopt(this._tipContainer);
		},
	
		'open': function(swiffElement, mixedContent, px, py, avoidClose) {
			if (this._locked) return;
			
			this.close(0);
			
			var bodyText = '';
			if ($type(mixedContent) == 'array') {
				var len = mixedContent.length;
				
				bodyText += '<div class="tooltipDidiver">&nbsp;</div>';
				
				for (var i = 0; i < len; i ++) {
					bodyText += '<a href="' + mixedContent[i]['link'] + '" onmouseover="highlightBodyGraph(\'' + mixedContent[i]['name'] + '\');" class="tooltipNavigation">' + mixedContent[i]['name'] + '</a><div class="tooltipDidiver">&nbsp;</div>';
				}
			}
			else if ($type(mixedContent) == 'string') {
				bodyText += '<span style="padding:0 8px;">' + mixedContent + '<span>';
			}
			
			if (avoidClose) {
				$$('[class=tooltipN]').each(function(item, index) {
					item.set('html', '');
				});
			}
			else {
				$$('[class=tooltipN]').each(function(item, index) {
					item.set('html', '<a href="' + document.location.href + '" onclick="tip.close(0, true); return false;" title="schlie&szlig;en">x</a>');
				});
			}
			
			$$('[class^=tooltipWE]').each(function(item, index) {
				item.set('html', bodyText);
			});
			
			this._tipContainer.setStyle('left', String(px + this.options.offsetX) + 'px');
			this._tipContainer.setStyle('top', String(py + this.options.offsetY) + 'px');
			this._tipContainer.setStyle('display', 'block');
			
			this._swiffElement = swiffElement;
		},
		
		'keep': function() {
			if (this._toId) {
				$clear(this._toId);
				this._toId = null;
			}
		},
		
		'close': function(delay, ignoreLock) {
			if (!ignoreLock && this._locked) return;
			
			this.keep();
			this._toId = function() {
				if (this._swiffElement) {
					Swiff.remote(this._swiffElement, 'jsEvent', 'release'); 
					this._swiffElement = null;
				}
				this._tipContainer.setStyle('display', 'none');
			}.delay(($type(delay) == 'number' ? Math.abs(delay) : 0), this);
		}
		
	});

}
