/**
 *  Originally jQuery Tooltip Plugin
 *  @requires jQuery v1.2.6
 *  http://www.socialembedded.com/labs
 *
 *  Copyright (c)  Hernan Amiune (hernan.amiune.com)
 *  Dual licensed under the MIT and GPL licenses:
 *  http://www.opensource.org/licenses/mit-license.php
 *  http://www.gnu.org/licenses/gpl.html
 * 
 *  Version: 1.0
 *  Revised for Exchange range tooltip
  */
 
(function($){ $.fn.exratetooltip = function(options){

    var defaults = {
        primaryCurrency: "$",     //CSS class or classes to style the tooltip
        convTitle: "APPROXIMATED amounts in other currencies:",
        cssClass: "",     //CSS class or classes to style the tooltip
        delay : 0,        //The number of milliseconds before displaying the tooltip
        duration : 500,   //The number of milliseconds after moving the mouse cusor before removing the tooltip.
        xOffset : 15,     //X offset will allow the tooltip to appear offset by x pixels.
        yOffset : 15,     //Y offset will allow the tooltip to appear offset by y pixels.
        opacity : 0,      //0 is completely opaque and 100 completely transparent
        fadeDuration: 0 //[toxi20090112] added fade duration in millis (default = "normal")
    };
  
    var options = $.extend(defaults, options);
    
    var href = '/lot-item/exrate/';
    response = $.ajax({url:href,async:false}).responseText;
    
    var objResp = JSON.parse(response);
    var currency = objResp["Currencies"];
    
    return this.each(function(index) {
        
        if (currency.length == 0) return;
        
        var $this = $(this);
        
        //use just one div for all tooltips
        // [toxi20090112] allow the tooltip div to be already present (would break currently)
        $exratetooltip=$("#divTooltip");
        if($exratetooltip.length == 0){
            $exratetooltip = $('<div id="divTooltip"></div>');            
            $('body').append($exratetooltip);
            $exratetooltip.hide();
        }
        
        //displays the tooltip
        $this.mouseover( function(e){
            //compatibility issue
            e = e ? e : window.event;
            
            //don't hide the tooltip if the mouse is over the element again
            clearTimeout($exratetooltip.data("hideTimeoutId"));
            
            //set the tooltip class
            $exratetooltip.removeClass($exratetooltip.attr("class"));
            $exratetooltip.css("width","");
            $exratetooltip.css("height","");
            $exratetooltip.addClass(options.cssClass);
            $exratetooltip.css("opacity",1-options.opacity/100);
            $exratetooltip.css("position","absolute");            
            
            //save the title text and remove it from title to avoid showing the default tooltip
            $exratetooltip.data("title",$this.html());
            
            var amount = $exratetooltip.data("title");
                amount = amount.replace(options.primaryCurrency, ''); 
                amount = amount.replace(',', ''); 

            var html = '<div class="exrate-wrap">';
                html = html + '<div class="clear">'+ options.convTitle +'</div>';
            for(var i=0;i<currency.length;i++) {
                var rate = currency[i][2] *  amount;
                    rate = number_format(rate,2);
                html = html + '<div class="label">' + currency[i][0] + '</div>';
                html = html + '<div class="rate">' + currency[i][1] + rate + '</div>';
            
            }
            html = html + '<div class="clear"></div>';
            html = html + '</div>';
            $exratetooltip.html(html);
            
            //set the tooltip content
            // [toxi20090112] only use ajax if there actually is an href attrib present
            //var href='/lot-item/exrate/amount/' + $exratetooltip.data("title") ;
            //$exratetooltip.html($.ajax({url:href,async:false}).responseText);
            
            //set the tooltip position
            winw = $(window).width();
            w = $exratetooltip.width();
            xOffset = options.xOffset;
            
            //right priority
            if(w+xOffset+50 < winw-e.clientX)
              $exratetooltip.css("left", $(document).scrollLeft() + e.clientX+xOffset);
            else if(w+xOffset+50 < e.clientX)
              $exratetooltip.css("left", $(document).scrollLeft() + e.clientX-(w+xOffset));
            else{
              //there is more space at left, fit the tooltip there
              if(e.clientX > winw/2){
                $exratetooltip.width(e.clientX-50);
                $exratetooltip.css("left", $(document).scrollLeft() + 25);
              }
              //there is more space at right, fit the tooltip there
              else{
                $exratetooltip.width((winw-e.clientX)-50);
                $exratetooltip.css("left", $(document).scrollLeft() + e.clientX+xOffset);
              }
            }
            
            winh = $(window).height();
            h = $exratetooltip.height();
            yOffset = options.yOffset;
            //top position priority
            if(h+yOffset + 50 < e.clientY)
              $exratetooltip.css("top", $(document).scrollTop() + e.clientY-(h+yOffset));
            else if(h+yOffset + 50 < winh-e.clientY)
              $exratetooltip.css("top", $(document).scrollTop() + e.clientY+yOffset);
            else 
              $exratetooltip.css("top", $(document).scrollTop() + 10);
            
            //start the timer to show the tooltip
            //[toxi20090112] modified to make use of fadeDuration option
            $exratetooltip.data("showTimeoutId", setTimeout("$exratetooltip.fadeIn("+options.fadeDuration+")",options.delay));
        });
        
        $this.mouseout(function(e){
            //don't show the tooltip if the mouse left the element before the delay time
            clearTimeout($exratetooltip.data("showTimeoutId"));
            //start the timer to hide the tooltip
            //[toxi20090112] modified to make use of fadeDuration option
            $exratetooltip.data("hideTimeoutId", setTimeout("$exratetooltip.fadeOut("+options.fadeDuration+")",options.duration));
        });
        
        $this.click(function(e){
            e.preventDefault();
        });

    });

}})(jQuery);