﻿var VIDEOSCRIPT_KEY = "123";
var VIDEOSCRIPT_BEFORE = "<p>Для просмотра необходимо ввести ключ:</p>";
var VIDEOSCRIPT_AFTER = "<p>Ключ для просмотра видео вы можете найти в конце каждого письма нашей рассылки.</p><p><a href='http://www.freekey.autocad-lessons.ru' target='_blank'>Получить ключ бесплатно</a></p>";
var VIDEOSCRIPT_WRONGKEY = "<p>Неправильный ключ</p>";
var VIDEOSCRIPT_ENTERKEY = "<p><a href='http://www.freekey.autocad-lessons.ru' target='_blank'>Получить ключ к видео бесплатно</a></p>";

jQuery(function () {
    function videoScriptCheckKey(){
        if ( VIDEOSCRIPT_KEY == input.val() ) {
            jQuery(popupWrapper).hide();
            jQuery('.videoscript-image').hide();
            jQuery(popupWrapper.data('el')).show();
            wrongkey.hide();
        } else {
            input.val('');
            wrongkey.show();
        }
    }

    var popupWrapper = jQuery("<div id='videoscript-popupWrapper'></div>").css({
        backgroundColor: 'rgba(100,100,100,0.6)',
        width: '100%',
        height: '100%',
        position: 'fixed',
        left: 0,
        top: 0,
        display: 'none'
    }).click(function(e){
        if(e.target.id == 'videoscript-popupWrapper') popupWrapper.hide();
    }).appendTo('body');
    var popup = jQuery("<div id='videoscript-popup'></div>").html(VIDEOSCRIPT_BEFORE).css({
        backgroundColor: 'rgb(255,255,255)',
        width: 380,
        minHeight: 180,
        position: 'absolute',
        left: '50%',
        top: '50%',
        marginTop: -100,
        marginLeft: -200,
        borderRadius: 10,
        padding: 10,
        textAlign: 'center',
        fontSize: 18
    }).appendTo(popupWrapper);
    var input = jQuery('<input/>', { type: 'password', id: 'videoscript-key' }).keypress(function (event) {
        if (event.which == 13) {
            videoScriptCheckKey();
        }
    }).appendTo(popup);
    jQuery('<input type="button" value="OK">').click(function(e){
        videoScriptCheckKey();
    }).appendTo(popup);
    jQuery( VIDEOSCRIPT_AFTER ).appendTo(popup);
    var wrongkey = jQuery('<div id="videoscript-wrongkey"/>').html(VIDEOSCRIPT_WRONGKEY).css({
        display: 'none'
    }).appendTo(popup);
    jQuery("<img src='/youtube_close_button.png' width='16' height='16' alt='close' style='cursor:pointer;position:absolute;top:7px;right:7px;'>").
        click(function(){
            popupWrapper.hide()
        }).appendTo(popup);
    
    jQuery('object').filter(function(i){
        var param = jQuery(this).find('param[name="movie"]');
        if ( param && param[0].value.indexOf('http://www.youtube.com/') == 0 ) return true;
    }).each(function(i,el){
        jQuery(el).wrap('<div class="videoscript-wrapper"></div>');
        var param = jQuery(el).children('param[name="movie"]');
        var id = param.val().substring( 25, param.val().indexOf('?') );
        var imgPre = 'http://img.youtube.com/vi/';
        var imgSrc = imgPre + id + '/0.jpg';
        var embed = jQuery(el).children('embed');
        jQuery(el).hide().after( jQuery("<div class='videoscript-image'/>").css({
                width: embed.width(),
                height: embed.height(),
                cursor: 'pointer',
                position: 'relative'
            }).click(function () {
                popupWrapper.data({'el':el}).show();
            }).append("<img src='" + imgSrc + "' alt='screenshot' style='position:absolute;top:0;left:0' width='100%' height='100%'><img src='/youtube_play_button.png' alt='' width='100' height='72' style='position:absolute;top:50%;left:50%;margin-top:-36px;margin-left:-50px'>" +VIDEOSCRIPT_ENTERKEY)
        );
    });
    jQuery('.videoscript-image').children('p').css({
        position: 'absolute',
        bottom: -30,
        left: 0
    });
});
