﻿function ZooTubePlayer(parentId) {
    try {
    	eval('background:document.body.style.backgroundColor="#ck"');
    } catch(e){}

    this._hostname = ExpressionPlayer.Player._getUniqueName("xamlHost");
    Silverlight.createObjectEx( {source: '/video/player/player.xaml',
                                  parentElement: $get(parentId),
                                  id: this._hostname, 
//                                  properties: { width:'100%', height:'100%', version:'1.0', background:document.body.style.backgroundColor, isWindowless:'false', inplaceInstallPrompt:true },
                                  properties: { width: '100%', height: '100%', version: '1.0', isWindowless: 'false', inplaceInstallPrompt: true },
                                  events: { onLoad: Function.createDelegate(this, this._handleLoad) }
                                });
}

ZooTubePlayer.prototype = {
    _handleLoad: function(plugIn, userContext, rootElement) {
        this._currentMediainfo = 0;
		this.mediaElement = rootElement.findName("VideoWindow");
        this._player = $create(   ExtendedPlayer.Player, 
                                  { // properties
                                    autoPlay       : false, 
                                    autoCue        : true,
				                    scaleMode 	   : this.scaleModeParam(),
                                    muted          : storedMutedParam(),
				                    enableCaptions : this.enableCaptionsParam(),
                                    volume         : storedVolumeParam()
                                  }, 
                                  { // event handlers
                                    mediaOpened: Function.createDelegate(this, this._onMediaOpened),
                                    mediaEnded: Function.createDelegate(this, this._onMediaEnded),
                                    mediaFailed: Function.createDelegate(this, this._onMediaFailed)
                                  },
                                  null, $get(this._hostname)  );   

        var currentVideo = readCookie("tube_current");
        if(currentVideo == "-1") {
            // no current video, start with tubey roulette
	        this._currentIsTubey = true;
            this._currentAutoPlay = false;
            this._playlist = [{"mediaSource" : "/ZooTube/Tubey.wmv", "placeholderImageSource" : "", "chapters" : []}];
	        this._playNextVideo();
        } else {
            // current video, load that one and skip to saved frame
	        this._currentIsTubey = false;
            this._currentAutoPlay = false;
            this._playlist = [{"mediaSource" : "/MediaBinHandler.ashx?action=get-homepage-video", "placeholderImageSource" : "", "chapters" : []}];
            this._loadPlaceholder();
    	    this._playNextVideo();
        }
        this._tickBlock = true;
        this._tickTimer();
    },

    chooseVideo: function() {
        this._currentMediainfo = 0;
        this._currentIsTubey = true;
        this._currentAutoPlay = true;
        this._playlist = [{"mediaSource" : "/ZooTube/tubey.wmv", "placeholderImageSource" : "", "chapters" : []}];
	    this._playNextVideo();
    },

    _onMediaOpened: function(sender, eventArgs) {
        if(this._currentAutoPlay) {
            this._autoPlay();
        } else {
            this._videoLoaded();
        }
        this._tickBlock = false;
    },

    _videoLoaded : function() {
        if(!this._currentIsTubey) {
            var tube_current = readCookie("tube_current");
            if(tube_current == null)
                tube_current = "0";
            var tube_current_fl = parseFloat(tube_current);
            if(tube_current_fl == 0) {
            }
            this._player.set_timeIndex(tube_current_fl);
        }
    },

    _tickTimer: function() {
        if(!this._tickBlock) {
            if(this._currentIsTubey) {
                createCookie("tube_current", -1);
            } else {
                var current = ExpressionPlayer.Player.callBaseMethod(this._player, 'get_position');
                if(current > 0)
                    this.mediaElement.findName("ThumbImage").SetValue("Source", "");
                createCookie("tube_current", current);
            }
            createCookie("tube_volume", ExpressionPlayer.Player.callBaseMethod(this._player, 'get_volume'));
            createCookie("tube_muted", ExpressionPlayer.Player.callBaseMethod(this._player, 'get_muted'));
        }
        window.setTimeout(Function.createDelegate(this, this._tickTimer), 200);
    },

    _onMediaEnded: function(sender, eventArgs) {
        this._currentMediainfo = 0;
        if(this._currentIsTubey) {
	        this._currentIsTubey = false;
            this._currentAutoPlay = true;
            this._playlist = [{"mediaSource" : "/MediaBinHandler.ashx?action=get-next-homepage-video", "placeholderImageSource" : "", "chapters" : []}];
    	    this._playNextVideo();
        } else {
	        this._currentIsTubey = true;
            this._currentAutoPlay = false;
            this._playlist = [{"mediaSource" : "/ZooTube/tubey.wmv", "placeholderImageSource" : "", "chapters" : []}];
    	    this._playNextVideo();
        }
    },

    _loadPlaceholder: function() {
        this.mediaElement.findName("ThumbImage").SetValue("Source", "/MediaBinHandler.ashx?action=get-homepage-still");
    },

    _autoPlay: function() {
        this._player.play();
    },

    _playNextVideo: function() {
        if (this._playlist!=null) {
            if (this._currentMediainfo<this._playlist.length)
                this._player.set_mediainfo( this._playlist[ this._currentMediainfo++ ] );
        }
    },

    _onClickGalleryItem : function (galleryItemIndex) {
        this._player.set_mediainfo( this._playlist[ galleryItemIndex ] ); 
        this._currentMediainfo = galleryItemIndex+1;
    },

    _onMediaFailed: function(sender, eventArgs) {
        alert(String.format( Sys.UI.Silverlight.MediaPlayer.Res.mediaFailed, this._player.get_mediaSource() ) );
    },

    scaleModeParam: function() {
	    var scaleMode = 1/*Normal*/;
	    try {
		    eval("scaleMode=1;");
	    } catch(e){}
	    return scaleMode;
    },

    enableCaptionsParam: function() {
	    var enableCaptions=true;
	    try {
		    eval("enableCaptions=('True'!=='False');");
	    } catch(e){}
	    return enableCaptions;
    }
}