// Cambrian House Tooltip jQuery Plugin
// Copyright 2010 Cambrian House
// Author: Mike Barnlund [mike@chaordix.com]
//
// Description: Tooltip for use in Cambrian House Multi-Call projects. X-Browser compatible.
//
// Usage:
//
// $( '.selector' ).tooltip( content, [settings] );
//
// content: Plain text or html
// settings: Configuration Options
// - width: Sets the width of the tooltip (default: 200px)
// - padding: Sets the padding between the element and the tooltip in pixels. Use numbers only (no 'px'). (default: 8)
// - location: Sets the location of the tooltip (top, bottom, left, right). (default: top)

(function( $ ) {

	//Create tooltip collection
	var tooltips = [];

	$.fn.chtooltip = function( content, settings ) {
	    var config = { 'width': '200px', 'padding': 8, 'location': 'top' };

	    if ( settings ) $.extend( config, settings );

	    this.each(function() {

			//$.jGrowl( 'Setting up tooltip events for ' + $( this ).attr( 'id' ) );

			$( this ).hover( function() {
				//Hide any other visible tooltips

				for ( var tipId in tooltips ) {
					var opentip = tooltips[ tipId ];
					if ( opentip.is( ':visible' ) ) { opentip.hide(); }
				}

				//Get Tooltip Element
				var tooltip = getTooltip( $( this ).attr( 'id' ), content, config );

				var position = getPosition( this, tooltip, config.location, config.padding );

				//Position Popup Element
				tooltip.css( 'top', position.top );
				tooltip.css( 'left', position.left );

				//Fade In Tooltip Element if we aren't using a crappy browser
				if (!$.browser.msie) tooltip.fadeIn( 100 );
				else tooltip.show();
			}, function() { } );

			//Wait a few seconds then hide the tooltip
			$( this ).mouseleave( function() {
				//Get Tooltip Element
				var tooltip = getTooltip( $( this ).attr( 'id' ), content, config );

				if ( $( tooltip ).is( ':visible' ) ) {
					if (!$.browser.msie) var fadepopup = function() { $( tooltip ).fadeOut( 100 ); };
					else var fadepopup = function() { $( tooltip ).hide(); };
					setTimeout( fadepopup, 500 );
				}
			} );
		});

		return this;

	};

	function getTooltip( elementId, content, config ) {

		//Check in tooltip collection if this tooltip has already been created
		var tooltip = tooltips[ elementId ];

		//If it hasn't, create it and add it to the collection
		if ( tooltip == null ) {
			tooltip = $( '<div style="width: ' + config.width + '" class="chtooltip"><div class="tipcontent">' + content + '</div></div>' );
			tooltip.hide();
			tooltip.insertBefore( document.body.firstChild );

			//add tooltip to the collection
			tooltips[ elementId ] = tooltip;
			//$.jGrowl( 'Registered tooltip for ' + elementId + '.' );
		}

		return tooltip;
	}

	function getPosition( element, tooltip, location, padding ) {
		var position = new Object();

		//$.jGrowl( 'Getting position.<br/><br/>Location: ' + location + '<br/>Padding: ' + padding + '<br/>Image Top: ' + $( element ).offset().top + '<br/>Tooltip Height: ' + tooltip.height() );

		if ( location.toLowerCase() == 'bottom' ) {
			//offset the tooltip location by the dimensions of the tooltip plus a few pixels
			position.top = $( element ).offset().top + $( element ).height() + Number( padding );

			//find the center (but don't go off the window)
			position.left = $( element ).offset().left + ( $( element ).width() / 2 ) - ( tooltip.width() / 2);
			position.left = ( position.left < 0 ) ? 0 : position.left;
		} else if ( location.toLowerCase() == 'right' ) {
			//offset the tooltip location by the dimensions of the tooltip plus a few pixels
			position.top = $( element ).offset().top + ( $( element ).height() / 2 ) - ( tooltip.height() / 2);

			//move the tooltip to the right of the element
			position.left = $( element ).offset().left + $( element ).width() + Number( padding );
		} else if ( location.toLowerCase() == 'left' ) {
			//offset the tooltip location by the dimensions of the tooltip plus a few pixels
			position.top = $( element ).offset().top + ( $( element ).height() / 2 ) - ( tooltip.height() / 2);

			//move the tooltip to the right of the element
			position.left = $( element ).offset().left - tooltip.width() - Number( padding );
			position.left = ( position.left < 0 ) ? 0 : position.left;
		} else /*( location.toLowerCase.equals ===  'top' )*/ {
			//offset the tooltip location by the dimensions of the tooltip plus a few pixels
			position.top = $( element ).offset().top - ( tooltip.height() + Number( padding ) );
			position.top = ( position.top < 0 ) ? 0 : position.top;

			//find the center (but don't go off the window)
			position.left = $( element ).offset().left + ( $( element ).width() / 2 ) - ( tooltip.width() / 2);
			position.left = ( position.left < 0 ) ? 0 : position.left;
		}

		//$.jGrowl('Top: ' + position.top + '<br/>Left: ' + position.left);

		return position;
	}

})( jQuery );
