(function($){

	$.fn.extend({
		
		//bigcore_tooltip plugin
		bigcore_tooltip: function(options)
		{
			//set defaults 
			var defaults = 
			{
				position: 'right',
				offsetX: 10,
				offsetY: 10,
				maxWidth: 500,
				maxHeight: -1,
				delay:1000,
				speed:50
			}
			
			//get option
			var options = $.extend(defaults,options);
			var obj = $('[tooltip]');
			var time = {};
			
			initalize();
			
			//on mouse over
			$(obj).mouseover(function()
			{
				$('.bigcore_tooltip_'+$(this).attr('id')).slideDown(options.speed);	
			});
			
			//on mouse out
			$(obj).mouseout(function()
			{
				var o = this;
				$('.bigcore_tooltip_'+$(o).attr('id')).bind('mouseover',function()
				{
					eval("clearTimeout(time."+$(o).attr('id')+")");
				});
				$('.bigcore_tooltip_'+$(o).attr('id')).bind('mouseout',function()
				{
					eval("time."+$(o).attr('id')+"=setTimeout(function(){hideToolbox(o);},options.delay)");
				});
				eval("time."+$(o).attr('id')+"=setTimeout(function(){hideToolbox(o);},options.delay)");
			});
			
			function hideToolbox(o)
			{
				$('.bigcore_tooltip_'+$(o).attr('id')).slideUp(options.speed);	
			}
			
			//initalize plugin
			function initalize()
			{
				$(obj).each(function()
				{
					var tooltip_box = document.createElement('div');
					$(tooltip_box)
						.html($(this).attr('tooltip'))
						.addClass('bigcore_tooltip')
						.addClass('bigcore_tooltip_'+$(this).attr('id'));
					$('body').append(tooltip_box);
					
					setSize(tooltip_box,$(this));
					
					var position = setPosition($(this),tooltip_box);
					$(tooltip_box)
						.css('left',(position['left'])+'px')
						.css('top',position['top']+'px');	
				});
				
			}
			
			//Set tooltip size
			function setSize(o,input)
			{
				
				
				var margin = getMargin(o);
				var input_margin = getMargin(input);
				var padding = getPadding(o);
				var border = getBorder(o);
				var input_position = $(input).position();
				
				if(options.position == 'top')
				{
					options.maxHeight = (input_position.top-options.offsetX);
				}
				
				if(options.position == 'left')
				{
					options.maxWidth = ((input_position.left-options.offsetY)+input_margin.left);
				}
				
				var width = $(o).width()+padding.left+padding.right+margin.left+margin.right+border.left+border.right+options.offsetX;
				if(options.maxWidth > 0 && width > options.maxWidth)
				{
					$(o).css('width',options.maxWidth+'px');
				}
				var height = $(o).height()+padding.top+padding.bottom+margin.top+margin.bottom+border.top+border.bottom+options.offsetY;
				
				if(options.maxHeight > 0 && height > options.maxHeight)
				{
					$(o).css('height',options.maxHeight+'px');
					$(o).css('overflow','hidden');
				}
				
			}
			
			function getMargin(o)
			{
				var margin = {top:0,right:0,bottom:0,left:0};
				margin.top = parseInt($(o).css('margin-top'));
				margin.right = parseInt($(o).css('margin-right'));
				margin.bottom = parseInt($(o).css('margin-bottom'));
				margin.left = parseInt($(o).css('margin-left'));
				return margin;
			}
			
			function getPadding(o)
			{
				var padding = {top:0,right:0,bottom:0,left:0};
				padding.top = parseInt($(o).css('padding-top'));
				padding.right = parseInt($(o).css('padding-right'));
				padding.bottom = parseInt($(o).css('padding-bottom'));
				padding.left = parseInt($(o).css('padding-left'));
				return padding;
			}
			
			function getBorder(o)
			{
				var border = {top:0,right:0,bottom:0,left:0};
				border.top = parseInt($(o).css('border-top-width'));
				border.right = parseInt($(o).css('border-right-width'));
				border.bottom = parseInt($(o).css('border-bottom-width'));
				border.left = parseInt($(o).css('border-left-width'));
				return border;
			}
			
			function setPosition(o,toolbox)
			{
				var opos = o.position();
				var left = opos.left;
				var top = opos.top;
				var width = o.width();
				var height = o.height();
				var pos = {top:0,left:0};
				var margin = getMargin(o);
				var padding = getPadding(o);
				var border = getBorder(o);

				switch(options.position)
				{
					case 'right':		
						pos.left = Math.round(width+left+options.offsetX+margin.right+padding.left+padding.right+border.right+border.left);
						pos.top = Math.round(top+margin.top);
					break;
					
					case 'left':		
						pos.left = Math.round(left-options.offsetX-$(toolbox).width()+margin.left);
						pos.top = Math.round(top+margin.top);
					break;
					
					case 'top':		
						pos.left = Math.round(left+margin.left);
						pos.top = Math.round(top+margin.top-options.offsetY-$(toolbox).height());
					break;
					
					case 'bottom':		
						pos.left = Math.round(left+margin.left);
						pos.top = Math.round(top+height+options.offsetY+padding.top+padding.bottom+border.top+border.bottom+margin.top);
						console.log(pos);
						console.log(top);
						console.log(height);
						break;
					
				}
				return pos;
			}
		}
	});
	
})(jQuery);
