﻿/**
* ------------------------------------------------------------------------------------------------
* @author 	Diego Tres me@diegotres.com | Ivo Rafael ivo.rafael@gmail.com
* @version 	0.1
* ------------------------------------------------------------------------------------------------
*/
(function($) {
    $.fn.neoHover = function(options) {
        var defaults = {
            init: true,
            hoverElement: 'span',
            queue: false,
            duration: 200,
            opacity: true,
            easing: 'linear',
            initWidth: null,
            initHeight: null,
            maxWidth: null,
            maxHeight: null,
            inverted: false,
            validSubOL: false
        };

        var options = $.extend(defaults, options);

        return this.each(function() {
            var $this = $(this);

            $this.fadeTo(0, 1);

            var elWidth = $this.width();
            var elHeight = $this.height();
            var elBg = $this.css('background-image');
            
            var elPosBg = '0px ' + (-elHeight) + 'px';

            if (options.inverted) {
                $this.css('background-position', elPosBg);
                elPosBg = '0 0';
            }

            //var nocache = '?nocache=' + Math.random()
            //elBg = elBg.split(')')[0] + nocache + ')';

            if ($(options.hoverElement, $this).html() == null || $(options.hoverElement, $this).html() == undefined || $(options.hoverElement, $this).html() == '') {
                $this.html('<' + options.hoverElement + '>' + $this.html() + '</' + options.hoverElement + '>');
            }

            $(options.hoverElement, this)
				.fadeTo(0, 0)
				.height(elHeight)
				.width(elWidth)
				.css({
				    'float': 'left',
				    'display': 'block',
				    'margin': '0',
				    'padding': '0',
				    'width': options.initWidth ? options.initWidth : elWidth,
				    'height': options.initHeight ? options.initHeight : elHeight,
				    'background-image': elBg,
				    'background-position': elPosBg,
				    'cursor': 'pointer'
				});

            if (options.init) {
                $(options.hoverElement, this).hover(function() {
                    $(this).animate({
                        opacity: options.opacity ? 1 : 0,
                        width: options.maxWidth ? options.maxWidth : elWidth,
                        height: options.maxHeight ? options.maxHeight : elHeight
                    }, {
                        queue: options.queue,
                        duration: options.duration,
                        easing: options.easing,
                        complete: options.complete
                    });
                }, function() {
                    $(this).animate({
                        opacity: options.opacity ? 0 : 1,
                        width: options.initWidth ? options.initWidth : elWidth,
                        height: options.initHeight ? options.initHeight : elHeight
                    }, {
                        queue: options.queue,
                        duration: options.duration,
                        easing: options.easing,
                        complete: options.complete
                    });
                });
            }

        });
    },

	$.fn.neoHoverText = function(options) {

	    var defaults = {
	        hoverElement: 'span',
	        queue: false,
	        duration: 200,
	        opacity: 0,
	        position: "absolute",
	        top: 0,
	        left: 0
	    };

	    $.each(this, function(i, n) {
	        if ($(n).parent().parent().attr('tagName') == "UL") {
	            $(n).append('<' + defaults.hoverElement + '>' + $(n).text() + '</' + defaults.hoverElement + '>');
	            $(n).find("span").css({
	                opacity: defaults.opacity,
	                position: defaults.position,
	                top: defaults.top,
	                left: defaults.left
	            });
	        }
	    });

	    $(this).find("span").mouseenter(function() {
	        $(this).animate({
	            opacity: 1
	        }, {
	            duration: defaults.duration,
	            queue: defaults.queue
	        });
	    }).mouseleave(function() {
	        $(this).animate({
	            opacity: defaults.opacity
	        }, {
	            duration: defaults.duration,
	            queue: defaults.queue
	        });
	    });

	};

	$.fn.neoHoverText2 = function(options) {

	    var defaults = {
	        hoverElement: 'span',
	        queue: false,
	        duration: 200,
	        opacity: 0,
	        position: "absolute",
	        top: 0,
	        left: 0
	    };

	    $.each(this, function(i, n) {
            $(n).append('<' + defaults.hoverElement + '>' + $(n).text() + '</' + defaults.hoverElement + '>');
            $(n).find("span").css({
                opacity: defaults.opacity,
                position: defaults.position,
                top: defaults.top,
                left: defaults.left
            });
	    });

	    $(this).find("span").mouseenter(function() {
	        $(this).animate({
	            opacity: 1
	        }, {
	            duration: defaults.duration,
	            queue: defaults.queue
	        });
	    }).mouseleave(function() {
	        $(this).animate({
	            opacity: defaults.opacity
	        }, {
	            duration: defaults.duration,
	            queue: defaults.queue
	        });
	    });

	};
})(jQuery); 
