debug = function(s) {

    if(document.domain != 'everlast.local')
        return;

    if($('#debug').length < 1)
        $('body').prepend('<div id="debug"></div>');

    $('#debug').prepend('<div>' + s + '</div>');
}

MyStage = new (function() {

    this.master = null;
    this.volume = 50;
    this.mute = false;
    this.onReady = function() {};
    this.onPlay = function() {};
    this.onStop = function() {};
    this.onBuffer = function() {};
    this.onID3 = function() {};

    this.embed = function(s) {

        var flashvars = { };
        var params = {wmode: 'transparent'};
        var attributes = { };

        swfobject.embedSWF("/mystage/swf/MyStage.swf", s, "1", "1", "9.0.0","expressInstall.swf", flashvars, params, attributes);
    }

    this.initialize = function(m) {

        this.master = m;

        this.setVolume(this.volume);
    }

    this.playChannel = function(id) {

        this.master.playChannel(id);
    }

    this.play = function() {

        this.master.play();
    }

    this.stop = function(id) {

        this.master.stop();
    }

    this.setVolume = function(i) {

        this.volume = i;

        this.master.setVolume(i);
    }

    this.setMute = function(b) {

        this.mute = b;

        this.master.setVolume(b ? 0 : this.volume);
    }

    this.isPlaying = function() {

        return this.master.getIsPlaying();
    }

    this.getChannelId = function() {

        return this.master.getChannelId();
    }
});

function initMystage() {

    MyStage.onPlay = function(ch) {

        if(MyStage.getChannelId() == null) {

            alert('Choose a channel!');

            return false;
        }

        $('#controller div.status').html('Playing channel: ' + ch);
        $('#controller a.play').addClass('on');
        $('#controller a.play').removeClass('off');
        $('#controller a.mute').addClass('off');
        $('#controller a.mute').removeClass('on');
        $('#controller div.channels a.channel.active').removeClass('active');
        $('#controller div.channels a.channel[name="'+ ch +'"]').addClass('active');

        MyStage.setMute(false);
    }

    MyStage.onStop = function() {

        $('#controller div.status').html('Stopped.');
        $('#controller a.play').addClass('off');
        $('#controller a.play').removeClass('on');
    }

    MyStage.onBuffer = function() {

        $('#controller div.status').html('Buffering...');
    }

    MyStage.onID3 = function(id3) {

        //$('#controller div.status').html(id3);
    }

    MyStage.onReady = function() {

        var m;

        if (navigator.appName.indexOf ("Microsoft") !=-1) {
            m = window['MyStagePlayer'];
        } else {
            m = document['MyStagePlayer'];
        }
        
        //m = document.getElementById('MyStagePlayer')

        if(m) {
            
            MyStage.initialize(m);

            $('#controller div.channels a.channel').bind('click', function(event) {

                MyStage.playChannel($(this).attr('name'));

                $('#welcome_msg').html('&nbsp;');

                return false;
            });

            $('#controller a.play').bind('click', function(event) {

                if(MyStage.isPlaying()) {

                    MyStage.stop();

                } else {

                    MyStage.play();
                }

                return false;
            });

            $('#controller a.mute').bind('click', function(event) {

                if(MyStage.mute) {

                    MyStage.setMute(false);
                    $(this).addClass('off');
                    $(this).removeClass('on');
                    $('#controller div.volume div.slider').slider('value', MyStage.volume);

                } else {

                    MyStage.setMute(true);
                    $(this).addClass('on');
                    $(this).removeClass('off');
                    $('#controller div.volume div.slider').slider('value', 0);
                }

                //event.preventDefault();

                return false;
            });

            var slideCh = function(event, ui) {

                if(MyStage.mute)
                    return;

                MyStage.setVolume($(this).slider('value'));
            };

            $('#controller div.volume div.slider').slider({
                orientation: "vertical",
                value: MyStage.volume,
                slide: slideCh,
                change: slideCh
            });

            $('#controller div.status').html('Ready - Choose a channel!');

            //MyStage.playChannel('mix');
        }
    };

    MyStage.embed("MyStagePlayer");


    /* MyStage ui */

    $('#mystage div.closed').click(function() {

        $(this).hide();
        $(this).parent().find('div.opened').show();
    });

    $('#mystage a.close').click(function() {

        $('#mystage div.opened').hide();
        $('#mystage div.closed').show();
    });
}

$(document).ready(function() {

    initMystage();
});

