/* 
 * Copyright 2009 Squawkfox.com
 * All rights reserved.
 * 
 * Requires JQuery
 */


var LinkTracker = function() {
    return {

        //
        // Track the current page
        //
        TrackCurrentPage: function() {
            var rootUrl = LinkTracker.GetRootUrl();

            jQuery.get(
                rootUrl + "/currentview/currentview.php",
                currentview);  // currentview defined in page.
        },

        //
        // Request and process data from the server
        //
        LoadData: function() {
            var rootUrl = LinkTracker.GetRootUrl();

            jQuery.get(
                rootUrl + "/currentview/links.data?t=" + new Date().getTime(), // Tack on a timestamp to bust caching
                LinkTracker.LoadDataCallback);

            setTimeout(LinkTracker.LoadData, 30000);
        },

        //
        // Handle data passed back from the server
        //
        LoadDataCallback: function(data) {
            // Get the ID of the top current list element.                   
            var topId = jQuery("#Current li:first-child").attr("ID");
            

            if (!topId) {
                topId = "RecentViewed0";
            }
            
            var postId = topId.substr(12);
            var lines = data.split('\n');

            var startIndex = (lines.length - 1) - 5
            if (startIndex < 0) {
                startIndex = 0;
            }

            for (var i = startIndex; i < lines.length - 1; i++) {
                if (postId == LinkTracker.GetIdFromLine(lines[i])) {
                    startIndex = i + 1;
                    break;
                }
            }

            for (var j = startIndex; j < lines.length - 1; j++) {
                LinkTracker.AddLine(lines[j]);
            }
        },

        //
        // Push a line onto the stack.
        //
        AddLine: function(line) {

            if (jQuery(".recentViewedItem").length > 4) {
                jQuery("#Current li:last-child").remove();
            }

            var items = line.split("&&&");
            var html = LinkTracker.GetHtmlForLine(items[0], items[1], items[2], items[3]);

            jQuery(html).hide().prependTo("#Current").fadeIn("slow");
        },

        //
        // Generate HTML for the line to display
        //
        GetHtmlForLine: function(postId, url, title, imageUrl) {

            return ' ' +
'<li id="RecentViewed' + postId + '" class="recentViewedItem" >' +
'   <a href="' + url + '">' +
'       <img src="' + imageUrl + '"> ' + title.replace("\\'", "'") +
'   </a>' +
'   <div style="clear: both;"></div>' +
'</li>';
    

        },

        //
        // Parse the post ID from the line.
        //
        GetIdFromLine: function(line) {
            return line.substring(0, line.indexOf("&&&"));
        },

        //
        // Get the base URL
        //
        GetRootUrl: function() {
            return home_link;
        }
    };
}();

function sf_track_current() {
    LinkTracker.TrackCurrentPage();
}

function sf_load_current() {
    LinkTracker.LoadData();
}




