$(document).ready(function() {
    highlightTable();
    $('#newsNav a').click(
        function(){
            getNewsForPage(this);
            return false;
        }
    );
});

function highlightTable(){ 
    $('#documents tr').each(function(index){
        if(index %2 != 0){
            $(this).addClass("highlight");
        }
        $(this).click(function(){
            onDocumentClick(this);
        });
    });
    $('#news tr').each(function(index){
        if(index %2 != 0){
            $(this).addClass("highlight");
        }
        $(this).click(function(){
            onNewsClick(this);
        });
    });
}

function onDocumentClick( oRow ){
    var location = $(oRow).find("a").attr("href");
    window.location = location;
}

function getNewsForPage( inoLink ){
    var nPage = $(inoLink).attr( 'id' ).substr(4, 1);
    if(!nPage){
        return;
    }
    $.ajax(
        {
            url     : "processor.php",
            type    : "POST",
            context : inoLink,
            data    : {
                "class"     : "News",
                "method"    : "getStoriesForPage",
                "page"      : nPage
            },
            success : function(oResponse, textStatus, jqXHR){
                oResponse = $.parseJSON(oResponse);

                if(oResponse.success){
                    var next = oResponse.next;
                    var previous = oResponse.previous;
                    var pages = oResponse.pages;
                    $('#news table tbody').empty();
                    var aRows = oResponse.data;

                    for(var i = 0; i < aRows.length; i++){
                        var sHTML = "<tr><td>&nbsp;<a href='news.php?id="+oResponse.data[i].eventId+"'>"+oResponse.data[i].headline+"</a></td>";
                        sHTML += "<td class='date'>"+oResponse.data[i].date+"&nbsp;</td></tr>";
                        $('#news table tbody').append(sHTML);
                    }
                    highlightTable();
                    $("#news p").html("Page "+nPage+" of "+pages);
//                    $('#news tr').each(function(index){
//                        $(this).find( 'a' ).attr('href', 'news.php?id='+oResponse.data[index].eventId);
//                        $(this).find( 'a' ).html(oResponse.data[index].headline);
//                        $(this).find( 'td' ).last().html(oResponse.data[index].date);
//                    });
                   $("#newsNav a").first().attr('id', 'prev'+previous);
                   if(nPage == 1){
                       $("#newsNav a").first().addClass("disabled");
                   }else{
                       $("#newsNav a").first().removeClass("disabled");
                   }
                   if(nPage == pages){
                       $("#newsNav a").last().addClass("disabled");
                   }else{
                       $("#newsNav a").last().removeClass("disabled");
                   }
                   $("#newsNav a").last().attr('id', 'next'+next);
                }else{
                    alert("Unable to process your request");
                }
            }
        }
    );
}

