function URLDecode(url) {
	var HEXCHARS = "0123456789ABCDEFabcdef";
	url = url.replace("%C3%89","%C9");
	url = url.replace("%C3%A9","%E9");
	var encoded = url;
	var plaintext = "";
	var i = 0;
	while (i < encoded.length) {
		var ch = encoded.charAt(i);
		if (ch == "+") {
			plaintext += " ";
			i++;
		} else if (ch == "%") {
			if (i < (encoded.length-2)&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
			plaintext += ch;
			i++;
		}
	} 
	return plaintext;
}; 

function utf8_encode ( string ) {
	string = (string+'').replace(/\r\n/g, "\n").replace(/\r/g, "\n");
	var utftext = "";
	var start, end;
	var stringl = 0;
	start = end = 0;
	stringl = string.length;
	for (var n = 0; n < stringl; n++) {
		var c1 = string.charCodeAt(n);
		var enc = null;
		if (c1 < 128) {
			end++;
		} else if((c1 > 127) && (c1 < 2048)) {
			enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
		} else {
			enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
		}
		if (enc != null) {
			if (end > start) {
				utftext += string.substring(start, end);
			}
			utftext += enc;
			start = end = n+1;
		}
	}
	if (end > start) {
		utftext += string.substring(start, string.length);
	}
	return utftext;
}

function utf8_decode ( str_data ) {
    var tmp_arr = [], i = ac = c1 = c2 = 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('');
}


var ajaxtooltip={
	fadeeffect: [false, 200], //enable Fade? [true/false, duration_milliseconds]
	useroffset: [-100, -140], //additional x and y offset of tooltip from mouse cursor, respectively
	loadingHTML: '<div style="font-style:italic">Loading...</div>',

	positiontip:function($tooltip, e){
		var tipx=e.pageX+this.useroffset[0]
		var tipy=e.pageY+this.useroffset[1]
		$tooltip.css({left: tipx, top: tipy})
	},

	showtip:function($tooltip, e){
		if (this.fadeeffect[0])
			$tooltip.hide().fadeIn(this.fadeeffect[1]);
		else
			$tooltip.show();
	},

	hidetip:function($tooltip, e){
		if (this.fadeeffect[0])
			$tooltip.fadeOut(this.fadeeffect[1])
		else
			$tooltip.hide()
	}

}

var bigajaxtooltip={
	bigfadeeffect: [false, 200], //enable Fade? [true/false, duration_milliseconds]
	biguseroffset: [10, -310], //additional x and y offset of tooltip from mouse cursor, respectively
	loadingHTML: '<div style="font-style:italic">Loading...</div>',

	bigpositiontip:function($tooltip, e){
		var tipx=e.pageX+this.biguseroffset[0]
		var tipy=e.pageY+this.biguseroffset[1]
		$tooltip.css({left: tipx, top: tipy})
	},

	bigshowtip:function($tooltip, e){
		if (this.bigfadeeffect[0])
			$tooltip.hide().fadeIn(this.bigfadeeffect[1]);
		else
			$tooltip.show();
	},

	bighidetip:function($tooltip, e){
		if (this.bigfadeeffect[0])
			$tooltip.fadeOut(this.bigfadeeffect[1])
		else
			$tooltip.hide()
	}

}

jQuery(document).ready(function(){
	ajaxtooltip.iebody=(document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
	var tooltips=[] //array to contain references to all tooltip DIVs on the page
	$('*[@title^="ajax:"]').each(function(index){ //find all links with "title=ajax:" declaration
		this.titleurl=jQuery.trim(this.getAttribute('title').split(':')[1])
		this.titleurl=URLDecode(this.titleurl);
		this.titleposition=index+' pos' //remember this tooltip DIV's position relative to its peers
		tooltips.push($('<div class="movabletooltip"></div>').appendTo('body'))
		var $target=$(this)
		$target.removeAttr('title')
		$target.hover(
			function(e){ //onMouseover element
				var $tooltip=tooltips[parseInt(this.titleposition)]
				if (!$tooltip.get(0).loadsuccess){ //first time fetching Ajax content for this tooltip?
					$tooltip.html(ajaxtooltip.loadingHTML).show()
					$tooltip.html(this.titleurl, '', function(){
						ajaxtooltip.positiontip($tooltip, e)
						ajaxtooltip.showtip($tooltip, e)
						$tooltip.get(0).loadsuccess=true
					})

				}
				else{
					ajaxtooltip.positiontip($tooltip, e)
					ajaxtooltip.showtip($tooltip, e)
				}
			},
			function(e){ //onMouseout element
				var $tooltip=tooltips[parseInt(this.titleposition)]
				ajaxtooltip.hidetip($tooltip, e)		
			}
		)
		$target.bind("mousemove", function(e){
			var $tooltip=tooltips[parseInt(this.titleposition)]
			ajaxtooltip.positiontip($tooltip, e)
		})
	});
	
	bigajaxtooltip.iebody=(document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
	$('*[@title^="bigajax:"]').each(function(index){ //find all links with "title=bigajax:" declaration
		this.titleurl=jQuery.trim(this.getAttribute('title').split(':')[1])
		this.titleurl='<div class="imagecontainer"><img src="/fhimages/'+this.titleurl+'.jpg" alt="" /></div>';
		this.titleposition=index+' pos' //remember this tooltip DIV's position relative to its peers
		tooltips.push($('<div class="fhmovabletooltip"></div>').appendTo('body'))
		var $target=$(this)
		$target.removeAttr('title')
		$target.hover(
			function(e){ //onMouseover element
				var $tooltip=tooltips[parseInt(this.titleposition)]
				if (!$tooltip.get(0).loadsuccess){ //first time fetching Ajax content for this tooltip?
					$tooltip.html(ajaxtooltip.loadingHTML).show()
					$tooltip.html(this.titleurl, '', function(){
						bigajaxtooltip.bigpositiontip($tooltip, e)
						bigajaxtooltip.bigshowtip($tooltip, e)
						$tooltip.get(0).loadsuccess=true
					})

				}
				else{
					bigajaxtooltip.bigpositiontip($tooltip, e)
					bigajaxtooltip.bigshowtip($tooltip, e)
				}
			},
			function(e){ //onMouseout element
				var $tooltip=tooltips[parseInt(this.titleposition)]
				bigajaxtooltip.bighidetip($tooltip, e)		
			}
		)
		$target.bind("mousemove", function(e){
			var $tooltip=tooltips[parseInt(this.titleposition)]
			bigajaxtooltip.bigpositiontip($tooltip, e)
		})
	});
})


