﻿var VIDEO_SCHEMA = "http://www.SimpleConnexion.com/app/video/1.0";

function createVideoMessage(id, videoid, viewed){
    var m = new Message();
    m.id = id;
    m.command = "sharevideo";
    m.schema = VIDEO_SCHEMA;
    m.attributes.set("videoid", videoid);
    m.attributes.set("provider", "youtube");
    if(typeof(viewed) != "undefined"){
        m.attributes.set("viewed", viewed);
    }
    return m;
}

function createVideoApplication(_connection, msgDisplay) {
    return new VideoApplication(_connection, msgDisplay);
}

function VideoApplication(_connection, msgDisplay) {
    
    this.objectId = generateID("VideoAPP");
    this.connection = _connection;
    this.displayPanel = msgDisplay;
    this.name = "Video Application";
    this.appType = "video";

    _connection.messageListeners.addListener(new MessageListener("msg2", "notify", "sharevideo", VIDEO_SCHEMA, this.handleMessages, this));
    //_connection.messageListeners.addListener(new MessageListener("app", this.appType, "*", this.handleMessages, this));
    window[this.objectId] = this;
}


VideoApplication.prototype = {
    handleMessages: VID_handleMessages,
    shareVideo: VID_ShareVideo
}

function VID_handleMessages(sender, msg, targetObj) {
    if (typeof (msg.provider) != "undefined") {
        if (msg.provider == "youtube") {
            if (typeof (msg.videoid) != "undefined") {
                if (typeof (msg.viewed) == "undefined") {
                    targetObj.displayPanel.writeHtml("Video> Stranger suggested <a onclick='noPrompt()' href='javascript:playSuggestedVideo(\"" + targetObj.objectId + "\",\"" + msg.videoid + "\", true)' + >this video</a>.", "DisplayMessageLine_AppNotif", false);
                } else if(msg.viewed){
                    targetObj.displayPanel.writeHtml("Video> Stranger is viewing <a onclick='noPrompt()' href='javascript:playSuggestedVideo(\"" + targetObj.objectId + "\",\"" + msg.videoid + "\", false)' + >suggested video</a>.", "DisplayMessageLine_AppNotif", false);
                }
            }
        }
    }
}

function playSuggestedVideo(appId, vid, tellPeer) {
    var vidApp = window[appId];
    if (vidApp != null) {
        if (tellPeer) {
            var vm = createVideoMessage(vidApp.connection.nextMessageID(),vid, '1');
            vidApp.connection.postMessageObject(vm);
            //vidApp.connection.postMessage("<app type='video' provider='youtube' vid='" + vid + "' viewed='1' />");
        }
        vidApp.displayPanel.writeHtml("Video will be displayed in few seconds, please wait until it loads.", "DisplayMessageLine_AppNotif", false);
        //apTabs.show($('toolboxTabs'), 5);
        //apTabs.scroll($('toolboxTabs'), 'last');
        playThisVideo(vid);
    }
}

function VID_ShareVideo(vid) {
    var vm = createVideoMessage(this.connection.nextMessageID(), vid);
    this.connection.postMessageObject(vm);
    this.displayPanel.writeHtml("Video> You suggested a video.", "DisplayMessageLine_AppNotif");
}
