﻿VideoKontrol = function(obj,active,PN) 
{
    this.pn=PN;
	this.fullstate="";
	this.mark = "00:00:00.0";
	this.name = obj.name;
	var prm = obj.Tag.split("|");
	this.repeat = prm[1];
	this.auto = prm[2];
	this.type = prm[3];
	this.first = prm[4];
	obj.visibility = "Collapsed";
	this.setevents(obj, active);
}
VideoKontrol.prototype =
{
    setevents: function(obj, active) {
        this.Video = obj;
        this.ba = active;
        this.playhide = obj.findName("PlaySymbol_Hide" + this.name);
        this.playshow = obj.findName("PlaySymbol_Show" + this.name);
        this.pausehide = obj.findName("PauseSymbol_Hide" + this.name);
        this.pauseshow = obj.findName("PauseSymbol_Show" + this.name);
        this.PlayButton = obj.findName("PlayPauseButton" + this.name);
        if (this.PlayButton) {
            this.token1 = this.PlayButton.addEventListener("mouseEnter", Silverlight.createDelegate(this, this.onPlayButtonMouseEnter));
            this.token2 = this.PlayButton.addEventListener("mouseLeave", Silverlight.createDelegate(this, this.onPlayButtonMouseLeave));
            this.token3 = this.PlayButton.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, this.onPlayButtonMouseDown));
            this.token4 = this.PlayButton.addEventListener("mouseLeftButtonUp", Silverlight.createDelegate(this, this.onPlayButtonMouseUp));
            this.token5 = this.Video.addEventListener("CurrentStateChanged", Silverlight.createDelegate(this, this.onCurrentStateChanged));
        }
        this.token6 = obj.addEventListener("MediaEnded", Silverlight.createDelegate(this, this.onMediaEnded));
        this.token7 = obj.addEventListener("MediaOpened", Silverlight.createDelegate(this, this.autoplay));
        this.token8 = obj.addEventListener("mouseEnter", onHC);
        this.token9 = obj.addEventListener("mouseLeave", offHC);
    },
    onMediaEnded: function() {
        this.end = "1";
        this.mLog = 0;
        SendLog((this.type == 1 ? 24 : 29), this.Video.name);
        if (this.repeat == "1") {
            this.Video.Position = "00:00:00.0";
            this.Video.play();
        }
    },
    autoplay: function() {
        if (this.Video) {
            this.Video.Position = this.mark;
            if (this.ba && this.auto == "1") {
                this.Video.visibility = "Visible";
                this.Video.play();
            }
        }
    },
    stop: function() {
        if (this.Video) {
            this.mark = this.Video.Position;
            this.Video.Pause();
        }
    },
    release: function() {
        this.stop();
        if (this.PlayButton) {
            this.PlayButton.RemoveEventListener("mouseEnter", this.token1);
            this.PlayButton.RemoveEventListener("mouseLeave", this.token2);
            this.PlayButton.RemoveEventListener("mouseLeftButtonDown", this.token3);
            this.PlayButton.RemoveEventListener("mouseLeftButtonUp", this.token4);
            this.Video.RemoveEventListener("CurrentStateChanged", this.token5);
        }
        this.Video.RemoveEventListener("MediaEnded", this.token6);
        this.Video.RemoveEventListener("MediaOpened", this.token7);
        this.Video.RemoveEventListener("mouseEnter", this.token8);
        this.Video.RemoveEventListener("mouseLeave", this.token9);
        this.Video = null;
    },
    onCurrentStateChanged: function(s, e) {
        var _state = this.Video.CurrentState;
        if (_state == "Playing" || _state == "Buffering") {
            if (this.mLog != 1 && _state == "Playing") {
                SendLog((this.type == 1 ? 23 : 28), this.Video.name);
                this.mLog = 1;
            }
            this.playhide.begin();
            this.pauseshow.begin();
        } else {
            this.pausehide.begin();
            this.playshow.begin();
        }
    },
    onPlayButtonMouseEnter: function(s, e) {
        onHC();
        mAnim = this.Video.findName("PlayPauseButton_MouseEnter" + this.name);
        mAnim.begin();
    },
    onPlayButtonMouseLeave: function(s, e) {
        mAnim = this.Video.findName("PlayPauseButton_MouseLeave" + this.name);
        mAnim.begin();
    },
    onPlayButtonMouseDown: function(s, e) {
        mAnim = this.Video.findName("PlayPauseButton_MouseDown" + this.name);
        mAnim.begin();
        var _state = this.Video.CurrentState;
        if (_state == "Playing" || _state == "Buffering")
            this.Video.pause();
        else {
            if (this.end == "1") this.Video.Position = "00:00:00.0";
            this.Video.visibility = "Visible";
            this.Video.play();
            this.end = "0";
        }
    },
    onPlayButtonMouseUp: function(s, e) {
        mAnim = this.Video.findName("PlayPauseButton_MouseUp" + this.name);
        mAnim.begin();
    }
}