var g_clientDots;
var g_readUnreadStates;
var g_markAsReadUnreadInterval = 1000 * 1; // 1 second

/* Read tracking */
function ClientDot(dotId, offsetTop) {

    this.dotId = dotId;
    this.offsetTop = offsetTop;
}

function ReadUnreadState(dotId, isRead, isRecorded) {

    this.dotId = dotId;
    this.isRead = isRead;
    this.isRecorded = isRecorded;
}

function enableReadTracking() {

    var dots;
    var nextButton;
    var previousButton;
    var docElement;
    var isIe;
    var dotIndex;
    
    g_clientDots = new Array();
    g_readUnreadStates = new Object();
    
    isIe = isIE();

    dots = $("dotBucket").childNodes;
    nextButton = $("topicPagerNextButton");
    previousButton = $("topicPagerPreviousButton");
    dotIndex = 0;

    for (var i = 0; i < dots.length; i++) {

        if ((dots[i].className == "nada") || (dots[i].className == "topicDotListAd")) {
            continue;
        }

        g_clientDots[dotIndex++] = new ClientDot(dots[i].id.substring(3), Position.cumulativeOffset(dots[i])[1]);
    }

    if (g_clientDots.length == 0) {
        return;
    }

    Event.observe(window, "scroll", onScrollDotList);
    Event.observe(window, isIe ? "unload" : "pagehide", recordDotReadUnreadState);

    if (nextButton) {
        Event.observe(nextButton, "mousedown", markAllAsRead);    
    }

    if (previousButton) {
        Event.observe(previousButton, "mousedown", markAllAsRead);    
    }        

    createFocusBox();
    onScrollDotList(null, true);

    docElement = document.documentElement;
    
    if (isIe) {
        $("whiteSpacer").style.height = (docElement.offsetHeight * .7) + "px";
    } else {
        $("whiteSpacer").style.height = (docElement.clientHeight * .7) + "px";
    }

    window.setInterval(recordDotReadUnreadState, g_markAsReadUnreadInterval);    
}

function createFocusBox() {

    var focusContainer;
    var top;
    var middle;
    var bottom;

    focusContainer = $("rmFocusBox");

    if (focusContainer) {
        Element.remove(focusContainer);
    }

    focusContainer = document.createElement("div");
    focusContainer.id = "rmFocusBox";
    
    top = document.createElement("div");
    top.id = "rmFocusBoxTop";
    
    middle = document.createElement("div");
    middle.id = "rmFocusBoxMiddle";

    bottom = document.createElement("div");
    bottom.id = "rmFocusBoxBottom";

    focusContainer.appendChild(top);
    focusContainer.appendChild(middle);
    focusContainer.appendChild(bottom);    

    document.body.appendChild(focusContainer);
}

function onScrollDotList(e, preventFocusDotFromBeingMarkedRead) {

    var clientDot;
    var link;
    var scrollTop;
    var isS;

    isS = isSafari();
    preventFocusDotFromBeingMarkedRead = !!preventFocusDotFromBeingMarkedRead;

    for (var i = 0; i < g_clientDots.length; i++) {

        clientDot = g_clientDots[i];

        if (isS) {
            scrollTop = document.body.scrollTop;
        } else {
            scrollTop = document.documentElement.scrollTop;
        }

        if (scrollTop < clientDot.offsetTop) {

            if (preventFocusDotFromBeingMarkedRead) {
                break;
            }

            if (!implicitMarkAsRead(clientDot.dotId)) {
                break;
            }
            
            link = $("markAsRead:dot" + clientDot.dotId);

            if (!link) { // may happen if you are too quick
                break;
            }
            
            link.className = "read";
            break;
        }
    }

    positionFocusElement(clientDot.dotId);
}

function positionFocusElement(dotId) {
    
    var focusRectangle;
    var focusRectangleStyle;
    var dot;

    dot = $("dot" + dotId);

    focusRectangle = $("rmFocusBox");
    focusRectangleStyle = focusRectangle.style;
    focusRectangleStyle.width = dot.offsetWidth + "px";
    focusRectangleStyle.left = (Position.cumulativeOffset(dot)[0] + 3) + "px";
    focusRectangleStyle.top = (Position.cumulativeOffset(dot)[1] - 2) + "px";

    $("rmFocusBoxMiddle").style.height = (dot.offsetHeight - 10) + "px";
}

function toggleMarkAsRead(dotId) {

    var link;
    var state;

    if (g_readUnreadStates == null) {
        return;
    }

    state = g_readUnreadStates[dotId];
    link = $("markAsRead:dot" + dotId)

    if (state == null) {

        if (link.className == "unread") {
            explicitMarkAsRead(dotId);
        } else {
            explicitMarkAsUnread(dotId);
        }
        
    } else {

        if (state.isRead) {
            explicitMarkAsUnread(dotId);
        } else {
            explicitMarkAsRead(dotId);
        }
    }    

    link.className = (link.className == "unread") ? "read" : "unread";
}

function markAllAsRead() {

    var clientDot;
    
    for (var i = 0; i < g_clientDots.length; i++) {

        clientDot = g_clientDots[i];

        if (g_readUnreadStates[clientDot.dotId] == null) {
            g_readUnreadStates[clientDot.dotId] = new ReadUnreadState(clientDot.dotId, true, false);
        }
    }

    recordDotReadUnreadState();
}

function implicitMarkAsRead(dotId) {

    if (g_readUnreadStates[dotId] != null) {
        return false;
    }
    
    g_readUnreadStates[dotId] = new ReadUnreadState(dotId, true, false);
    return true
}

function explicitMarkAsRead(dotId) {

    var state = g_readUnreadStates[dotId];
    
    if (state == null) {
        g_readUnreadStates[dotId] = new ReadUnreadState(dotId, true, false);
        return;
    } 

    if (state.isRead) {
        return;
    }

    state.isRead = true;
    state.isRecorded = false;
}

function explicitMarkAsUnread(dotId) {

    var state = g_readUnreadStates[dotId];

    if (state == null) {
        g_readUnreadStates[dotId] = new ReadUnreadState(dotId, false, false);
        return;
    }

    if (!state.isRead) {
        return;
    }

    state.isRead = false;
    state.isRecorded = false;
}

function recordDotReadUnreadState() {

    var readDotIds;
    var unreadDotIds;

    readDotIds = new Array();
    unreadDotIds = new Array();
    
    for (var dotId in g_readUnreadStates) {

        if (g_readUnreadStates[dotId].isRecorded) {
            continue;
        }

        if (g_readUnreadStates[dotId].isRead) {
            readDotIds.push(dotId);
        } else {
            unreadDotIds.push(dotId);
        }

        g_readUnreadStates[dotId].isRecorded = true;
    }

    if ((readDotIds.length == 0) && (unreadDotIds.length == 0))
        return;

    RecommendationsHandler.RecordMarkAsReadAndUnread(readDotIds, unreadDotIds);
}

