﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Windows");

Windows.ScreenShotViewerBehavior = function(element) {
    Windows.ScreenShotViewerBehavior.initializeBase(this, [element]);
    
    this.previousClickDelegate = Function.createDelegate( this, this.previousClick );
    this.nextClickDelegate = Function.createDelegate( this, this.nextClick );
    this.selectedIndex = null;
}

Windows.ScreenShotViewerBehavior.prototype = {
    initialize: function() {
        Windows.ScreenShotViewerBehavior.callBaseMethod(this, 'initialize');
        
        $addHandler( this.get_PreviousButton().get_element(), 'click', this.previousClickDelegate );
        $addHandler( this.get_NextButton().get_element(), 'click', this.nextClickDelegate );
        
        this.selectedIndex = 0;
        this.refreshNavigation();
    },   
        
    dispose: function() {        
        Windows.ScreenShotViewerBehavior.callBaseMethod(this, 'dispose');
    },
    
    //******************************************
    //	Parameters
    //******************************************
    get_ImageSection : function() {
        return this._imageSection;
    },
    set_ImageSection : function(value) {
        if (this._imageSection != value) {
            this._imageSection = value;
            this.raisePropertyChanged('ImageSection');
        }
    },
    
    get_TextSection : function() {
        return this._textSection;
    },
    set_TextSection : function(value) {
        if (this._textSection != value) {
            this._textSection = value;
            this.raisePropertyChanged('TextSection');
        }
    },
    
    get_TitleSection : function() {
        return this._titleSection;
    },
    set_TitleSection : function(value) {
        if (this._titleSection != value) {
            this._titleSection = value;
            this.raisePropertyChanged('TitleSection');
        }
    },
    
    get_NextButton : function() {
        return this._nextButton;
    },
    set_NextButton : function(value) {
        if (this._nextButton != value) {
            this._nextButton = value;
            this.raisePropertyChanged('NextButton');
        }
    },
    
    get_PreviousButton : function() {
        return this._previousButton;
    },
    set_PreviousButton : function(value) {
        if (this._previousButton != value) {
            this._previousButton = value;
            this.raisePropertyChanged('PreviousButton');
        }
    },
    
    get_IndexElement : function() {
        return this._indexElement;
    },
    set_IndexElement : function(value) {
        if (this._indexElement != value) {
            this._indexElement = value;
            this.raisePropertyChanged('IndexElement');
        }
    },
    
    get_ContentIDs : function() {
        return this._contentIDs;
    },
    set_ContentIDs : function(value) {
        if (this._contentIDs != value) {
            this._contentIDs = value ? Array.parse( value ) : [];
            this.raisePropertyChanged('ContentIDs');
        }
    },
    
    //******************************************
    //	Handlers
    //******************************************
    previousClick : function ( args ) 
    {
		if ( this.get_PreviousButton()._isValidEvent( args ) )
		{
			this.selectedIndex--;
			this.refreshContent();
		}
    },
    
    nextClick : function ( args ) 
    {
		if ( this.get_NextButton()._isValidEvent( args ) )
		{
			this.selectedIndex++;
			this.refreshContent();
		}
    },
    
    //******************************************
    //	Other functions
    //******************************************
    refreshContent : function ()
    {
		this.selectedIndex = Math.min( this.get_ContentIDs().length - 1, Math.max( 0, this.selectedIndex ) );
		this.get_ImageSection().getContentSection( this.get_ContentIDs()[this.selectedIndex].ImageSectionID );
		this.get_TextSection().getContentSection( this.get_ContentIDs()[this.selectedIndex].TextSectionID );
		this.get_TitleSection().getContentSection( this.get_ContentIDs()[this.selectedIndex].TitleSectionID );
		
		this.refreshNavigation();
    },
    
    refreshNavigation : function ()
    {
		this.get_PreviousButton().set_IsEnabled( this.selectedIndex != 0 );
		this.get_NextButton().set_IsEnabled( this.selectedIndex != ( this.get_ContentIDs().length - 1 ) );
		this.get_IndexElement().innerHTML = this.selectedIndex + 1;
    }
}
Windows.ScreenShotViewerBehavior.registerClass('Windows.ScreenShotViewerBehavior', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
