/*

	jQuery tip plugin
	Copyright (c) 2010 Gael Jaunin

	For support and tutorials visit
	http://www.xuserver.net/

	License: GNU Lesser General Public License (LGPL) 
	at http://opensource.org/licenses/lgpl-2.1.php

	This plugin is free software;  you can redistribute it  and/or  modify  it 
	under the terms of the GNU Lesser General Public License as  published  by 
	the Free Software Foundation;  either version 2.1 of the License,  or  (at 
	your option) any later version.
	This software is distributed in the hope  that  it  will  be  useful,  but 
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
	or FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU Lesser  General  Public 
	License for more details.
	You should have received a copy of  the  GNU Lesser General Public License 
	along with this library;  if not,  write to the  Free Software Foundation, 
	Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

	THIS COMMENT AND COPYRIGHT NOTICE MUST BE RETAINED IN THE CODE AS IS FOR LEGAL USE

*/




(function($){
  $.fn.extend({
    jxTip:function(options){
      var defaults = {
				content:'',
				url:'',
				preload:false,
				event:'mousemove', 
        width:250,
        maxheight:250,
        delay:500,
        icon:true
			}; 
			var moveWithMouse = function(tip, e, offset){
        pos = tip.position();
        if(offset==null)offset=20;
        initPos(tip, e, offset);        
      };
      var initPos = function(tip, e, offset){
        if(offset==null)offset=20;
        if(e.pageX+offset+tip.width()>$(window).width()){
          tip.css("left", e.pageX-tip.width() + 'px');  
        }else{
          tip.css("left", e.pageX+offset + 'px');
        };
        if(e.pageY+offset+tip.height()>$(window).height()){
          tip.css("top", e.pageY-tip.height() + 'px');  
        }else{
          tip.css("top", e.pageY+offset + 'px');
        };                
      };
      var setContent = function(o,t){
        //var head = '<b>help tip</b><br/>';
        if(o.attr('title') !=''){head = '<b>'+o.attr('title')+'</b><br/>';}
        if(typeof  options.content == 'object'){
          //txt = options.content.html();
          options.content.show();
          t.append(options.content);
          o.tip = options.content.parent();                     
        }else{
          txt = options.content;
          t.append('<div>'+txt+'</div>');
          o.tip = t;
        };
        
      };
      var options =  $.extend(defaults, options);
		  /* widget loop */
      return this.each(function() {
		    
        var $obj = $(this); 		  
        
        var head = '<b>help tip</b><br/>';
        var $tipc = $('<div style="width:'+options.width+'px;max-height:'+options.maxheight+'px;" class=""  style="padding:5px;"></div>');        
        var $tip = $('<div class="ui-widget ui-state-highlight ui-corner-all" style="z-index:99999;padding:5px;overflow:hidden;position:absolute;display:none"></div>');
        $tip.append($tipc);		          
        if ( options.url =='' && options.content =='' &&  $obj.attr('url')==null && $obj.attr('title')=='' && $obj.attr('content')==null ){
          /* no content defined : no tip to display */          
        }else if ($obj.attr('content') !=null && $('#'+$obj.attr('content')).html() =="" ){
            /* no content */          
        }else if ($obj.attr('jxTip') !=null){
            /* tip already defined on this element */          
        }else{
          /* defining tip */
          $obj.attr('jxTip' , 'ok');            
          if ($obj.attr('url')!=null ){ 
            $obj.click(function(e) {
              $.ajax({
                url: $obj.attr('url'),
                cache: false,
                success: function(html){
                  $tipc.html(html);
                  initPos ( $tip, e );            
                }
              });
              $tip.show();
            });
            $tipc.html('click to load tip');
            //$tip.click(function() {$tip.fadeOut();});
            
          }else if ($obj.attr('content')!=null ){ 
            if($('#'+$obj.attr('content')).html() == null ){
              //options.content = 'specifyed id does not exist ('+$obj.attr('content')+')';
              options.content = '';
              setContent($obj,$tipc);
              $tip= $('');
            }else{
              options.content = $('#'+$obj.attr('content')).html();
              setContent($obj,$tipc);
            };
          }else if (options.url!='' ){ 
            if(options.preload){
              $.ajax({
                url: options.url,
                cache: true,
                error: function(){
                  $tip= $('');
                },
                success: function(html){
                  $tipc.html(html);            
                }
              });
            }else{
              $obj.click(function(e) {
                $.ajax({
                  url: options.url,
                  cache: true,
                  error: function(){
                    $tip= $('');
                  },
                  success: function(html){
                    $tipc.html(html);            
                  }
                });
                $tip.show();
              });
              tt = '';
              if($obj.attr('title') != ''){tt = $obj.attr('title')+'<br/>';}
              if(options.title != ''){tt += options.title+'<br/>';}              
              $tipc.html(tt + ' click to load tip');
            };
          }else{
            setContent($obj, $tipc);
          };
          $obj.attr('title','');
          
          if(options.icon || $obj.attr('icon') !=null ){
            var $jxIcon = $('<span style="cursor:pointer;margin:0px;padding:0px;" class="jx-inline ui-icon ui-icon-comment" />');
            $obj.append($jxIcon);      
            $jxIcon.click(function() {$tip.toggle();});
          };
          
          $('body').append($tip);                            
          $obj.css('cursor','pointer');
          if($(window).width()<500 ){        
            options.event = "handheld";        
          }else{
            $obj.bind('mousemove',function(e){moveWithMouse($tip, e);});
          };
          
          if(options.event == 'mousemove'){
            $obj.mouseover(function() {$tip.show();}).mouseout(function() {$tip.hide();});
            //$obj.click(function() {$tip.toggle();});
            $tip.click(function() {$tip.fadeOut();});
          }else if(options.event == 'handheld'){
            $obj.css('text-decoration','underline');
            $tip.css('left','15px');
            $obj.click(function(e) {
              $tip.css('top',  e.pageY - $tip.height()-5  +'px');
              $tip.width($(window).width()-30);
              $tip.toggle();
            });
            $tip.click(function() {$tip.fadeOut();});
          }else if(options.event == 'click'){
            //$obj.attr('title','click to show tip');
            if ($obj.attr('url')!=null ){
              $obj.mouseover(function() {$tip.show();}).mouseout(function() {$tip.hide();});
              //$obj.click(function() {$tip.toggle();});            
            }else{
              $obj.click(function() {$tip.toggle();});            
            }
            
            $tip.click(function() {$tip.fadeOut();});
            $obj.dblclick(function() {$tip.fadeOut();});
          };      
        }; 
      }); 
    }
	}); 
})(jQuery);

