function getTopVideo() {
    var myret = $.ajax({
        type: "GET",
        url: "http://gdata.youtube.com/feeds/api/videos",
        data : "q=" + keyword + "&orderby=relevance&max-results=8&v=2&alt=json",
        dataType: "json",
        success: function(msg) {
            $("#topVid").html("");
            for (keyVar in msg.feed.entry) {
                var videoentry = msg.feed.entry[keyVar];

                var statistics = videoentry.yt$statistics;
                if (statistics != undefined) {
                    var viewCount = statistics.viewCount;
                } else {
                    var viewCount = 0;
                }
                var title = videoentry.title.$t;
                if (title.length > 30) {
                    title = title.substr(0, 20) + " ...";
                }
                var thumb = videoentry.media$group.media$thumbnail[0].url;
                var vidID = videoentry.media$group.yt$videoid.$t;
                var author = videoentry.author[0].name.$t;
                var myRe = /(\d{4})-(\d{1,2})-(\d{1,2})/;
                var pd = videoentry.published.$t;
                var myArray = myRe.exec(pd);
                var pubDate = myArray[0];
                $("#topVid").html($("#topVid").html() + "\
                            <div class=\"thumb\">\
                                <div class=\"imgHolder\">\
                                    <a href=\"view.php?videoID=" + vidID + "\">\
                                        <img border=0 width=120 height=120 src=\"" + thumb + "\" alt=\"\"/>\
                                    </a>\
                                </div>\
                                <p>\
                                    <a href=\"view.php?videoID=" + vidID + "\">" + title + "</a>\
                                </p>\
                                <p>" + viewCount + " views</p>\
                                <p>" + pubDate + "</p>\
                                <span class=\"author\">\
                                    <a href=\"search.php?q=" + author + "\">\
                                        " + author + "\
                                    </a>\
                                </span>\
                            </div>\
                    ");
            }

        }
    });
}

function getPopularVideo() {
    var myret = $.ajax({
        type: "GET",
        url: "http://gdata.youtube.com/feeds/api/standardfeeds/most_popular",
        data : "orderby=relevance&max-results=8&v=2&alt=json",
        dataType: "json",
        success: function(msg) {
            $("#popVid").html("");
            for (keyVar in msg.feed.entry) {
                var videoentry = msg.feed.entry[keyVar];

                var statistics = videoentry.yt$statistics;
                if (statistics != undefined) {
                    var viewCount = statistics.viewCount;
                } else {
                    var viewCount = 0;
                }
                var title = videoentry.title.$t;
                if (title.length > 20) {
                    title = title.substr(0, 20) + " ...";
                }
                var thumb = videoentry.media$group.media$thumbnail[0].url;
                var vidID = videoentry.media$group.yt$videoid.$t;
                var author = videoentry.author[0].name.$t;
                var myRe = /(\d{4})-(\d{1,2})-(\d{1,2})/;
                var pd = videoentry.published.$t;
                var myArray = myRe.exec(pd);
                var pubDate = myArray[0];

                $("#popVid").html($("#popVid").html() + "\
                            <div class=\"thumb\">\
                                <div class=\"imgHolder\">\
                                    <a href=\"view.php?videoID=" + vidID + "\">\
                                        <img border=0 width=120 height=120 src=\"" + thumb + "\" alt=\"\"/>\
                                    </a>\
                                </div>\
                                <p>\
                                    <a href=\"view.php?videoID=" + vidID + "\">" + title + "</a>\
                                </p>\
                                <p>" + viewCount + " views</p>\
                                <p>" + pubDate + "</p>\
                                <span class=\"author\">\
                                    <a href=\"search.php?q=" + author + "\">\
                                        " + author + "\
                                    </a>\
                                </span>\
                            </div>\
                    ");
            }

        }
    });
}

function getVideoInfo() {
    var myret = $.ajax({
        type: "GET",
        url: "http://gdata.youtube.com/feeds/api/videos/" + videoID,
        data : "v=2&alt=json",
        dataType: "json",
        success: function(msg) {
            //Video Title
            var videoentry = msg.entry;

            $("#vidTitle").text(videoentry.title.$t);
            $("#fbb").attr("href", $("#fbb").attr("href") + videoentry.title.$t);
            $("#gb").attr("href", $("#gb").attr("href") + videoentry.title.$t);

            //Added video day
            var myRe = /(\d{4})-(\d{1,2})-(\d{1,2})/;
            var pd = videoentry.published.$t;
            var myArray = myRe.exec(pd);
            var pubDate = myArray[0];
            $("#vidadd").text("Added " + pubDate);

            //Author
            $("#vidauthor").text(videoentry.author[0].name.$t);
            $("#vidauthor").attr("href", "search.php?q=" + videoentry.author[0].name.$t);

            //Video views
            var statistics = videoentry.yt$statistics;
            if (statistics != undefined) {
                var viewCount = statistics.viewCount;
            } else {
                var viewCount = 0;
            }
            $("#vidview").text(viewCount + " views");

            //Video description
            $("#viddesc").text(videoentry.media$group.media$description.$t);

            getComments();
            showRelated();
        }
    });
}

function getComments() {
    var myret = $.ajax({
        type: "GET",
        url: "http://gdata.youtube.com/feeds/api/videos/" + videoID + "/comments",
        data : "v=2&alt=json&max-results=10",
        dataType: "json",
        success: function(msg) {
            $("#blockComment").html("");
            for (keyVar in msg.feed.entry) {
                var commententry = msg.feed.entry[keyVar];

                var content = commententry.content.$t
                if (content.length > 300) {
                    content = content.substr(0, 300) + " ...";
                }

                var author = commententry.author[0].name.$t;

                var myRe = /(\d{4})-(\d{1,2})-(\d{1,2})/;
                var pd = commententry.published.$t;
                var myArray = myRe.exec(pd);
                var pubDate = myArray[0];

                $("#blockComment").html($("#blockComment").html() + "\
                    <div class=\"left\"></div>" +
                        "<div class=\"right\">" +
                        "<span class=\"date\">"+pubDate+"</span>" +
                        "<span class=\"title\">Comment by " +
                        "   <a href = \"search.php?q=" + author + "\" class=\"author\" >" + author + "</a>" +
                        "</span >" +
                        "<p>" + content + "</p>" +
                        "</div>" +
                        "<div class=\"clear\"></div>" +
                        "<br><br>");
            }
        }
    });
}

function showRelated(){
    var myret = $.ajax({
        type: "GET",
        url: "http://gdata.youtube.com/feeds/api/videos/" + videoID + "/related",
        data : "v=2&alt=json&max-results=6",
        dataType: "json",
        success: function(msg) {
            $("#related").html("");
            for (keyVar in msg.feed.entry) {
                var related = msg.feed.entry[keyVar];

                var statistics = related.yt$statistics;
                if (statistics != undefined) {
                    var viewCount = statistics.viewCount;
                } else {
                    var viewCount = 0;
                }
                var title = related.title.$t;
                if (title.length > 20) {
                    title = title.substr(0, 20) + " ...";
                }
                var thumb = related.media$group.media$thumbnail[0].url;
                var vidID = related.media$group.yt$videoid.$t;
                var author = related.author[0].name.$t;
                var myRe = /(\d{4})-(\d{1,2})-(\d{1,2})/;
                var pd = related.published.$t;
                var myArray = myRe.exec(pd);
                var pubDate = myArray[0];


                $("#related").html($("#related").html() + "" +
                        "<div class=\"thumb\">" +
                        "   <div class=\"imgHolder\">" +
                        "       <a href=\"view.php?videoID=" + vidID + "\">" +
                        "           <img height=\"120\" width=\"120\" src=\"" + thumb + "\" alt=\"\"/>" +
                        "       </a>" +
                        "   </div>" +
                        "   <p>" +
                        "       <a href=\"view.php?videoID=" + vidID + "\">" + title + "</a>" +
                        "   </p>" +
                        "   <p>" + viewCount + " views</p>" +
                        "   <p>" + pubDate + "</p>" +
                        "   <span class=\"author\">" +
                        "       <a href=\"search.php?q=" + author + "\">" + author + "</a>" +
                        "   </span>" +
                        "</div>");

            }
        }
    });
}

function getSearchVideo() {
    var myret = $.ajax({
        type: "GET",
        url: "http://gdata.youtube.com/feeds/api/videos",
        data : "q=" + keyword + "&orderby=relevance&max-results=10&v=2&alt=json",
        dataType: "json",
        success: function(msg) {
            $("#videoResultsBlock").html("");
            for (keyVar in msg.feed.entry) {
                var videoentry = msg.feed.entry[keyVar];

                var statistics = videoentry.yt$statistics;
                if (statistics != undefined) {
                    var viewCount = statistics.viewCount;
                } else {
                    var viewCount = 0;
                }
                var title = videoentry.title.$t;
                if (title.length > 100) {
                    title = title.substr(0, 100) + " ...";
                }
                var thumb = videoentry.media$group.media$thumbnail[0].url;
                var vidID = videoentry.media$group.yt$videoid.$t;
                var author = videoentry.author[0].name.$t;
                var myRe = /(\d{4})-(\d{1,2})-(\d{1,2})/;
                var pd = videoentry.published.$t;
                var myArray = myRe.exec(pd);
                var pubDate = myArray[0]

                var description = videoentry.media$group.media$description.$t;
                if (description.length > 300) {
                    description = description.substr(0, 300) + " ...";
                }

                $("#videoResultsBlock").html($("#videoResultsBlock").html() + "" +
                        "   <div class=\"blockVideoResult\">" +
                        "       <div class=\"imgHolder\">" +
                        "           <a href=\"view.php?videoID=" + vidID + "\">" +
                        "               <img border=0 width=120 height=120 src=\"" + thumb + "\" alt=\"\"/>" +
                        "           </a>" +
                        "       </div><br>" +
                        "       <a href=\"view.php?videoID=" + vidID + "\">" + title + "</a><br/><br/>" +
                        "       <p>" + description + "</p>"+
                        "       <br />" +
                        "       <a href=\"search.php?q=" + author + "\" class=\"author\">" + author + "</a> | " +
                        "       <span class=\"date\">" + pubDate + "</span> | " +
                        "       <span class=\"rose\">" + viewCount + " views</span>" +
                        "   </div><div class=\"clear\"></div>");
            }

        }
    });
}
