﻿//
// create closure
//
(function($) {

    //
    // Add the default tip class
    //
    //$('.tip').SCCtip();
    //
    // plugin definition
    //

    $.fn.SCCtip = function(options) {
        //debug(this);
        // build main options before element iteration
        // iterate and reformat each matched element
        return this.each(function() {
            $this = $(this);
            // build element specific options
            var opts = $.extend({}, $.fn.SCCtip.defaults, options);
            var o = $.metadata ? $.extend({}, opts, $(this).metadata()) : opts;
            var tip = tip_item[o.tip_nb];
            //alert(tip);
            $this.hover(function(e) {
                // get mouse coordinates
                var mouseX = e.pageX || (e.clientX ? e.clientX + document.body.scrollLeft : 0);
                var mouseY = e.pageY || (e.clientY ? e.clientY + document.body.scrollTop : 0);
                mouseX += 10;
                mouseY += 10;
                // if there is no div containing the tooltip
                if (!this.tooltipdiv) {
                    // create a div and style it
                    var div = document.createElement("div");
                    this.tooltipdiv = div;

                    $(div).css(
          			    {
          			        border: o.border,
          			        padding: "2px",
          			        backgroundColor: o.bgcolour,
          			        color: o.fgcolour,
          			        position: "absolute"
          			    })
          			.html(tip);
                    $("body").append(div);
                    this.tooltipset = true;
                }
                $(this.tooltipdiv).show().css({ left: mouseX + "px", top: mouseY + 3 + "px" });

            } // mouseover       
                     , function() {
                         if (this.tooltipdiv) {
                             $(this.tooltipdiv).hide();
                         }
                     } // mouseout
                     , function() {
                         // unbind the mouseover and mouseout events 
                         $(this)
                         .unbind('mouseover')
                         .unbind('mouseout');

                     }); // hover  
        }); // each
    }; // fn.SCCTip

    //
    // private function for debugging
    //
    function debug($obj) {
        if (window.console && window.console.log)
            window.console.log('SCCtip selection count: ' + $obj.size());
    };
    //
    // plugin defaults
    //
    $.fn.SCCtip.defaults = {
        bgcolour: "#eee"
        , fgcolour: "#000"
        , tip_nb: 0
        , border: "2px outset #ddd"
    };
})(jQuery);




