/**
 * Mogul Tooltip jQuery Plugin
 * @version: 2.0.0
 * @author Emil Löfquist
 * Dependencies: jQeury v. >= 1.3.2 (not tested with any versions below)
 * 
 * !!!!!!!!!OBS!!!!!!!
 * This file has been modified from the original to meet the needs
 * for this particular website. Do not replace this version with a newer one.
 */
(function($) {
	$.fn.tooltip = function(settings) {
		/**
		 * Default settings
		 */
		settings = $.extend({
			offX: 15, 				/** {Number} OffX pixels from the mouse pointer */
			offY: 15, 				/** {Number} OffY pixels from the mouse pointer */
			tooltipID: 'tooltip', 	/** {String} The default tooltip container ID. */
			track: true, 			/** {Boolean} setings to false if the tooltip should be fixed */
			delay: 200, 			/** {Number} In milliseconds before the tooltips should appear */			
			ajaxSettings: {			/** {Object} */
				spinner: '',		/** {String} Ex. "Loading..." or spinning gif */
				enable: false
			},
			fadein: {
				enable: false,
				speed: 0
			},
			fadeout: {
				enable: false,
				speed: 0
			},
			localHTML: false, 		/** {Boolean} Fetch the tooltip data from a local container. */
			tooltipCss: { 			/** {Object} Default css for the tooltip container */
				background: 'lightyellow',
				border:	'solid 2px gray',
				borderWidth: '1px 2px 2px 1px',
				padding: '3px 5px',
				fontFamily: 'Arial',
				fontSize: '12px',
				width: '200px'
			}
		}, settings);
		
		var tooltipMethods = (function() {
			
			// Private variables
			var tooltipContainer = null;
			var tooltipText = null;
			var timer = null;
			var ajaxCache = new Array();
			var winWidth = 0;
			var winHeight = 0;
			
			// Private functions
			
			/**
			 * 
			 * @param {String} $text
			 */
			function fillTooltip($text) {
				timer = setTimeout(function() {
					if(settings.fadein.enable) {						
						tooltipContainer.html($text).fadeIn(settings.fadein.speed);
					}
					else tooltipContainer.html($text).show();
				}, settings.delay);
			};
			
			/**
			 * 
			 * @param {Object} $this
			 */
			function viewTooltip($this) {
				if(settings.localHTML) {
					tooltipText = $('.' + $this.attr('rel')).html();
				}
				else {
					tooltipText = $this.attr('title');
					$this.attr('title', '');
				}
				fillTooltip(tooltipText);
			};
			
			/**
			 * 
			 * @param {Object} $this
			 */
			function viewAjaxTooltip($this) {
				
				tooltipText = $this.attr('title');
				$this.attr('title', '');
				
				// check the cache if we already have made an
				// ajax call to the current url.
				if(ajaxCache.length > 0) {
					for(var i=0, j=ajaxCache.length; i<j; i++) {
						if(tooltipText === ajaxCache[i][0]) {
							fillTooltip(ajaxCache[i][1]);
							return;
						}
					}
				}
				
				// Else, make an ajax call
				
				// show the spinner without any delay
				tooltipContainer.html(settings.ajaxSettings.spinner).show();
				$.ajax({
					url: tooltipText,
					dataType: 'text',
					success: function(data) {
						fillTooltip(data);
						ajaxCache.push([tooltipText, data]);
					}
				});
			};
			
			
			/**
			 * 
			 * @param {Object} $this
			 */
			function hideTooltip($this) {
				if (settings.localHTML) {
					if(settings.fadeout.enable) {
						tooltipContainer.fadeOut(settings.fadeout.speed);
					}
					else tooltipContainer.hide();
				}
				else {
					$this.attr('title', tooltipText);
					if(settings.fadeout.enable) {
						tooltipContainer.fadeOut(settings.fadeout.speed, function() {
							$(this).html('');
						});
					}
					else tooltipContainer.empty().hide();
				}
			};
			
			/**
			 * 
			 * @param {Object} $e
			 */
			function positionTooltip($e) {
				var tw = tooltipContainer.outerWidth();			
				var isCommercePage = $("body").is("body[class*='template-commerce']");
				//Customize vertical position of popup on commerce template page
				if (isCommercePage) {
					if (($e.pageX + tw + (settings.offX) + 20) > winWidth) { // position the tooltip to the left of the pointer
						tooltipContainer.css({
							left: ($e.pageX + settings.offX) - (tw + (settings.offX-55)) + 'px',
							//top: ($e.pageY + settings.offY) + 'px'
							top: '292px'
						}).addClass('flipped');
					}
					else {
						tooltipContainer.css({
							left: ($e.pageX + settings.offX) + 'px',
							//top: ($e.pageY + settings.offY) + 'px'
							top: '292px'
						}).removeClass('flipped');
					}					
				} else {
					if (($e.pageX + tw + (settings.offX) + 20) > winWidth) { // position the tooltip to the left of the pointer
						tooltipContainer.css({
							left: ($e.pageX + settings.offX) - (tw + (settings.offX-55)) + 'px',
							top: ($e.pageY + settings.offY) + 'px'
						}).addClass('flipped');
					}
					else {
						tooltipContainer.css({
							left: ($e.pageX + settings.offX) + 'px',
							top: ($e.pageY + settings.offY) + 'px'
						}).removeClass('flipped');
					}					
				}

			};
			
			/**
			 * 
			 * @param {Object} $this
			 * @param {Object} settings
			 */
			function init($this) {
				
				// Hide the related html tooltip containers
				if(settings.localHTML) { 
					$('.' + $this.attr('rel')).hide();
				};
				
				// mouse over
				$this.mouseover(function(e) {
					if(!settings.track) {
						positionTooltip(e);
					}
					if (settings.ajaxSettings.enable) {
						viewAjaxTooltip($this);
					}
					else {
						viewTooltip($this);
					}
				});
				
				// mouse out
				$this.mouseout(function(e) {
					hideTooltip($this);
					clearTimeout(timer);
				});
				
				// mouse move
				if (settings.track) {
					$this.mousemove(function(e){
						positionTooltip(e);
					});
				}
			};
			
			/**
			 * 
			 * @param {Object} tooltipID
			 * @param {Object} tooltipCss
			 */
			function insertTooltipContainer(tID, tooltipCss) {
				$('body').append('<div id="' + tID + '"></div>');
				tooltipContainer = $('#' + tID)
				tooltipContainer.css({
					position: 'absolute',
					left: '0',
					top: '0',
					zIndex: '999'
				}).css(tooltipCss).hide();
			};
			
			/**
			 * 
			 */
			function getWindowProp() {
				winWidth = $(document).width();
				winHeight = $(document).height();
				
				$(window).resize(getWindowProp);
			};
			
			// Public
			return {
				init: init,
				insertTooltipContainer: insertTooltipContainer,
				getWindowProp: getWindowProp
			};
		})();
		
		tooltipMethods.insertTooltipContainer(settings.tooltipID, settings.tooltipCss);
		tooltipMethods.getWindowProp();
		
		return this.each(function() {
			tooltipMethods.init($(this));
		});
	};
	
})(jQuery);
