/**
 * @author felipe lerena
 */
Event.observe(window,"load",onLoads);

//this is the messageQueue
var queue = Array();
//evaluates if is the first time we require data
var first = true;
//this indicates the message display format, this is an implementation of the state disign pattern
var state = null;
var boxList = Array();

function onLoads()
{
	$('personalize').style.display = "";

	createList();
	
	var list = readCookie("state");
	
	if ( (list == null) || (list == 'boxed') )
	{
		state = new BoxState();
	}
	else
	{
		state = new SneakState();
	}
	
	getData();
	
	createWatcher(getData,63);
	createWatcher(messageQueue,4);
}

function messageQueue()
{
	if(queue.length > 0)
	{
		shuffle(queue);
		var elem = queue[0]
		if (elem != null)
		{
			state.messageAdd(elem[0],elem[1],elem[2],elem[3],true);
			queue.shift();
		}
	}
}

function createWatcher(callback, interval)
{
	var pe = new PeriodicalExecuter(callback, interval);	
}




function getData()
{
	var dir = "getData2.php";
	var pars = "?";
	
	if(first == true)
	{
		first = false;
		pars += "first";
	}
	
	var fetcher = new Ajax.Request
		(
			dir,
		  	{
				method:'post',
				parameters: pars, 
				onComplete: addAndDisplay
			}
		);
}

function addAndDisplay(req)
{
	var json = eval('('+req.responseText+')');

	var blink = null;
	for (i in json)
	{
		try
		{
			blink = !state.evaluateFirst(json[i].id_box);
			
			if( (blink == false) )
			{
				state.messageAdd(json[i].id_box,json[i].text,json[i].link,json[i].id_items,blink);
				setLastItem(json[i].id_box,json[i].id_items);
			}
			else
			{
				messageEnQueue(json[i].id_box,json[i].text,json[i].link,json[i].id_items);
			}
		}
		catch(e)
		{
			/**/
		}
	}
	hideLoadingData();
	state.makeVisible();
}
function hideLoadingData()
{
	$('loadingData').style.display = "none";
}
function messageEnQueue(container,data,link,boxid)
{
	message = [container,data,link,boxid];
	queue.push(message);
	setLastItem(container,boxid);
}

function primerHijo(container)
{
	var firstBox = container.firstChild;
	
	if( firstBox.nodeType == 3 )
	{
		container.removeChild(firstBox);
		firstBox = container.firstChild;
	}
	return firstBox;
}

function addLastEvent(container,link)
{
	$('lastEvent').innerHTML = container + ":&nbsp;";
	$('lastEvent').appendChild(link);
}

function shuffle(o)
{
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};
function createList()
{
	boxList[0]  = {id:"1", 
				   title:"Planets",
				   url:"http://planet.ubuntu.com"
				  };
	boxList[1]  = {id:"2", 
				   title:"Forums",
				   url:"http://www.ubuntuforums.org"
				  };
	boxList[2]  = {id:"3",
				   title:"News",
				   url:"http://fridge.ubuntu.com"
				  };
	boxList[3]  = {id:"4",
				   title:"Wiki Recent Changes",
				   url:"https://wiki.ubuntu.com/RecentChanges"
				  };
	boxList[4]  = {id:"5", 
				   title:"Launchpad Bugs",
				   url:"https://bugs.launchpad.net"
				  };
	boxList[5]  = {id:"6", 
				   title:"LoCos (other languages)",
				   url:"https://wiki.ubuntu.com/LoCoTeamList"
				  };
	boxList[6]  = {id:"7",
					title:"Forums (other languages)",
					url:"https://wiki.ubuntu.com/LoCoTeamList"
				  };
	boxList[7]  = {id:"8", 
				   title:"Uploaded Packages",
				   url:"https://launchpad.net/ubuntu/+allpackages"
				  };
	boxList[8]  = {id:"9", 
				   title:"Security",
				   url:"http://www.ubuntu.com/usn"
				  };
	boxList[9]  = {id:"10", 
				   title:"Launchpad Answers",
				   url:"https://answers.launchpad.net"
				  };
        boxList[10]  = {id:"11",
                                   title:"Brainstorm",
                                   url:"http://brainstorm.ubuntu.com"
                                  };
	boxList[11]  = {id:"12",
                                   title:"Pulse of Ubuntu",
                                   url:"http://pulseofubuntu.tweetpeek.com/"
                                  };
	boxList[12]  = {id:"13",
                                   title:"Planets (other languages)",
                                   url:"http://planet.ubuntu.com/"
                                  };

}

