﻿var videoInformation = null;
var videoInfoDataObject = null;
var userInformation = null;
var userInfoDataObject = null;
document.observe("dom:loaded", initPage);

function initPage() {
    userInformation = new UserInformation();
    userInfoDataObject = userInformation.GetByID();
    if (userInfoDataObject == null)
        $('dvRegister').show();
}

function validateVideo() {
    $('videoURLerr').hide();
    if ($('txtVideoURL').value == "") {
        $('videoURLerr').update("Please enter a valid (non empty) Video URL");
        $('videoURLerr').show();
        return;
    }
    $('vidURLLoader').show();
    var RESTurl = '/Request/VideoInformation?pageUrl=' + escape($('txtVideoURL').value) + "&product=captions&force=true";
    new Ajax.Request(RESTurl, {
        method: 'post',
        asynchronous: true,
        onSuccess: function(transport) {
            videoInformation = transport.responseText.evalJSON();
            validateVideoResponse(videoInformation);
        },
        onFailure: function(transport) {
            $('videoURLerr').update("Video URL is not suported");
            $('videoURLerr').show();
            $('verifyInputIcon').addClassName('verifyBad');
            $('vidURLLoader').hide();
        }
    });
}

function validateVideoResponse(videoInfoDataObject) {
    $('vidURLLoader').hide();
    if (!videoInfoDataObject.Valid) {
        $('videoURLerr').update("Video URL is not suported");
        $('videoURLerr').show();
        $('verifyInputIcon').addClassName('verifyBad');
        return;
    }
    
    $('vidDetails').removeClassName('oCompGray');
    $('vidSourceBox').hide();
    $('vidSourceRO').update(videoInfoDataObject.PageUrl);
    $('vidSourceRO').show();
    createEmbed(videoInfoDataObject);
    $('videoId').value = videoInfoDataObject.VideoId;
    //Show Extra Details if Title is Missing
    if (videoInfoDataObject.Title != null && videoInfoDataObject.Title != 'Unknown') {
        $('vidDetailsBox').hide();
        $('vidDetailsRO').show();
        $('videoLockedTitle').update(videoInfoDataObject.Title);
        $('videoTitle').value = videoInfoDataObject.Title;
        $('videoTitle').disabled = 'disabled';
    } else {
        $('vidDetailsBox').show();
        $('vidDetailsRO').hide();
    }
    if (videoInfoDataObject.Description != null) {
        $('videoDescription').value = videoInfoDataObject.Description;
        $('videoDescription').disabled = 'disabled';
    }
    if (videoInfoDataObject.PrimaryLanguage != null) {
        $('cmbPrimaryLang').selectedIndex = dropdownFindByValue($('cmbPrimaryLang'), videoInfoDataObject.PrimaryLanguage);
        //$('videoPrimLang').disabled = 'disabled';
        $('SpokenLanguageLocked').update(dropdownGetText($('cmbPrimaryLang'), $('cmbPrimaryLang').value));
    } else {
        $('vidDetailsBox').show();
        $('vidDetailsRO').hide();
    }
    if (videoInfoDataObject.PrimaryLanguage != null && videoInfoDataObject.Title != null) {
        show3();
    }
}

function createEmbed(video) {
    if (video.Domain == "YouTube") {
        var embed;
        var videoId;
        videoId = video.VideoId.replace(/\-/gi, '');
        embed = '<object width="240" height="180"><param name="wmode" value="opaque"/><param name="movie" value="http://www.SubPLY.com/players/YouTube/embed/v' + videoId + '"></param><param name="allowFullScreen" value="true"></param>\r\n';
        embed = embed + '<embed wmode="opaque" src="http://www.SubPLY.com/players/YouTube/embed/v' + videoId + '" type="application/x-shockwave-flash" width="240" height="180" allowFullScreen="true"></embed></object>';
        $('videoPlayer').update(embed);
    } else {
    var s1 = new SWFObject("/en/video/JWPlayer.swf", "ply", "240", "180", "9", "#333333");
        s1.addParam("allowfullscreen", "true");
        s1.addParam("allowscriptaccess", "always");
        s1.addParam("wmode", "opaque");
        s1.addParam("flashvars", "file=" + video.VideoUrl + "&plugins=PLY_AS3");
        s1.write("videoPlayer");
    }
}

function setVideoDetails() {
    $('errTitle').hide();
    if ($('videoTitle').value == "") {
        $('errTitle').show();
        return;
    }
    $('videoLockedTitle').update($('videoTitle').value);
    $('errPSpokenLang').hide();
    if ($('cmbPrimaryLang').value == '') {
        $('errPSpokenLang').show();
        return;
    }
    $('SpokenLanguageLocked').update(dropdownGetText($('cmbPrimaryLang'), $('cmbPrimaryLang').value));
    show3();
}

function startWork() {
    $('errValidOutput').hide();
    if ($('captioningLang').value == '') {
        $('errValidOutput').show();
        return;
    }
    var postUrl;
    postUrl="/Video/AddVideo?txtVideoId=" + $('videoId').value + "&txtVideoTitle=" + $('videoTitle').value + "&txtVideoDescription=" + $('videoDescription').value + "&primeLang=" + $('cmbPrimaryLang').value + "&captioningLang=" + $('captioningLang').value;
    document.location.href = postUrl;
}

function show1() {
    hideErrors();
    $('vidDetails').addClassName('oCompGray');
    $('vidDetailsBox').hide();
    $('vidSourceBox').show();
    $('vidSourceRO').hide();
}

function show2() {
    hideErrors();
    $('vidDetails').removeClassName('oCompGray');
    $('vidDetailsBox').show();
    $('vidSourceBox').hide();
    $('vidSourceRO').show();
    $('vidCaptioningLangBox').hide();
    $('vidCaptioningLang').addClassName('oCompGray');
    validateVideo();
}

function show3() {
    hideErrors();
    $('vidDetailsBox').hide();
    $('vidDetailsRO').show();
    $('vidCaptioningLang').removeClassName('oCompGray');
    $('vidCaptioningLangBox').show();
}

function hideErrors() {
    $('errTitle').hide();
    $('errValidOutput').hide();
    $('errPSpokenLang').hide();
    $('errValidOutput').hide();
}



