/*
 * ZeeLinify - jQuery Plugin
 * draws Z-Line from a root object
 *
 *
 * Copyright (c) 2010 Rob Berwick
 *
 * Version: 0.1.0 (28/11/2010)
 * Requires: jQuery v1.3+
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */

;(function($){  
  
    $.fn.extend({   
          
        zeelinify: function(options) {  
        
        	//Settings list and the default values  
            var defaults = {  
                targetSelector: '',  
                lineColor: '#9999ff',
                removeHoverStyle: false 
            };  
              
            var options = $.extend(defaults, options);
            
            var $rootObj = $(this);
            
            var resetCanvas=function()
			{
            	$rootObj.removeClass('hover');
            	ctx.clearRect(0,0,$(document).width(),$(document).height());
				ctx.save();
			}
			
			var clearline=function()
			{
				resetCanvas();
				
			}
			
			var drawline = function($startObj, $endObj)
			{
				$rootObj.addClass('hover');
				
				ctx.beginPath();
				ctx.lineWidth = "0.7";

				ctx.moveTo($startObj.offset().left + $startObj.width(),$startObj.offset().top + $startObj.height())
				ctx.lineTo($endObj.offset().left, $endObj.offset().top);
				ctx.stroke();
				ctx.closePath();
			
			}
            
            if (this.length > 0)
            {
            //create a canvas tag
            var canvas = $('<canvas>').appendTo('body');
            canvas.attr({"class": "zeeline-canvas", "width":$(document).width(), 'height':$(document).height()});
            canvas.css({'position':'absolute', 'top':0, 'left':0, 'z-index': -1 });
            
            var ctx = canvas[0].getContext("2d");
			ctx.strokeStyle = options.lineColor;
			
			}
            
            //Iterate over the current set of matched elements  
            return this.each(function() {  
              
                //code to be inserted here
                var o =options;  
				var obj = $(this);
				var items = $(o.targetSelector).add(obj);
                
                items.hover( 
                function(){
                	drawline( obj, $(this));
				},
				function(){
					resetCanvas();
				})
				
				//remove hover style if required
				if (o.removeHoverStyle){
					items.hover( function () {
						$(this).css({"background-color":"inherit", "text-decoration":"inherit"});
						}
					);
				}
				
                
                
              
            });  
        }  
    });  
      
        
})(jQuery);
