var album = new Object();
$(document).observe("dom:loaded", function() {	
  album = new Album();
});


var Album = Class.create({
	initialize: function(){
		this.albumPath = "";
		this.duration = 0.5;
		this.opacity = 0.8;
		this.pos=0;
		new Ajax.Request('album.html',
		{
			method:'get',
			onSuccess: function(transport){
				this.paths = transport.responseText.evalJSON();
				this.cat = $H(this.paths).keys();
				this.section = this.cat[0];				
				this.createSection();
			}.bind(this),
			onFailure: function(){ alert('Something went wrong...'); }
		});
	},
	
	createSection: function(){
		var content = '';
		for(var i=0;i<this.cat.length;i++){			
			content = content + "<div id='"+this.cat[i]+"_albumMenu' class='section'><a href='javascript:void(0);' onclick='album.changeSection("+i+")'>"+this.cat[i]+"</a></div>";
		}

		$('albumcenterMenu').update(content);
		//set default

	},
	
	changeSection:function(section){
		$(this.section+'_albumMenu').removeClassName('selected');
		this.section = this.cat[section];
		$(this.section+'_albumMenu').addClassName('selected');
		this.pos = 0;
		this.next();
	},
	
	
	loadAlbum:function(section){
		$('main').hide();
		document.body.style.background = '#000000'
		this.createSection();
		if(section){
			if( typeof (section) == 'string'){
				this.section = section;		
			}else{
				this.section = this.cat[section];
			}
		}
		$(this.section+'_albumMenu').addClassName('selected');
		this.next();

		
		$('album').show();
		
		new Effect.Opacity('album', {duration:this.duration, from:0.0, to:1.0});
	},


	close : function(){
		document.body.style.background = '#bcad9f';
		this.fadeout('album');
		$('main').show();
		window.setTimeout(function() {
			$('album').hide(); }, 1000);
	},

	fadeout: function(section){
		new Effect.Opacity(section, {duration:this.duration, from:this.opacity, to:0.0});
	},

	fadein: function(section){
		new Effect.Opacity(section, {duration:this.duration, from:this.opacity, to:0.80});
	},


	next: function(){
		var section = this.paths[this.section];
		var picture = section[this.pos];
		$('pictureFrame').src=	this.albumPath+picture.filename;
		this.pos = this.pos + 1;		
		if(this.pos >= section.length){
			this.pos = 0;
		}
	},

	previous: function(){
		var section = this.paths[this.section];
		var picture = section[this.pos];
		
		$('pictureFrame').src=	this.albumPath+picture.filename;
		this.pos = this.pos - 1;		
		if(this.pos < 0){
			this.pos = section.length-1;
		}

	}	
});