// MIT License
// ===========
// Copyright (c) 2006 Edward J. Cianci (http://boygeni.us/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// Modified by fm.worx for mapGallery-Module (02/2011)

function utf8_decode (str_data) {
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Norman "zEh" Fuchs
    // +   bugfixed by: hitwork
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: utf8_decode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
    var tmp_arr = [],
        i = 0,
        ac = 0,
        c1 = 0,
        c2 = 0,
        c3 = 0;

    str_data += '';

    while (i < str_data.length) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if (c1 > 191 && c1 < 224) {
            c2 = str_data.charCodeAt(i + 1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i + 1);
            c3 = str_data.charCodeAt(i + 2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }

    return tmp_arr.join('');
}


function URLDecode(psEncodeString)
{
  // Create a regular expression to search all +s in the string
  var lsRegExp = /\+/g;
  // Return the decoded string
  return unescape(String(psEncodeString).replace(lsRegExp, " "));
}

var Tooltip = {
	Version: '1.1.0',
	
	EffectOptions: {},

	prepare: function(links, options) {
		$A(links).each(function(a){

			var title = a.title, href = a.href;
			if(!title || !href || !a.id) return;

			Tooltip.EffectOptions = options;

			Tooltip.render(a, title, href);
			a.removeAttribute('title');

			['mouseover', 'mouseout', 'click'].each(function(e){
				Event.observe(a, e, Tooltip['on' + e].bindAsEventListener(this));
			});
		});
	},

	render: function(element, head, html) {
		var element = $(element);
		
		var srcPath = "http://localhost/solanj2010/modules/galeriemap_pix/";
		var srcPath = "http://www.solanj.net/modules/galeriemap_pix/";

		var ttid = 'tt' + element.id;
		element._ttid = ttid;
		
		pics = html.split(";");
		
		//Beschreibung
		pics.shift();
		pics_href = pics[0];
		
		pics.shift();
		var pics_text = pics[0];
		
		//Browserweiche für Umlaute
		if(navigator.appName.indexOf("Internet Explorer")!=-1)
		{
			//IE
			pics_text = URLDecode(pics_text);
		}
		else
		{
			if(navigator.appName.indexOf("Chrome")!=-1)
			{
				//IE
				pics_text = URLDecode(pics_text);
			}
			else
			{
				pics_text = utf8_decode(URLDecode(pics_text));
			}
		}
		
		//Bilder
		pics.shift();
		
		if(pics.length==1)
		{
			var container = Builder.node('div', { id: ttid, className: 'tt' }, [
				Builder.node('h3', head),
				Builder.node('p', pics_text),
				//Builder.node('a',{id: 'morelink', href: 'index.php?katID=21&show_entry='+pics_href, className: 'ttlink'}, Builder.node('p', 'Weitere Informationen')),	
				Builder.node('img',{src: srcPath+pics[0] , height: '100',width: '100'}),
				Builder.node('a',{id: 'closelink', href: 'javascript:', onclick: 'new Effect.Fade(\''+ttid+'\',{duration:0.1});', className: 'ttlink'}, Builder.node('p', 'Fenster schliessen'))
			]);
		}
		else if(pics.length==2)
		{
			var container = Builder.node('div', { id: ttid, className: 'tt' }, [
				Builder.node('h3', head),
				Builder.node('p', pics_text),
				//Builder.node('a',{id: 'morelink', href: 'index.php?katID=21&show_entry='+pics_href, className: 'ttlink'}, Builder.node('p', 'Weitere Informationen')),		
				Builder.node('img',{src: srcPath+pics[0] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[1] , height: '100',width: '100'}),
				Builder.node('a',{id: 'closelink', href: 'javascript:', onclick: 'new Effect.Fade(\''+ttid+'\',{duration:0.1});', className: 'ttlink'}, Builder.node('p', 'Fenster schliessen'))
			]);
		}
		else if(pics.length==3)
		{
			var container = Builder.node('div', { id: ttid, className: 'tt' }, [
				Builder.node('h3', head),
				Builder.node('p', pics_text),
				//Builder.node('a',{id: 'morelink', href: 'index.php?katID=21&show_entry='+pics_href, className: 'ttlink'}, Builder.node('p', 'Weitere Informationen')),		
				Builder.node('img',{src: srcPath+pics[0] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[1] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[2] , height: '100',width: '100'}),
				Builder.node('a',{id: 'closelink', href: 'javascript:', onclick: 'new Effect.Fade(\''+ttid+'\',{duration:0.1});', className: 'ttlink'}, Builder.node('p', 'Fenster schliessen'))
			]);
		}
		else if(pics.length==4)
		{
			var container = Builder.node('div', { id: ttid, className: 'tt' }, [
				Builder.node('h3', head),
				Builder.node('p', pics_text),
				//Builder.node('a',{id: 'morelink', href: 'index.php?katID=21&show_entry='+pics_href, className: 'ttlink'}, Builder.node('p', 'Weitere Informationen')),		
				Builder.node('img',{src: srcPath+pics[0] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[1] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[2] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[3] , height: '100',width: '100'}),
				Builder.node('a',{id: 'closelink', href: 'javascript:', onclick: 'new Effect.Fade(\''+ttid+'\',{duration:0.1});', className: 'ttlink'}, Builder.node('p', 'Fenster schliessen'))
			]);
		}
		else if(pics.length==5)
		{
			var container = Builder.node('div', { id: ttid, className: 'tt' }, [
				Builder.node('h3', head),
				Builder.node('p', pics_text),
				//Builder.node('a',{id: 'morelink', href: 'index.php?katID=21&show_entry='+pics_href, className: 'ttlink'}, Builder.node('p', 'Weitere Informationen')),		
				Builder.node('img',{src: srcPath+pics[0] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[1] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[2] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[3] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[4] , height: '100',width: '100'}),
				Builder.node('a',{id: 'closelink', href: 'javascript:', onclick: 'new Effect.Fade(\''+ttid+'\',{duration:0.1});', className: 'ttlink'}, Builder.node('p', 'Fenster schliessen'))
			]);
		}
		else if(pics.length==6)
		{
			var container = Builder.node('div', { id: ttid, className: 'tt' }, [
				Builder.node('h3', head),
				Builder.node('p', pics_text),
				//Builder.node('a',{id: 'morelink', href: 'index.php?katID=21&show_entry='+pics_href, className: 'ttlink'}, Builder.node('p', 'Weitere Informationen')),		
				Builder.node('img',{src: srcPath+pics[0] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[1] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[2] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[3] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[4] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[5] , height: '100',width: '100'}),
				Builder.node('a',{id: 'closelink', href: 'javascript:', onclick: 'new Effect.Fade(\''+ttid+'\',{duration:0.1});', className: 'ttlink'}, Builder.node('p', 'Fenster schliessen'))
			]);
		}
		else if(pics.length==7)
		{
			var container = Builder.node('div', { id: ttid, className: 'tt' }, [
				Builder.node('h3', head),
				Builder.node('p', pics_text),
				//Builder.node('a',{id: 'morelink', href: 'index.php?katID=21&show_entry='+pics_href, className: 'ttlink'}, Builder.node('p', 'Weitere Informationen')),		
				Builder.node('img',{src: srcPath+pics[0] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[1] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[2] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[3] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[4] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[5] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[6] , height: '100',width: '100'}),
				Builder.node('a',{id: 'closelink', href: 'javascript:', onclick: 'new Effect.Fade(\''+ttid+'\',{duration:0.1});', className: 'ttlink'}, Builder.node('p', 'Fenster schliessen'))
			]);
		}
		else if(pics.length==8)
		{
			var container = Builder.node('div', { id: ttid, className: 'tt' }, [
				Builder.node('h3', head),
				Builder.node('p', pics_text),
				//Builder.node('a',{id: 'morelink', href: 'index.php?katID=21&show_entry='+pics_href, className: 'ttlink'}, Builder.node('p', 'Weitere Informationen')),	
				Builder.node('img',{src: srcPath+pics[0] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[1] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[2] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[3] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[4] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[5] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[6] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[7] , height: '100',width: '100'}),
				Builder.node('a',{id: 'closelink', href: 'javascript:', onclick: 'new Effect.Fade(\''+ttid+'\',{duration:0.1});', className: 'ttlink'}, Builder.node('p', 'Fenster schliessen'))	
			]);
		}
		else if(pics.length==9)
		{
			var container = Builder.node('div', { id: ttid, className: 'tt' }, [
				Builder.node('h3', head),
				Builder.node('p', pics_text),
				//Builder.node('a',{id: 'morelink', href: 'index.php?katID=21&show_entry='+pics_href, className: 'ttlink'}, Builder.node('p', 'Weitere Informationen')),	
				Builder.node('img',{src: srcPath+pics[0] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[1] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[2] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[3] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[4] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[5] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[6] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[7] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[8] , height: '100',width: '100'}),
				Builder.node('a',{id: 'closelink', href: 'javascript:', onclick: 'new Effect.Fade(\''+ttid+'\',{duration:0.1});', className: 'ttlink'}, Builder.node('p', 'Fenster schliessen'))	
			]);
		}
		else if(pics.length==10)
		{
			var container = Builder.node('div', { id: ttid, className: 'tt' }, [
				Builder.node('h3', head),
				Builder.node('p', pics_text),
				//Builder.node('a',{id: 'morelink', href: 'index.php?katID=21&show_entry='+pics_href, className: 'ttlink'}, Builder.node('p', 'Weitere Informationen')),	
				Builder.node('img',{src: srcPath+pics[0] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[1] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[2] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[3] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[4] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[5] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[6] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[7] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[8] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[9] , height: '100',width: '100'}),
				Builder.node('a',{id: 'closelink', href: 'javascript:', onclick: 'new Effect.Fade(\''+ttid+'\',{duration:0.1});', className: 'ttlink'}, Builder.node('p', 'Fenster schliessen'))	
			]);
		}
		else if(pics.length==11)
		{
			var container = Builder.node('div', { id: ttid, className: 'tt' }, [
				Builder.node('h3', head),
				Builder.node('p', pics_text),
				//Builder.node('a',{id: 'morelink', href: 'index.php?katID=21&show_entry='+pics_href, className: 'ttlink'}, Builder.node('p', 'Weitere Informationen')),	
				Builder.node('img',{src: srcPath+pics[0] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[1] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[2] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[3] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[4] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[5] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[6] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[7] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[8] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[9] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[10] , height: '100',width: '100'}),
				Builder.node('a',{id: 'closelink', href: 'javascript:', onclick: 'new Effect.Fade(\''+ttid+'\',{duration:0.1});', className: 'ttlink'}, Builder.node('p', 'Fenster schliessen'))	
			]);
		}
		else if(pics.length>11)
		{
			var container = Builder.node('div', { id: ttid, className: 'tt' }, [
				Builder.node('h3', head),
				Builder.node('p', pics_text),
				//Builder.node('a',{id: 'morelink', href: 'index.php?katID=21&show_entry='+pics_href, className: 'ttlink'}, Builder.node('p', 'Weitere Informationen')),	
				Builder.node('img',{src: srcPath+pics[0] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[1] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[2] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[3] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[4] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[5] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[6] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[7] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[8] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[9] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[10] , height: '100',width: '100'}),
				Builder.node('img',{src: srcPath+pics[11] , height: '100',width: '100'}),
				Builder.node('a',{id: 'closelink', href: 'javascript:', onclick: 'new Effect.Fade(\''+ttid+'\',{duration:0.1});', className: 'ttlink'}, Builder.node('p', 'Fenster schliessen'))	
			]);
		}
	
		element.parentNode.insertBefore(container, element.nextSibling);
		Position.absolutize(container);
		container.hide();
	},
	
	onmouseover: function(ev) {
		ev._over = true;
		Tooltip.display(ev);
	},

	onmouseout: function(ev) {
		//ev._over = false;
		//Tooltip.display(ev);
	},
	
	onclick: function(ev) {
		ev._over = false;
		Tooltip.display(ev);
	},

	reset: function(element) {
		var effect = $(element)._effect;
		if(effect) effect.cancel();
	},

	display: function(ev) {
		var over = ev._over;
	
		var element = Event.element(ev);
		if(element.nodeType == 3) element = element.parentNode; //fix safari bug
		element = $(element._ttid);

		Tooltip.reset(element);
		
		for(z=0;z<30;z++)
		{
			if(document.getElementById("tt"+z) && element!=document.getElementById("tt"+z))
			{
				 old_element = document.getElementById("tt"+z);
				// new Effect['Fade'](old_element,{duration:0.1});
				$(old_element).hide();
				// new Effect['Hide'](old_element);
			}
		}
		
		var options = $H({
			duration: over ? 0.1 : 0.1,
			offset: 8
		}).merge(Tooltip.EffectOptions);
		
		if(over) Tooltip.position(element, ev, options);

		element._effect = new Effect[over ? 'Appear' : 'Fade'](element, options);
		
	},
	
	position: function(element, ev, options) {
		var offset = options.offset;

		var dimensions = element.getDimensions(),
			boundaries = Tooltip.boundaries();

		var x = Tooltip.determine(Event.pointerX(ev), dimensions.width, offset, boundaries.width),
			y = Tooltip.determine(Event.pointerY(ev), dimensions.height, offset, boundaries.height);


		//alert(Event.pointerX(ev)+","+Event.pointerY(ev));

		element.setStyle({
			top: (y-10) + 'px',
			left: (x+10) + 'px'
		});
	},
	
	determine: function(i, current, min, max) {
		if(i + current + min >= max) {
			i = i - current - min;
		}
		else {
			i += min;
		}

		return (i < min) ? min : i;
	},
	
	boundary: function(hw) {
		var bound;

		if(self['inner' + hw]) {
			bound = self['inner' + hw];
		}
		else {
			bound = (document.documentElement || document.body)['client' + hw];
		}

		return bound;
	},
	
	boundaries: function() {
		return {
			height: Tooltip.boundary('Height'),
			width: Tooltip.boundary('Width')
		}
	}
}
