/**
 * name:          jquery-foxitip-0.1.js
 * author:        Stefan Benicke - www.opusonline.at
 * version:       0.1
 * last update:   19.08.2009
 * category:      jQuery plugin
 * copyright:     (c) 2009 Stefan Benicke (www.opusonline.at)
 * license:       GNU GPLv3
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * documentation: www.opusonline.at/foxitools/foxitip/
 */
(function($) {
	$.fn.foxitip = function(settings) {

    // global variables
    var window_width;
    var window_height;
    var scroll_top;
    var scroll_left;
    var tip_width;
    var tip_height;
    var rel;
		settings = jQuery.extend({
      title: true,
      url: true,
      offsetX: -305,
      offsetY: -210,
      className: 'foxitip',
			callback: function(){}
		}, settings);

    getSize();
    getScroll();
    $(window).resize(function(){ getSize(); });
		$(window).scroll(function(){ getScroll(); });

    // check tips if plugin has been used
    var counter = 0;
    $('body').find('div[id^="foxitip"]').each(function(){
      counter++;
    });
    
    // create a tip for every link
    $(this).each(function(){
      counter++;
      // if link has no rel
      if(!$(this).attr('rel')){
        var id = 'foxitip'+counter;
        var container = '<div id="'+id+'" class="'+settings.className+'">';
        if(settings.title && $(this).attr('title'))
          container += '<div class="foxitip_title">'+$(this).attr('title')+'</div>';
        if(settings.title && !$(this).attr('title'))
          container += '<div class="foxitip_title">'+$(this).text()+'</div>';
        if(settings.url)
          container += '<div class="foxitip_url">'+$(this).attr('href')+'</div>';
        container += '</div>';

        $('body').append(container);
        $('#'+id).hide()
          .bind('mouseenter', function(){ $(this).show(); })
          .bind('mouseleave', function(){ $(this).hide(); });

        // set the tip-id to the rel of the link
        $(this).attr('rel', '#'+id);
      }

      // entering the link sets global rel and sizes
      // to make mousemove react faster
      $(this).bind('mouseenter', function(event){
        rel = $(this).attr('rel');

        tip_width = $(rel).outerWidth();
        tip_height = $(rel).outerHeight();

        setTip(event.pageX, event.pageY);
				$(rel).show();
        return false;
			})

      .bind('mousemove', function(event){
        setTip(event.pageX, event.pageY);

        // return false to prevent browsertitle
        return false;
			})

      .bind("mouseleave",function(){
        $(rel).hide();
      });
		});

    function setTip(x_coord, y_coord){
      var mouse_x = x_coord+settings.offsetX;
      var mouse_y = y_coord+settings.offsetY;

      // hold the tip inside the window
      if(mouse_x + tip_width > window_width + scroll_left)
        mouse_x = window_width + scroll_left - tip_width;
      if(mouse_y + tip_height > window_height + scroll_top)
        mouse_y = window_height + scroll_top - tip_height;

      $(rel).css({"left":mouse_x, "top":mouse_y});
    }

    function getSize(){
      window_width = $(window).width();
      window_height = $(window).height();
    }

		function getScroll(){
      scroll_top = $(document).scrollTop();
			scroll_left = $(document).scrollLeft();
		}
  }
})(jQuery);


(function($) {
	$.fn.foxitipgran = function(settings) {

    // global variables
    var window_width;
    var window_height;
    var scroll_top;
    var scroll_left;
    var tip_width;
    var tip_height;
    var rel;
		settings = jQuery.extend({
      title: true,
      url: true,
      offsetX: -190,
      offsetY: -270,
      className: 'foxitip',
			callback: function(){}
		}, settings);

    getSize();
    getScroll();
    $(window).resize(function(){ getSize(); });
		$(window).scroll(function(){ getScroll(); });

    // check tips if plugin has been used
    var counter = 0;
    $('body').find('div[id^="foxitip"]').each(function(){
      counter++;
    });
    
    // create a tip for every link
    $(this).each(function(){
      counter++;
      // if link has no rel
      if(!$(this).attr('rel')){
        var id = 'foxitip'+counter;
        var container = '<div id="'+id+'" class="'+settings.className+'">';
        if(settings.title && $(this).attr('title'))
          container += '<div class="foxitip_title">'+$(this).attr('title')+'</div>';
        if(settings.title && !$(this).attr('title'))
          container += '<div class="foxitip_title">'+$(this).text()+'</div>';
        if(settings.url)
          container += '<div class="foxitip_url">'+$(this).attr('href')+'</div>';
        container += '</div>';

        $('body').append(container);
        $('#'+id).hide()
          .bind('mouseenter', function(){ $(this).show(); })
          .bind('mouseleave', function(){ $(this).hide(); });

        // set the tip-id to the rel of the link
        $(this).attr('rel', '#'+id);
      }

      // entering the link sets global rel and sizes
      // to make mousemove react faster
      $(this).bind('mouseenter', function(event){
        rel = $(this).attr('rel');

        tip_width = $(rel).outerWidth();
        tip_height = $(rel).outerHeight();

        setTip(event.pageX, event.pageY);
				$(rel).show();
        return false;
			})

      .bind('mousemove', function(event){
        setTip(event.pageX, event.pageY);

        // return false to prevent browsertitle
        return false;
			})

      .bind("mouseleave",function(){
        $(rel).hide();
      });
		});

    function setTip(x_coord, y_coord){
      var mouse_x = x_coord+settings.offsetX;
      var mouse_y = y_coord+settings.offsetY;

      // hold the tip inside the window
      if(mouse_x + tip_width > window_width + scroll_left)
        mouse_x = window_width + scroll_left - tip_width;
      if(mouse_y + tip_height > window_height + scroll_top)
        mouse_y = window_height + scroll_top - tip_height;

      $(rel).css({"left":mouse_x, "top":mouse_y});
    }

    function getSize(){
      window_width = $(window).width();
      window_height = $(window).height();
    }

		function getScroll(){
      scroll_top = $(document).scrollTop();
			scroll_left = $(document).scrollLeft();
		}
  }
})(jQuery);

