﻿/*
http://www.codylindley.com/blogstuff/js/jtip/
http://15daysofjquery.com/examples/jqueryTooltips/demo2.php
http://jquery.bassistance.de/tooltip/demo/
http://jquery.bassistance.de/tooltip/
*/

var jTip = (function(){

	/*
		Panel
		.ctor
	*/
	var Panel = function(key, url){
		this.key = key;
		this.url = url;
		this.id = "jtip-panel-"+(++tipCount);

		var me = this,
			qindex = url.indexOf("?"),
			p = me.params = get.hash(qindex != -1 ? url.substring(qindex+1) : null);
		p.width = (p.width || 250)*1;
		p.title = p.title || "";

		// append to dom
		this.panel = make("div", "jtip-panel", this.id, { display: "none", width: "250px" } );
		this.panel.appendChild(this.arrow = make("div", "jtip-arrow"));
		this.panel.appendChild(this.head  = make("div", "jtip-head"));
		this.panel.appendChild(this.body  = make("div", "jtip-body"));
		this.body.appendChild(this.loader = make("div", "jtip-loader"));
		$(this.head).html(this.params.title || "&nbsp;");
		$("body").append(this.panel);

		// load
		$(this.body).load(url);
		this.show();

		if(p.link){
			$('#'+key)
				.each(function(){ this.href = p.link; })
				.click(function(){ return true; })
				//.click(function(){ window.location = p.link; })
				.css('cursor', 'pointer');
		}
	};

	/*
		Panel
		prototype
	*/
	Panel.prototype = {
		show: function(){
			if( this.visible ) return;
			this.visible = true;

			var viewport = self.innerWidth || (document.documentElement && document.documentElement.clientWidth) || document.body.clientWidth,
				width = this.params.width,
				required = width + 75,
				x = get.absLeft(this.key),
				y = get.absTop(this.key) - 7,
				space = viewport - x,
				leftPositioned = required >= space && required < viewport;

			x = !leftPositioned
				? x + get.width(this.key) + 18
				: x - width - 18;

			$(this.panel).css({ width: width+"px" });
			$(this.arrow).css({ left: (!leftPositioned ? "" : (width+1) + "px")});
			$(this.panel)
				.css({ left: x + "px", top: y + "px" })
				.removeClass( leftPositioned ? "jtip-onright" : "jtip-onleft" )
				.addClass( leftPositioned ? "jtip-onleft" : "jtip-onright" )
				.show();

		},
		hide: function(){
			if( !this.visible ) return;
			this.visible = false;
			$(this.panel).hide();
		}
	};

	/*
		get
		helper methods
	*/
	var get = {
		el: function(element){
			return typeof(element) == "string"
				? document.getElementById(element)
				: element;
		},
		accumulatedProp: function(id, prop){
			var o = get.el(id), amount = o[prop];
			while(null != (o = o.offsetParent))
				amount += o[prop];
			return amount;
		},
		width: function(id){
			return get.el(id).offsetWidth;
		},
		absLeft: function(id){
			return get.accumulatedProp(id, 'offsetLeft');
		},
		absTop: function(id){
			return get.accumulatedProp(id, 'offsetTop');
		},
		hash: function(from){
			var params = {};
			if(from){
				var pairs = from.split(/[;&]/);
				for(var i = 0; i < pairs.length; i++){
					var parts = pairs[i].split('=');
					if(parts && parts.length == 2)
						params[unescape(parts[0])] = unescape(parts[1]).replace(/\+/g, ' ');
				}
			}
			return params;
		}
	},
	make = function( tag, cls, id, style ){
		var dom = document.createElement( tag );
		if(cls) dom.className = cls;
		if(id) dom.id = id;
		if(style) $(dom).css(style);
		return dom;
	},
	items = {},
	over = function(){
		if(!this.id) this.id = "jtip-autoid-"+(++newId);
		show(this.id, this.href);
	},
	out = function(){
		hide(this.id);
	},
	noclick = function(){
		return false;
	},
	init = function(expression){
		if(typeof expression != "string") var expression = "a.jtip";
		$(expression)
			.hover(over, out)
			.click(noclick);
	},
	register = function(key, url){
		return items[key] = new Panel(key, url);
	},
	show = function(key, url){
		//(items[key] || register(key, url, title)).show();

		var item = items[key];
		if(!item){
			if(!url){
				var src = get.el(key),
					url = src.href;
			}
			item = register(key, url);
		}
		item.show();
	},
	hide = function(key){
		var item = items[key];
		if(item) item.hide();
	},
	newId = -1,
	tipCount = -1;

	/* init */
	if(typeof jTipAutoInit == "undefined" || jTipAutoInit !== false)
		$().ready(init);

	return {
		init: init,
		show: show,
		hide: hide
	}
})();

