﻿/*!
 * HTML 5 Buttons
 *
 * Copyright 2011, http://webworld-develop.blogspot.com/
 * Artistic License 2.0
 * http://www.opensource.org/licenses/artistic-license-2.0
 *
 * Date: 5/10/2011
**/

(function ($) {
    var setting = 
    {
        //styles
        animationSpeed: "fast",
        animationSize: 15,
        opacity: { normal:0.9, hover: 1, disabled: 0.2 },
        
        //callback
        over: function(){},
        out: function(){},
        click: function() {}
    };

    var _innerData = 
    {
        defTop: 0,
        defLeft: 0,
        boxShadow: 0
    };

    var methods = {
        init : function(options) 
        {
            return $(this).each(function()
            {
                if (options) { $.extend(setting, options); }

                var $t = $(this);

                _innerData.defTop = parseInt($t.css("top"));
                _innerData.defLeft = parseInt($t.css("left"));
                
                if (isNaN(_innerData.defTop)) 
                { 
                    _innerData.defTop = 0; 
                }
                
                if (isNaN(_innerData.defLeft)) 
                { 
                    _innerData.defLeft = 0; 
                }

                $t.css({ opacity: setting.opacity.normal })
                    .hover(
                        function() 
                        {
                            if (!$t.attr("disabled") && !$t.hasClass("active"))
                            {
                                $t.stop().animate({ opacity: setting.opacity.hover, top: -setting.animationSize }, setting.animationSpeed,
                                function()
                                {
                                    $t.animate({ top: parseInt(setting.animationSize/2) - setting.animationSize }, 300, setting.over);
                                });
                            }
                        },
                        function()
                        {
                            if (!$t.attr("disabled") && !$t.hasClass("active"))
                            {
                                $t.stop().animate({ opacity: setting.opacity.normal, top: _innerData.defTop, left: _innerData.defLeft }, setting.animationSpeed, setting.out);
                            }
                        });

                $t.click(function()
                {
                    if ($t.hasClass("active"))
                    {
                        return false;
                    }

                    setting.click();
                });
                
                if ($t.attr("disabled"))
                {
                    $t.wwbutton('toggle', false);
                }

                if ($t.hasClass("active"))
                {
                    $t.css({ top: parseInt(setting.animationSize/2) - setting.animationSize });
                }
            });  
        },

        toggle : function(enable) 
        {
            if (!enable)
            {
                $(this).animate({ top: _innerData.defTop, left: _innerData.defLeft, opacity: setting.opacity.disabled }, "fast").attr("disabled", true);
            }
            else
            {
                $(this).animate({ opacity: setting.opacity.normal }, "fast").removeAttr("disabled");
            }  
        }
    };

    $.fn.wwbutton = function( method ) {    
        if ( methods[method] ) { return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )); } 
        else if ( typeof method === 'object' || ! method ) { return methods.init.apply( this, arguments ); } 
        else { $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' ); }     
    };
})(jQuery);
