﻿function randOrd() {
    return (Math.round(Math.random()) - 0.5);
}

var currentQuote = 0;
var firstTime = 1;
var numberOfQuote = 0;

function recentpostsquote(json) {
    numberOfQuote = json.feed.entry.length;

    json.feed.entry.sort(randOrd);

    for (var i = 0; i < json.feed.entry.length; i++)
    {
        for (var j = 0; j < json.feed.entry[i].link.length; j++) 
        {
            if (json.feed.entry[i].link[j].rel == 'alternate') 
            {
                break;
           }
      
        }

        var entry = json.feed.entry[i];

        var thumburl;
        try 
        {
            thumburl=entry.media$thumbnail.url;
        }
        catch (error)
        {
            if ("content" in entry) {
                s = entry.content.$t;
            }
            else if ("summary" in entry) {
                s = entry.summary.$t;
            }
            else {
                s = "";
            }
            a = s.indexOf("<img");
            b = s.indexOf("src=\"", a);
            c = s.indexOf("\"", b + 5);
            d = s.substr(b + 5, c - b - 5);
            if ((a != -1) && (b != -1) && (c != -1) && (d != "")) { thumburl = d; } else thumburl = 'http://lh3.ggpht.com/_sS1Q7guTuCw/TO-xB-FcO_I/AAAAAAAAAh0/6y_91wP4vbU/blank.png';
        }

        
        var entryUrl = "'" + json.feed.entry[i].link[j].href + "'";
        var entryTitle = json.feed.entry[i].title.$t;


        if (entryTitle.length >= 30) {
            entryTitle = entryTitle.substring(0, 30);
            var quoteEnd = entryTitle.lastIndexOf(" ");
            entryTitle = entryTitle.substring(0, quoteEnd) + "...";
        }

        var item = "<a href=" + entryUrl + '" >' + entryTitle + "</a>";
        var image = "<a href=" + entryUrl + "><img src=\"" + thumburl + "\" width=\"46\"/></a>";

        if ("content" in entry) {
            var postcontent = entry.content.$t;
        }
        else if ("summary" in entry) {
            var postcontent = entry.summary.$t;
        }
        else {
            var postcontent = "";
        }
        a = postcontent.indexOf("<div");
        a = postcontent.indexOf(">", a);
        b = postcontent.indexOf("</div", a);
        quoteA = postcontent.substr(a + 1, b - a - 1);

        a = postcontent.indexOf("<div", b);
        a = postcontent.indexOf(">", a);
        b = postcontent.indexOf("</div", a);
        quoteB = postcontent.substr(a + 1, b - a - 1);

        //var re = /<\S[^>]*>/g;
        //postcontent = postcontent.replace(re, "");
        //postcontent = removeHTMLTags(postcontent);
        
        /*if (postcontent.length >= 70) {
        postcontent = postcontent.substring(0, 70);
        var quoteEnd = postcontent.lastIndexOf(" ");
        postcontent = postcontent.substring(0, quoteEnd) + "...";
        }*/

        var link = "<a href=" + entryUrl + "><small>tovább...</small></a>";
        if (i == 0) {
            document.write("<div id=\"quote_" + i + "\" style=\"opacity:1.0;filter:alpha(opacity=100);position:absolute;width:530px;height:70px;\">");
            document.write("<div style=\"font-family:sans-serif;font-size:16px;line-height:21px;font-weight:normal;text-align:center;\">" + quoteA + "</div>");
            document.write("<div style=\"font-family:sans-serif;font-size:11px;text-align:center; width:530px;\">" + quoteB);
            document.write("</div>");
            document.write("</div>");
        }
        else {
            document.write("<div id=\"quote_" + i + "\" style=\"opacity:0.0;filter:alpha(opacity=0);position:absolute;width:530px;height:70px;font-family:sans-serif;font-size:16px;font-weight:normal;text-align:center;\">");
            document.write("<div style=\"font-family:sans-serif;font-size:16px;line-height:21px;font-weight:normal;text-align:center;\">" + quoteA + "</div>");
            document.write("<div style=\"font-family:sans-serif;font-size:11px;text-align:center; width:530px;\">" + quoteB);
            document.write("</div>");
            //document.write("<div style=\"position:absolute;left:450px; top:40px; font-size:11px;\">összes</div>");
            document.write("</div>");
        }

    }
    document.write("<div style=\"position:absolute;left:480px; top:42px; font-size:11px;\"><a href=\"http://www.uzenetek.com/search/label/Idezet\">összes>></a></div>");
    setTimeout("showHideQuote()", 100);
}

function showHideQuote() {
    if (firstTime == 0) {
        $("#quote_" + currentQuote).animate({ opacity: 100 }, 3000, function () { $("#quote_" + currentQuote).delay(5000).animate({ opacity: 0 }, 3000, function () { currentQuote++; if (currentQuote == numberOfQuote) { currentQuote = 0 }; showHideQuote(); }); });
    }
    else {
        $("#quote_" + currentQuote).delay(5000).animate({ opacity: 0 }, 3000, function () { currentQuote++; showHideQuote(); });
        firstTime = 0;
    }
}

function removeHTMLTags(htmlString) 
{
    if (htmlString) 
    {
        var mydiv = document.createElement("div");
        mydiv.innerHTML = htmlString;

        if (document.all) // IE Stuff
        {
            return mydiv.innerText;

        }
        else // Mozilla does not work with innerText
        {
            return mydiv.textContent;
        }
    }
}


