var gWebSocket = null;
var gVideo = null;
var gVideoInfo = {title:'h5title',url:'h5url'};
//var count = 0;
var gPlayer =null;

var Html5Client = function() {
    var a = this;
    this.init = function() {
        console.log('in init');
        findVideoElements();
        //Connect();
    };
    var Disconnect = function() {
        gWebSocket instanceof WebSocket && (gWebSocket.onopen = null, gWebSocket.onclose = null, gWebSocket.onerror = null, gWebSocket.close, gWebSocket = null)
    };
    var Connect = function() {
        //console.log('in Connect');
        try {
            gWebSocket = new WebSocket("ws://localhost:5053");
            gWebSocket.binaryType = "arraybuffer";
            gWebSocket.onopen = function() {
                console.log("ws://localhost:5053 open1"),
                RecvMsg()
            };
            gWebSocket.onerror = function() {
                console.log("ws://localhost:5053 onerror"),
                Disconnect()
            };
            gWebSocket.onclose = function() {
                console.log("ws://localhost:5053 onclose"),
                Disconnect()
            };
        } catch(b) {
            console.log("Websocket exception = " + b.message),
            Disconnect()
        }
    };
    findVideoElements = function() {
        //console.log('in findVideoElements');
        var videos = document.getElementsByTagName('video');
        if (videos.length > 0) {
            console.log('findVideoElements');
            gVideo = videos[0];
			Connect();
        }
    };
    RecvMsg = function() {
		
		if(gPlayer==null){
			gPlayer = new Html5MMRPlayer;
			gPlayer.initialize();
		}
		
    };
}

var gControl = new Html5Client();
gControl.init();