﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("BandSite");

BandSite.Photos = function(element) {
    BandSite.Photos.initializeBase(this, [element]);
    
    this._photoID = null;
}

BandSite.Photos.prototype = {
    initialize: function() {
        BandSite.Photos.callBaseMethod(this, 'initialize');
        
        // Add custom initialization here
        var that = this;
        
        if (this._galleryCount == 1) {
            this._backToGalleryListDiv.style.display = 'none';
            // this.loadGallery(this._gallery1ID);
        }
        
        this._galleryListView.add_itemClicked(
            function(sender, e) {
                that.loadGallery(e.id);
            }                
        );
        
        this._backToGalleryListButton.add_buttonClicked(
            function(sender, e) {
                that._panelSet.showPanel("GalleryListViewPanel");
            }
        );
        
        this._photoListView.add_itemClicked(
            function(sender, e) {
//                that._panelSet.showPanel("LoadingPanel");
//                setTimeout(function() {
//                    that._panelSet.showPanel("PhotoViewPanel");
//                }, 1000);

//                that._photoID = e.id;
//                that._photoDetails.set_itemID(e.id);
//                that._photoDetails.loadItem();
//                that._commentList.set_foreignID(e.id);
//                that._commentList.loadList();

                that.loadPhoto(e.id);
            }
        );

        this._photoDetails.add_itemLoaded(
            function(sender, e) {
                setTimeout(function() {
                    // that._panelSet.showPanel("PhotoViewPanel");                
                }, 400);
            }
        );

//        this._backToGalleryButton.add_buttonClicked(
//            function(sender, e) {
//                that._panelSet.showPanel("GalleryViewPanel");
//            }
//        );
        
        if (this._commentList) {
            this._commentList.add_constructingNewComment(
                function(sender, e) {
                    if (! that._photoID) {
                        programError("PhotoID must be set to construct new photo comment");
                    }
                    e.item.PhotoID = that._photoID;            
                }
            );
        }

        // Call a function when the image has fully loaded in the browser...
        this._photoImage.get_element().onload = function(evt) {
            // alert('Image loaded');
            that.photoImageLoaded();
        };
        
    },
    dispose: function() {        
        //Add custom dispose actions here
        BandSite.Photos.callBaseMethod(this, 'dispose');
    },
    
    loadGallery: function(galleryID) {
        var that = this;
        this._panelSet.showPanel("LoadingPanel");
        setTimeout(function() {
            that._panelSet.showPanel("GalleryViewPanel");
        }, 1000);
        this._galleryDetails.set_itemID(galleryID);
        this._galleryDetails.loadItem();
        
        this._photoListView.set_foreignID(galleryID);
        this._photoListView.loadList();
        
        // Need to select the first photo here...
        
        this._photoListView.add_listLoaded(
            function(sender, e) {
                if (e.firstItemID) {
                    that.loadPhoto(e.firstItemID);
                }
                
                // This is to make sure the footer stays at the bottom in IE 6/7
                // (If it works, we should pass through the footerpanel ClientID rather than hardcoding it like this)
                // (Also it breaks encapsulation here but we'll put up with it for now)
                // (And really we only need to do it for IE <= 7)
//                $get('BandSite_footerpanel').style.bottom = '1px';
//                $get('BandSite_footerpanel').style.bottom = '0px';
//                
//                setTimeout(function() {
//                    $get('BandSite_footerpanel').style.bottom = '1px';
//                    $get('BandSite_footerpanel').style.bottom = '0px';
//                }, 600);
            }
        );
        
        // this._panelSet.showPanel("GalleryViewPanel");
    },
    
    loadPhoto: function(photoID) {
        var that = this;
        this._photoDetailsPanelSet.showPanel("DetailsLoadingPanel");
//        setTimeout(function() {
//            that._photoDetailsPanelSet.showPanel("DetailsPanel");
//        }, 1000);
        
        this._photoID = photoID;
        this._photoDetails.set_itemID(photoID);
        this._photoDetails.loadItem();                
        
        if (this._commentList) {
            this._commentList.set_foreignID(photoID);
            this._commentList.loadList();    
        }
        
        // Automatically position page to view photo...
        
        var scrollTop = document.body.scrollTop;         
        if (scrollTop == 0)
        {
            if (window.pageYOffset)
                scrollTop = window.pageYOffset;
            else
                scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
        }                
        if (scrollTop > 400) {
            window.scrollTo(0, 380);
        }
    },

    // This is called by the onload event of the image element
    photoImageLoaded: function() {
        // Shows the details panel only when the photo has loaded...
        // (Perhaps we should still enforce a minimum time?)
        this._photoDetailsPanelSet.showPanel("DetailsPanel");
    },

    resetSection: function() {
        this._panelSet.showPanel("GalleryListViewPanel");
    }
        
}
BandSite.Photos.createProperty("bandID");
BandSite.Photos.createProperty("panelSet");
BandSite.Photos.createProperty("galleryListView");
BandSite.Photos.createProperty("galleryDetails");
BandSite.Photos.createProperty("photoListView");
BandSite.Photos.createProperty("backToGalleryListDiv");
BandSite.Photos.createProperty("backToGalleryListButton");
BandSite.Photos.createProperty("photoDetailsPanelSet");
BandSite.Photos.createProperty("photoDetails");
BandSite.Photos.createProperty("photoImage");
//BandSite.Photos.createProperty("backToGalleryDiv");
//BandSite.Photos.createProperty("backToGalleryButton");
BandSite.Photos.createProperty("galleryCount");
BandSite.Photos.createProperty("gallery1ID");
BandSite.Photos.createProperty("commentList");
BandSite.Photos.registerClass('BandSite.Photos', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
