/*
 * Deluxe Tip 0.1
 * 
 * Author: Ryan Fitzer
 *
 * Description:
 *
 * Usage: Coming Soon!
 *
 */
function deluxeTip() {

	$.fn.extend({
		deluxeTip: function(options) {
			return this.each(function() {
				new $.DeluxeTip(this, options);
			});
		}
	});

	$.DeluxeTip = function(element, options) {

		var defaults = {
			x: 0,
			y: 0,
			mode: 'simple',
			defaultSide: 'right',
			tooltipClass: ''
		};

		this.options = $.extend({}, defaults, options || {});
		this.element = $(element);

		// Simple mode
		if(this.options.mode == 'simple') {

			this.content = this.element.attr('title');
			this.element.removeAttr('title');

		} else {
		// Deluxe mode

			var oldDeluxeTip = this.element.next('.deluxeTipWrap');
			if(this.options.tooltipClass != '') {
				oldDeluxeTip.addClass(this.options.tooltipClass);						
			}
			this.deluxeTip = oldDeluxeTip.clone();
			oldDeluxeTip.remove();
			$('body').append(this.deluxeTip);
		}

		this.setup();

	};

	$.extend($.DeluxeTip.prototype, {

		setup: function() {

			var self = this;

			// Simple mode
			if(this.options.mode == 'simple') {
				if(!$('.' + this.options.tooltipClass).get(0)) {
					$('body').append($('<div style="display:none;" class="simpleTipWrap ' + this.options.tooltipClass + '"><div class="simpleTipContent"></div></div>'));	
				}
				this.element.hover(function(e) {
					self.display(e);
					e.stopPropagation();
				});
				if(!$(this.element).hasClass('smallBtn')) {
					this.element.live('click', function(e) {
						return false;
					});
				}

			} else {
			// Deluxe mode

				this.element.live('mouseenter mouseleave', function(e) {
				    $(this).toggleClass('over');
				});		
				this.element.live('click', function(e) {
					self.display(e);
					e.stopPropagation();
					return false;
				});
			}
		},

		display: function(e) {

			var self = this;

			// Simple mode
			if(this.options.mode == 'simple') {
				this.element.removeClass('tooltip_open');
				this.element.addClass('tooltip_open');
				this.element.live('mouseleave', function() {
					self.hide();
				});
				var tooltip	= $('.' + this.options.tooltipClass);
				tooltip.find('.simpleTipContent').html(this.content);

				tooltip.css({
					'opacity': '0',
					'display': 'block',
					'height' : 'auto'
				});						

				var pos = this.position(e);
				$(tooltip).css({
					'top'	: pos.y,
					'left'	: pos.x,
					'height': pos.tooltipHeight
				}).stop().animate({
					'opacity': '1'
				}, 200);

			} else {
			// Deluxe mode

				this.element.unbind('click');
				this.element.removeClass('tooltip_open');
				this.element.addClass('tooltip_open');
				this.deluxeTip.find('.close').bind('click', function() {
					self.hide()
				});
				$('body').bind('click',function(e){
					if($(e.target).parents('.deluxeTipWrap').length < 1) {
						self.hide();
					}
				});
				var tooltip		 = this.deluxeTip;
				var tooltipPoint = this.deluxeTip.find('.deluxeTipPoint');
				tooltip.css({
					'opacity': '0',
					'display': 'block',
					'height' : 'auto'
				});
				var pos = this.position(e);
				$(tooltip).css({
					'top'	: pos.y,
					'left'	: pos.x,
					'height': pos.tooltipHeight
				}).animate({
					'opacity': '1'
				}, 200);
				/*
				if(utils.lvl1) {
					this.support(pos, 'show');
				}
				*/
			}

		},

		hide: function(e) {

			var self = this;
			this.element.removeClass('tooltip_open');
			// Simple mode
			if(this.options.mode == 'simple') {
				$('.' + this.options.tooltipClass).hide();
			} else {
			// Deluxe mode
				this.deluxeTip.hide();
				this.element.bind('click', function(e){
					self.display(e);
					e.stopPropagation();
					return false;
				});
			}
			if(!$.support.opacity) {
				this.support('', 'hide');
			}
			$('body').unbind('click');

		},

		position: function(e) {

			// Simple mode
			if(this.options.mode == 'simple') {

				var clientWidth		= $('body').width();            
				var tooltip			= $('.' + this.options.tooltipClass);
				var	tooltipWidth	= tooltip.width();
				var	tooltipHeight	= tooltip.height();
				var targetOffset	= $(e.target).offset();
				var displayArea		= targetOffset.left + tooltipWidth + 30;

				if(displayArea > clientWidth){
					var xPos = targetOffset.left - tooltipWidth - (this.options.x - 22);
					tooltip.addClass('viewport-left');

				} else {
					var xPos = targetOffset.left + this.options.x;
					tooltip.removeClass('viewport-left');
				}
				var yPos = targetOffset.top - tooltipHeight + this.options.y;
				var tooltipHeight	= tooltip.height();
				return {
					'x': xPos,
					'y': yPos,
					'tooltipHeight': tooltipHeight
				}

			} else {
			// Deluxe mode

				var clientWidth		= $(window).width();            
				var tooltip			= this.deluxeTip;
				var	tooltipWidth	= tooltip.width();
				var	tooltipHeight	= tooltip.height();
				var targetOffset	= $(e.target).offset();
				var targetWidth		= $(e.target).width();
				var targetRightX	= targetWidth + targetOffset.left;
				var displayArea		= targetRightX + tooltipWidth + 30;
				var defaultSide		= (this.options.defaultSide == 'right') ? true : false;

				// Open on the left
				if(!defaultSide || (displayArea > clientWidth && clientWidth > 500)){
					xPos = targetOffset.left - tooltipWidth - this.options.x;
					tooltip.addClass('viewport-left-deluxe');
				} else {
				// Open on the right
					xPos = targetRightX + this.options.x;
					tooltip.removeClass('viewport-left-deluxe');
				}
				var yPos = targetOffset.top - (tooltipHeight / 2) + this.options.y;
				return {
					'x': xPos,
					'y': yPos,
					'tooltipHeight': tooltipHeight
				}

			}
		},

		support: function(pos, action) {

			if(!$('.tooltip_iframe').get(0)) {
				$('body').append('<iframe class="tooltip_iframe" frameborder="0" src="javascript:\'<html></html>\';"></iframe>');
			}
			var frame = $('.tooltip_iframe');
			switch(action) {
				case 'show':
					frame.css({
						'top'	 : pos.y,
						'left'	 : pos.x,
						'height' : pos.tooltipHeight + 40,
						'opacity': 0,
						'display': 'block'
					}).show();
					break;
				case 'hide':
					frame.css('left', '-3000px');
			}

		}

	});
}

