﻿
$(document).ready(function () {
    //$('#contentInner').fadeIn(200);

    GetVidePrinter();
    setInterval("GetVidePrinter()", 30000);
    $(".bloggerSummaryItem").mouseover(function () {
        $(this).addClass("bloggerSummaryItemMouseOver");
        $(this).css("cursor", "pointer");
    })

    $(".bloggerSummaryItem").mouseout(function () {
        $(this).removeClass("bloggerSummaryItemMouseOver");
        $(this).css("cursor", "default");
    })

    $(".bloggerSummaryItem").click(function () {
        $(location).attr("href", $(this).children(".bloggerSummaryText").children("a").attr("href"));
    })

    if ($("#galleryPhotoList").length != 0) {
        $("#galleryPhotoList").showcase({
            navigator: { showMiniature: false },
            css: { border: "solid 3px #121212", "-moz-border-radius": "6px", width: "450px", height: "266px" },
            navigator: { position: "top-left", showNumber: true,
                item: {
                    css: {
                        width: "15px", height: "15px", "line-height": "15px",
                        backgroundColor: "#EEDA2D", borderColor: "#E1CD23", "font-weight": "normal"
                    },
                    cssHover: {
                        backgroundColor: "#F4F376", borderColor: "#EBE95F"
                    },
                    cssSelected: {
                        backgroundColor: "#F4973C", borderColor: "#EC711F", "font-weight": "bold"
                    }
                }
            },
            titleBar: { enabled: false }

        });
    };

    LoadTwitter();

})

function LoadTwitter() {
    var twitterDivs = $(".tweets");
    if (twitterDivs.length > 0) {
        twitterDivs.each(function () {
            var twitterDiv = $(this)
            twitterDiv.append("<p>Updating Twitter feed...</p>");
            var twitterOptions = twitterDiv.attr("data").split(",");
            var twitterUser = twitterOptions[0];
            if (twitterUser != "") {
                var url = "http://twitter.com/status/user_timeline/" + twitterUser + ".json?count=" + twitterOptions[1] + "&callback=?";
                var userString = ""
                $.getJSON(url, function (data) {
                    $.each(data, function (i, item) {
                        userString = userString + "<tr>"
                                            + "<td><img src='" + item.user.profile_image_url + "'></td>"
                                            + "<td><div class='tweetText'>" + item.text.linkify() + "</div><div class='tweetDate'>" + item.created_at + "</div></td>"
                                            + "</tr>";
                    });
                    twitterDiv.html("<table>" + userString + "</table>");
                    //twitterDiv.prepend("<h2>@" + twitterUser + "</h2>");
                    twitterDiv.append("<a href='https://twitter.com/" + twitterUser + "' class='twitter-follow-button' data-show-count='false'>Follow @" + twitterUser + "</a>")
                    var script = document.createElement('script');
                    script.type = 'text/javascript';
                    script.src = '//platform.twitter.com/widgets.js';
                    twitterDiv.append(script);
                });
            };

        });
    }
}

var notificationIcon;
var notificationTitle;
var notificationBody;

function GetVidePrinter() {
    var url = "http://news.bbc.co.uk/sport/hi/english/static/football/statistics/collated/videprinter.json?jsoncallback=?";
    var videPrinterDiv = $("#videPrinter");

    $.ajax({
        url: url,
        cache: false,
        dataType: "json",
        success: function (data) {
            var hideVideprinter = "none";
            var fullString = "";
            var matchString = "";
            var matchImage = "";
            var lastID = "";
            var firstRun = true;

            try {
                $.each(data.footballVideprinter.matchEvent, function (key, match) {
                    if (firstRun == true) {
                        lastID = match.event.eventID + match.matchID;
                        firstRun == false;
                    }
                    matchString = "";
                    hideVideprinter = "block";
                    switch (match.event.eventType) {
                        case "GOAL":
                            matchImage = "images/icons/goal.png";
                            matchString = matchString + "<div class='match'><div class='matchIcon'><img src='" + matchImage + "' alt='Goal'/></div><div class='matchHead'>" + match.event.minutesIntoMatch + " mins - " + match.event.eventType + " - " + match.competition.name + " " + (match.round.name == undefined ? "" : match.round.name) + "</div><div class='matchDetail'>"
                            + "<span style='" + (match.event.teamType == "HOME" ? "font-weight:bold" : "") + "'>" + match.homeTeam.name + "</span> " + match.event.homeScore + "-" + match.event.awayScore + " <span style='" + (match.event.teamType == "AWAY" ? "font-weight:bold" : "") + "'>" + match.awayTeam.name + "</span>"
                            + " (" + match.event.player.name + (match.event.ownGoal == "true" ? " og" : "") + ")"
                            + "</div>" + (match.event.comment == undefined ? "" : "<div style='font-style:italic'>" + match.event.comment + "</div>") + "</div>";
                            break;
                        case "PENALTY":
                            switch (match.event.penaltyOutcome) {
                                case "SAVED":

                                    matchString = matchString + "<div class='match'><div class='matchHead'>" + match.event.minutesIntoMatch + " mins - " + match.event.eventType + " " + match.event.penaltyOutcome + " - " + match.competition.name + " " + (match.round.name == undefined ? "" : match.round.name) + "</div><div class='matchDetail'>"
                                    break;
                                case "SCORED":
                                    matchImage = "images/icons/goal.png";
                                    matchString = matchString + "<div class='match'><div class='matchIcon'><img src='" + matchImage + "' alt='Goal'/></div><div class='matchHead'>" + match.event.minutesIntoMatch + " mins - " + match.event.eventType + " " + match.event.penaltyOutcome + " - " + match.competition.name + "</div><div class='matchDetail'>"
                                    break;
                            }
                            matchString = matchString + "<span style='" + (match.event.teamType == "HOME" ? "font-weight:bold" : "") + "'>" + match.homeTeam.name + "</span> " + match.event.homeScore + "-" + match.event.awayScore + " <span style='" + (match.event.teamType == "AWAY" ? "font-weight:bold" : "") + "'>" + match.awayTeam.name + "</span>"
                            + " (" + match.event.player.name + ")"
                             + "</div>" + (match.event.comment == undefined ? "" : "<div style='font-style:italic'>" + match.event.comment + "</div>") + "</div>";
                            break;
                        case "MATCHSTATUS":
                            matchImage = "images/icons/status.png";
                            matchString = matchString + "<div class='match'><div class='matchIcon'><img src='" + matchImage + "' alt='Goal'/></div><div class='matchHead'>" + match.event.matchStatus + " - " + match.competition.name + " " + (match.round.name == undefined ? "" : match.round.name) + "</div><div class='matchDetail'>"
                            + match.homeTeam.name + " " + match.event.homeScore + "-" + match.event.awayScore + " " + match.awayTeam.name
                             + "</div>" + (match.event.comment == undefined ? "" : "<div style='font-style:italic'>" + match.event.comment + "</div>") + "</div>";
                            break;
                        case "DISMISSAL":
                            matchImage = "images/icons/cards.png";
                            matchString = matchString + "<div class='match'><div class='matchIcon'><img src='" + matchImage + "' alt='Goal'/></div><div class='matchHead'>" + match.event.minutesIntoMatch + " mins - " + match.event.eventType + " - " + match.competition.name + " " + (match.round.name == undefined ? "" : match.round.name) + "</div><div class='matchDetail'>"
                            + match.event.player.name + " (" + (match.event.teamType == "HOME" ? match.homeTeam.name : match.awayTeam.name) + ")"
                             + "</div>" + (match.event.comment == undefined ? "" : "<div style='font-style:italic'>" + match.event.comment + "</div>") + "</div>";
                            break;
                        default:

                    };


                    fullString = fullString + matchString;
//                    try {
//                        notificationIcon = matchImage;
//                        notificationTitle = match.event.eventType;
//                        notificationBody = matchString;
//                        ShowNotification();
//                    }
//                    catch (ex) {

//                    };

                });
            }
            catch (err) {
            }
            $("#videPrinter .match").remove();
            videPrinterDiv.append(fullString);
            $("#videPrinterWrapper").css("display", hideVideprinter);

            $("#videPrinter .match:even").addClass("altMatch");
        },
        error: function (xhr, ajaxOptions, thrownError) {
            //alert(xhr.status);
            //alert(thrownError);
        }
    });

};



function RequestPermission(callback) {
    window.webkitNotifications.requestPermission(callback);
}

function ShowNotification() {
    if (window.webkitNotifications.checkPermission() > 0) {
        RequestPermission(ShowNotification);
    }

    //icon = window.location.protocol + "//" + window.location.host + "/" + icon;
    
    var popup = window.webkitNotifications.createNotification(notificationIcon, notificationTitle, notificationBody);

    popup.show();
    setTimeout(function () {
        popup.cancel();
    }, '15000');
};

String.prototype.linkify = function () {
    return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function (m) {
        return m.link(m);
    });
};
