$(document).ready(function() {
    //Specify the marquee's width (in pixels)
    var marqueewidth="140px";
    //Specify the marquee's height
    var marqueeheight="140px";
    //Specify the marquee's marquee speed (larger is faster 1-10)
    var marqueespeed=1;
    //Pause marquee onMousever (0=no. 1=yes)?
    var pauseit=1;

    marqueespeed=(document.all)? marqueespeed : Math.max(1, marqueespeed-1); //slow speed down by 1 for NS
    var copyspeed = marqueespeed;
    var pausespeed = (pauseit==0)? copyspeed: 0;
    var iedom = document.all || document.getElementById;
    var actualheight = '';
    var cross_marquee, ns_marquee;

    function scrollmarquee_x(){

        if (parseInt(cross_marquee.style.top)>(actualheight*(-1)+8))
            cross_marquee.style.top=parseInt(cross_marquee.style.top)-copyspeed+"px";
        else
            cross_marquee.style.top=parseInt(marqueeheight)+8+"px";
    }


    var form_data = {
        csurl: encodeURIComponent("https://www.cypruswebsites.com/acrons/anews/phileleftheros_news.json"),
        baseurl: baseurl
    };

    $.ajax({
        url: baseurl + '/proxy.php',
        type: "POST",
        data: form_data,
        dataType: "json",
        cache: false,
        success: function(r) {
            var keys = [];
            for (var rkey in r) {
                if (r.hasOwnProperty(rkey)) keys.push(rkey);
            }

            if(keys.length > 0) {
                var $news_container = $('#phileleftheros-news');
                var $newsScroller = $('<div></div>')
                    .attr('id', 'newsScroller')
                    .css({
                        position: "relative",
                        overflow: "hidden",
                        height: marqueeheight
                    })
                    .on("mouseover", function(mover){
                        copyspeed = pausespeed;
                    })
                    .on("mouseout", function(mout){
                        copyspeed = marqueespeed
                    });
                var $iemarquee = $('<div></div>')
                    .attr('id', 'iemarquee')
                    .css({
                        position: "absolute",
                        left: "0",
                        top: "0",
                        width: "100%"
                    });

                $newsScroller.html($iemarquee);
                $news_container.html($newsScroller);

                for (var i=0; i<keys.length; i++) {
                    var news_item = r[keys[i]];

                    var $p = '<p><a href="' + news_item.url + '" target="_blank" rel="nofollow noopener noreferrer" title="' + news_item.title + '">' + news_item.title + '</a></p>';

                    $iemarquee.append($p);
                }

                cross_marquee = document.getElementById("iemarquee");
                cross_marquee.style.top = parseInt(marqueeheight)+8+"px";
                actualheight = cross_marquee.offsetHeight;

                var lefttime = setInterval(function(){
                    if (parseInt(cross_marquee.style.top)>(actualheight*(-1)+8))
                        cross_marquee.style.top=parseInt(cross_marquee.style.top)-copyspeed+"px";
                    else
                        cross_marquee.style.top=parseInt(marqueeheight)+8+"px";
                }, 50);
            }


            //console.log(keys);
        }
    });



});