(function ($) {
    'use strict';

    var browserWindow = $(window);

    // :: 1.0 Preloader Active Code
    browserWindow.on('load', function () {
        $('#preloader').fadeOut('slow', function () {
            $(this).remove();
        });
    });

    // :: 2.0 Newsticker Active Code
    $.simpleTicker($("#breakingNewsTicker"), {
        speed: 1000,
        delay: 5000,
        easing: 'swing',
        effectType: 'roll'
    });
    $.simpleTicker($("#internationalTicker"), {
        speed: 1000,
        delay: 6000,
        easing: 'swing',
        effectType: 'roll'
    });

    // :: 3.0 Nav Active Code
    if ($.fn.classyNav) {
        $('#newspaperNav').classyNav();
    }

    // :: 4.0 Gallery Active Code
    // if ($.fn.magnificPopup) {
    //     $('.videoPlayer').magnificPopup({
    //         type: 'iframe'
    //     });
    // }

    // :: 5.0 ScrollUp Active Code
    if ($.fn.scrollUp) {
        browserWindow.scrollUp({
            scrollSpeed: 1500,
            scrollText: '<i class="fa fa-angle-up"></i>'
        });
    }

    // :: 6.0 CouterUp Active Code
    if ($.fn.counterUp) {
        $('.counter').counterUp({
            delay: 10,
            time: 2000
        });
    }

    $(document).ready(function () {
            
                 $('#cal').jalendar({                                                                                                                                      
                     color: '#fff',                                                                                                                                        
                     type: 'linker',                                                                                                                                      
                     customUrl: 'http://old.tv25.ge/allnews.php?lang=ge&date=',                                                                               
                     dateType: 'yyyy-mm-dd',                                                                                                                               
                     titleColor: '#666',                                                                                                                                   
                     dayColor: '#000',                                                                                                                                     
                     weekColor: '#BD0F11',                                                                                                                                 
                     todayColor: '#BD0F11',                                                                                                                                
                     lang:'KA' 
                 });                                                                                                                                                          
        if ($.fn.sticky) {
            console.log('sticky')
            $("#stickyMenu").sticky({
                topSpacing: 0
            });
        }
        $('img').error(function () {
			return
            console.log($(this).attr('src'));
            $(this).attr('src', '/images/noimage.png');
            console.log('active');
            $('.anounsment ul').addClass('list-group');
            $('.anounsment li').addClass('list-group-item');

        });
    });
    // :: 7.0 Sticky Active Code



    // :: 8.0 wow Active Code
    if (browserWindow.width() > 767) {
        new WOW().init();
    }

    // :: 9.0 prevent default a click
    $('a[href="#"]').click(function ($) {
        $.preventDefault()
    });

})(jQuery);
function changeAnouncment(event, self, id) {
    event.preventDefault();
    $('.navigator').removeClass('active');
    $('.anounsment').hide();

    $(self).addClass('active');
    $('#accor' + id).show('active');

}
console.log('active1');

$(document).ready(function () {
	//return
    //console.log('active list-group');
    //$('img').error(function () {
      //  $(this).attr('src', '/images/noimage.png');
    //});
    //$('.anounsment ul').addClass('list-group');
    //$('.anounsment li').addClass('list-group-item');
});

function PlayVideo(articletype, videoid, videorul, videocover, videotitle, videodate, lang) {
    var videorul;
    var videotitle;
    var videodate;
    var videohtml = '';
    var downloadstring = '';
    if (lang == 'ge') { downloadstring = 'ვიდეოს გადმოწერა!'; } else { downloadstring = 'Download Video!'; }
    if (lang == 'ge') { embedstring = 'Embed'; } else { embedstring = 'Embed'; }
    if (lang == 'ge') { sharedstring = 'გაზიარება'; } else { sharedstring = 'Share'; }
    videohtml += '<div class="videoBlock">';
    videohtml += '<div class="videoBack" onclick="javascript:CloseVideo();"></div>';
    videohtml += '<div class="videoPlayer">';
    console.log(videorul.indexOf('youtu.be'))
    if (videorul.indexOf('files') == -1) {
        console.log(videorul.indexOf('youtu.be'))
        if (videorul.indexOf('youtu.be')) {
           splited = videorul.split('/')
videohtml += '<iframe width="800" height="450" src="https://www.youtube.com/embed/' + splited[splited.length-1] + '?autoplay=1" frameborder="0" allowfullscreen></iframe>';
        } else {
  videohtml += '<iframe width="800" height="450" src="https://www.youtube.com/embed/' + videorul + '?autoplay=1" frameborder="0" allowfullscreen></iframe>';
        }

        videohtml += '<iframe width="800" height="450" src="https://www.youtube.com/embed/' + videorul + '?autoplay=1" frameborder="0" allowfullscreen></iframe>';

    } else {
        videohtml += '<video id="vplayer" poster="' + videocover + '" width="800" height="450" controls autoplay>';
        videohtml += '	<source src="' + videorul + '" type="video/mp4">';
        videohtml += '	Your browser does not support the video tag.';
        videohtml += '</video>';
    }


    //videohtml += '<iframe width="800" height="450" src="https://www.youtube.com/embed/'+videorul+'?autoplay=1" frameborder="0" allowfullscreen></iframe>';
    videohtml += '</div>';
    videohtml += '<div class="videoInfoBack"></div>';
    videohtml += '<div class="videoShowDate"><span>' + videodate + '</span></div>';
    videohtml += '<div class="videoDownload"><a href="javascript:void(0);" onclick="javascript:SaveToDisk(\'' + videorul + '\',\'' + videodate + '-' + videotitle + '.mp4\');">' + downloadstring + '</a></div>';
    if (articletype == 'video') {
        videohtml += '<div class="videoTitle"><span>' + videotitle + '</span></div>';
        videohtml += '<div class="videoEmbed"><a href="javascript:void(0);" onclick="javascript:EmbedVideo(\'' + videoid + '\',\'' + lang + '\');">' + embedstring + '</a></div>';
        videohtml += '<div class="videoShare"><a href="javascript:void(0);" onclick="javascript:ShareVideo(\'' + videoid + '\',\'' + lang + '\');">' + sharedstring + '</a></div>';
    }
    // if(articletype == 'news'){
    // 	videohtml += '<div class="videoTitle" style="width:400px;"><span>'+videotitle+'</span></div>';
    // 	videohtml += '<div class="videoEmbed"><a href="javascript:void(0);" onclick="javascript:EmbedNewsVideo(\''+videoid+'\',\''+lang+'\');">'+embedstring+'</a></div>';
    // 	videohtml += '<div class="videoShare"><a href="javascript:void(0);" onclick="javascript:ShareNewsVideo(\''+videoid+'\',\''+lang+'\');">'+sharedstring+'</a></div>';
    // }
    videohtml += '<div class="videoClose" onclick="javascript:CloseVideo();"></div>';
    videohtml += '</div>';
    $('body').append(videohtml);
}

function CloseVideo() {
    $('.videoBlock').fadeOut();
    $('.videoBlock').remove();
    if ($(".videoEmbedBox").is(':visible')) {
        $('.videoEmbedBox').remove();
    }
}

function ShareVideo(videoID, lang) {
    var shareurl = 'http://new.tv25.ge/_share.php?id=' +parseInt(videoID)  + '&lang=' + lang;
    console.log(shareurl);
    var url = 'http://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(shareurl) + '';
    window.open(url, 'ShareWin', 'width=600,height=400,status=no,location=no,titlebar=no,scrollbars=no');
}

function EmbedVideo(videoID, lang) {
    if (!$(".videoEmbedBox").is(':visible')) {
        $('body').append('<div class="videoEmbedBox"><textarea onmouseover="this.focus()" onclick="this.select();" readonly="readonly"><iframe src="http://new.tv25.ge/_share.php?id=' + videoID + '&lang=' + lang + '" width="800" height="450"></iframe></textarea><button class="videoEmbedBoxClose" onclick="$(\'.videoEmbedBox\').remove();">x</button></div>');
    }
}

document.addEventListener("keydown", keyDownTextField, false);
function keyDownTextField(e) {
    var keyCode = e.keyCode;
    if (keyCode == 27) {
        if ($(".videoBlock").is(':visible')) {
            CloseVideo();
        }
    }
}

function SaveToDisk(fileUrl, fileName) {
    var hyperlink = document.createElement('a');
    hyperlink.href = fileUrl;
    hyperlink.target = '_blank';
    hyperlink.download = fileName || fileUrl;

    var mouseEvent = new MouseEvent('click', {
        view: window,
        bubbles: true,
        cancelable: true
    });

    hyperlink.dispatchEvent(mouseEvent);
    (window.URL || window.webkitURL).revokeObjectURL(hyperlink.href);
}


function expandVideos() {
    $('.show-video-box').css('height', 'auto');
    $('.show-video-more').fadeOut();

}
