﻿(function ($) {

    $.fn.applyroundedcorners = function (element, value) {
        switch (value) {
            case 'all': element.addClass('viblend-rc-all');
                break;
            case 'top': element.addClass('viblend-rc-t');
                break;
            case 'bottom': element.addClass('viblend-rc-b');
                break;
            case 'left': element.addClass('viblend-rc-l');
                break;
            case 'right': element.addClass('viblend-rc-r');
                break;
            case 'top-right': element.addClass('viblend-rc-tr');
                break;
            case 'top-left': element.addClass('viblend-rc-tl');
                break;
            case 'bottom-right': element.addClass('viblend-rc-br');
                break;
            case 'bottom-left': element.addClass('viblend-rc-br');
                break;
        }
    }

    $.fn.viblendbutton = function (settings) {
        var s = $.extend({}, $.fn.viblendbutton.defaults, settings);
        $(this).each(function (i) {
            this.onselectstart = function () { return false; };

            var vars = {};
            var element = $(this)[0];
            var host = $(this);
            $.data(element, 'vButton', vars);
            vars.host = host;

            height = s.height ? s.height : $(this).height();
            width = s.width ? s.width : $(this).width();

            var theme = s.theme ? '-' + s.theme + '-' : '-';
            vars.theme = theme;

            $(this).addClass('viblend' + theme + 'button');

            if (s.cursor)
                $(this).css({ cursor: s.cursor });

            if (s.enableAnimations)
                vars.enableAnimations = true;

            $.fn.applyroundedcorners($(this), s.roundedCorners);

            var clNormal = 'viblend' + theme + 'control-state-normal';
            var clHover = 'viblend' + theme + 'control-state-hover';
            var clSelected = 'viblend' + theme + 'control-state-selected';
            var clPressed = 'viblend' + theme + 'control-state-pressed';

            // save normal background color
            $(this).addClass(clNormal);
            vars.bckgColorNormal = $(this).css('background-color');
            $(this).removeClass(clNormal);

            // save hover background color
            $(this).addClass(clHover);
            vars.bckgColorHover = $(this).css('background-color');
            $(this).removeClass(clHover);

            // save selected background color
            $(this).addClass(clSelected);
            vars.bckgColorSelected = $(this).css('background-color');
            $(this).removeClass(clSelected);

            // save pressed background color
            $(this).addClass(clPressed);
            vars.bckgColorPressed = $(this).css('background-color');
            $(this).removeClass(clPressed);

            $(this).addClass(clNormal);

            $(this).bind('mouseenter', function (event) {
                $.fn.viblendbutton.toggleHover(event, $(this), true);
            });
            $(this).bind('mouseleave', function (event) {
                $.fn.viblendbutton.toggleHover(event, $(this), false);
            });

            $(document).bind('mousemove', function (event) {
                var offset = host.offset();
                if (offset == null)
                    return;

                if (!vars.animStart)
                    return;

                var now = new Date();
                if (now.getTime() - vars.animStart < 400)
                    return;

                if (event.clientX > offset.left && event.clientX < offset.left + host.width() &&
                    event.clientY > offset.top && event.clientY < offset.top + host.height()
                ) {
                    host.css({ backgroundColor: vars.bckgColorHover });
                }
                else {
                    host.css({ backgroundColor: vars.bckgColorNormal });
                }

                vars.animStart = false;
            });

        });
    }

    $.extend($.fn.viblendbutton,
    {
        toggleHover: function (event, element, isMouseEnter) {
            var vars = $.data(element[0], 'vButton');
            vars.isMouseOver = isMouseEnter;

            if (vars.enableAnimations) {
                element.stop();
                vars.animStart = new Date().getTime();
            }
            if (vars.isMouseDown)
                return;

            if (isMouseEnter) {
                if (vars.enableAnimations)
                    element.animate({ backgroundColor: vars.bckgColorHover }, 400);
                else {
                    element.removeClass('viblend' + vars.theme + 'control-state-normal');
                    element.addClass('viblend' + vars.theme + 'control-state-hover');
                }

            }
            else {
                if (vars.enableAnimations)
                    element.animate({ backgroundColor: vars.bckgColorNormal }, 400);
                else {
                    element.removeClass('viblend' + vars.theme + 'control-state-hover');
                    element.addClass('viblend' + vars.theme + 'control-state-normal');
                }
            }
        }
    });

    //// LinkButton
    $.fn.viblendlinkbutton = function (settings) {
        var s = $.extend({}, $.fn.viblendlinkbutton.defaults, settings);
        $(this).each(function (i) {
            this.onselectstart = function () { return false; };

            var vars = {};
            var element = $(this)[0];
            var host = $(this);
            $.data(element, 'vLinkButton', vars);

            height = s.height ? s.height : $(this).height();
            width = s.width ? s.width : $(this).width();

            vars.href = $(this).attr('href');
            vars.content = $(this).html();
            vars.host = host;

            $(this).wrapInner("<table id='vLinkButtonWrapper' text-align:'center'><tr><td align='center'></td></tr></table>");
            $(this).addClass('viblend-link');

            var wrapElement = $(this).find('#vLinkButtonWrapper');
            wrapElement.css({ width: width });
            wrapElement.css({ height: height });
            wrapElement.viblendbutton(s);
            wrapElement.bind('click', function () {
                window.location = vars.href;
            });
        });
    }

    //// End of LinkButton



})(jQuery);
