/**
 * Interface Elements for jQuery
 * Tooltip
 * 
 * http://interface.eyecon.ro
 * 
 * Copyright (c) 2006 Stefan Petre
 * Dual licensed under the MIT (MIT-LICENSE.txt) 
 * and GPL (GPL-LICENSE.txt) licenses.
 *   
 * $Revision: 1.5 $
 * $Log: itooltip.js,v $
 * Revision 1.5  2006/08/31 16:08:16  Stef
 * Added dual licencing
 *
 * Revision 1.4  2006/08/27 19:27:33  Stef
 * *** empty log message ***
 *
 * Revision 1.3  2006/08/27 19:05:55  Stef
 * *** empty log message ***
 *
 * Revision 1.2  2006/08/02 17:59:25  Stef
 * *** empty log message ***
 *
 */

$.iUtil = {
	getPos : function (e, s)
	{
		var l = 0;
		var t  = 0;
		var sl = 0;
		var st  = 0;
		var w = $.css(e,'width');
		var h = $.css(e,'height');
		var wb = e.offsetWidth;
		var hb = e.offsetHeight;
		while (e.offsetParent){
			l += e.offsetLeft + (e.currentStyle?parseInt(e.currentStyle.borderLeftWidth)||0:0);
			t += e.offsetTop  + (e.currentStyle?parseInt(e.currentStyle.borderTopWidth)||0:0);
			if (s) {
				sl += e.parentNode.scrollLeft||0;
				st += e.parentNode.scrollTop||0;
			}
			e = e.offsetParent;
		}
		l += e.offsetLeft + (e.currentStyle?parseInt(e.currentStyle.borderLeftWidth)||0:0);
		t += e.offsetTop  + (e.currentStyle?parseInt(e.currentStyle.borderTopWidth)||0:0);
		st = t - st;
		sl = l - sl;
		return {x:l, y:t, sx:sl, sy:st, w:w, h:h, wb:wb, hb:hb};
	},
	getPosition : function(e)
	{
		var x = 0;
		var y = 0;
		var restoreStyle = false;
		es = e.style;
		if ($(e).css('display') == 'none') {
			oldVisibility = es.visibility;
			oldPosition = es.position;
			es.visibility = 'hidden';
			es.display = 'block';
			es.position = 'absolute';
			restoreStyle = true;
		}
		el = e;
		while (el && el.tagName != 'BODY'){
			x += el.offsetLeft + (el.currentStyle && !$.browser.opera ?parseInt(el.currentStyle.borderLeftWidth)||0:0);
			y += el.offsetTop  + (el.currentStyle && !$.browser.opera ?parseInt(el.currentStyle.borderTopWidth)||0:0);
			el = el.offsetParent;
		}
		el = e;
		while (el.tagName != 'BODY' && el.parentNode)
		{
			x -= el.scrollLeft||0;
			y -= el.scrollTop||0;
			el = el.parentNode;
		}
		if (restoreStyle) {
			es.display = 'none';
			es.position = oldPosition;
			es.visibility = oldVisibility;
		}
		return {x:x, y:y};
	},
	getSize : function(e)
	{
		var w = $.css(e,'width');
		var h = $.css(e,'height');
		var wb = 0;
		var hb = 0;
		es = e.style;
		if ($(e).css('display') != 'none') {
			wb = e.offsetWidth;
			hb = e.offsetHeight;
		} else {
			oldVisibility = es.visibility;
			oldPosition = es.position;
			es.visibility = 'hidden';
			es.display = 'block';
			es.position = 'absolute';
			wb = e.offsetWidth;
			hb = e.offsetHeight;
			es.display = 'none';
			es.position = oldPosition;
			es.visibility = oldVisibility;
		}
		return {w:w, h:h, wb:wb, hb:hb};
	},
	getClient : function(e)
	{
		if (e) {
			w = e.clientWidth;
			h = e.clientHeight;
		} else {
			w = window.innerWidth || document.documentElement.clientWidth || document.body.offsetWidth;
			h = window.innerHeight || document.documentElement.clientHeight || document.body.offsetHeight;
		}
		return {w:w,h:h};
	},
	getScroll : function (e) 
	{
		if (e) {
			t = e.scrollTop;
			l = e.scrollLeft;
			w = e.scrollWidth;
			h = e.scrollHeight;
			iw = 0;
			ih = 0;
		} else  {
			if (document.documentElement && document.documentElement.scrollTop) {
				t = document.documentElement.scrollTop;
				l = document.documentElement.scrollLeft;
				w = document.documentElement.scrollWidth;
				h = document.documentElement.scrollHeight;
			} else if (document.body) {
				t = document.body.scrollTop;
				l = document.body.scrollLeft;
				w = document.body.scrollWidth;
				h = document.body.scrollHeight;
			}
			iw = self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth||0;
			ih = self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight||0;
		}
		return { t: t, l: l, w: w, h: h, iw: iw, ih: ih };
	},
	getMargins : function(e)
	{
		el = $(e);
		t = el.css('marginTop') || '';
		r = el.css('marginRight') || '';
		b = el.css('marginBottom') || '';
		l = el.css('marginLeft') || '';
		return {t: t, r: r,	b: b, l: l};
	},
	getPadding : function(e)
	{
		el = $(e);
		t = el.css('paddingTop') || '';
		r = el.css('paddingRight') || '';
		b = el.css('paddingBottom') || '';
		l = el.css('paddingLeft') || '';
		return {t: t, r: r, b: b, l: l};
	},
	getBorder : function(e)
	{
		el = $(e);
		t = el.css('borderTopWidth') || '';
		r = el.css('borderRightWidth') || '';
		b = el.css('borderBottomWidth') || '';
		l = el.css('borderLeftWidth') || '';
		return {t: t, r: r, b: b, l: l};
	}
};

function TB_getPageSize(){
  var de = document.documentElement;
  var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
  
  arrayPageSize = new Array(w,h) 
  return arrayPageSize;
}

$.iTooltip = {
	helper : null,
	current : null,
	focused : false,
	oldTitle : null,
	timeout : null,
	url : "ajaxydef.php?anchor=",
	focus : function(e)
	{
		$.iTooltip.focused = true;
		$.iTooltip.show(e, this);
	},
	hidefocused : function(e)
	{
		if ($.iTooltip.current != this)
			return ;
		$.iTooltip.focused = false;
		$.iTooltip.hide(e, this);
	},
	show : function(e, el)
	{
		if (!el) {
			el = this;
		}

		jEl = $(el);

		if ($.iTooltip.current != null)
		{
			return ;
		}

		if ($.iTooltip.timeout) {
			clearTimeout($.iTooltip.timeout);
			$.iTooltip.timeout = false;
		}

		$.iTooltip.current = el;
		pos = $.extend(
			$.iUtil.getPosition(el),
			$.iUtil.getSize(el)
		);


		href = jEl.attr('href');

		$.get($.iTooltip.url + el.anchor, function(data) {
			var json = eval(data);
			var title = json[1];
			var named = json[0];
			if (title) {
				href = false;

				bodysize = TB_getPageSize();

				// Fix the position for IE only. Strange.
				/*@cc_on
					if (bodysize[0] > 1100)
						pos.x += (bodysize[0] - 1100)*.5;
				@*/

				// Make sure the tooltop isnt past the RHS.
				//console.log(pos.x + " " + (bodysize[0]));
				if ((pos.x + 375) > bodysize[0])
				{
					pos.x = bodysize[0] - 375;
					//console.log("Up: " + pos.x);
				}

				$.iTooltip.helper.empty()
					.append("<strong>Definition:</strong> <em>" + named + "</em><br/><br/>" + title)

					.css(
						{
							top: 	pos.y + 24 + 'px',
							left:	pos.x + 'px',
							width: 325 + 'px',
							display: 'block'
						}
					).fadeIn("slow");
				if(el.tE.ttClass){
					$.iTooltip.helper.addClass(el.tE.ttClass);
				}
			}
		});
	},
	hide : function(e, el)
	{
		if (!el) {
			el = this;
		}
		if ($.iTooltip.focused != true && $.iTooltip.current == el) {
			
			$.iTooltip.current = null;
			
			$.iTooltip.timeout = setTimeout(function() {
				//console.log("Fired");
				//$("div#tooltipHelper:visible").fadeOut('slow');
				$.iTooltip.helper.fadeOut('slow');
			},500);
			
			/*if(el.tE.ttClass){
				$.iTooltip.helper.removeClass(el.tE.ttClass);
			}*/
			
		}
	},
	build : function(c, o)
	{
		if (!$.iTooltip.helper)
		{
			$('body').append('<div id="tooltipHelper"></div>');
			$.iTooltip.helper = $('#tooltipHelper');
			$.iTooltip.helper.css(
				{
					position:	'absolute',
					zIndex:		3000,
					display: 	'none'
				}
			);
			$.iTooltip.helper.hover(function(){
				if ($.iTooltip.timeout) {
					//console.log("Clear (hover) " + $.iTooltip.timeout);
					clearTimeout($.iTooltip.timeout);
					$.iTooltip.timeout = false;
				}
			},function(){
				$.iTooltip.helper.hide();			
			});
		}
		return this.each(
			function(){
				var definition = $(this).attr('href');
				definition = definition.substr(definition.indexOf('#')+1);
				if (definition && definition.length != 0)
					this.anchor = definition;
				if(this.anchor) {
					var el = $(this);
					if (c && c.constructor == String) {
						this.ttClass = c;
					}
					el.bind(
						'mouseover',
						$.iTooltip.show
					);
					el.bind(
						'focus',
						$.iTooltip.focus
					);
					el.bind(
						'mouseout',
						$.iTooltip.hide
					);
					el.bind(
						'blur',
						$.iTooltip.hidefocused
					);
					el.get(0).tE = this;
				}
			}
		);
	}
};


$.fn.ToolTip = $.iTooltip.build;

// A console.log emulator

/*
if (!window.console) {
  window.console = {
    timers: {},
    openwin: function() {
      window.top.debugWindow = window.open("","Debug","left=0,top=0,width=300,height=700,scrollbars=yes,"+"status=yes,resizable=yes");
      window.top.debugWindow.opener = self;
      window.top.debugWindow.document.open();
      window.top.debugWindow.document.write('<html><head><title>debug window</title></head><body><hr /><pre>');
    },

    log: function(entry) {
      window.top.debugWindow.document.write(entry+"\n");
    },

    time: function(title) {
      window.console.timers[title] = new Date().getTime();
    },

    timeEnd: function(title) {
      var time = new Date().getTime() - window.console.timers[title];
      console.log(['<strong>', title, '</strong>: ', time, 'ms'].join(''));
    }

  }
  if (!window.top.debugWindow) { console.openwin(); }
}
*/

