(function( $ )
{
$.fn.channel_selector = function( method )
{
    var methods =
    {
        init: function( options )
        {

            var options = $.extend(
            {
                animationDelay: 30,
                animationClsPrefix: "channel-btn-push",
                animationReleasedIndex: 1,
                animationPressedIndex: 4,
                animationPressedIndex: 4,
                
/*        
                pressSound: "./snd/vkl.mp3",
                releaseSound: "./snd/vykl.mp3",
                switchSound: "./snd/vkl+vykl.mp3",

                jPlayerSwfPath: "./jquery",
*/
                
            }, options);

            return this.each(function()
            {
                var self = this;
/*
                var playerDiv = document.createElement('div');

                $("body").append(playerDiv);
                $(playerDiv).css({ position: 'absolute', width: 0, height: 0, left: "-200px", top: "-200px", overflow: 'hidden'});

                var pressDiv = document.createElement('div');
                var releaseDiv = document.createElement('div');
                var switchDiv = document.createElement('div');

                $(playerDiv).append(pressDiv);
                $(playerDiv).append(releaseDiv);
                $(playerDiv).append(switchDiv);

                $(playerDiv).children().jPlayer(
                {
                    solution: "html",
                    supplied: "mp3",
                    swfPath: options.jPlayerSwfPath
                });

                $(pressDiv).jPlayer("setMedia", { mp3: options.pressSound }).jPlayer('load');
                $(releaseDiv).jPlayer("setMedia", { mp3: options.releaseSound }).jPlayer('load');
                $(switchDiv).jPlayer("setMedia", { mp3: options.switchSound }).jPlayer('load');
*/
                var animateBtn = function ( btn, dir, onComplete )
                {
                    var state = $(btn).data('state');

                    if ((state == options.animationReleasedIndex && !dir) || (state == options.animationPressedIndex && dir))
                    {
                        $(btn).data('animation', false);
                        if (onComplete)
                            onComplete(btn);
                    }
                    else
                    {
                        $(btn).data('animation', true);
                        
                        $(btn).removeClass(options.animationClsPrefix + state);
                        state += dir ? 1 : -1;
                        $(btn).addClass(options.animationClsPrefix + state);
                        $(btn).data('state', state);
                        var f = function() { animateBtn(btn, dir, onComplete); };
                        setTimeout(f, options.animationDelay);
                    }
                }

                var isBtnPressed = function ( btn ) { return $(btn).data('state') == options.animationPressedIndex; }
                var isBtnReleased = function ( btn ) { return $(btn).data('state') == options.animationReleasedIndexIndex; }

                var onBtnClick = function ( e )
                {
                    e.preventDefault();

                    if ($(self).data('disable_btn'))
                        return;
                    
                    if ($(this).data('animation'))
                        return;
                    
                    if (isBtnPressed(this))
                    {
                        animateBtn(this, false);
                        $(self).data('selected', null);
                        //$(releaseDiv).jPlayer('play', 0);
                    }
                    else
                    {
                        var selected = $(self).data('selected');
                        if (selected)
                        {
                            //$(switchDiv).jPlayer('play', 0);
                            animateBtn(selected, false);
                        }
                        else
                        {
                            //$(pressDiv).jPlayer('play', 0);
                        }
                        animateBtn(this, true);
                        $(self).data('selected', this);
                    }

                    if (options.onSelect)
                        options.onSelect.call(self, $(self).data('selected') ? $(this).data('chID') : null);
                }

                $(this).bind('mousedown', function( e ) { e.preventDefault(); });

                for (var n = 0; n < options.buttonsNumber; n++)
                {
                    var div = document.createElement('div');
                    $(this).append(div);

                    var iconDiv = document.createElement('p');
                    var btnDiv = document.createElement('div');

                    var chID = options.channels[n];

                    $(div).css("background", "url(\"" + Channels[chID].Icon + "\") no-repeat center top");

                    $(div).append(iconDiv);
                    $(div).append(btnDiv);

                    $(btnDiv).data('chID', chID);
                    $(btnDiv).data('state', options.animationReleasedIndex);
                    $(btnDiv).addClass(options.animationClsPrefix + options.animationReleasedIndex);
                    $(btnDiv).click(onBtnClick);

                    $(iconDiv).droppable(
                    {
                        accept: '.channel-drag',
                        hoverClass: 'channel_selector_hover',
                        activeClass: 'channel_selector_highlight',
                        drop: function( event, ui )
                        {
                            var chID = $(ui.draggable).text();
                            $(this).parent().children('div').data('chID', chID);
                            $(this).parent().css("background", "url(\"" + Channels[chID].Icon + "\") no-repeat center top");

                            var channels = '';
                            
                            $(this).parent().parent().children('div').each(function(index, el)
                            {
                                channels += $(el).children('div').data('chID') + ',';
                            });

                            channels = channels.substring(0, channels.length - 1);
                            
                            $.ajax({ url: './save_channels.php', data: { channels: channels } });
                        }
                    });
                }
            });
        },

        selectNoChannel: function()
        {
            return this.each(function()
            {
                if ($(this).data('selected'))
                    $($(this).data('selected')).trigger('click');
            });
        },

        disableButtons: function()
        {
            return this.each(function()
            {
                $(this).data('disable_btn', true);
                $(this).find('div div').addClass('inactive');
            });
        },
 
        enableButtons: function()
        {
            return this.each(function()
            {
                $(this).data('disable_btn', false);
                $(this).find('div div').removeClass('inactive');
            });
        }
 
    };

    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' );
};
})( $ );

