﻿$(document).ready(function () {

    SetupFacebook();

    SetupPage();

    SetupFBShare();
    AdjustLeagueTable()
});

function AdjustLeagueTable() {
    $("#pc_leaguetable_10058753 tr").each(function () {
        if ($(this).children("td:nth-child(1)").text() == "") {
            $(this).append("<th>Max</th>");
        } else {
            var maxPoints = (18 - parseInt($(this).children("td:nth-child(2)").text())) * 20 + parseInt($(this).children("td:nth-child(15)").text());
            $(this).append("<td>" + maxPoints + "</td>");
        };
        if ($(this).children("td:nth-child(4)").text() == 1) {
            $(this).children("td:nth-child(4)").text(2);
            $(this).children("td:nth-child(2)").text(parseInt($(this).children("td:nth-child(2)").text()) + 1);
            $(this).children("td:nth-child(15)").text(parseInt($(this).children("td:nth-child(15)").text()) + 20);
        };
        if ($(this).children("td:nth-child(4)").text() == 0) {
            $(this).children("td:nth-child(2)").text(18);
            $(this).children("td:nth-child(3)").text(18);
        };
    });

    $("#pc_leaguetable_2066 tr").each(function () {
        if ($(this).children("td:nth-child(1)").text() == "") {
            $(this).append("<th>Max</th>");
        } else {
            var maxPoints = (18 - parseInt($(this).children("td:nth-child(2)").text())) * 20 + parseInt($(this).children("td:nth-child(13)").text());
            $(this).append("<td>" + maxPoints + "</td>");
        };
        if ($(this).children("td:nth-child(4)").text() == 1) {
            $(this).children("td:nth-child(4)").text(2);
            $(this).children("td:nth-child(2)").text(parseInt($(this).children("td:nth-child(2)").text()) + 1);
            $(this).children("td:nth-child(13)").text(parseInt($(this).children("td:nth-child(13)").text()) + 20);
        };
        if ($(this).children("td:nth-child(4)").text() == 0) {
            $(this).children("td:nth-child(2)").text(18);
            $(this).children("td:nth-child(3)").text(18);
        };
    });

    $("#pc_leaguetable_10058753 table").prepend("<thead>");
    $("#pc_leaguetable_10058753 table tr:first-child").appendTo("#pc_leaguetable_10058753 table thead");

    $("#pc_leaguetable_10058753 table").tablesorter({
        sortList: [[14, 1]]
    });

    $("#pc_leaguetable_2066 table").prepend("<thead>");
    $("#pc_leaguetable_2066 table tr:first-child").appendTo("#pc_leaguetable_2066 table thead");

    $("#pc_leaguetable_2066 table").tablesorter({
        sortList: [[12, 1]]
    }); 
    
};

function SetupFBShare() {
    $(".bloggerSummaryItem").each(function () {
        $(this).children(".bloggerSummaryText").prepend("<a href='#' class='fbShare'><img src='images/icons/fbshare.png' alt='Share on Facebook' title='Share on Facebook'/></a>");
    });
    $(".fbShare").click(function () {
        var story = $(this).siblings("a").text();
        var link = "http://www.sosemtcc.org.uk/" + $(this).siblings("a").attr("href");
        fb_publish(story, "Southend on Sea & EMT CC news", "Click the link for full story", link, "");
        return false;
    });
}

function fb_publish(header, title, content, link, image) {
    if (image == "") {
        image = "http://www.sosemtcc.org.uk/images/badge.png";
    };
    FB.ui(
   {
       method: 'feed',
       name: header,
       link: link,
       caption: title,
       description: content,
       message: '',
       picture: image
   },
   function (response) {
       if (response && response.post_id) {
           //alert('Post was published.');
       } else {
           //alert('Post was not published.');
       }
   }
 );

};



function SetupPage() {

    $(".middleRow").equalHeights();
    //$(".bottomRow").equalHeights();

    LoadLiveScores();
    LoadRSS()

    //$(".bottomRow").equalHeights();

    //$("#frontPagePics").fadeIn(500);
    if ($("#galleryPhotoList").length != 0) {
        $("#galleryPhotoList").nivoSlider({
            animSpeed: 500, // Slide transition speed
            pauseTime: 5000 // How long each slide will show
        });
    };

    $("td:contains('Southend-on-Sea & EMT')").parent().children("td").css("font-weight", "bold");
    $("td:contains('Southend on Sea & EMT')").parent().children("td").css("font-weight", "bold");
    $("td:contains('Southend & EM & T')").parent().children("td").css("font-weight", "bold");
    
    $(".leagueTable table tr td").css("padding-right", "10px");
    $(".leagueTable table tr th").css("padding-right", "10px");

    $(".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"));
    })

    $(".performanceTable a").each(function () {
        if ($(this).attr("href") == "#") {
            $(this).parent().html("");
        }
    });
};

function SetupFacebook() {
    FB.init({
        appId: '190262988474',
        status: true, // check login status
        cookie: true, // enable cookies to allow the server to access the session
        xfbml: true  // parse XFBML
    });
};



$.fn.equalHeights = function (minHeight, maxHeight) {
    tallest = (minHeight) ? minHeight : 0;
    this.each(function () {
        if ($(this).height() > tallest) {
            tallest = $(this).height();
        }
    });
    if ((maxHeight) && tallest > maxHeight) tallest = maxHeight;
    return this.each(function () {
        $(this).height(tallest).css("overflow", "auto");
    });
}



function LoadLiveScores() {
    var liveScoreDivs = $(".liveScores");
    if (liveScoreDivs.length > 0) {
        var liveScoreString = "";
        liveScoreDivs.append("<p>Updating...</p>");
        $.jGFeed('http://static.espncricinfo.com/rss/livescores.xml',
        function (feeds) {
            // Check for errors
            if (!feeds) {
                // there was an error
                liveScoreDivs.text("There was a problem loading live scores from CricInfo");
                return false;
            }
            // do whatever you want with feeds here
            for (var i = 0; i < feeds.entries.length; i++) {
                var entry = feeds.entries[i];
                // Entry title
                liveScoreString = liveScoreString + "<div class='score'>" + entry.title + " <a href='" + entry.link + "' target='_blank'>[Scorecard]</a></div>"
                entry.title;
            }
            liveScoreDivs.html(liveScoreString);
            liveScoreDivs.find("div.score:even").addClass("scoreAlt");
            
        }, 100);
    };
};


function LoadRSS() {
    var rssDivs = $(".rss");
    if (rssDivs.length > 0) {
        var rssString = "";
        rssDivs.append("<p>Updating...</p>");

        rssDivs.each(function () {
            var rssDiv = $(this);
            var rssOptions = $(this).attr("title").split(",");
            $.jGFeed(rssOptions[0],
            function (feeds) {
                // Check for errors
                if (!feeds) {
                    // there was an error
                    $(this).text("There was a problem loading RSS");
                    return false;
                }
                // do whatever you want with feeds here
                for (var i = 0; i < feeds.entries.length; i++) {
                    var entry = feeds.entries[i];
                    // Entry title
                    rssString = rssString + "<div class='rssStory'><div class='rssTitle'><a href='" + entry.link + "' target='_blank'>" + entry.title + "</a></div><div class='rssDescription'>" + entry.content + "</div></div>"

                }
                //rssDiv.html("<h3>" + feeds.title + "</h3>";
                rssDiv.html(rssString);
            }, rssOptions[1]);
        });
    };
};

