// JavaScript Document
/*-------------------------------------------------------------------------------
	A Better jQuery Tooltip
	Version 1.0
	By Jon Cazier
	jon@3nhanced.com
	01.22.08
-------------------------------------------------------------------------------*/

jQuery.fn.yippyOverlay = function(options){
	
	/* Setup the options for the tooltip that can be 
	   accessed from outside the plugin              */
	var defaults = {
		speed: 200,
		func:function(){},
		html:'',
		loading:false
	};
	
	var options = jQuery.extend(defaults, options);
	
	/* Create a function that builds the tooltip 
	   markup. Then, prepend the tooltip to the body */

	getOverlay = function() {
		var tOverlay = 
			"<div class='overlay-bg'>" +
			"</div>" + 
			"<div class='overlay-cargo'>" +
			"<div class='overlay-container'>" +
			"</div>" + 
			"<a href='#' class='overlay-close'>" +
			"</a>" + 
			"</div>";
		return tOverlay;
	}
	if(!jQuery(".overlay-bg")[0]){
		jQuery("body").append(getOverlay());
	}
	
	jQuery(window).resize(function() {
	  setOverlay();
	});

	
	var overlay = jQuery('.overlay-cargo');
	var overlayBg = jQuery('.overlay-bg');
	var overlayInner = jQuery('.overlay-cargo .overlay-container');
	
	jQuery(".overlay-close").each(function(){
		var jQuerythis = jQuery(this);
		jQuerythis.click(
			function() {
				hideOverlay();
				return false;
			}
		);
		
	});
	
	jQuery(".overlay-bg").each(function(){
		var jQuerythis = jQuery(this);
		jQuerythis.click(
			function() {
				hideOverlay();
				return false;
			}
		);
		
	});
	
	/* Give each item with the class associated with 
	   the plugin the ability to call the tooltip    */

	if(options.html==''){
		jQuery(this).each(function(){
			var jQuerythis = jQuery(this);
			//alert(jQuerythis);
			/* Mouse over and out functions*/
			
			jQuerythis.click(
				function() {
					var oLink = (this.getAttribute("href"));
					//alert(oLink);
					if(oLink){
						loadPage(oLink);
					}
					
					return false;
				}
			);	
			
		});
	}else{
		insertHTML(options.html);
	}
	
	loadPage = function(path){
		
		jQuery.get(path,function(data){
			insertHTML(data);	
		});
	}
	
	insertHTML = function (html){
		overlayInner.html(html);	
		showOverlay();
	}
	
	
	showOverlay = function(){
		
		overlay.css({"display":"block"});
		setOverlay();
		overlay.hide();
		//overlay.animate({"opacity": "1.0"}, options.speed, "linear",function(){ var call=eval(options.func);call();});
		overlay.fadeIn(options.speed,function(){ var call=eval(options.func);call();});
		overlayBg.css({"display":"block","opacity":"0"});
		overlayBg.animate({"opacity": "0.5"}, options.speed, "linear");
	}
	
	setOverlay = function (){
		var display_size=displaySize();
		var object_size=objectSize(overlayInner);
		
		
		if(object_size.width>0&&object_size.height>0){
			overlay.css({"width":object_size.width+"px","height":object_size.height+"px"});
			
			var object_size=objectSize(overlay);
			if(object_size.width>display_size.width){
				display_size.width=object_size.width;
			}
			if(object_size.height>display_size.height){
				display_size.height=object_size.height;
			}
			var position_display=positionDisplay(object_size.width,object_size.height,display_size.width,display_size.height);
			
			overlay.css({"left":position_display.left+"px","top":position_display.top+"px"});
			
			//alert(display_size.width+","+display_size.height);
			overlayBg.css({"width":display_size.width,"height":display_size.height});
		}
	}
	
	hideOverlay = function(){
		overlay.fadeOut(options.speed,function (){overlay.css({"width":"auto","height":"auto"});});
		overlayBg.fadeOut(options.speed);
	}
	
	displaySize = function (){
		var size=new Array(),tem_doc=new Array(),tem_body=new Array();
		if(document.documentElement){
			tem_doc["width"]=document.documentElement.scrollWidth;
			tem_doc["height"]=document.documentElement.scrollHeight;
			if(tem_doc["height"]<document.documentElement.clientHeight){
				tem_doc["height"]=document.documentElement.clientHeight;
			}
			
		}
		
		
		
		if(document.body){
			tem_body["width"]=document.body.scrollWidth;
			tem_body["height"]=document.body.scrollHeight;
						 
			if(tem_body["height"]<document.body.clientHeight){
				tem_body["height"]=document.body.clientHeight;
			}
		}
		
		
		
		if(tem_doc["width"]>tem_body["width"]){
			size["width"]=tem_doc["width"];
		}else{
			size["width"]=tem_body["width"];
		}
		
		if(tem_doc["height"]>tem_body["height"]){
			size["height"]=tem_doc["height"];
		}else{
			size["height"]=tem_body["height"];
		}
		
		obj={width:size["width"],height:size["height"]}
		return obj;
	}
	
	objectSize = function(element){
		//obj={width:element[0].offsetWidth,height:element[0].offsetHeight}
		obj={width:element.width(),height:element.height()}
		return obj;
	}
	
	positionDisplay = function(object_width,object_height,display_width,display_height){
		var left=parseInt((display_width-object_width)/2);
		var top=0,b_scroll_top=0,e_scroll_top=0,tem_scroll_top=0,b_monitor_height=0,e_monitor_height=0,tem_monitor_height=0;
		
		if(document.documentElement){
			e_scroll_top=document.documentElement.scrollTop;
		}
		b_scroll_top=document.body.scrollTop;
		
		if(e_scroll_top>b_scroll_top){
			tem_scroll_top=e_scroll_top;
		}else{
			tem_scroll_top=b_scroll_top;
		}
		
		if(document.documentElement){
			e_monitor_height=document.documentElement.clientHeight;
		}
		b_monitor_height=document.body.clientHeight;
		
		if(e_monitor_height<b_monitor_height){
			tem_monitor_height=e_monitor_height;
		}else{
			tem_monitor_height=b_monitor_height;
		}

		
		if(tem_monitor_height>object_height){
			top=tem_scroll_top+(parseInt((tem_monitor_height-object_height)/2));
		}else{
			
			if(display_height>(tem_scroll_top+object_height)){
				top=tem_scroll_top;
			}else{
				top=parseInt(display_height-object_height);
			}
		}

		obj={left:left,top:top}
		return obj;
	}
	
	
	
};
