ctv.ca.SlVideoWindow = function (parentVideoWindow, parentElement) {
    this.parentVideoWindow = parentVideoWindow;
    this.movieControl = null;
    this.plugin = null;
    this.bugImage = null;
    
    this.loaded = false;
    this.url = null;
    this.volume = .5;
    this.positionTimer = null;
    this.root = null;
    this.timeLastLeftButtonUp = 0;
    
    this.playerOriginX = 0;
    this.playerOriginY = 0;
    this.playerOriginW = 416;
    this.playerOriginH = 312;
    

    Silverlight.createObjectEx({source: appPath + '/xaml/videoWindow.xml', 
                                    parentElement:parentElement,
                                    id:'slPlayer', 
                                    
                                    //transparent
                                    //properties:{width:"416", height:"312", background: '#00000000', isWindowless:'true', framerate:"24", version:'1.0'},
                                    
                                    //black blackground
                                    properties:{width:"416", height:"312", background: '#000000', isWindowless:'true', framerate:"24", version:'1.0'},
                                    events:{onError:Function.createDelegate(this, this.onError), onLoad:Function.createDelegate(this, this.onLoad)},
                                    context:null});
}

ctv.ca.SlVideoWindow.prototype = {
    onLoad : function(control, userContext, rootElement) {
        this.root = rootElement;
        this.plugIn = control;      // Store the host plug-in
        this.movieControl = rootElement.findName("mediaElement");
        this.bugImage = rootElement.findName("bugImage");
        this.loaded = true;
        this.currentPosition = 0;
        this.movieControl.source = this.url;
        this.getPosition();
        
        //events
        this.plugIn.content.OnFullScreenChange = fullScreenChangedEvent;
        this.movieControl.addEventListener("CurrentStateChanged", Function.createDelegate(this, this.stateChange)); 
        this.movieControl.addEventListener("MediaOpened", Function.createDelegate(this, this.mediaOpened)); 
        this.movieControl.addEventListener("MediaEnded", Function.createDelegate(this, this.mediaEnded)); 
        this.movieControl.addEventListener("MediaFailed", Function.createDelegate(this, this.mediaError)); 
        this.movieControl.addEventListener("MouseLeftButtonUp", Function.createDelegate(this, this.onLeftButtonUp)); 

        this.parentVideoWindow.onReady(); 
    },
	
	onError:function(sender, errorArgs) {
		alert(errorArgs);
	},
    
    playUrl : function (url) {
        if (this.loaded)
        {
            try {
                //debug("sl playing url: " + url);
                this.movieControl.source = url;
                this.getPosition();
            } catch (e) 
			{
				//debug(e)
			}
        }
        else
        {
            this.url = url;
        }
    },
    
    play : function () {
        try {
            this.movieControl.Play();
            this.getPosition();
        } catch (e) {}
    },
    
    stop : function () {
        try {
            this.movieControl.Stop();
        } catch (e) {}
        clearTimeout(this.positionTimer);        
    },
    
    pause : function () {
        try {
            this.movieControl.Pause();
        } catch (e) {}
        clearTimeout(this.positionTimer);
    },
    
    mute : function (goMute) {
        if (goMute)
        {
            this.movieControl.volume = 0;
        }
        else
            this.movieControl.volume = this.volume;
    },
    
    setVolume : function (value) {      
        this.volume = value/100;
        this.movieControl.volume = value/100;
    },
    
    getMediaLength: function () {
        return this.movieControl.naturalDuration;
    },
    
    getPosition: function () {
        clearTimeout(this.positionTimer);
        try {
            this.currentPosition = this.movieControl.position.seconds;            
	        this.parentVideoWindow.onPosition(this.currentPosition);
	    } catch(e) {
            this.parentVideoWindow.onPosition(0);
	    }
        var thisObj = this;
        this.positionTimer = setTimeout(function() { thisObj.getPosition() },500);
    },
    
    gotoPosition : function (pos) {
        this.set_time(pos);
    },
    
    fullScreen : function (gotoFullScreen) {
        if (this.plugIn.content.fullScreen == gotoFullScreen)
            return;
            
        if (gotoFullScreen)
            alert("Double click the video to go full screen!");
        else
            this.plugIn.content.fullScreen = false;
    },
    
    get_time: function () {
        return this.movieControl().position.seconds;
    },
    
    set_time: function (time) {
	    var position = this.movieControl.position;
        position.seconds = time;
        this.movieControl.position = position;
    },
    
    show : function (visible) {
        try {
            if (visible) {
        	    this.plugIn.Opacity = 1; 
            } else {
                this.plugIn.Opacity = 0; 
            }
        } catch (e) {}
    },
    
    stateChange : function() {
        var state = this.movieControl.currentState;
        //debug(state);
        this.showOverlay(false);
        
        if (state == "Stopped")
            this.parentVideoWindow.onPlaystateChange(1);
        else if (state == "Paused")
            this.parentVideoWindow.onPlaystateChange(2);
        else if (state == "Playing") {
            this.parentVideoWindow.onPlaystateChange(3);
        }
        else if (state == "Buffering")
            this.parentVideoWindow.onPlaystateChange(6);
        else if (state == "Opening")
            this.parentVideoWindow.onPlaystateChange(9);
        else if (state == "Error")
            this.mediaError(); 

    },
    
    mediaOpened : function() {
        this.parentVideoWindow.onPlaystateChange(10);
		this.parentVideoWindow.isLive = false;
		if (this.movieControl.naturalduration.seconds == 0)
			this.parentVideoWindow.isLive = true;
        this.parentVideoWindow.onMediaLength(this.movieControl.naturalduration.seconds);
    },

    mediaEnded : function() {
        this.movieControl.Stop();
        this.parentVideoWindow.onPlaystateChange(8);    
    },
    
    mediaError : function () {    
        this.parentVideoWindow.onPlaystateChange(10000); 
    },
    
    onLeftButtonUp: function (sender, eventArgs) {
        var tmNow=new Date().getTime();
        if (this.timeLastLeftButtonUp !== 0) {
            var msPassed = tmNow - this.timeLastLeftButtonUp;
            if (msPassed < 300) {
                this.onLeftButtonDoubleClick(sender, eventArgs);
                return;
            }
        }
        this.timeLastLeftButtonUp = tmNow;
    },
    
    onLeftButtonDoubleClick: function (sender, eventArgs) {
         this.timeLastLeftButtonUp = 0;
         this.plugIn.content.fullScreen=!this.plugIn.content.fullScreen;
    },
    
    fullScreenChangedEvent : function (sender, eventArgs) {
        var reSizer = this.plugIn.content.findName("reSizer");
        
        if (this.plugIn.content.fullScreen) {
            this.plugIn.opacity = 0;
            reSizer.ScaleX = this.plugIn.content.actualWidth/this.plugIn.content.findName("root").width;
            reSizer.ScaleY = this.plugIn.content.actualHeight/this.plugIn.content.findName("root").height;
        }
        else {
            this.plugIn.opacity = 1;
            reSizer.ScaleX = 1;
            reSizer.ScaleY = 1;
        }
        
        return;    

    },
    
    setOverlay : function (imageUrl, visible) {
        return;
        if (this.bugImage != null)
        {
            this.bugImage.source = imageUrl;
            
            if (visible)
            {
                this.bugImage.opacity = 0.2;
            }
            else
            {
                this.bugImage.opacity = 0; 
            }
        }
    },
    
    showOverlay : function (show) {
        if (this.bugImage == null)
            return;
            
        if (show)
            this.bugImage.opacity = 0.2;
        else
            this.bugImage.opacity = 0;
            
    },
    
    cleanUp : function () {    	
    	
    	try{
        	this.movieControl.stop();
        	this.movieControl.source = null;  
        }
        catch(e)
        {}
        
        //this.movieControl = null;
        //this.plugIn = null;
        //this.root = null;        
    }
    
}

ctv.ca.SlVideoWindow.registerClass('ctv.ca.SlVideoWindow', Sys.Component);

function fullScreenChangedEvent (sender, eventArgs)
{
    try {
        ctvVideoWindow.videoWindowObj.fullScreenChangedEvent(sender, eventArgs);
    }
    catch (e) {}
}
