var projects = [];
var selection = [];
var selection_group = [];
var cur_sel_group = 0;

var Project = new Class({
		initialize: function(data){
			var list;
		
			this.color = data.getAttribute("color");
			this.group = data.getAttribute("group");
			if (!this.group) this.group = 0;
			
			list = data.getElementsByTagName("visual");
			if (list.length)
				this.visual = list.item(0).getAttribute("src");
			list = data.getElementsByTagName("link");
			if (list.length)
				this.link = list.item(0).getAttribute("href");
				
			list = data.getElementsByTagName("title");
			if (list.length)
				this.title = list.item(0).textContent;
			if (!this.title)
				this.title = list.item(0).text;
				
			list = data.getElementsByTagName("description");
			if (list.length)
				this.description = list.item(0).textContent;
			if (!this.description)
				this.description = list.item(0).text;

			this.load();
		},
		load: function(){
			var project = this;
			this.image = new Asset.image(
				this.visual, {
				onload: function() {
					project.ready();
				}
			});
		},
		ready: function(){
			this.loaded = true;
		},
		isLoaded: function(){
			return this.loaded;
		},
		render: function(){
			
			$('background').setStyle("backgroundImage", 
					$('fader').getStyle("backgroundImage"));
			
			$('fader').setOpacity(0);
			$('fader').setStyle("backgroundImage",
					"url(" + this.image.src + ")");
			if ($('project_link')) {
				$('project_link').href = this.link;
			}
			
			var project = this;
			var color = this.color;
			var fader = $('fader').effect('opacity', {
					duration: 2000});
			fader.start(0,1);
			
			var colorizer = new Fx.Styles($('bar'), {
					duration:2000, 
					transition:Fx.Transitions.Sine.easeIn});
			colorizer.start({'background-color': color});
				
			var slider = $('description').effect('opacity', {duration: 1000});
			slider.start(1,0).chain(function(){
				$$("#description h2").setHTML(project.title);
				$$("#description p").setHTML(project.description);
				$('description').effect('opacity', {duration: 1000}).start(0, 1);
			});
			
		}
});


window.addEvent('domready', function() {

	new Ajax("projects.xml?" + (new Date().getTime()), {method: 'get', onComplete:function () {
		if (this.isSuccess){
			var list = this.response.xml.getElementsByTagName("project");
			var index;
		
			for (index = 0 ; index < list.length; index++)
				projects.push(new Project(list.item(index)));
				
			initialize.delay(100);
					
		}}}).request();
});


function initialize() {
	var loaded = false;
	
	projects.each(function(project, index) {
		if (!selection_group[project.group])
			selection_group[project.group] = [];
		if (project.isLoaded())
			loaded = true;
	});
	
	if (loaded){
		refresh();
	} else {
		initialize.delay(100);
	}
}

function refresh() {
	var project;
	var index;
	
	loaded = true;
	
	selection_group.each(function(group, idx) {
		if (!group.length) {
			projects.each(function(project, index){
				if (project.group == idx /*&& project.isLoaded()*/)
					group.push(project);
			});
		}
	});
	
	cur_sel_group++;
	if (cur_sel_group >= selection_group.length) cur_sel_group = 0;
	
	//cur_group = selection_group[cur_sel_group];
	selection = selection_group[cur_sel_group];
	/*
	if (!selection.length) {
		projects.each(function(project, index){
			if (project.isLoaded())
				selection.push(project);
		});
	}
	*/
	if (!selection.length) {
		refresh.delay(100);
	}
	
	index = Math.floor(Math.random() * selection.length);
	
	project = selection[index];
	if (!project.isLoaded()) {
		refresh.delay(100);
		return;
	}
	selection.splice(index, 1);
	
	
	project.render();
	refresh.delay(10000);
}
