﻿displayCount = 6;
currentPage = 0;
currentList = null; 


function formatList(List, from) {
    currentList = List; 
    var tweetText = ""; var count = 0;
    if (typeof from === 'undefined')
        from = 0;
    for (var i = from, l = List.items.length; i < l && i <= from+displayCount; ++i) {
        if (count >= displayCount) { break; }
        tweetText += "<li class=\"tweet\"><h4><span class=\"marked\">Meetology</span> Meetology.com</h4><p>" + List.ElementAt(i).Status + "</p><span class=\"small\">" + List.ElementAt(i).Created + "</span></li>";
        ++count;
    }
    setText(tweetText);
    formatHeading();
    CreatePageButtons();
}

function GoToPage(Page) {
    if (Page <= (Math.floor(currentList.items.length / displayCount)) && Page >= 0) {
        currentPage = Page;
        formatList(currentList, currentPage * displayCount);
    }
}

function NextPage() {
    if (currentPage < Math.ceil(currentList.items.length / displayCount)-1) {
        ++currentPage;
        formatList(currentList, currentPage*displayCount);
    }
}

function PreviousPage() {
    if (currentPage != 0) {
        --currentPage;
        formatList(currentList, currentPage * displayCount);
    }
}

function CreatePageButtons() {
    From = currentPage == 0 ? currentPage : currentPage - 1;
    To = From + 2 > From + 2 ? From + 2 : Math.ceil(currentList.items.length / displayCount);
    buttons = '';
    for (i = From, j=0; j<3 && i < To; ++i, ++j) {
        buttons += CreatePageButton((i), (parseInt(i)+1));
    }
    $('#pageNumbers').html(buttons);
}

function CreatePageButton(id, text) {
    return '<a href\"#\" class=\"minibutton'+((id==parseInt(currentPage))?' selected':'')+'\" id=\"p' + id + '\"><span>' + text + '</span></a>';
}


function formatHeading(){
    var selectedList = (allTag) ? '#Social&nbsp; #Body&nbsp; #Mind&nbsp; #Space&nbsp; #Brain&nbsp; #Tech&nbsp; #WW' :
    ((socialTag) ? '#Social&nbsp; ' : '')
    + ((bodyTag) ? '#Body&nbsp; ' : '')
    + ((mindTag) ? '#Mind&nbsp; ' : '')
    + ((spaceTag) ? '#Space&nbsp; ' : '')
    + ((brainTag) ? '#Brain&nbsp; ' : '')
    + ((techTag) ? '#Tech&nbsp; ' : '')
    + ((wwTag) ? '#WW ' : '')
    $('#selectedHeading').html(selectedList)
}

function TechClick() {
    disableAll();
    techTag = !techTag;
    formatList(sortList(TweetList));
}

function BodyClick() {
    disableAll();
    bodyTag = !bodyTag;
    formatList(sortList(TweetList));
}

function SocialClick() {
    disableAll();
    socialTag = !socialTag;
    formatList(sortList(TweetList));
}

function MindClick() {
    disableAll();
    mindTag = !mindTag;
    formatList(sortList(TweetList));
}

function BrainClick() {
    disableAll();
    brainTag = !brainTag;
    formatList(sortList(TweetList));
}

function SpaceClick() {
    disableAll();
    spaceTag = !spaceTag;
    formatList(sortList(TweetList));
}

function WWClick() {
    disableAll();
    wwTag = !wwTag;
    formatList(sortList(TweetList));
}

function disableAll() {
    allTag = false;
    $('#All').removeClass('selected');
    currentPage = 0;
}

function AllClick() {
    allTag = true;
    socialTag = techTag = mindTag = brainTag = bodyTag = spaceTag = wwTag = false;
    $('#mainRight a.minibutton.selected').not('#All').removeClass('selected');
    currentPage = 0;
    formatList(sortList(TweetList));
}

function setText(tweetText) {
    $(".tweetContainer").html(tweetText);
}

function clearText() {
    $(".tweetContainer").html("");
}


function formatAndWrite(myList) {
    var example = JSLINQ(myList);
    var tweetText = "";
    for (var key in example.items) {
        tweetText = tweetText + key +  "<br/>" + example.ElementAt(key).Status + "<br/>";
    }
    setText(tweetText);
}

function sortList() {
    if (!techTag && !bodyTag && !socialTag && !mindTag && !brainTag && !spaceTag && !wwTag) {
        $('#All').addClass('selected');
        allTag = true;
        return JSLINQ(myList);
    }
    var Body = JSLINQ(myList).Where(function (item) {
        if (item) {
            return ((techTag && item.Tag.indexOf("#tech") != -1) || (bodyTag && item.Tag.indexOf("#body") != -1) || (socialTag && item.Tag.indexOf("#social") != -1) || (mindTag && item.Tag.indexOf("#mind") != -1) || (brainTag && item.Tag.indexOf("#brain") != -1) || (spaceTag && item.Tag.indexOf("#space") != -1) || (wwTag && item.Tag.indexOf("#ww") != -1));
        }
        else return false;
    });
    return Body;
}
