var audioplayer = {
    current: null,
    last: null,
    id: ''
}
 
function contentloaded() {
    audioplayer.current.contentloaded();
}

function stopcontent() {
    audioplayer.current.stopcontent();
    audioplayer.last = null;
    audioplayer.id = '';
}

function AudioPlayer(current, options) {

    this.current = current;

    this.css = options.css;

    this.flash = null;

    this.run = function() {
        if (this.current.href) {

            var details = this.parseDetails(this.current.href);
            if (details.length == 2) {
                var current_id = this.fetchId(details);
                if (current_id == audioplayer.id) {
                    this.stopcontent();
                } else {
                    this.current.firstChild.className = this.css + '-load';
                    this.playContent(details[0], details[1]);
                }

                if (audioplayer.last) {
                    audioplayer.last.firstChild.className = this.css;
                }

                if (current_id != audioplayer.id) {
                    audioplayer.id = current_id;
                    audioplayer.last = this.current;
                } else {
                    audioplayer.last = null;
                    audioplayer.id = '';
                }
            }
        }
    }

    this.getCurrentTime = function() {
        var current = new Date();
        var secs = current.getSeconds();
        var mins = current.getMinutes();
        var hours = current.getHours();
        var day = current.getDay();
        var date = current.getDate();
        var month = current.getMonth();
        var year = current.getFullYear();
         
        return secs + '' + mins + '' + hours + '' + day + '' + date + '' + month + '' + year; 
    }

    this.parseDetails = function(detail) {
        var details = detail.split('#');
        if (details.length == 2) {
            var detail = details[1];
            var details = detail.split('-');
            
            return details;
        }

        return [];
    }

    this.fetchId = function(details) {
        return details[0] + '-' + details[1];
    }

    this.playContent = function(service_id, content_id) {
        var pc = this.getCurrentTime()
        var att = { data: '/audioplayer.swf', width: '0', height: '0' };
        var par = { flashvars: 'service=' + service_id + '&file=' + content_id + '&playit=1&pc=' + pc };
        this.flash = swfobject.createSWF(att, par, 'flashcontent');
    }

    this.contentloaded = function() {
        if (this.current.firstChild) {
            this.current.firstChild.className = this.css + '-on';
        }   
    }

    this.stopcontent = function() {
        if (this.current.firstChild) {
            this.current.firstChild.className = this.css;

            this.flash = null;
            swfobject.removeSWF('flashcontent');
            $('body').append('<div id="flashcontent"></div>');
        }   
    }

}

(function($){

    $.fn.audioplayer = function (options) {

        var defaults = {
            css: 'audioplayer',
            auto: false
        }

        options = $.extend(defaults, options)

        if (swfobject.getFlashPlayerVersion().major >= 7) {

            if (options['auto'] == true) {
                if (this.length == 1) {
                    audioplayer.current = new AudioPlayer(this[0], options);
                    audioplayer.current.run();
                }
            }

            if ($('#flashcontent').length == 0) {
                $('body').append('<div id="flashcontent"></div>');
            }

            return this.each(function() {
                $(this).click(function() {
                    
                    audioplayer.current = new AudioPlayer($(this)[0], options);
                    audioplayer.current.run();

                    return false;
                });
            });

        }

    }

})(jQuery);
