/*-------------------------------------------------------------------------------
	A Better jQuery Tooltip
	Version 1.0
	By Jon Cazier
	jon@3nhanced.com
	01.22.08
-------------------------------------------------------------------------------*/

$.fn.betterTooltip = function(options) {

    /* Setup the options for the tooltip that can be 
    accessed from outside the plugin              */
    var defaults = {
        speed: 200,
        delay: 300
    };

    var options = $.extend(defaults, options);

    /* Create a function that builds the tooltip 
    markup. Then, prepend the tooltip to the body */
    getTip = function() {
        var tTip =
			"<div class='tip'>" +
				"<div class='tipMid'>" +
				"</div>" +
				"<div class='tipBtm'></div>" +
			"</div>";
        return tTip;
    }
    $("body").prepend(getTip());

    /* Give each item with the class associated with 
    the plugin the ability to call the tooltip    */
    $(this).each(function() {

        var $this = $(this);
        var tip = $('.tip');
        var data = $(this).children('div').html();
        var key = data.split('|')[0];
        var code = data.split('|')[1];
        var shortcode = data.split('|')[2];

        if (jQuery.trim(key) == '') {
            key = '';
        }

        if (jQuery.trim(code) == '') {
            code = this.title;
        }

        if (jQuery.trim(shortcode) == '') {
            shortcode = '54646';
        }

        var tipInner = $('.tip .tipMid');
        var barcode = 'http://chart.apis.google.com/chart?cht=qr&chs=160x150&chl=smsto%3A' + shortcode + '%3A' + key + '+' + code;
        var tTitle = ('<font color=#634545><u><b>' + $(this).children('a').html() + '</b></u></font><br /><br /><img src=images/mobile.png align=left hspace=3 /><font color=#000>To download this Ring Tone, Send <b>' + key + ' ' + code + '</b> to <b>' + shortcode + '</b>. Rs. 10+GST.</font><br/><img src=a width=160 height=150  alt=loadingQRCode /><div style=display:none>' + barcode + '</div>');
        //alert(barcode);
        this.title = "";

        var offset = $(this).offset();
        var tLeft = offset.left;
        var tTop = offset.top;
        var tWidth = $this.width();
        var tHeight = $this.height();

        /* Mouse over and out functions*/
        $this.hover(
			function() {
			    tipInner.html(tTitle);
			    setTip(tTop, tLeft);
			    //alert($(tipInner).children('div').html());
			    //$(tipInner).children('img:eq(1)').attr('src', 'images/mobile.png');
			    //var myimg = $(tipInner).children('img:eq(1)');
			    //alert(myimg);
			    //document.getElementById(myimg).src = 'images/mobile.png';
			    $(tipInner).children('img:eq(1)').attr('src', $(tipInner).children('div').text());
			    //alert($(tipInner).children('div').text());
			    //alert($(tipInner).children('img:eq(1)').attr('src'));
			    setTimer();

			},
			function() {
			    stopTimer();
			    tip.hide();
			}
		);

        /* Delay the fade-in animation of the tooltip */
        setTimer = function() {
            $this.showTipTimer = setInterval("showTip()", defaults.delay);
        }

        stopTimer = function() {
            clearInterval($this.showTipTimer);
        }

        /* Position the tooltip relative to the class 
        associated with the tooltip                */
        setTip = function(top, left) {
            var topOffset = tip.height();
            var xTip = (left - 30) + "px";
            var yTip = (top - topOffset - 60) + "px";
            tip.css({ 'top': yTip, 'left': xTip });
        }

        /* This function stops the timer and creates the
        fade-in animation                          */
        showTip = function() {
            stopTimer();
            tip.animate({ "top": "+=20px", "opacity": "toggle" }, defaults.speed);
        }
    });
};