$(document).ready(function(){
    
    var newsFeed = $("#cat_hl_86652");
    if (newsFeed.length) 
    {
        var update;
        var showUpdateCounter = 0;
        var checkShowUpdate = false;
    
        var i = 1;
        var topContent = $("#line" + intFC + "contenttop").text();
    
        //arrays to store updates
        var arrNewsFeedTitle;
        var arrNewsFeedUrl;
        var arrNewsFeedImg;
        var arrNewsFeedType;
        var arrNewsFeedTime;

        var monthName = {"Jan":"January", "Feb":"February", "Mar":"March", "Apr":"April", "May":"May", "Jun":"June", "Jul":"July", "Aug":"August", "Sep":"September", "Oct":"October", "Nov":"November", "Dec":"December"};
        
        $("#" + nfHighlightId).everyTime(65000, 'controlled', function() { 
            getNewsFeedUpdate();
        }).everyTime(10000, 'controlled', function() { 
            showFeedUpdate();
        });    
     
        function showFeedUpdate() 
        {
            if (checkShowUpdate) 
            {
                update = new feedUpdate(arrNewsFeedTitle[showUpdateCounter], arrNewsFeedUrl[showUpdateCounter], arrNewsFeedImg[showUpdateCounter], arrNewsFeedType[showUpdateCounter], arrNewsFeedTime[showUpdateCounter]);
                animateFeedUpdate();
                if (showUpdateCounter == 0) 
                {
                    checkShowUpdate = false;
                }
                showUpdateCounter--;            
            }
        }
        
        function animateFeedUpdate() 
        {
            //hide the bottom one
            $("#line" + i).hide("slow");  
            
            // update the new content            
            var info = update.img_caption;
            var newId = "line" + (i + intFC);
            $("#" + newId + "img").html('<img src="' + update.img_url + '">');
            
            var slideshowUrl = "";
            if (info == "SLIDESHOW") 
            {
                var arrStrUrl = update.url.split("?");
                if (arrStrUrl.length >= 2)
                {
                    var arrStrUrlDomain = arrStrUrl[0].split(".");
                    var strDomainUrl = arrStrUrlDomain[0] & ".ninemsn.com.au";
                    var arrStrUrlQs = arrStrUrl[1].split("&");
                    
                    if (arrStrUrlQs.length >= 2)
                    {
                        var arrQsFirst = arrStrUrlQs[0].split("=");
                        var arrQsSecond = arrStrUrlQs[1].split("=");
                        
                        if (arrQsFirst.length >= 2 && arrQsSecond.length >= 2) 
                        {   
                            var strSectionId = "";
                            var strSubsectionId = "";
                            
                            if (arrQsFirst[0].toLowerCase() == "sectionid")
                            {
                                strSectionId = arrQsFirst[1];
                            }
                            else if (arrQsFirst[0].toLowerCase() == "subsectionid")
                            {
                                strSubsectionId = arrQsFirst[1];
                            }                        
                            
                            if (arrQsSecond[0].toLowerCase() == "sectionid")
                            {
                                strSectionId = arrQsSecond[1];
                            }
                            else if (arrQsSecond[0].toLowerCase() == "subsectionid")
                            {
                                strSubsectionId = arrQsSecond[1];
                            }
                            
                            slideshowUrl = "<a target=\"new\" href=\"" + update.url + "\" onclick=\"return launch_popup('slideshow','" + strSectionId + "','" + strSubsectionId + "','slideshow','','" + strDomainUrl + "');\">" + update.title + "</a>";
                        }
                    }
                }
                if (slideshowUrl != "")
                {
                    $("#" + newId + "contenttop").html(slideshowUrl);
                }
                else
                {
                    $("#" + newId + "contenttop").html('<a href="' + update.url + '">' + update.title + '</a>');
                }            
            }
            else 
            {
                $("#" + newId + "contenttop").html('<a href="' + update.url + '">' + update.title + '</a>');
            }
            
            var strInfo = "";
            if (info == "SLIDESHOW") {strInfo = "New slideshow";}
            else if (info == "VOTE") {strInfo = "New vote";}
            else if (info == "VOTENO1") {strInfo = "Most popular vote";}
            else if (info == "ARTICLE") {strInfo = "New article";}
            else if (info == "UPDATED") {strInfo = "Updated article";}
            else if (info == "ARTICLENO1") {strInfo = "Most popular article";}
            else if (info == "VIDEO") {strInfo = "New video";}
            else if (info == "COMMENTNO1") {strInfo = "Most popular comments";}
            else if (info == "COMMENT") {strInfo = "First comment added";}   
            else if (info == "COMMENT100") {strInfo = "100 comments";}
            else if (info == "COMMENT150") {strInfo = "150 comments";}
            else if (info == "COMMENT200") {strInfo = "200 comments";}
            else if (info == "COMMENT250") {strInfo = "250 comments";}
            else if (info == "COMMENT300") {strInfo = "300 comments";}
            else if (info == "COMMENT350") {strInfo = "350 comments";}
            else if (info == "COMMENT400") {strInfo = "400 comments";}
            else if (info == "COMMENT450") {strInfo = "450 comments";}
            else if (info == "COMMENT500") {strInfo = "500 comments";}
            else if (info == "COMMENT1000") {strInfo = "1000 comments";}
            else if (info == "ARTICLE5000") {strInfo = "5000 views";}
            else if (info == "ARTICLE20000") {strInfo = "20,000 views";}
            else if (info == "ARTICLE30000") {strInfo = "30,000 views";}
            else if (info == "ARTICLE40000") {strInfo = "40,000 views";}
            else if (info == "ARTICLE50000") {strInfo = "50,000 views";}
            else if (info == "ARTICLE60000") {strInfo = "60,000 views";}
            else if (info == "ARTICLE70000") {strInfo = "70,000 views";}
            else if (info == "ARTICLE80000") {strInfo = "80,000 views";}
            else if (info == "ARTICLE90000") {strInfo = "90,000 views";}
            else if (info == "ARTICLE100000") {strInfo = "100,000 views";}            
            else if (info == "VOTE1000") {strInfo = "1000 votes";}
            else if (info == "VOTE5000") {strInfo = "5000 votes";}
            else if (info == "VOTE10000") {strInfo = "10,000 votes";}
            else if (info == "VOTE15000") {strInfo = "15,000 votes";}
            else if (info == "VOTE20000") {strInfo = "20,000 votes";}
            else if (info == "VOTE25000") {strInfo = "25,000 votes";}
            else if (info == "VOTE30000") {strInfo = "30,000 votes";}
            else if (info == "VOTE35000") {strInfo = "35,000 votes";}
            else if (info == "VOTE40000") {strInfo = "40,000 votes";}
            else if (info == "VOTE45000") {strInfo = "45,000 votes";}
            else if (info == "VOTE50000") {strInfo = "50,000 votes";}
            else if (info == "VIDEONO1") 
            {
                var updateUrl = update.url;
                if ((updateUrl.toLowerCase()).indexOf("money") > -1)
                {
                    strInfo = "Most popular money video";
                }
                else if ((updateUrl.toLowerCase()).indexOf("news") > -1)
                {
                    strInfo = "Most popular news video";
                }
                else
                {
                    strInfo = "Most popular video";
                }
            }
            $("#" + newId + "info").html(strInfo);
            
            var dt = update.ingestiontime;
            $("#" + newId + "timespan").html(getTimeSpan(dt));
            $("#" + newId + "time").html(dt);
            
            //prepend the top one
            var newIdContainer = "line" + (i + intFC + 1);
            $("#" + nfHighlightId).prepend("<div id='" + newIdContainer + "' class='updatenewtop'></div>");
            $("#" + newIdContainer).append("<div id='" + newIdContainer + "img' class='updateimg'></div>");
            $("#" + newIdContainer).append("<div id='" + newIdContainer + "content' class='updatecontent'></div>");
            $("#" + newIdContainer + "content").append("<div id='" + newIdContainer + "contenttop' class='updatecontenttop'></div>");
            $("#" + newIdContainer + "content").append("<div id='" + newIdContainer + "contentbtm' class='updatecontentbtm'></div>");
            $("#" + newIdContainer + "content").append("<span id='" + newIdContainer + "time' class='updatetime'></span>");
            $("#" + newIdContainer + "contentbtm").append("<span id='" + newIdContainer + "info' class='breakingnewstext'></span><span id='" + newIdContainer + "timespan' class='breakingnewstime'></span>");
            //slide down the very top one
            $("#" + newIdContainer).slideDown("slow",fadeBackground(newId));
            //update the time
            for (var j=i+intFC-1; j>i+intFC-6; j--) {
                var x = $("#line" + j + "time").text();
                if (x) 
                {
                    var y = $("#line" + j + "timespan").html(getTimeSpan(x));
                }            
            }        
            //increase counter
            i++;
            topContent = $("#" + newId + "contenttop").text();
        }
      
        function fadeBackground(x) 
        {
            $("#" + x).oneTime(1500, 'controlled', function() {
                $("#" + x).animate({backgroundColor: "white"},'slow');
            });    
            return false;
        };
      
        function getNewsFeedUpdate() 
        {
            var hostUrl = window.location.hostname;
            var intRnd = Math.random() + Math.random() + Math.random();
            var fetchUrl;
            if(hostUrl.indexOf("syd") != -1 || hostUrl.indexOf("sbkprod") != -1 || hostUrl.indexOf("prev01") != -1)
            {
                fetchUrl = "http://data.prev01.ninemsn.com.au/Money/MoneyGuide.aspx?siteid=2339&sectionid=6227&subsectionid=0&groupid=8356&mode=preview&reset=" + intRnd;
            }
            else 
            {
                fetchUrl = "http://data.ninemsn.com.au/Money/MoneyGuide.aspx?siteid=2339&sectionid=6227&subsectionid=0&groupid=8356&reset=" + intRnd;
            }            
            
            var _oSwap = function (res)
            {
                if(res && res != null && res.MoneyGuideRatingResult != null && res.MoneyGuideRatingResult.RatingDS && res.MoneyGuideRatingResult.RatingDS.Ratings && res.MoneyGuideRatingResult.RatingDS.RatingEntries) 
                {                
                    arrNewsFeedTitle = new Array();
                    arrNewsFeedUrl = new Array();
                    arrNewsFeedImg = new Array();
                    arrNewsFeedType = new Array();
                    arrNewsFeedTime = new Array();
                    var title, updateTitle, url, img_url, updateType, ingestiontime;                
                    
                    for (var i = 0; i < res.MoneyGuideRatingResult.RatingDS.RatingEntries.length; i++) {
                        title = res.MoneyGuideRatingResult.RatingDS.RatingEntries[i].title;
                        if (title) 
                        {
                            updateTitle = title.length>42? title.substring(0,42)+"..." : title;
                        }
                        url = res.MoneyGuideRatingResult.RatingDS.RatingEntries[i].url;
                        img_url = res.MoneyGuideRatingResult.RatingDS.RatingEntries[i].image_url;
                        updateType = res.MoneyGuideRatingResult.RatingDS.RatingEntries[i].ratingstars;
                        ingestiontime = res.MoneyGuideRatingResult.RatingDS.RatingEntries[i].rated;    
                        
                        if (updateTitle && url && img_url && updateType && ingestiontime)
                        {
                            if (updateTitle == topContent)
                            {
                                break;
                            }                        
                            arrNewsFeedTitle[i] = updateTitle;
                            arrNewsFeedUrl[i] = url;
                            arrNewsFeedImg[i] = img_url;
                            arrNewsFeedType[i] = updateType;
                            arrNewsFeedTime[i] = ingestiontime;
                        }
                    }
                    if (arrNewsFeedTitle.length > 0) 
                    {
                        showUpdateCounter = arrNewsFeedTitle.length-1;
                        checkShowUpdate = true;
                    }                
                    return false;
                } 
            }
            var jsonPropList = new Ninemsn.Global.ContentManager.JsonProp (10000, -1, null, "JSONResult", Ninemsn.Global.ContentManager.Json.ResponseFunction.Unique);	
            Ninemsn.Global.ContentManager.GetContent(fetchUrl, _oSwap ,jsonPropList);
        };   
        
        function feedUpdate (title,url,img_url,img_caption,ingestiontime) 
        {
            this.title = title;
            this.url = url;
            this.img_url = img_url;
            this.img_caption = img_caption;
            this.ingestiontime = ingestiontime;
        };    
        
        function convertTime(t) 
        {
            var arrT = t.split(":");
            var h = arrT[0];
            var m = arrT[1].substring(0,2);
            var ampm = arrT[1].substring(2);
            var intH = parseInt(h);
            if (ampm.toUpperCase()== "PM" && h<12) 
            {
                intH = intH + 12;
            }
            return intH + ":" + m + ":00";
        };
        
        function getTimeSpan(inpDate) 
        {
            var rep = inpDate.replace("  ", " ", "gi");
            var arrDt = rep.split(" ");
            if (arrDt.length == 5) 
            {
                var strDt = monthName[arrDt[0]] + " " + arrDt[1] + ", " + arrDt[2] + " " + convertTime(arrDt[4]);
            } 
            else if (arrDt.length == 4) 
            {
                var strDt = monthName[arrDt[0]] + " " + arrDt[1] + ", " + arrDt[2] + " " + convertTime(arrDt[3]);
            }
            var ingestionTime = new Date(strDt);
            var currentTime = new Date();
            var elapsed = parseInt((currentTime-ingestionTime)/60000) + 1;
            var res;
            if (elapsed) {
                if (elapsed < 60) {
                    res = elapsed + "";
                    if (elapsed == 1) {
                        res = res + " min ago";
                    } else  {
                        res = res + " mins ago";
                    }   
                } else if (elapsed >= 60) {
                    var hr = parseInt(elapsed/60);
                    var mt = elapsed - hr*60;
                    
                    res = hr + "";
                    
                    if (hr == 1) {
                        res = res + " hour";
                    } else {
                        res = res + " hours";
                    }
                    
                    if (mt == 0) {
                        res = res + " ago";
                    } else  {
                        var mts = mt + "";
                        res = res + " " + mts;
                        if (mt == 1) {
                            res = res + " min ago";
                        } else {
                            res = res + " mins ago";
                        }
                    }
                }
            } else {
                res = "";
            }
            return " " + res;
        };
    };
});










