
function Album ( size, empty ){
	try{
		this.insertList = new Array();
		if (typeof(size) == "string"){
			var string = size;
			this.readAlbumParam(string);
		}
		else{
			this.size = size;
			if ( empty != true ){
				var insert = new SideInsert(null,"odd");
				this.addInsert(insert);
				insert = new SideInsert();
				this.addInsert(insert);
			}
		}
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album]</B> ' + e + '</FONT>' ); return false; } 
}


Album.prototype.toString = function(){
	try{
		var string = "(a)";
		string += this.id;
		string += "," + this.size.width + "," + this.size.height;
		string += this.getInsertsString();
		return string;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.toString]</B> ' + e + '</FONT>' ); return false; } 
}


Album.prototype.getInsertsString = function(){
	try{
		var string = "";
		for ( var i = 0; i < this.insertList.length; i++){
			string += (this.insertList[i]).toString();
		}
		return string;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.toString]</B> ' + e + '</FONT>' ); return false; } 
}

Album.prototype.readAlbumParam = function( string ){
	try{
		string = string.replace(/\(a\)/,"");
		var albumData = string.split("(i)");
		var inserts = albumData.splice(1,albumData.length);
		albumData = albumData[0].split(",");
		this.id = albumData[0];
		this.size = { width: Number(albumData[1]), height: Number(albumData[2])};
		//alert("Album string: "+string+"\nthis.id: "+this.id+"\nthis.size: "+this.size.width+","+this.size.height+"\nthis.albumType: "+this.albumType);
		//dbg.add("<FONT color='green'><B>Reading Album string</B>: "+string,"<I>this.id</I>: "+this.id,"<I>this.size</I>: "+this.size.width+","+this.size.height,"<I>this.albumType</I>: "+this.albumType,"<I>inserts.length</I>: "+inserts.length+"<BR></FONT>");
		for ( var i = 0; i < inserts.length; i++){
			this.addInsert(inserts[i]);
		}
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.readAlbumParam]</B> ' + e + '</FONT>' ); return false; } 
}


Album.prototype.getInsertQuantity = function (){
	try{
		return this.insertList.length;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.getInsertQuantity]</B> ' + e + '</FONT>' ); return false; } 
}

Album.prototype.getWidth = function (){
	try{
		return this.size.width;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.getWidth]</B> ' + e + '</FONT>' ); return false; } 
}

Album.prototype.getHeight = function (){
	try{
		return this.size.height;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.getWidth]</B> ' + e + '</FONT>' ); return false; } 
}

Album.prototype.getSize = function (){
	try{
		return this.size;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.getSize]</B> ' + e + '</FONT>' ); return false; } 
}

Album.prototype.setSize = function (size){
	try{
		this.size = size;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.getSize]</B> ' + e + '</FONT>' ); return false; } 
}

Album.prototype.getInsertSize = function (){
	try{
		return this.size;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.getInsertSize]</B> ' + e + '</FONT>' ); return false; } 
}


Album.prototype.getInserts = function (){
	try{
		if ( this.insertList != null )
			return this.insertList;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.getInserts]</B> ' + e + '</FONT>' ); return false; } 
}

Album.prototype.getInsert = function (position){
	try{
		return this.insertList[position];
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.getInsert]</B> ' + e + '</FONT>' ); return false; } 
}

Album.prototype.getInsertPosition = function (insert){
	try{
		var found = false;
		for ( var i = 0; i < this.insertList.length && found == false; i++){
			if ( this.insertList[i] === insert )
				found = true;
		}
		return (found?(i-1):null);
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.getInsertPosition]</B> ' + e + '</FONT>' ); return false; } 
}


Album.prototype.addBlankInserts = function (quantity, color, position){
	try{
		if ( position == null )
			position = this.insertList.lenght;
		if ( color == null)
			color = "white";
		for ( var i = 0; i < quantity; i++){
			insert = new SideInsert(color);
			this.addInsert(insert, position);
		}
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.addBlankInserts]</B> ' + e + '</FONT>' ); return false; } 
}

Album.prototype.addInsert = function (insert, position){
	try{
		if ( typeof(insert) == "string" ){
			var insertaux = new BaseInsert();
			insertaux.readInsertParam(insert);
			insert = insertaux;
		}
		insert.changeSize(this.size);
		if ( position == null )
			position = this.insertList.length;
		this.insertList.splice(position,0,insert);
		return position;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.addInsert]</B> ' + e + '</FONT>' ); return false; } 
}


Album.prototype.replaceInsert = function (insert, position){
	try{
		if ( typeof(insert) == "string" ){
			var insertaux = new BaseInsert();
			insertaux.readInsertParam(insert);
			insert = insertaux;
		}
		insert.changeSize(this.size);
		if ( position == null )
			position = this.insertList.length - 1;
		this.insertList.splice(position,1,insert);
		return position;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.replaceInsert]</B> ' + e + '</FONT>' ); return false; } 
}


Album.prototype.deleteInsert = function (position){
	try{
		if ( position == null )
			position = this.insertList.length - 1;
		return (this.insertList.splice(position,1))[0];
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.deleteInsert]</B> ' + e + '</FONT>' ); return false; } 
}

Album.prototype.moveInsert = function (fromPosition, toPosition, replace){
	try{
		return this.insertList.move(fromPosition,toPosition, replace);
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Album.moveInsert]</B> ' + e + '</FONT>' ); return false; } 
}



