﻿var WhatWeAreDoing;
var LoopDoingItems = true;
var iDoingItemCount;
var iDoingDelay = 13500; // milliseconds until next item displayed 
var DoingListHolder;
var MaxDisplay = 7;
var DoingTemplate;
var direction = 'forward';

function startTellingMeWhatYouAreDoing() {
    GetWhatWeAreDoingData()
}
function GetWhatWeAreDoing() {
    this.WhatWeAreDoingData;
    this.LoopDoingItems = LoopDoingItems;
    this.iDoingItemCount = iDoingItemCount;
    this.iDoingDelay = iDoingDelay;
    this.iMaxDisplay = MaxDisplay;
    this.iItemIndex = 0;
    this.RowIndex = 0;
    this.timer;
    this.Increment = 1;
    this.LoadTopToBottom = true;
}
function GetWhatWeAreDoingData() {
    HelloMusic.Services.WhatWeAreDoing.GetWhatWeAreDoingData("WhatWeAreDoing.xml", ProcessWhatWeAreDoing);
}
function ProcessWhatWeAreDoing(data) {
    WhatWeAreDoing = new GetWhatWeAreDoing();
    WhatWeAreDoing.WhatWeAreDoingData = data;
    WhatWeAreDoing.iDoingItemCount = WhatWeAreDoing.WhatWeAreDoingData.length;
    //To go in reverse we need to set out increment as -1 and start indes as the total number available.
    WhatWeAreDoing.iItemIndex = WhatWeAreDoing.iDoingItemCount - 1;
    WhatWeAreDoing.Increment = -1;
    quickAndDirtyLoaddoingItems();
    //QueueDoingItems();
    WhatWeAreDoing.timer = setInterval(QueueDoingItems, WhatWeAreDoing.iDoingDelay);
}
function quickAndDirtyLoaddoingItems() {
    for (var i = 0; i < 7; i++)
        QueueDoingItems()
}
function QueueDoingItems() {
//this is for debugging/testing that all the items appear and not have to wait so long...
//    if (WhatWeAreDoing.iItemIndex == 5) {
//        WhatWeAreDoing.iDoingDelay = 2000;
//        alert('slowDown!!');
//        clearTimeout(WhatWeAreDoing.timer);
//        WhatWeAreDoing.timer = setInterval(QueueDoingItems, WhatWeAreDoing.iDoingDelay);
//    }
    ShowNextDoingItem(WhatWeAreDoing.WhatWeAreDoingData[WhatWeAreDoing.iItemIndex], WhatWeAreDoing.RowIndex);
    WhatWeAreDoing.iItemIndex = WhatWeAreDoing.iItemIndex + WhatWeAreDoing.Increment;
    WhatWeAreDoing.RowIndex++;

    if (WhatWeAreDoing.LoopDoingItems) {
        if (WhatWeAreDoing.Increment == 1) {
            if (WhatWeAreDoing.iDoingItemCount == WhatWeAreDoing.iItemIndex) {
                WhatWeAreDoing.iItemIndex = 0;
            }
        }
        else {
        //we are going in reverse so we need to reset back to the total number.
            if (WhatWeAreDoing.iItemIndex == -1) {
                WhatWeAreDoing.iItemIndex = WhatWeAreDoing.iDoingItemCount - 1;
            }
        }
    }
    if (WhatWeAreDoing.iItemIndex < WhatWeAreDoing.iDoingItemCount) {
    }
    else {
        clearTimeout(WhatWeAreDoing.timer);
    }
}
function wait(millis) {
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while (curDate - date < millis);
}

function ShowNextDoingItem(doingItem, iIndex) {
    if (DoingListHolder == null)
        DoingListHolder = document.getElementById("DoingListHolder");
    if (DoingTemplate == null)
        DoingTemplate = new GetDoingTemplate();

    DoingTemplate.DoingArtisThumb.src = doingItem.ImagePath;
    DoingTemplate.DoingDate.innerHTML = doingItem.PubDate;
    DoingTemplate.DoingText.innerHTML = doingItem.Text;

    var c = DoingTemplate.DoingHolder.cloneNode(true);
    c.id = "DoingRow" + iIndex;
    jQuery(c).animate({ "opacity": "show", height: '75px' }, 1000, function() { });
    if (WhatWeAreDoing.LoadTopToBottom) {
        DoingListHolder.insertBefore(c, document.getElementById("DoingRow" + (iIndex - 1)));
    }
    else {
        DoingListHolder.appendChild(c);
    }
    if (iIndex > WhatWeAreDoing.iMaxDisplay - 1) {
        var RemoveNode = document.getElementById("DoingRow" + (iIndex - WhatWeAreDoing.iMaxDisplay));
        jQuery(RemoveNode).animate({ opacity: .1, height: '1px'}, 1000, function() { RemoveDoingItem(RemoveNode); });
    }
}
function RemoveDoingItem(RemoveNode) {

        if (RemoveNode != null)
            RemoveNode.parentNode.removeChild(RemoveNode);
}
function GetDoingTemplate() {
    var doingHolder = document.getElementById('DoingItemHolder');
    var doingArtisThumb = document.getElementById('DoingArtisThumb');
    var doingText = document.getElementById('DoingDescription');
    var doingDate = document.getElementById('Doingdate');
    doingHolder.parentNode.removeChild(doingHolder);
    doingHolder.className = "homeBodyFeature_div";
    doingHolder.style.height = '1px';
    doingHolder.id = '';
    doingArtisThumb.id = '';
    doingText.id = '';
    doingDate.id = '';
    this.DoingArtisThumb = doingArtisThumb;
    this.DoingHolder = doingHolder;
    this.DoingText = doingText;
    this.DoingDate = doingDate;
}

