﻿
var ListOfAct = new Object();

ListOfAct.activities = [];
ListOfAct.topActivityID = 0;
ListOfAct.minInterval=4000;
ListOfAct.maxIntervalAddon=8000;
ListOfAct.newXmlInterval=20000;
ListOfAct.isFirst=1;
ListOfAct.nodesToShow=4;
ListOfAct.animationTime=1200;

ListOfAct.ajaxRequestCount = 0;

ListOfAct.tempElement=document.createElement('div');


ListOfAct.findLastDivChild = function() {
    var lastNodeFound;
		var actList=$('#Przewijanie_boxAll');
		for (j=0;j<actList.childNodes.length;j++)
			{
				if (actList.childNodes[j].nodeType == 1) {
			  	if (actList.childNodes[j].tagName.toLowerCase()=="div") { lastNodeFound=actList.childNodes[j] }
				}
			}
   return lastNodeFound;
}



ListOfAct.activityToPage = function() {
    var nodesLeft = this.tempElement.childNodes.length;

    if (nodesLeft > 0) {

        var newNode = this.tempElement.childNodes[0];
        var currentActivityId = newNode.getAttribute("activityid");//.substring(9);

        this.topActivityID = currentActivityId;

        var actList = $('#Przewijanie_boxAll');

        var list_item = "<div class='Przewijanie_box' id='activity_" + currentActivityId + "' style='margin-top:-90px'>" + newNode.innerHTML + "</div>";
        $('#Przewijanie_boxAll').prepend(list_item);
        //new Insertion.Top('#Przewijanie_boxAll', list_item);

        this.tempElement.removeChild(newNode);

        // usuwamy jakies starsze nody jesli sa...


        //ustawiamy cookie z ostatnim wyswielanym activitem
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + 1);
        document.cookie = "lastActivity=" + currentActivityId + ";expires=" + exdate.toGMTString();



        var animTime = this.animationTime;
        $("#activity_" + currentActivityId).animate({ marginTop: 0 }, animTime, function() {
    
            // kasujemy ostatniego itema dopiero po zjechaniu...
            if (actList.children().length == this.nodesToShow) {
                actList.children().last().fadeOut("slow", function() { $(this).empty().remove() });
            }            
        });


        return true;
    }
    return false
}

ListOfAct.executeObserver = function() {
    if (this.activityToPage()) {
        // jesli mamy strony w kolejce, to ponownie ustawiamy timer...
        this.enableObserver();

    } else {
        // nie ma wiecej stron, wiec pobieramy nowe z XMLa
        if (this.ajaxRequestCount == 0) {
            ListOfAct.makeAjaxRequest();
        } else {
            setTimeout(function() { ListOfAct.makeAjaxRequest() }, this.newXmlInterval);
        }
    }
}


ListOfAct.enableObserver = function() {
    // ustawiamy timer do wyswietlenia kolejnego wpisu
    var actList = $("#Przewijanie_boxAll");
    var len = actList.children("div").length;
    
    if (len < this.nodesToShow - 1) {
        this.isFirst = 0;
        setTimeout(function() { ListOfAct.executeObserver() }, 500);
    } else {
        setTimeout(function() { ListOfAct.executeObserver() }, this.minInterval + (Math.random() * this.maxIntervalAddon));
    }
}



ListOfAct.newQueue = function(body) {
    this.tempElement.innerHTML = body;
    this.executeObserver();
}


ListOfAct.makeAjaxRequest = function() {
    this.ajaxRequestCount++;
    $.ajax({
        url: '/aspx/async/activities.aspx?id=' + this.topActivityID,
        type: 'GET',
        success: function(xml) {
            ListOfAct.newQueue(xml)
        }
    });

}



