YAHOO.namespace('zoeyplayer');

YAHOO.zoeyplayer.html_entity_decode = function(str) {
    var ta=document.createElement("textarea");
    ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
    return ta.value;
};

YAHOO.zoeyplayer.PlayableItemWidget = function(item, el) {
    YAHOO.zoeyplayer.PlayableItemWidget.superclass.constructor.call(this, item, el);
};
YAHOO.extend(YAHOO.zoeyplayer.PlayableItemWidget, YAHOO.util.Element, {
    init: function(item, el) {
        el = typeof(el) != 'undefined' ? el: document.createElement('div');
        YAHOO.zoeyplayer.PlayableItemWidget.superclass.init.call(this, el);
        this.playback_manager = YAHOO.zoeyplayer.playback;
        this.playlist = null;

        this.addClass('playable-item');



        if (item == null) {
            this.item = this.parseItemFromDOM();
        } else {
            this.item = item;
            this.initDOM();
        }


        this.subscribeEvents();

    },
    destroy: function() {
        // TODO: Implement 
    },
    getPlaylist: function() {
        return this.playlist;
    },
    setPlaylist: function(playlist) {
        this.playlist = playlist;
        return this.getPlaylist();
    },
    initDOM: function() {
        var artist_name = this.item.getArtistName();
        var album_title = this.item.getAlbumTitle();
        var track_title = this.item.getTrackTitle();

        var track_title_div = document.createElement('div');
        YAHOO.util.Dom.addClass(track_title_div, 'track-title');
        this.appendChild(track_title_div);

        var track_title_node = document.createTextNode(' ' + track_title);
        track_title_div.appendChild(track_title_node);

        var album_info_div = document.createElement('div');
        YAHOO.util.Dom.addClass(album_info_div, 'album-info');
        this.appendChild(album_info_div)


        if (artist_name != null) {
            var by_node = document.createTextNode('by ');
            album_info_div.appendChild(by_node);

            var artist_name_span = document.createElement('span');
            YAHOO.util.Dom.addClass(artist_name_span, 'artist-name');
            var artist_name_node = document.createTextNode(artist_name);
            artist_name_span.appendChild(artist_name_node);

            album_info_div.appendChild(artist_name_span);
        }

        if (album_title != null) {
            var from_node = document.createTextNode(' from ');
            album_info_div.appendChild(from_node);

            var album_title_span = document.createElement('span');
            YAHOO.util.Dom.addClass(album_title_span, 'album-title');
            var album_title_node = document.createTextNode(album_title);
            album_title_span.appendChild(album_title_node);
            album_info_div.appendChild(album_title_span);
        }
        this.scrub_bar = new YAHOO.zoeyplayer.ScrubBar();
        this.appendChild(this.scrub_bar.get('element'));

        this.updateClasses();
    }, 
    parseArtistNameFromDOM: function() {
        var artist_name_el = YAHOO.util.Dom.getElementsBy(function(el) {
            if (YAHOO.util.Dom.hasClass(el, 'artist-name')) {
                return true;
            } else {
                return false;
            }
        }, '*', this.get('element'));
        if (artist_name_el.length > 0) {
            return YAHOO.zoeyplayer.html_entity_decode(artist_name_el[0].innerHTML);
        }
        return null;
    },
    parseAlbumTitleFromDOM: function() {
        var album_title_el = YAHOO.util.Dom.getElementsBy(function(el) {
            if (YAHOO.util.Dom.hasClass(el, 'album-title')) {
                return true;
            } else {
                return false;
            }
        }, '*', this.get('element'));
        if (album_title_el.length > 0) {
            return YAHOO.zoeyplayer.html_entity_decode(album_title_el[0].innerHTML);
        }
        return null;
    },
    parseTrackTitleFromDOM: function() {
        var track_title_el = YAHOO.util.Dom.getElementsBy(function(el) {
            if (YAHOO.util.Dom.hasClass(el, 'track-title')) {
                return true;
            } else {
                return false;
            }
        }, '*', this.get('element'));
        if (track_title_el.length > 0) {
            return YAHOO.zoeyplayer.html_entity_decode(track_title_el[0].innerHTML);
        } else {
            track_title_el = YAHOO.util.Dom.getElementsBy(function(el) {
                if (YAHOO.util.Dom.hasClass(el, 'name')) {
                    return true;
                } else {
                    return false;
                }
            }, '*', this.get('element'));
            if (track_title_el.length > 0) {
                return YAHOO.zoeyplayer.html_entity_decode(track_title_el[0].innerHTML);
            }
        }
        return null;
    },
    parsePlaybackUrlFromDOM: function() {
        var playback_url_a = YAHOO.util.Dom.getElementsBy(function(el) {

            var rel = new YAHOO.util.Element(el).get('rel');
            if (rel=='playback') {
                return true;
            } else {
                return false;
            }
        }, 'a', this.get('element'));
        if (playback_url_a.length > 0) {
            return new YAHOO.util.Element(playback_url_a[0]).get('href');
        }
        return null;
    },
    parseTrackUrlFromDOM: function() {
        var track_url_a = YAHOO.util.Dom.getElementsBy(function(el) {

            var rel = new YAHOO.util.Element(el).get('rel');
            if (rel=='lyrics') {
                return true;
            } else {
                return false;
            }
        }, 'a', this.get('element'));
        if (track_url_a.length > 0) {
            return new YAHOO.util.Element(track_url_a[0]).get('href');
        }
        return null;
    },
    parseArtworkUrlFromDOM: function() {
        var imgs = YAHOO.util.Dom.getElementsBy(function(el) {
            var el = new YAHOO.util.Element(el);
            if (el.hasClass('artwork')) {
                return true;
            } else {
                return false;
            }
        }, 'img', this.get('element'));
        if (imgs.length > 0) {
            var artwork_img = new YAHOO.util.Element(imgs[0]);
            return artwork_img.get('src');
        }
    },
    parseAlbumUrlFromDOM: function() {
        var album_url_a = YAHOO.util.Dom.getElementsBy(function(el) {
            var rel = new YAHOO.util.Element(el).get('rel');
            if (rel=='album') {
                return true;
            } else {
                return false;
            }
        }, 'a', this.get('element'));
        if (album_url_a.length > 0) {
            var album_url_a = new YAHOO.util.Element(album_url_a[0]);
            return album_url_a.get('href');
        }
    },
    parseArtistUrlFromDOM: function() {
        var artist_url_a = YAHOO.util.Dom.getElementsBy(function(el) {
            var rel = new YAHOO.util.Element(el).get('rel');
            if (rel=='artist') {
                return true;
            } else {
                return false;
            }
        }, 'a', this.get('element'));
        if (artist_url_a.length > 0) {
            var artist_url_a = new YAHOO.util.Element(artist_url_a[0]);
            return artist_url_a.get('href');
        }
    },
    parseArtbookUrlFromDOM: function() {
        var artbook_url_a = YAHOO.util.Dom.getElementsBy(function(el) {
            var rel = new YAHOO.util.Element(el).get('rel');
            if (rel=='artbook') {
                return true;
            } else {
                return false;
            }
        }, 'a', this.get('element'));
        if (artbook_url_a.length > 0) {
            var artbook_url_a = new YAHOO.util.Element(artbook_url_a[0]);
            return artbook_url_a.get('href');
        }
    },
    parseItemFromDOM: function() {
        var artist_name = this.parseArtistNameFromDOM();
        var album_title = this.parseAlbumTitleFromDOM();
        var track_title = this.parseTrackTitleFromDOM();
        var playback_url = this.parsePlaybackUrlFromDOM();
        var artwork_url = this.parseArtworkUrlFromDOM();
        var track_url = this.parseTrackUrlFromDOM();
        var album_url = this.parseAlbumUrlFromDOM();
        var artist_url = this.parseArtistUrlFromDOM();
        var artbook_url = this.parseArtbookUrlFromDOM();

        var item = YAHOO.zoeyplayer.PlayableItem.get(playback_url);
        item.setArtistName(artist_name);
        item.setAlbumTitle(album_title);
        item.setTrackTitle(track_title);
        item.setArtworkUrl(artwork_url);
        item.setTrackUrl(track_url);
        item.setAlbumUrl(album_url);
        item.setArtistUrl(artist_url);
        item.setArtbookUrl(artbook_url);

        var scrub_bar_el = YAHOO.util.Dom.getElementsByClassName(
                'playback-scrub-bar', 'div', this.get('element'));
        if (scrub_bar_el.length > 0) {
            this.scrub_bar = new YAHOO.zoeyplayer.ScrubBar(scrub_bar_el[0]);
        } else {
            this.scrub_bar = new YAHOO.zoeyplayer.ScrubBar();
            //this.appendChild(this.scrub_bar);
        }
        return item;
    },
    onClick: function(e) {
        if (this.item.isPlaying()) {
            this.playback_manager.togglePause();
        } else {
            var playlist = this.getPlaylist();
            if (playlist != null) {
                this.playback_manager.setPlaylist(playlist);
            } else {
                this.playback_manager.clear();
            }
            this.playback_manager.playItem(this.item);
        }
    },
    subscribeEvents: function() {
        this.item.subscribe('item_playback_started', 
            this.scrub_bar.connectPlaybackEvents, this.scrub_bar, true);
        this.item.subscribe('item_playback_stopped', 
            this.scrub_bar.disconnectPlaybackEvents, this.scrub_bar, true);
        this.item.subscribe('item_playback_finished', 
            this.scrub_bar.disconnectPlaybackEvents, this.scrub_bar, true);
        if (this.item.isPlaying()) {
            this.scrub_bar.connectPlaybackEvents({
                manager:YAHOO.zoeyplayer.playback
            });
        }

        this.item.subscribe('item_playback_started', this.updateClasses, this, true);
        this.item.subscribe('item_playback_stopped', this.updateClasses, this, true);
        this.item.subscribe('item_playback_paused', this.updateClasses, this, true);
        this.item.subscribe('item_playback_resumed', this.updateClasses, this, true);
        this.item.subscribe('item_playback_finished', this.updateClasses, this, true);
        this.subscribe('click', this.onClick, this, true);
    },
    updateClasses: function() {
        if (this.item.isPlaying()) {
            this.addClass('playing');
        } else {
            this.removeClass('playing');
        }
        if (this.item.isPaused()) {
            this.addClass('paused');
        } else {
            this.removeClass('paused');
        }
    }
});


YAHOO.zoeyplayer.PlaylistItemWidget = function(playlist, item, el) {
    YAHOO.zoeyplayer.PlaylistItemWidget.superclass.constructor.call(this, item, el);
    if (item == null) { 
        this.playable_item_widget = this.parseItemFromDOM();
    } else {
        this.playable_item_widget = new YAHOO.zoeyplayer.PlayableItemWidget(item);
        this.appendChild(this.playable_item_widget);
    }
    this.playable_item_widget.setPlaylist(playlist);
}
YAHOO.extend(YAHOO.zoeyplayer.PlaylistItemWidget, YAHOO.util.Element, {
    init: function(item, el) {
        el = typeof(el) != 'undefined' ? el: document.createElement('li');

        YAHOO.zoeyplayer.PlaylistItemWidget.superclass.init.call(this, el);
        this.addClass('playlist-item');

    },
    destroy: function() {
        this.playable_item_widget = null;
        YAHOO.zoeyplayer.PlaylistItemWidget.superclass.destroy.call(this, el);
    },
    parseItemFromDOM: function() {
        var playable_item_el = YAHOO.util.Dom.getElementsByClassName(
            'playable-item', 'div', this.get('element'));
        if (playable_item_el.length) {
            return new YAHOO.zoeyplayer.PlayableItemWidget(null, playable_item_el[0]);
        }
        return null;
    },
    getItem: function() {
        return this.playable_item_widget.item;
    },
    updateClasses: function() {
        this.playable_item_widget.updateClasses();
    }
});


YAHOO.zoeyplayer.PlaylistWidget = function(el, parsePlaylist, userConfig) {
    YAHOO.zoeyplayer.PlaylistWidget.superclass.constructor.call(this, el, parsePlaylist, userConfig);
};
YAHOO.extend(YAHOO.zoeyplayer.PlaylistWidget, YAHOO.widget.Module, {
    init: function(el, parsePlaylist, userConfig) {
        parsePlaylist = typeof(parsePlaylist) != 'undefined' ? parsePlaylist: false;
        YAHOO.zoeyplayer.PlaylistWidget.superclass.init.call(this, el, userConfig);

        this.playlist = null
        this.playlist_item_widget_list = [];

        if (parsePlaylist==true) {
            this.playlist = this.parsePlaylistFromDOM();
        } else {
            this.initDOM();
        }
    },
    destroy: function() {
        this.setPlaylist(null);
        this.playlist_item_ul = null;
        var i;
        for (i in this.playlist_item_widget_list) {
            var widget = this.playlist_item_widget_list[i];
            try {
                widget.destroy();
            } catch(e) {
            }
        }
        this.playlist_item_widget_list = [];
        YAHOO.zoeyplayer.PlaylistWidget.superclass.destroy.call(this);
    },
    initDOM: function() {
        YAHOO.util.Dom.addClass(this.element, 'playlist-widget');

        this.playlist_item_ul = new YAHOO.util.Element(document.createElement('ul'));
        this.playlist_item_ul.addClass('playlist-item');
        this.setBody(this.playlist_item_ul.get('element'));
        this.setHeader('Playlist');

        //this.initPlaybackControls();
    },
    parsePlaylistFromDOM: function() {
        var playlist = new YAHOO.zoeyplayer.Playlist();

        var playlist_items = YAHOO.util.Dom.getElementsByClassName(
                'playlist-item', 'li', this.element);
        var index;
        for (index in playlist_items) {
            var item_el = playlist_items[index];
            var playlist_item_widget = new YAHOO.zoeyplayer.PlaylistItemWidget(
                    playlist, null, item_el);
            this.playlist_item_widget_list[index] = playlist_item_widget;

            playlist.append(playlist_item_widget.getItem());
        }
        if (YAHOO.util.Dom.hasClass(this.element, 'autoplay')) {
            if (soundManager.supported()) {  
                if (!YAHOO.zoeyplayer.playback.isPlaying()) {
                    YAHOO.zoeyplayer.playback.setPlaylist(playlist);
                    YAHOO.zoeyplayer.playback.play();
                }
            } else {
                YAHOO.zoeyplayer.sm2_load_success.subscribe(function() {
                    if (!YAHOO.zoeyplayer.playback.isPlaying()) {
                        YAHOO.zoeyplayer.playback.setPlaylist(playlist);
                        YAHOO.zoeyplayer.playback.play();
                    }
                });
            } 
            
        }
        return this.setPlaylist(playlist, false);
    },
    initPlaybackControls: function() {
        this.playback_controls = new YAHOO.util.Element(document.createElement('div'));
        this.playback_controls.addClass('yuimenubar');

        this.playback_buttons = new YAHOO.zoeyplayer.ControlButtonSet();

        this.playback_controls.appendChild(this.playback_buttons);

        this.setFooter(this.playback_controls.get('element'));
    },
    getPlaylist: function() {
        return this.playlist;
    },
    setPlaylist: function(playlist, replaceWidgets) {
        replaceWidgets = typeof(replaceWidgets) != 'undefined' ? replaceWidgets: true;

        if (playlist != this.playlist) {
            if (this.playlist != null) {
                this.playlist.unsubscribe('playlist_item_inserted', this.onInsertPlaylistItem);
                this.playlist.unsubscribe('playlist_item_removed', this.onRemovePlaylistItem);
            }
            this.playlist = playlist;
            if (playlist == null) {
                return null;
            }

            if (replaceWidgets==true) {
                this.setHeader(this.playlist.getTitle());
                while (this.playlist_item_widget_list.length > 0) {
                    this.removePlaylistItemWidget(0);
                }

                var index;
                for (index in this.playlist.item_list) {
                    var item = this.playlist.item_list[index];
                    this.insertPlaylistItemWidget(item, index);
                }
            }
            this.playlist.subscribe('playlist_item_inserted', this.onInsertPlaylistItem, this, true);
            this.playlist.subscribe('playlist_item_removed', this.onRemovePlaylistItem, this, true);
        }
        this.updateItemClasses();
        return this.getPlaylist();
    },
    onInsertPlaylistItem: function(info) {
        var item_widget = this.insertPlaylistItemWidget(info.item, info.index);
        this.updateItemClasses();
        return item_widget;
    },
    onRemovePlaylistItem: function(info) {
        var item_widget = this.removePlaylistItemWidget(info.index);
        this.updateItemClasses();
        return item_widget;
    },
    insertPlaylistItemWidget: function(item, index) {
        var item_widget = new YAHOO.zoeyplayer.PlaylistItemWidget(
                this.playlist, item);
        if (index == this.playlist_item_widget_list.length) {
            this.playlist_item_widget_list[index] = item_widget;
        } else {
            this.playlist_item_widget_list.split(index, 0, item_widget);
        }
        this.playlist_item_ul.insertBefore(item_widget, 
            this.playlist_item_ul.get('childNodes')[index] || null);
        return item_widget;
    },
    removePlaylistItemWidget: function(index) {
        var item_widget = this.playlist_item_widget_list.splice(index, 1)[0];
        this.playlist_item_ul.removeChild(item_widget);
        return item_widget;
    },
    updateItemClasses: function() {
        var index;
        for (index in this.playlist_item_widget_list) {
            var item_widget = this.playlist_item_widget_list[index];
            item_widget.updateClasses();
            if (index % 2 == 0) {
                item_widget.removeClass('odd');
                item_widget.addClass('even');
            } else {
                item_widget.removeClass('even');
                item_widget.addClass('odd');
            }
        }
    }
}); 
YAHOO.lang.augmentProto(YAHOO.zoeyplayer.PlaylistWidget, YAHOO.util.EventProvider);


YAHOO.zoeyplayer.ScrubBar = function(el) {
    el = typeof(el) != 'undefined' ? el: document.createElement('div');
    YAHOO.zoeyplayer.ScrubBar.superclass.constructor.call(this, el);
    this.playback_manager = null;
    this.initDOM();
};
YAHOO.extend(YAHOO.zoeyplayer.ScrubBar, YAHOO.util.Element, {
    initDOM: function() {
        this.addClass('playback-scrub-bar');    
        this.addClass('yui-button');    
        this.addClass('yui-buttongroup');    
        this.setStyle('position', 'relative');
        this.setStyle('overflow', 'hidden');

        this.loading_div = new YAHOO.util.Element(document.createElement('div'));
        this.loading_div.addClass('loading');
        this.loading_div.addClass('yui-button');
        this.loading_div.addClass('yui-button-active');
        this.loading_div.setStyle('position', 'absolute');
        this.loading_div.setStyle('left', '0px');
        this.loading_div.setStyle('top', '0px');
        this.loading_div.setStyle('bottom', '0px');

        this.position_div = new YAHOO.util.Element(document.createElement('div'));
        this.position_div.addClass('position');
        this.position_div.addClass('yui-button');
        this.position_div.addClass('yui-checkbox-button-checked');
        this.position_div.setStyle('position', 'absolute');
        this.position_div.setStyle('left', '0px');
        this.position_div.setStyle('top', '0px');
        this.position_div.setStyle('bottom', '0px');

        this.appendChild(this.loading_div);
        this.appendChild(this.position_div);
    },
    resetToZero: function(info) {
        this.loading_div.setStyle('width', '0%');
        this.position_div.setStyle('width', '0%');
    },
    updateLoadingProgress: function(info) {
        var playable_item = info.playable_item;
        var sound_object = playable_item.sound_object;
        var percentage = ((sound_object.bytesLoaded/sound_object.bytesTotal)*100) + '%';
        this.loading_div.setStyle('width', percentage);
    },
    updatePosition: function(info) {
        var playable_item = info.playable_item;
        var sound_object = playable_item.sound_object;
        var percentage = ((sound_object.position/playable_item.getDuration())*100) + '%';
        this.position_div.setStyle('width', percentage);
    },
    disconnectPlaybackEvents: function(info) {
        this.resetToZero();
        if (this.playback_manager != null) {
            this.playback_manager.unsubscribe('item_load_status', 
                    this.updateLoadingProgress);
            this.playback_manager.unsubscribe('item_playback_status', 
                    this.updatePosition);
            this.playback_manager.unsubscribe('item_playback_finished', 
                    this.resetToZero);
            this.playback_manager.unsubscribe('item_playback_stopped', 
                    this.resetToZero);
        }
    },
    connectPlaybackEvents: function(info) {
        this.disconnectPlaybackEvents();
        this.playback_manager = info.manager;
	this.playable_item = info.playable_item;
        this.playback_manager.subscribe('item_load_status', 
                this.updateLoadingProgress, this, true);
        this.playback_manager.subscribe('item_load_finished', 
                this.updateLoadingProgress, this, true);
        this.playback_manager.subscribe('item_playback_status', 
                this.updatePosition, this, true);
        this.playback_manager.subscribe('item_playback_finished', 
                this.resetToZero, this, true);
        this.playback_manager.subscribe('item_playback_stopped', 
                this.resetToZero, this, true);
        this.subscribe('mousedown', this.draggingStarted, this, true);
        YAHOO.util.Event.addListener(document, 'mouseup', this.draggingStopped, this, true);
    },
    draggingStarted: function(e, scrubbar) {
        this.dragging = true;

        this.playback_manager.unsubscribe('item_playback_status',
                    this.updatePosition);

        this.subscribe('mousemove', this.draggingHappening, this, true);
        this.addClass('scrub-bar-dragging');
	e.preventDefault();
        return false;
    },
    draggingHappening: function(e, scrubbar) {
        if (this.dragging) {
            this.setScrubbarPosition(e);
        }
        return false;
    },
    draggingStopped: function(e, scrubbar) {
        if (this.dragging) {
            this.removeClass('scrub-bar-dragging');
            this.unsubscribe('mousemove', this.draggingHappening);

            this.dragging = false;

	    if (!this.playback_offset_percent) {
		this.setScrubbarPosition(e);
	    }

	    this.playable_item.sound_object.setPosition((this.playback_offset_percent/100) * this.playable_item.getDuration());

	    this.playback_manager.subscribe('item_playback_status', 
                this.updatePosition, this, true);
        }
	this.playback_offset_percent = null;
        return false;
    },
    setScrubbarPosition: function(e) {
        var region = YAHOO.util.Dom.getRegion(this.get('element'));
        this.playback_offset_percent = Math.floor(((e.clientX - region.left) / (region.right - region.left)) * 100);
        this.position_div.setStyle('width', this.playback_offset_percent + '%');
    }
});


YAHOO.zoeyplayer.ControlButtonSet = function(el) {
    el = typeof(el) != 'undefined' ? el: document.createElement('div');
    YAHOO.zoeyplayer.ControlButtonSet.superclass.constructor.call(this, el);
    this.playback_manager = YAHOO.zoeyplayer.playback;
    this.initDOM();
};
YAHOO.extend(YAHOO.zoeyplayer.ControlButtonSet, YAHOO.util.Element, {
    initDOM: function() {
        this.addClass('playback-control-button-set');    

        this.previous_btn = new YAHOO.widget.Button({
            label: 'Previous',
            className: 'previous-btn',
            container: this,
            onclick: {
                fn: function(e) {
                    this.playback_manager.previous();
                },
                scope: this
            }
        });
        this.play_btn = new YAHOO.widget.Button({
            label: 'Play',
            className: 'play-btn',
            container: this,
            onclick: {
                fn: function(e) {
                    if (this.playback_manager.isPaused()) {
                        this.playback_manager.resume();
                    } else {
                        this.playback_manager.play();
                    }
                },
                scope: this
            }
        });
        this.pause_btn = new YAHOO.widget.Button({
            label: 'Pause',
            className: 'pause-btn',
            container: this,
            onclick: {
                fn: function(e) {
                    this.playback_manager.togglePause();
                },
                scope: this
            }
        });
        this.stop_btn = new YAHOO.widget.Button({
            label: 'Stop',
            className: 'stop-btn',
            container: this,
            onclick: {
                fn: function(e) {
                    this.playback_manager.stop();
                },
                scope: this
            }
        });
        this.next_btn = new YAHOO.widget.Button({
            label: 'Next',
            className: 'next-btn',
            container: this,
            onclick: {
                fn: function(e) {
                    this.playback_manager.next();
                },
                scope: this
            }
        });
    }
});




YAHOO.zoeyplayer.SimpleControlButtonSet = function(el) {
    YAHOO.zoeyplayer.SimpleControlButtonSet.superclass.constructor.call(this, el);
}
YAHOO.extend(YAHOO.zoeyplayer.SimpleControlButtonSet, YAHOO.zoeyplayer.ControlButtonSet, {
    initDOM: function() {
        YAHOO.zoeyplayer.SimpleControlButtonSet.superclass.initDOM.call(this);
        this.stop_btn.setStyle('display', 'none');
        this.play_btn.setStyle('display', 'none');
        this.pause_btn.replaceClass('pause-btn', 'play-btn');

        this.playback_manager.subscribe('item_playback_started', this.showPause, this, true);
        this.playback_manager.subscribe('item_playback_stopped', this.showPlay, this, true);
        this.playback_manager.subscribe('item_playback_paused', this.showPlay, this, true);
        this.playback_manager.subscribe('item_playback_resumed', this.showPause, this, true);
        this.playback_manager.subscribe('item_playback_finished', this.showPlay, this, true);
    },
    showPlay: function() {
        this.pause_btn.replaceClass('pause-btn', 'play-btn');
    },
    showPause: function() {
        this.pause_btn.replaceClass('play-btn', 'pause-btn');
    }
});


YAHOO.zoeyplayer.ArtworkPagerWidget = function(el, image_width) {
    YAHOO.zoeyplayer.ArtworkPagerWidget.superclass.constructor.call(this, el, image_width);
};
YAHOO.extend(YAHOO.zoeyplayer.ArtworkPagerWidget, YAHOO.util.Element, {
    init: function(el, image_width) {
        el = typeof(el) != 'undefined' ? el: document.createElement('div');
        image_width = typeof(image_width) != 'undefined' ? image_width: 480;
        YAHOO.zoeyplayer.ArtworkPagerWidget.superclass.init.call(this, el);
        this.addClass('artwork-pager');
        this.setStyle('position', 'relative');
        this.setStyle('padding', '0px');
        this.setStyle('overflow', 'hidden');


        this.current_index = 0;
        this.image_width = null;
        this.animation = null;

        this.parseFromDOM();
        this.initNavigation();

        this.setImageWidth(image_width)

        this.subscribeEvents();
        this.displayImage(this.current_index);
    },
    initDOM: function() {
    },
    getCurrentIndex: function() {
        return this.current_index;
    },
    setCurrentIndex: function(new_index) {
        if (new_index != this.current_index) {
            this.current_index = new_index;
            if (this.current_index >= this.image_list.length) {
                this.current_index = 0;
            }
            if (this.current_index < 0) {
                this.current_index = this.image_list.length - 1;
            }
            this.displayImage(this.current_index);
        }
        return this.getCurrentIndex();
    },
    setImageWidth: function(width) {
        if (width != this.image_width) {
            this.image_width = width;
            for (image_index in this.image_list) {
                var image = this.getImage(image_index);
                image.setStyle('width', width+'px');
            }
            this.image_container.setStyle('width', (width*this.image_list.length) + 'px');
            this.setStyle('width', width + 'px');
        }
        this.displayImage(this.getCurrentIndex());
        return this.getImageWidth()
    },
    getImageWidth: function() {
        return this.image_width;
    },
    getImage: function(index) {
        return this.image_list[index];
    },
    displayImage: function(index) {
        var position = index * this.image_width;
        if (this.animation != null) {
            if (this.animation.isAnimated()) {
                this.animation.stop()
            }
            this.animation = null;
        }  
        this.animation = new YAHOO.util.Scroll(this.viewport.get('element'), {
            scroll: {to: [position, 0]}
        }, 0.5);
        this.animation.animate();
    },
    parseImageContainer: function() {
        var image_container = YAHOO.util.Dom.getElementBy(function(el) {
            if (YAHOO.util.Dom.hasClass(el, 'image-container')) {
                return true;
            } else {
                return false;
            }
        }, 'div', this.get('element'));
        image_container = new YAHOO.util.Element(image_container);
        image_container.setStyle('margin', '0px');
        image_container.setStyle('padding', '0px');
        image_container.setStyle('border-width', '0px');
        image_container.setStyle('overflow', 'hidden');
        return image_container

    },
    parseViewport: function() {
        var viewport = YAHOO.util.Dom.getElementBy(function(el) {
            if (YAHOO.util.Dom.hasClass(el, 'viewport')) {
                return true;
            } else {
                return false;
            }
        }, 'div', this.get('element'));
        viewport = new YAHOO.util.Element(viewport);
        viewport.setStyle('margin', '0px');
        viewport.setStyle('padding', '0px');
        viewport.setStyle('border-width', '0px');
        viewport.setStyle('overflow', 'hidden');
        return viewport

    },
    parseImages: function() {
        var image_list = YAHOO.util.Dom.getElementsBy(function(el) {return true;}, 'img', this.image_container);
        var image_index = 0;
        var current_index = 0;
        for (image_index in image_list) {
            var image = new YAHOO.util.Element(image_list[image_index]);
            image.setStyle('padding', '0px');
            image.setStyle('margin', '0px');
            image.setStyle('border-width', '0px');
            image.setStyle('display', 'inline');
            image_list[image_index] = image;
            if (image.hasClass('current')) {
                current_index = image_index;
            }
        }
        this.current_index = current_index;
        return image_list;
    },
    initNavigation: function() {
        this.previous_link = new YAHOO.util.Element(document.createElement('a'));
        this.previous_link.addClass('nav');
        this.previous_link.addClass('previous');
        this.previous_link.setStyle('z-index', 10);
        this.previous_link.setStyle('display', 'block');

        this.next_link = new YAHOO.util.Element(document.createElement('a'));
        this.next_link.addClass('nav');
        this.next_link.addClass('next');
        this.next_link.setStyle('z-index', 10);

        this.appendChild(this.previous_link);
        this.appendChild(this.next_link);

    },
    parseFromDOM: function() {
        this.image_container = this.parseImageContainer();
        this.image_list = this.parseImages();        
        this.viewport = this.parseViewport()
    },
    subscribeEvents: function() {
        var self = this;
        this.previous_link.on('click', function(e, pager) {
            self.setCurrentIndex(self.getCurrentIndex() - 1);
        }, this); 
        this.next_link.on('click', function(e, pager) {
            self.setCurrentIndex(self.getCurrentIndex() + 1);
        }, this); 
    }
});

