/**
 * PLY Utilities.
 *
 * This file inclues utility-driven jQuery extensions.   It should not be used for cosmetic effects..
 *
 * @package: Washington Wine Restaurant Awards Website - 2009
 **/

    /**
     * IE 6 is a Problem Browser.  Extend the jQuery Browser Object allow easy detection of IE 6
     **/
        jQuery.extend($.browser, {
           'isIE6' : ($.browser.msie == true && parseInt($.browser.version, 10) < 7)
        });

    /**
     * Tooltip Class
     *
     * @todo: Comment
     **/
        function Tooltip(containerElement, triggerElement) {
            var objPointer = this;
        /* Class Variables */
            this.container = $(containerElement).clone();
			$(containerElement).remove();
            this.triggerElement = $(triggerElement);
			this.isAppended	= false;

        /* Mozilla Handles PNG Alpha Slightly Differently Than Other Browsers */
            if($.browser.mozilla == false)
                this.container.find('.tooltipContent').css('background-color', '#9C9C9C');

            this.init = function() {
                objPointer.positionAndPlace();
                objPointer.setupEvents();
            }

            this.positionAndPlace = function() {
                topPosition = objPointer.triggerElement.offset().top + ($.browser.isIE6 == true ? 0 : (objPointer.triggerElement.outerHeight() / 2)) - ($.browser.msie == true ? 3 : 0);
                leftPosition = objPointer.triggerElement.offset().left + objPointer.triggerElement.outerWidth() + ($.browser.isIE6 == true ? 5 : 125);
                objPointer.container.css({
                    'position' : 'absolute',
                    'z-index'  : '99',
                    'top'      : parseInt(topPosition, 10) + 'px',
                    'left'     : parseInt(leftPosition, 10) + 'px'
                });
                objPointer.container.hide();
				if(!objPointer.isAppended) {
                	$('body').append(objPointer.container);
					objPointer.isAppended = true;
				}
            }

            this.show = function() {
				if(objPointer.container.find('.tooltipContent:first').text().length > 0) {
	            	objPointer.positionAndPlace();
	            	// IE 6 z-index fix
	                if($.browser.isIE6 == true) {
	                    objPointer.container.bgiframe();
					}
					objPointer.container.show();
				}
            }

            this.hide = function() {
                objPointer.container.hide();				
            }

            this.toggle = function() {
                objPointer.container.toggle();
            }

            this.setupEvents = function() {
            // Note: Ideally this would be refactored so we pass in the event we want
            //       to bind to... it was done this way for speed.
                if(isEditMode) {
                    objPointer.triggerElement.click(objPointer.toggle);
                } else {
                    objPointer.triggerElement.mouseover(objPointer.show);
					objPointer.triggerElement.focus(objPointer.show);
					objPointer.triggerElement.blur(objPointer.hide);
                    objPointer.triggerElement.mouseout(objPointer.hide);
                }
            }

        // Implicit Init
            this.init();
        }

    /**
     *  jQuery Shorthand / Plugin for instantiating a Tooltip
     **/
        jQuery.fn.extend({
            'setupTooltip' : function(tooltipContainer) {
                var tooltip = new Tooltip(tooltipContainer, this);
                return $(this);
            }
        });

    /**
     * End Tooltip JS
     **/
