﻿// JScript File

Type.registerNamespace("manathanWeb.Movie");

manathanWeb.Movie.SlideBar = function(parent, movie, movieId) {
    this.movie = movie;
    this.parent = parent;
    this.movieId = ((movieId) ? movieId : "mwQuicktimeMovie");
    // the controls
    
    // the slidebar
    this.slider = null;
    // the rail handle
    this.rail = null;
    // the progress bar
    this.progressBar = null;
    // the loading bar
    this.loadingBar = null;    
    
    // Delegate for SliderChanged Event
    this.SliderChanged = null;
    // Delegate for the KeyPressed Event
    this.KeyPressed = null;
    // Delegate for the KeyUp Event
    this.KeyUp = null;
    // Delegate thats called once the video is completed
    this.VideoCompleted = null;
    
    this.showTimer = null;
    this.showProgess = null;
    this.keyDelegate = null;
    this.keyUpDelegate = null;

    this.methodErrorCount = 0;
}

manathanWeb.Movie.SlideBar.prototype = {
    _SetHandle: function(left) {
        this.rail.style.left = left + 'px';
        if (this.progressBar)
            this.progressBar.style.width = left + 'px';
    },

    _SetHandlePosition: function(forTime) {
        /// <summary>Changes the current value (position) for slider's rail</summary>
        /// <param name="forTime">The new time for the rail</param>
        var percentageTime = forTime / this.movie.GetDuration();
        this._SetHandle(parseInt((this.slider.offsetWidth - this.rail.offsetWidth) * percentageTime));
    },
    
    _SetMoviePosition: function() {
        var percentageRail = parseInt((this.rail.style.width / 2) / this.slider.style.width);
        this.movie.SetTime(this.movie.GetDuration() * percentageRail);
    },

    Dispose: function() {
        try {
	clearTimeout(this.showTimer); 
	clearTimeout(this.showProgess);
	$clearHandlers(this.slider);
	$clearHandlers(this.rail);
        $removeHandler(document, 'keypress', this.keyDelegate);
        $removeHandler(document, 'keyup', this.keyUpDelegate); }
 	catch (e) { alert(e.message); }
    },
    
    OnHandleClick: function(eventElement) {
        eventElement.stopPropagation();
    },

    OnSliderChanged: function(eventElement) {
        /// <summary>Called when the slider value was changed by the user's click on the slider</summary>
        /// <param name="eventElement">The event source</param>
        this._SetHandle(eventElement.offsetX);
        var width = parseInt(this.slider.offsetWidth);
        var percentagePosition = eventElement.offsetX / width;
        var time = parseInt(this.movie.GetDuration() * percentagePosition);
        this.movie.SetTime(time);
        //this.movie.Play();
        if (!this.showTimer)
            this.ShowTime();
        
        this._SetHandlePosition(time);
        if (this.SliderChanged)
            this.SliderChanged(time);
    },
    
    OnKeyPressed: function(eventArgs) {
        if (this.KeyPressed)
            this.KeyPressed(eventArgs);
        this._SetHandlePosition(this.movie.GetTime());
        
    },
    OnKeyUp: function(eventArgs) {
        if (this.KeyUp)
            this.KeyUp(eventArgs);
    },
    
    ShowTime: function(){
        /// <summary>Called when the qt_timechanged event was called. Changes the slider position</summary>
        if (this.movie.GetTime && this.movie.GetDuration) {
	    var curTime = 0;
     	    try {
                curTime = this.movie.GetTime();
            	this._SetHandlePosition(curTime);
	        if (curTime == this.movie.GetDuration()) {
        	    if (this.VideoCompleted)
                	this.VideoCompleted();
   	             return;
        	} 
	    } catch (exc) {  }
        }
        var showTimeDelegate = Function.createDelegate(this, this.ShowTime);
        this.showTimer = window.setTimeout(showTimeDelegate, 100);
    },
    
    ShowProgress: function(){
        /// <summary>Called when the qt_progress event was called. Changes the loading bar</summary>
        try {
            var percentLoaded = 0 ;
	    if (!this.movie.GetMaxTimeLoaded) {
		this.methodErrorCount++; 
		if (this.methodErrorCount > 3)
		    throw 'Quicktime controls cannot be accessed. That may be due to your security settings.';
            }
	    else {
                percentLoaded = (this.movie.GetMaxTimeLoaded() / this.movie.GetDuration()) * 100;
                this.loadingBar.style.width = parseInt(percentLoaded) + "%";
                if (percentLoaded >= 100)
                    return;
                }
        } catch (e) {
	    //if (ReInit(e))
            //    return;
        }
        
        var loadDelegate = Function.createDelegate(this, this.ShowProgress);
        this.showProgess = window.setTimeout(loadDelegate, 200);
    },
    
//    myAddListener: function(obj, evt, handler, captures)
//    {
//        if ( document.addEventListener )
//            obj.addEventListener(evt, handler, captures);
//        else
//            obj.attachEvent('on' + evt, handler);
//    },
//    
//    AddQtEvent: function(eventName, target) {
//        /// <summary>Adds a quicktime event to the object</summary>
//        /// <param name="eventName">The event name to register</param>
//        /// <param name="target">The target delegate to call</param>
//        var obj = document.getElementById(this.movieId);
//        if ( !obj )
//            obj = document.getElementById(this.movieId + "_embedded");
//        if ( obj )
//            this.myAddListener(obj, eventName, target, false);
//    },
    
    Init: function() {
        /// <summary>Initalizes the slideshow</summary>
        if (!this.parent) {
            alert('manathanWeb.Movie.SlideBar.Init() error - Parent element not found.');
            return;
        }
        try {
//            var showProgressDelegate = Function.createDelegate(this, this.ShowProgress);
//            var showTimeDelegate = Function.createDelegate(this, this.ShowTime);
//            this.AddQtEvent('qt_progress', showProgressDelegate);
//            this.AddQtEvent('qt_timechanged', showTimeDelegate);
            
            var sliderChangedDelegate = Function.createDelegate(this, this.OnSliderChanged);
            this.slider = this.parent;
            $addHandler(this.slider, 'click', sliderChangedDelegate);
            
            this.rail = $get("rail", this.parent);
            sliderChangedDelegate = Function.createDelegate(this, this.OnHandleClick);
            $addHandler(this.rail, 'click', sliderChangedDelegate);
            
            this.loadingBar = $get("loaded", this.parent);
            this.progressBar = $get("progress", this.parent);
            
            this.keyDelegate = Function.createDelegate(this, this.OnKeyPressed);
            var parent = this.parent.parentNode;
            //$addHandler(this.parent, 'keypress', keyDelegate);
            this.keyUpDelegate = Function.createDelegate(this, this.OnKeyUp);
            $addHandler(document, 'keypress', this.keyDelegate);
            $addHandler(document, 'keyup', this.keyUpDelegate);
            
            this.ShowProgress();
            
        } catch (e) {
            alert("manathanWeb.Movie.SlideBar.Init() error - " + e.message);
        }
    }
}

manathanWeb.Movie.SlideBar.registerClass('manathanWeb.Movie.SlideBar');
