﻿var videoInformation = null;
var videoInfoDataObject = null;

var AddVideoComponent = Class.create({

    initialize: function() {
        this.initDialog();
        this.bindFunctions();
        this.attachEvents();
        this.videoInformation = new VideoInformation();
        this.params = window.location.toString().toQueryParams();
    },

    initDialog: function() {
        if (!window.dialog) {
            this.dialog = new Dialog('');
            $('divDialogContainer').setOpacity(0.7);
            $('divDialogContent').setStyle({ top: "2px" });
            $('divDialogContainer').setStyle({ zIndex: "50001" });
            window.dialog = this.dialog;
        }
        else {
            this.dialog = window.dialog;
        }
    },

    bindFunctions: function() {
        this.show = this.show.bind(this);
        this.hide = this.hide.bind(this);
        this.onKeyDown = this.onKeyDown.bind(this);
        this.getVideo = this.getVideo.bind(this);
        this.createEmbed = this.createEmbed.bind(this);
    },

    attachEvents: function() {
        if ($('buttonAddVideo')) {
            $('buttonAddVideo').observe('click', this.show)
        }
        if ($('closeAddDialog')) {
            $('closeAddDialog').observe('click', this.hide)
        }
        if ($('getVideoButton')) {
            $('getVideoButton').observe('click', this.getVideo)
        }
        if ($('btnContinue')) {
            $('btnContinue').observe('click', this.submitForm)
        }
        document.observe("keydown", this.onKeyDown);
    },

    onKeyDown: function(e) {
        if (e.keyCode == Event.KEY_ESC) {
            this.hide();
        }
    },

    clearInputs: function() {
        $('txtVideoURL').value = '';
        $('videoDescription').value = '';
        $('videoTitle').value = '';
        $('videoPrimLang').value = '';
    },

    hide: function() {
        if ($('enterVidstep2').visible()) {
            this.clearInputs();
        }
        $('errUrl').hide()
        $('enterVidstep2').hide();
        this.dialog.hide();
    },

    show: function() {
        $('divDialogContent').setStyle({ top: "2px" });
        this.dialog.setUsingAppendChild($('divEnterVideo'));
        this.dialog.show();

    },

    getVideo: function() {
        $('errUrl').hide();
        $('dvAddUrlNote').show();
        if ($('txtVideoURL').value == "") {
            $('enterVidstep2').hide()
            $('errUrl').show();
            return;
        }
        videoInfoDataObject = this.videoInformation.getByURL($('txtVideoURL').value, "captions", true);
        if (!videoInfoDataObject.Valid) {
            $('enterVidstep2').hide()
            $('errUrl').show();
            return;
        }
        $('dvAddUrlNote').hide();
        //Show Extra Details if Title is Missing
        if (videoInfoDataObject.Title != null) {
            $('dvExtraDetails').hide();
            $('dvExtraDetailsLocked').show();
            $('videoLockedTitle').value = videoInfoDataObject.Title;
            if (videoInfoDataObject.Description != null) {
                $('videoLockedDescription').value = videoInfoDataObject.Description;
                $('tdVideoDescription').update(videoInfoDataObject.Description);
            }
        } else {
            $('dvExtraDetails').show();
            $('dvExtraDetailsLocked').hide();
        }
        if (videoInfoDataObject.PrimaryLanguage != null) {
            $('videoPrimLang').selectedIndex = dropdownFindByValue($('videoPrimLang'), videoInfoDataObject.PrimaryLanguage);
            $('videoPrimLang').disabled = 'disabled';
        }
        $('videoId').value = videoInfoDataObject.VideoId;
        this.createEmbed(videoInfoDataObject);
        if (!($('enterVidstep2').visible())) {
            $('enterVidstep2').show();
        }
    },

    submitForm: function() {
        $('errPrimeLanguage').hide();
        if ($('videoPrimLang').value == '') {
            $('errPrimeLanguage').show();
            return;
        }
        document.location.href = "/Video/AddVideo?txtVideoId=" + $('videoId').value + "&txtVideoTitle=" + $('videoTitle').value + "&txtVideoDescription=" + $('videoDescription').value + "&primeLang=" + $('videoPrimLang').value;
    },

    createEmbed: function(video) {
        if (video.Domain == "YouTube") {
            var embed;
            var videoId;
            videoId = video.VideoId.replace(/\-/gi, '');
            embed = '<object width="320" height="257"><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 src="http://www.SubPLY.com/players/YouTube/embed/v' + videoId + '" type="application/x-shockwave-flash" width="320" height="257" allowFullScreen="true"></embed></object>';
            $('addVideoContainer').update(embed);
        } else {
            var s1 = new SWFObject("/en/video/JWPlayer.swf", "ply", "320", "257", "9", "#FFFFFF");
            s1.addParam("allowfullscreen", "true");
            s1.addParam("allowscriptaccess", "always");
            s1.addParam("flashvars", "file=" + video.VideoUrl + "&plugins=PLY_AS3");
            s1.write("addVideoContainer");
        }
    }
});


