﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("BandSite");

BandSite.Music = function(element) {
    BandSite.Music.initializeBase(this, [element]);
}

BandSite.Music.prototype = {
    initialize: function() {
        BandSite.Music.callBaseMethod(this, 'initialize');
        
        // Add custom initialization here
        var that = this;

//        if (this._albumCount == 1) {
//            // Hide the back to list button...
//            this._backToListDiv.style.display = 'none';

//            this.loadAlbum(this._album1ID);
//        }

        this._albumListView.add_itemClicked(
            function(sender, e) {
                that.loadAlbum(e.id);                        
            }
        );

        // Don't show listen button if no track filename
        this._albumTrackGrid.add_showButton(
            function(sender, e) {
                if (e.buttonName == 'Listen' && (! e.item.TrackFileName)) {
                    e.cancel = true;
                }                
            }
        );
        
        // Whether to show button or not...
        this._albumTrackGrid.add_showButton(
            function(sender, e) {
                if (e.buttonName == 'Lyrics' && ! e.item.Lyrics) {
                    e.cancel = true;
                }
                if (e.buttonName == 'Download' && ! e.item.Downloadable) {
                    e.cancel = true;
                }
            }
        );
        
        this._albumTrackGrid.add_buttonClicked(
            function(sender, e) {
                if (e.buttonName == 'Listen') {
                    that._listenClicked(e.item);
                } else if (e.buttonName == 'Lyrics') {
                    that._lyricsClicked(e.item);
                } else if (e.buttonName == 'Download') {
                    that._downloadClicked(e.item);
                }                                
            }
        );
        
//        this._backToListButton.add_buttonClicked(
//            function(sender, e) {
//                that._panelSet.showPanel("AlbumListViewPanel");
//            }
//        );

        if (this._albumCount > 0) {
            this.loadAlbum(this._album1ID);
            this._albumViewPanelSet.showPanel('AlbumViewPanel');
        } else {
            this._albumViewPanelSet.showPanel('NoAlbumPanel');
        }
        
        this._lyricsCloseButton.add_buttonClicked(
            function(sender, e) {
                that._lyricsModalDialog.hide();
            }
        );

    },
    dispose: function() {        
        //Add custom dispose actions here
        BandSite.Music.callBaseMethod(this, 'dispose');
    },
    
    loadAlbum: function(albumID) {
        var that = this;
        this._albumViewPanelSet.showPanel("LoadingPanel");
        setTimeout(function() {
            that._albumViewPanelSet.showPanel("AlbumViewPanel");
        }, 1000);    
    
        this._albumDetails.set_itemID(albumID);
        this._albumDetails.loadItem();      
        this._albumTrackGrid.set_foreignID(albumID);      
        this._albumTrackGrid.loadGrid();
        // this._panelSet.showPanel("AlbumViewPanel");
    },
    
    _listenClicked: function(item) {
        $find("MusicPlayer").playFile(item.TrackFileName, item.Title);
    },
    _lyricsClicked: function(item) {
        // alert("Show lyrics for track " + item.ID);
        this._lyricsTitleLabel.set_value(item.Title);
        // this._lyricsLabel.set_value("<b>" + item.Title + "</b><br/><br/>" + item.Lyrics);
        this._lyricsLabel.set_value(item.Lyrics);
        this._lyricsModalDialog.show();
    },
    _downloadClicked: function(item) {
        // alert("Download for track " + item.ID);
        // location.href = "Mp3Handler.ashx?visitID=" + this._vabandID=" + this._bandID + "&trackID=" + item.ID;
        location.href = this._baseTrackUrl + item.TrackFileName;
    },
    
    resetSection: function() {        
        // This is copied from above - maybe refactor
        if (this._albumCount == 1) {
            // Hide the back to list button...
            // this._backToListDiv.style.display = 'none';

            this.loadAlbum(this._album1ID);
        } else {
            // this._panelSet.showPanel("AlbumListViewPanel");
        }
    }
}
BandSite.Music.createProperty("bandID");
// BandSite.Music.createProperty("panelSet");
BandSite.Music.createProperty("albumListView");
BandSite.Music.createProperty("albumDetails");
BandSite.Music.createProperty("albumTrackGrid");
BandSite.Music.createProperty("albumViewPanelSet");
// BandSite.Music.createProperty("backToListDiv");
// BandSite.Music.createProperty("backToListButton");
BandSite.Music.createProperty("lyricsModalDialog");
BandSite.Music.createProperty("lyricsTitleLabel");
BandSite.Music.createProperty("lyricsLabel");
BandSite.Music.createProperty("lyricsCloseButton");
BandSite.Music.createProperty("albumCount");
BandSite.Music.createProperty("album1ID");
BandSite.Music.createProperty("baseTrackUrl");
BandSite.Music.registerClass('BandSite.Music', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
