
// constructor
Scale = function (key,type)
{

	this.notes = [ "A","Bb","B","C","C#","D","Eb","E","F","F#","G","G#" ];

	// setup scale interval array (semitone = 1)
	this.scale_intervals = 	{
							major : 			[2, 2, 1, 2, 2, 2, 1], // also ionian mode
							minor : 			[2, 1, 2, 2, 1, 2, 2], // also aeolian mode
							dorian : 			[2, 1, 2, 2, 2, 1, 2], // jazz feel
							phrygian : 			[1, 2, 2, 2, 1, 2, 2],
							lydian : 			[2, 2, 2, 1, 2, 2, 1],
							mixolydian :		[2, 2, 1, 2, 2, 1, 2], // blues and jazz
							locrain : 			[1, 2, 2, 1, 2, 2, 2], // japanese and hindu
							pentatonic :		[2, 2, 3, 2, 3],
							pentatonicMinor :	[3, 2, 2, 3, 2],
							pentatonicBlues : 	[2, 1, 1, 3, 2],							
							bluesScale :		[3,2,1,4,2]
							};
	this.scale_names = 		{
							major : 			'Major',
							minor : 			'Minor',
							dorian : 			'Dorian',
							phrygian : 			'Phrygian',
							lydian : 			'Lydian',
							mixolydian :		'Mixolydian',
							locrain : 			'Locrain',
							pentatonic :		'Pentatonic',
							pentatonicMinor :	'Pentatonic Minor',
							pentatonicBlues : 	'Pentatonic Blues',
							bluesScale :		'Blues Scale'
							};							
	notes = [ ];
	
	// check the format of key (number or string)
	if (typeof key == 'undefined') key = 'C';
	this.key = new String(key);
	if ( this.key.search(/^[0-9]+$/) == -1 ) // not numeric
	{
		for (var i=0; i<this.notes.length; i++)
			if (this.key == this.notes[i])
				this.key = i;
	}
	
	if (typeof type == 'undefined') type = 'major';
	this.type = type;
	this.intervals = this.scale_intervals[this.type];
	
	this.scale_notes = this.get_notes();
	this.name = this.notes[this.key] + ' ' + this.scale_names[this.type] + ' scale';
	
};

Scale.prototype = 
{

	get_notes : function ()
	{

		var i = 0;
		var index = this.key;
		var notes = [];
		
		notes[i] = this.notes[index]; // starting note
		i++;

		var interval;
		for (var j=0; j<this.intervals.length; j++)
		{
			interval = this.intervals[j];
			index += interval;

			if (index > (this.notes.length-1))
				index = index - this.notes.length;
				
			notes[i] = this.notes[index];

			i++;
		}

		return notes;
	}
	
};
