﻿
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.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("id").substring(9);

		this.topActivityID=currentActivityId;

		var actList=$('Przewijanie_boxAll');
		
		var list_item= "<div class='Przewijanie_box' id='activity_" + currentActivityId + "' style='display:none'>" + newNode.innerHTML + "</div>";
    new Insertion.Top('Przewijanie_boxAll', list_item);

		this.tempElement.removeChild(newNode);

		// usuwamy jakies starsze nody jesli sa...
		if (actList.childNodes.length==this.nodesToShow) {
			actList.removeChild(actList.childNodes[this.nodesToShow-1]);
		}
		
		//ustawiamy cookie z ostatnim wyswielanym activitem
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+1);
		document.cookie="lastActivity=" + currentActivityId +";expires="+exdate.toGMTString();

		
		//newNode.removeNode(true);
		// przenosimy node jako pierwsza pozycje na strone...
//		actList.insertBefore(newNode, actList.firstChild);

		// ustawiamy display na none, aby dobrze zadzialal skrypt pojawiania
//		newNode.setAttribute("style","display:none");

		//var lastSeenChild=$('glwn_najpop_content').childElements()[this.nodesToShow];
		//alert("curr=" + currentActivityId);

	    	
        new Effect.SlideDown("activity_"+currentActivityId, {duration: 2.5})


    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');


    if (actList.childNodes.length < 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++;
  new Ajax.Request('/async/activities.asp?id=' + this.topActivityID, {method: 'get', onSuccess: function(t) { ListOfAct.newQueue(t.responseText)}})  
}


