function MusicClass(opts){
    var settings = {
        jplayerId: '#player-block',
        controlsId: '#player-controls'
    };
    this.settings = jQuery.extend(settings, opts);
    this.current_track = null;
    this.current_track_id = null;
    this.track_meta = null;
    this.player = null; //jQuery(this.settings.jplayerId);
    this.is_init = false;
    this.debug = false;
}

MusicClass.prototype = {
    log: function(msg){
        if (!this.debug) return false;

        try{ console.log(msg); }catch(e){}
    },
    init: function(){
        if (this.is_init){
            this.log('Music already inited.');
            return true;
        }
        var self = this;
        this.player = jQuery(this.settings.jplayerId);
        this.player.jPlayer({
            swfPath:STATIC_URL+'js/jquery_plugins/jplayer',
            volume:1,
            muted:false,
            solution:'html,flash',
            supplied:'mp3',
            errorAlerts:false,
            wmode:"window"
        });
        this.player.bind(jQuery.jPlayer.event.play, function(){
            jQuery(this).jPlayer("pauseOthers");
        });
        this.player.bind(jQuery.jPlayer.event.ended, function(){
            self.play_next_or_stop();
        });
        this.log('Music was initialized.');
        this.is_init = true;
    },
    click: function(sender){
        Controller.load_page(jQuery(sender).attr('href').substr(1));

        return false;
    },
    download: function(sender){
        this.send_file(jQuery(sender).attr('href').substr(2));

        return false;
    },
    send_file: function(url){
        var _iframe_id = 'xxlife-music-download-iframe';
        var _iframe = jQuery('#'+_iframe_id);
        if (!_iframe.length){
            _iframe = jQuery('<iframe id="'+_iframe_id+'" style="display:none;"></iframe>');
            jQuery('body').append(_iframe);
        }

        _iframe.attr('src', url);
    },

    getTrack: function(sender){
        this.current_track = jQuery(sender).closest('.track');
        //console.log(jQuery(sender).attr('data-json'));
        this.track_meta = jQuery.parseJSON(jQuery(sender).attr('data-json'));
        //console.log(this.track_meta);
    },
    play_next_or_stop: function(event){
        var _class = 'tr.track'
        var next = this.current_track.next(_class);
        if (next.length){
            this.current_track = next;
            this.track_meta = jQuery.parseJSON(jQuery('span.track__play', this.current_track).attr('data-json'));
            this._play();
            return;
        }

        this.current_track.removeClass('active');
        jQuery(this.settings.controlsId).find('.play-button').removeClass('pause-button');
        this.current_track = null;
        this.track_meta = null;
        this.current_track_id = null;
    },
    play: function(sender){
        if (!this.is_init){
            this.init();
        }
        if (jQuery(sender).closest('.track').hasClass('active')){
            this.stop(sender);
            jQuery(this.settings.controlsId).find('.play-button').removeClass('pause-button');
        } else {
            this.getTrack(sender);

            this._play();
        }

        return false;
    },
    init_player: function(){
        var pl = jQuery(this.settings.controlsId);
        jQuery('.jp-title>span', pl).html(this.track_meta.title);
        jQuery('.jp-album>a', pl).attr('href', '#'+this.track_meta.album.url).html(this.track_meta.album.title);
        jQuery('.play-button', pl).addClass('pause-button');
        pl.show();
    },
    player_play: function(sender){
        if (jQuery(sender).hasClass('pause-button')){
            // stop playing
            jQuery(sender).removeClass('pause-button');
            this.pause(sender);
        } else {
            // start play
            jQuery(sender).addClass('pause-button');
            //this.current_track.addClass('active');
            this.active_current_track();
            this.player.jPlayer('play');

        }

        return false;
    },
    is_playing: function(){
        var pl = jQuery(this.settings.controlsId);
        if (jQuery('.play-button', pl).hasClass('pause-button'))
            return true;
        else
            return false;
    },
    album_page: function(sender){
        var self = this;
        Controller.load_page(jQuery(sender).attr('href').substr(1), null, function(){
            self.active_current_track();
        });

        return false;
    },
    active_current_track: function(){
        if (this.track_meta){
            var track_id = '#track-'+this.track_meta.id+'-album-'+this.track_meta.album.id;

            this.current_track = jQuery(track_id);
            if (this.is_playing()){
                this.current_track.addClass('active');
            }
        }
    },
    _play_test: function(){
        //http://www.jplayer.org/audio/m4a/Miaow-07-Bubble.m4a
        jQuery('.track.active').removeClass('active');
        this.current_track.addClass('active');
        this.player.jPlayer('setMedia', {mp3: 'http://www.jplayer.org/audio/mp3/Miaow-03-Lentement.mp3'});
        this.player.jPlayer('play');
    },
    _play: function(){
        var self = this;

        jQuery('.track.active').removeClass('active');
        this.current_track.addClass('active');
        if (this.current_track_id != this.getTrackId()){
            this.current_track_id = this.getTrackId();
            this.player.jPlayer('setMedia', {
                'mp3':self.getTrackFile(),
                'name':self.getTrackName()
                });
            //console.log(this.getTrackFile());
            //console.log(this.getTrackId());
            //console.log(this.getTrackName());
        }

        this.init_player();

        this.player.jPlayer('play');
    },
    stop: function(sender){
        this.current_track.removeClass('active');
        this.player.jPlayer('stop');
    },
    pause: function(sender){
        this.current_track.removeClass('active');
        this.player.jPlayer('pause');
    },
    getTrackName: function(){
        return this.track_meta.title;
    },
    getTrackFile: function(){
        //return 'http://www.jplayer.org/audio/mp3/Miaow-03-Lentement.mp3';
        return this.track_meta.url;
    },
    getTrackId: function(){
        return this.track_meta.id;
    }
};
var Music = new MusicClass();

