/*
 * jBubble v0.3
 *
 * copyright © 2009 Wouter van Dongen
 * http://jbubble.woutervandongen.com
 *
*/
 
(function($) {
		  
	//Variables
	var opts;
	var tooltipAreas = new Array();
	var tooltipBoxId = "#tooltip";
	var tooltipTemplate;
	var tooltipData;
	var linkRef;
	var showDelayTimer;
	var hideDelayTimer;
	var bubbleid;
	
	$.fn.jbubble = function(options) {
		debug(this);	    
		
		var opts = $.extend({}, $.fn.jbubble.defaults, options);
		
	    return this.each(function() {
			
			$this = $(this);								  
			
			var settings = $.metadata ? $.extend({}, opts, $this.metadata()) : opts;			
					
			$.fn.jbubble.initialize(settings);      	
			
			$(this).mouseover(function(e){
				linkRef = $(this);
				bubbleid = linkRef.attr("jbubbleid");
				//showDelayTimer = setTimeout ("$(this).jbubble.start(this);", settings.showDelay)
				$(this).jbubble.start(settings, e);
				//return false;
			});				
			
			$(this).bind("mouseout", function(settings, e){
				  if(settings.tooltipHover){
					hideDelayTimer = setTimeout ("$(this).jbubble.end(this);", settings.hideDelay);
					
					$(tooltipBoxId).bind("mouseenter", function(){
						clearTimeout(hideDelayTimer);			
					});	
					
					$(tooltipBoxId).bind("mouseleave", function(e){						
						clearTimeout(showDelayTimer);
						hideDelayTimer = setTimeout ("$(this).jbubble.end(this);", settings.hideDelay);
						$(this).jbubble.end(settings, e);							
					});	
					
				  }else{
					clearTimeout(showDelayTimer);
					hideDelayTimer = setTimeout ("$(this).jbubble.end(this);", settings.hideDelay);
				  }
				//return false;
			});
			
			if(settings.clickHide){
				$(document).bind("click", function(){
					clearTimeout(showDelayTimer);
					$(tooltipBoxId).hide();						
				});
			}
			
			if(settings.mouseTracking){
				$(this).mousemove(function(e){
					$(this).jbubble.track(e);
				});
			}
			
		});
	};	
	
	$.fn.jbubble.initialize = function(settings){
		$.get(settings.templateFile, function(data){
		  	tooltipTemplate = data;
			
		  	$("body").append(tooltipTemplate);
		  	$(tooltipBoxId).hide();
			//Get the data areas from the template and put these into an array
			var templateChildren = $(tooltipBoxId).children();
			for(var i = 0; i < templateChildren.length; i++){
				if(templateChildren[i].getAttribute('type')){
					var areaName = templateChildren[i].getAttribute('type').split("_");
					areaName = areaName[areaName.length-1];					
					areaName = areaName.substring(0, (areaName.length-1));
					tooltipAreas.push(new Array(areaName, templateChildren[i]));
				}
			}
		  	$(tooltipBoxId).hide();
			
		});
	};
	
	$.fn.jbubble.start = function(settings, e){
		if(settings.mouseTracking || settings.mousePosition){
			$(tooltipBoxId).css("top",	e.pageY+15);
		$(tooltipBoxId).css("left", e.pageX+15);	
		}else{
			$(tooltipBoxId).css("top",linkRef.offset().top+linkRef.height()+3);
			$(tooltipBoxId).css("left", linkRef.offset().left);
		}
		var xmlData = $.ajax({
			url: settings.xmlFile+"?id="+bubbleid,
			dataType: "xml", 
			async: false
		}).responseXML;
		//Pick the correct tooltip data and set it
		tooltipData = undefined;
		$(xmlData).find("tooltip").each(function() {
			if($(this).attr("id") == linkRef.attr("jbubbleid")){
				tooltipData = $(this);
			}
		});
		
		for(var i = 0; i < tooltipAreas.length; i++){			
			//match the tooltipAreas with the xmldata and insert it
			//empty the tooltip
			$(tooltipAreas[i][1]).empty();
			//fill the tooltip
			$(tooltipData).find(tooltipAreas[i][0]).each(function() {
			
				if(!$(tooltipAreas[i][1]).attr("src")){
					$(tooltipAreas[i][1]).append($(this).text());
				}else{
					$(tooltipAreas[i][1]).attr("src",$(this).text());
				}
			});
		}
		if(settings.animate){
			$(tooltipBoxId).animate({
			  "height": "show", "opacity": "show"
			}, "slow");
		}else{
			$(tooltipBoxId).show();		
		}
	};
	
	$.fn.jbubble.end = function(settings, e){
		if(settings.animate){
			$(tooltipBoxId).animate({
			  "height": "hide", "opacity": "hide"
			}, "slow");
		}else{
			$(tooltipBoxId).hide();		
		}
	};
	
	$.fn.jbubble.track = function(e){
		$(tooltipBoxId).css("top",	e.pageY+15);
		$(tooltipBoxId).css("left", e.pageX+15);		
	};
	
	$.fn.jbubble.defaults = {
		showDelay: 200,
		hideDelay: 200,
		animate: true,
		fx: null,
		mousePosition: false,
		mouseTracking: false,		
		clickHide: true,
		tooltipHover: false,
		templateFile: "jbubble.html",
		xmlFile: "jbubble.xml"
	};
	
	function debug($obj) {
		if (window.console && window.console.log)
			window.console.log('hilight selection count: ' + $obj.size());
	};
	
})(jQuery);