﻿// JScript File
Type.registerNamespace("manathanWeb.Movie");

manathanWeb.Movie.Core = function(movie, video, volumeBar, slidebar, log, playbutton, pausebutton, autoplay) {
    this.movie = movie;
    this.volumeBar = volumeBar;
    this.video = video;
    this.log = log;
    this.slidebar = slidebar;
    this.playbutton = playbutton;
    this.pausebutton = pausebutton;
    
    this.playing = autoplay;
    
    this.stepSize = 1;
    this.pointerCounter = 0;
    
    this.playDelegate = null;
    this.pauseDelegate = null;

    this.StartPlay = null;
    this.OnPlayError = null;
}

manathanWeb.Movie.Core.prototype = {
    
    OnKeyReleased: function(eventArgs) {
        this.stepSize = 1;
        this.pointerCounter = 0;
    },
    
    OnKeyPressed: function(eventArgs) {
        if (eventArgs.charCode == Sys.UI.Key.right && !this.playing) {
            this.movie.Step(1);
        }
        else if (eventArgs.charCode == Sys.UI.Key.left && !this.playing) {
            this.movie.Step(-1);
        }
        else if (eventArgs.charCode == Sys.UI.Key.space) {
            if (!this.playing)
                this.Play();
            else
                this.Stop();
            return;
        }
        else
            return;
            
        if (this.pointerCounter > 0 && this.pointerCounter % 5 == 0 && this.stepSize < 3)
            this.stepSize++;
        this.pointerCounter++;
        
    },
    OnVideoComplete: function() {
        this.Stop();
    },
    
    VolumeChanged: function(volume) {
        
    },
    
    SlideChanged: function(time) {
        if (this.playing) {
            this.Play();
        }
    },
    
    Play: function() {
        this.Log('Initalizing play');
        try {
	    if (this.StartPlay) this.StartPlay();
	    this.Log('Checking movie.play');
	    if (!this.movie.Play) {
                return;
            }
	    this.Log('Playing movie');
            this.movie.Play();
            this.playing = true;
	    this.Log('Show Time');
            this.slidebar.ShowTime();
            this.Log('Handle playbutton');
            if (this.pausebutton) {
                this.playbutton.disabled = true;
                this.pausebutton.disabled = false;
            } else { 
                $removeHandler(this.playbutton, 'click', this.playDelegate);
                $addHandler(this.playbutton, 'click', this.pauseDelegate);
                this.playbutton.value = "pause";
            }
        } catch (exc) {
            if (this.OnPlayError) this.OnPlayError(exc);
        }
    },
    
    Stop: function() {
       try {
           if (!this.movie.Stop) {
                return;
            }
           this.movie.Stop();
           this.playing = false;
           if (this.pausebutton) {
               this.playbutton.disabled = false;
               this.pausebutton.disabled = true;
           } else { 
               $removeHandler(this.playbutton, 'click', this.pauseDelegate);
               $addHandler(this.playbutton, 'click', this.playDelegate);
               this.playbutton.value = "play";
           }
       } catch (e) { 
            if (this.OnPlayError) this.OnPlayError(exc); 
       }
    },
    
    Log: function(message) {
        var log = $get(this.log);
        if (window.console) console.log(message);
        if (false && log)
            log.innerHTML += message + "<br />";
    },
    
    Init: function() {
        try {
            this.volumeBar.VolumeChanged = Function.createDelegate(this, this.VolumeChanged);
            this.video.WriteLog = Function.createDelegate(this, this.Log);
            this.slidebar.SliderChanged = Function.createDelegate(this, this.SlideChanged);
            this.slidebar.KeyPressed = Function.createDelegate(this, this.OnKeyPressed);
            this.slidebar.KeyUp = Function.createDelegate(this, this.OnKeyReleased);
            this.slidebar.VideoCompleted = Function.createDelegate(this, this.OnVideoComplete);
        
            this.playDelegate = Function.createDelegate(this, this.Play);
            this.pauseDelegate = Function.createDelegate(this, this.Stop);
            if (this.pausebutton) {
                this.playbutton.disable = this.playing;
                this.pausebutton.disabled = !this.playing;
                $addHandler(this.pausebutton, 'click', this.pauseDelegate);
                $addHandler(this.playbutton, 'click', this.playDelegate);
            }
            else {
                if (!this.playing) {
                    this.playbutton.value = "play";
                    $addHandler(this.playbutton, 'click', this.playDelegate);
                } else {
                    this.playbutton.value = "pause";
                    $addHandler(this.playbutton, 'click', this.pauseDelegate);
                }
            }
        } catch (e) {
            alert('Bad error in manathanWeb.Movie.Core.Init: ' + e.message);
        }
    }
}

manathanWeb.Movie.Core.registerClass('manathanWeb.Movie.Core');
