/*
 * BetterTip
 * Created by Edgar Verle
 * BetterTip is made for the jQuery library.
 */

$(BT_init);

var BT_open_wait = 500; //time in millis to wait before showing dialog
var BT_close_wait = 0; //time in millis to wait before closing dialog
var BT_direction = "left";
var BT_cache_enabled = true;
var BT_events = new Array();
var BT_titles = new Array();
var BT_event = null;
var BT_height = '';
var BT_deleteButton = '';
function BT_init(){
	
  /*  $("a.betterTip").parent("div.betterTip")
    .hover(function(){BT_hoverIn($(this).children("a.betterTip")[0]); return false;},
    	   function(){BT_hoverOut($(this).children("a.betterTip")[0])})
    ; */
    
    $("a.betterTip").filter(function(index){
    	return $(this).parent("div.betterTip").length == 0;
    })
    /*.hover(function(){BT_hoverIn(this)}, function(){BT_hoverOut(this)})*/
	.click(function(){
	BT_hoverIn(this);
	return false});	
	
}

function BT_setOptions(hash, e)
{
	if(hash["direction"] != null)
		BT_direction = hash["direction"];
	if(hash["openWait"] != null)
		BT_open_wait = hash["openWait"];
	if(hash["closeWait"] != null)
		BT_close_wait = hash["closeWait"];
	if(hash["cacheEnabled"] != null)
		BT_cache_enabled = hash["cacheEnabled"];
	if(hash["height"] != null)
		BT_height = 'height:' + hash["height"] + ';';
	if(hash["deleteButtonType"] != null)
		BT_deleteButton = hash["deleteButtonType"];
	BT_event = e;
}

function BT_hoverIn(a)
{
	var timeout = BT_open_wait;
	
	if($('#BT_cache_'+a.id).length > 0)
		timeout = 0;
	
	var title = a.title;
	
	if(!BT_titles[a.id])
	{
		if(!title || title.toLowerCase() == "$none")
			title = "";
		else if(title.toLowerCase() == "$content")
			title = $(a).text();
		
		BT_titles[a.id] = title;
		a.title = "";
	}
		
	BT_events[a.id] = 1;
	setTimeout(function(){BT_show(a.id)}, BT_open_wait);
}

function BT_hoverOut(a)
{
	BT_events[a.id] = 0;
	setTimeout(function(){BT_remove();}, BT_close_wait);
}

function BT_remove()
{
	$('#BT').remove();
}

function BT_show(id) {

	if(BT_events[id] == 0)
		return;
	
	var obj = $("#"+id);
	var url = obj[0].href;
	var title = BT_titles[id];
	
	$("#BT").remove();

	var parents = $("#"+id).parent("div.betterTip");
	
	if(parents.length > 0)
		id = $("#"+id).parent("div.betterTip")[0].id;

	obj = $("#"+id);
	
	var showTitle = true;

	if(title.length == 0)
		showTitle = false;
	
	var urlParts = url.split("\?", 2);
	var query = BT_parseQuery(urlParts[1]);
	urlParts[0] = urlParts[0].substr(urlParts[0].lastIndexOf('/')+1);
	
	if(!query["width"] || query["width"].length == 0)
		query["width"] = 250;
	
	var tipWidth = parseInt(query["width"]);
	
	var act_left = BT_getLeft(id);
	var act_width = BT_getWidth(id);
				
	var left = act_left + act_width + 12;
	var top = BT_getTop(id);
	
	var arrowDir = BT_direction;
	
	var docWidth = self.innerWidth || (document.documentElement&&document.documentElement.clientWidth) || document.body.clientWidth;
	var right = act_left + act_width + 11 + tipWidth + 16;
	var arrowLeft = -16;
	var arrowTop = -3;
	var shadowTop = -7;
	var shadowLeft = -7;
	/*if(showTitle)
		arrowDir = "title_" +arrowDir;*/
	var obj = $('#'+id)
	switch(BT_direction)
	{
		case "right":
			$.BT_style = "top:"+(top-8)+"px; right:"+(left + 22)+"px;";
			$.BT_arrow = "top:50%; right:"+arrowLeft+"px;";
			$.BT_delete = "position: absolute;top:3px; left:3px;margin-right: 3px;margin-bottom: 3px;"
		 break;
		case "up":
			top = BT_event.pageY +19; 
			left = BT_event.pageX - 80;
			//top = //obj[0].offsetTop + 42;
			//left = //act_width /2;
			$.BT_style = "top:"+top+"px; left:"+ left +"px;";
			var arrw_left = 20;
			if (query["width"] <=200) arrw_left = 30;
			$.BT_arrow = "left:" + arrw_left + "%; top:"+arrowLeft+"px;";
			$.BT_delete = "position: absolute;bottom:3px; right:3px;margin-left: 3px;margin-top: 3px;"
		 break;
		case "down":
			top = BT_event.pageY +19; //obj[0].offsetTop + 32;
			left = BT_event.pageX - 80; //obj[0].offsetLeft + obj.width()/2;
			//if(left >= 900) left = left -270;
			//else if(left >= 700) left = left - obj.width()/3;
			$.BT_style = "top:"+top+"px; left:"+left +"px;";
			var arrw_left = 20;
			if (query["width"] <=200) arrw_left = 30;
			$.BT_arrow = "left:" + arrw_left + "%; top:"+top +"px;";
			$.BT_delete = "position: absolute;bottom:3px; right:3px;margin-left: 3px;margin-top: 3px;";
			
		 break;
		default:
			top = BT_event.pageY - 20; //obj[0].offsetTop + 32;
			left = BT_event.pageX + 25; //obj[0].offsetLeft + obj.width()/2;
			$.BT_style = "top:"+(top)+"px; left:"+(left)+"px;";
			$.BT_arrow = "top:50%; left:"+arrowLeft+"px;";
			$.BT_delete = "position: absolute;top:10px; right:10px;margin-left: 10px;margin-bottom: 10px;"
		 break;
	}
	
	$("body").append("<div id='BT' class='BT_shadow0 clearfix' style='" + $.BT_style + "'>" +
					 "<div id='BT_main' class='clearfix' style='width:"+query["width"]+"px;"+ BT_height + "'>" +
					 (BT_direction != "up" ? "<div id='BT_delete" + BT_deleteButton + "' style='" + $.BT_delete + "' ></div>" : "") +
					 "<div id='BT_arrow_"+arrowDir+"' style='" + $.BT_arrow + "'></div>"  +
			         (showTitle?"<div id='BT_title'>"+title+"</div>":"") +
					 "<div style='padding:0px' class=''>" +
					 "<div id='BT_content'>" +
					 "<div class='BT_loader'></div>" +
					 "</div></div>" +
					 (BT_direction == "up" ? "<div id='BT_delete'" + BT_deleteButton + "' style='" + $.BT_delete + "' ></div>" : "") +					 
					 "</div></div>");
	
	
	$("#BT_delete" + BT_deleteButton).click(function(){
	BT_hoverOut(this);
	return false});
	
	if(urlParts[0].charAt(0) == '$')
	{
	  $('#BT_content').html($("#"+urlParts[0].substr(1)).html());
	  if(BT_direction== "left" || BT_direction== "right")
	  {
		var vertical_centered = top - ($('#BT').height()/2);
		$('#BT').css('top', vertical_centered + 'px');
		$('#BT_arrow_' + BT_direction).css('top', $('#BT').height()/2 + 'px');
	  }
	  else if(BT_direction== "down")
	  {
		var vertical_centered = top - $('#BT').height() - $('#BT_arrow_down').height() - $('#BT_arrow_down').height() +2;
		$('#BT').css('top', vertical_centered + 'px');
		$('#BT_arrow_down').css('top', ($('#BT_main').height() -6 ) + 'px');
	  }
		$('#BT').show();
		
	}
	else if(BT_cache_enabled)
	{
		if($('#BT_cache_'+id).length > 0)
			BT_loadCache(id);
		else
			$.post(url, {}, function(data){
				BT_createCacheElement(id, data);
			});
	}
	else
	{
		$.post(url, {}, function(data){
			$('#BT_content').html(data);
			$('#BT').show();
		})
	}
}

function BT_createCacheElement(id, data)
{
	$("body").append("<div id='BT_cache_"+id+"' style='display:none'>"+
		data+"</div>");
	
	BT_loadCache(id);
}

function BT_loadCache(id)
{
	$('#BT_content').html($('#BT_cache_'+id).html());
	$('#BT').show();
}

function BT_getWidth(id) {
	var x = document.getElementById(id);
	return x.offsetWidth;
}

function BT_getLeft(id) {
	
	var obj = $('#'+id)[0];
	var left = obj.offsetLeft;
	var parent = obj.offsetParent;
	
	while(parent) {
		left += parent.offsetLeft;
		parent = parent.offsetParent;
	}
	
	return left
}

function BT_getTop(id) {
	var obj = $('#'+id)[0];
	var top = obj.offsetTop;
	var parent = obj.offsetParent;
	
	while(parent) {
		top += parent.offsetTop;
		parent = parent.offsetParent;
	}
	
	return top;
}


function BT_parseQuery ( query ) {
	
	var params = new Object ();
   
	if ( ! query ) 
		return params;
		
	var pairs = query.split(/[;&]/);
	
	for ( var i = 0; i < pairs.length; i++ ) {
		
		var kv = pairs[i].split('=');
		
		if ( ! kv || kv.length != 2 ) 
			continue;
			
		var key = unescape( kv[0] );	
		var val = unescape( kv[1] );
		
		val = val.replace(/\+/g, ' ');
		params[key] = val;
	}
	
	return params;
}

function mouseX(element) {
	evt = BT_event || window.event;
	if (evt.pageX) return evt.pageX;
 else if (evt.clientX)
    return evt.clientX + (document.documentElement.scrollLeft ?
    document.documentElement.scrollLeft :
    document.body.scrollLeft);
 else return null;

 }
 function mouseY(element) {
	evt = BT_event || window.event;
	if (evt.pageY) return evt.pageY;
 else if (evt.clientY)
    return evt.clientY + (document.documentElement.scrollTop ?
    document.documentElement.scrollTop :
    document.body.scrollTop);
 else return null;

 }
