// Now Pattern
// TITLE:
var g_now_titles = [
	'Sound Tracing Soundwalk for',
	'City Soundwalk for',
	'Sound Tracing Soundwalk for',
	'Loud Soundwalk for',
	'Quiet Soundwalk for',
	'Rhythmic Soundwalk for',
	'Melodic Soundwalk for',
	'Following Soundwalk for'
]

// FIRST LINE:  begin
var g_now_begin= [
	'Begin your soundwalk now.  Listen.',
	'Begin your soundwalk where you are now.  Open your ears.',
	'Begin listening now.'
];


//SECOND LINE: drift (no destination)
var g_now_drift= [
	'Let the sounds direct your itinerary.',
	'Find your way to a city street.  Allow the street lights to direct your itinerary: at red lights, turn right, at green lights, turn left.',
	'When drawn by a particular sound, follow it.',
	'Follow the sounds you are attracted to.',
	'Walking -- and listening -- follow the loudest sounds you can locate.',
	'Follow the quietest sounds you can locate.',
	'Let the sounds direct your itinerary, by following the most rhythmic sounds you can locate.',
	'As you walk, and listen, follow the most melodic sounds you can locate.',
	'Choose a person to follow, listening as you do.'
];

// THIRD LINE: stop 
var g_now_stop_lines = [
	'Continue until you feel it is time to stop.  Wherever you are now:',
	'Continue for longer than you want to.  Only then:',
	'Continue until you feel it is time to stop.  Now:',
	'When you are ready to stop, don\'t.  Instead:'
];

// FORTH LINE: games that have an implied end
var g_now_game = [
  'Circle the area you are in three times.  Listen more closely each time.',
  'Choose an object and circle it three times.  Listen more closely each time.',
	'Identify the softest sound you can hear. Locate this sound.',
	'Listen to your internal dialogue. (Is it sound?)',
	'Slowly rotate 360 degrees. Repeat.  Compare your experience of the first and second rotations.',
	'Perform a routine action appropriate to the space you are in.',
	'Find an object and tap it. Listen for the end of each sound.  Repeat three times.',
	'Listen for a sound you cannot see the origin of. (You need not locate it.)',
	'Listen to the internal sounds of your body.',
	'Focus on the ugliest sound you can hear.  Stay with this sound a little while.',
	'Listen while standing still.',
	'Imagine your have taken a vow of silence for seven years.  Choose a sound to break the silence.  Make this sound.',
	'Sit and listen.',
	'Listen low to the ground.',
	'Consider, your whole body as an ear.',
	'Consider, 360 degree listening.',
	'Sing a song to yourself. Let the sounds around you be your accompaniment.',
	'Spending some time tapping, touching, stroking, or striking, an object in your environment. Listen.'
];


// Outside Pattern

// TITLE:
var g_outside_titles = [
	'Transit Soundwalk for',
	'Garden Soundwalk for',
	'Park Soundwalk for',
	'Place of Workship Soundwalk for',
	'Bus Stop Soundwalk for',
	'Traffic Light Soundwalk for'
];

// FIRST LINE: begin
var g_outside_begin = [
	'Begin your soundwalk outside.  Listen.',
	'Begin your soundwalk.  Listen.',
	'Begin your soundwalk now.  Go outside, listening.',
	'Open your ears.',
	'Begin listening.  Go outside.',
	'Listen.  Go outside.',
	'Begin listening now.',
	'Listen.'
];

// SECOND LINE: drift (with destination)
var g_outside_drift = [
	'Get on the nearest bus/skytrain/subway/metro.  Stay on until the end of the line (continuing to listen).',
	'Walk -- listening -- to the nearest garden.',
	'Walk -- listening -- to the nearest park.',
	'Walk -- listening -- to the nearest place of worship.',
	'Walk -- listening -- to the nearest bus stop.',
	'Walk -- listening -- to the nearest traffic light.',
	'Walk -- listening -- to the nearest alley.',
	'Walk -- listening -- to the nearest shop.',
	'Walk -- listening -- to the nearest laudromat.',
	'Walk -- listening -- to the nearest mail box.'
];

// THIRD LINE: game
var g_outside_game = [
	'Upon arrival, choose an object and circle it three times, listening more closely each time.',
	'At your destination, identify the softest sound you can hear. Locate this sound.',
	'At your destination, listen to your internal dialogue. (Is it sound?)',
	'At your destination, perform a routine action appropriate to the space you are in.',
	'At your destination, find an object and tap it. Listen for the end of each sound.  Repeat three times.',
	'Upon arrival, stop and listen for a sound you cannot see the origin of. (You need not locate it.)',
	'At your destination, stop and listen to the internal sounds of your body.',
	'At your destination, slowly rotate 360 degrees.  Repeat.  Compare your experience of the first and second rotations.'
];

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

// Ono Pattern

// TITLES
var g_ono_titles = [
	'Yoko Ono Soundwalk for',
	'Object Soundwalk for',
	'Soundride for',
	'Indeterminate Soundwalk for',
	'Commerce Soundwalk for',
	'Worship Soundwalk for',
	'Wet Soundwalk for',
	'Highway Soundwalk for',
	'Indeterminate Soundwalk for',
	'Indeterminate Soundwalk for',
	'Indeterminate Soundwalk for',
	'Indeterminate Soundwalk for',
	'Indeterminate Soundwalk for',
	'Indeterminate Soundwalk for',
	'Follow Walk for'
];

// FIRST LINE
var g_ono_begin = [
	'Draw an imaginary map.  Take a soundwalk through actual streets/spaces using this map.',
	'Attach a sound-making object (loud jewelry, bells, cutlery) to your body.  Go outside and begin your soundwalk.',
	'Get on the nearest bus/skytrain/subway/metro.  Listen.',
	'Allow the streetlights to direct your itinerary: at red lights, turn right, at green lights, turn left.',
	'Enter a place of commerce.  Listening, circle the interior.',
	'Enter a place of worship.  Listening, circle the interior.',
	'Make your way to a body of water.  Listen.',
	'Choose a highway bridge, or overpass, with a pedestrian path.  Cross the bridge, or overpass, very slowly.',
	'Walk in any direction, listening.  Allow the sounds to direct your itinerary',
	'Begin walking, begin listening.  Allow yourself to follow the loudest sounds you can hear.',
	'Begin walking, begin listening.  Allow yourself to follow the quietest sounds you can hear.',
	'Begin walking, begin listening.  Allow yourself to follow the most rhythmic sounds you can hear.',
	'Begin walking, begin listening.  Allow yourself to follow the most melodic sounds you can hear.',
	'Begin walking, begin listening.  Allow yourself to follow the most melodic sounds you can hear.',
	'Choose someone to follow -- listening -- until they reach their destination.'
];

//SECOND LINE (optional)
var g_ono_optional_lines = [
	'As you listen, consider: 360 degree listening.',
	'As you listen, consider: your whole body as an ear.',
	'Include all you are hearing in the same sonic experience.',
	'Consider: listening through your feet.',
	'Consider: listening through your skin.',
	'Begin to hum as you listen.'
];

// THIRD LINE
var g_ono_last_lines = [
  'Stop when you feel it is time to stop.',
	'Stop when it is time to stop.',
	'Continue until you feel like stopping.',
	'Continue your soundwalk longer than you feel like.',
	'Do not stop.',
	'Do not stop.',
	'Stop.',
	'Make a sound to end your soundwalk.'
];

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

// class RandThing - start
	function RandThing(intArray) {
		this.prng = new MersenneTwister19937; // creating an instance of MersenneTwister19937 class.
		this.prng.init_by_array(intArray,intArray.length);
	}

	RandThing.prototype.Seed = function(string) {
		this.prng = new MersenneTwister199937;
		this.prng.init_by_array(intArray,intArray.length);
	}

	RandThing.prototype.randInt = function(count) {
		var random = this.prng.genrand_int32();
		return random % count;
	}
// class RandThing - end

function AppendLines(array,lines) {
	var linesString = new String(lines);
	var linesArray = linesString.split('  ');
	return array.concat(linesArray);
}

function OutsideSoundWalkGen(randThing) {
	var resultArray = new Array();

	var newLines;

	newLines = g_outside_begin[randThing.randInt(g_outside_begin.length)];
	resultArray = AppendLines(resultArray,newLines);

	var driftIndex = randThing.randInt(g_outside_drift.length);
	newLines = g_outside_drift[driftIndex];
	resultArray = AppendLines(resultArray,newLines);

	resultArray.unshift(g_outside_titles[driftIndex]);

	newLines = g_outside_game[randThing.randInt(g_outside_game.length)];
	resultArray = AppendLines(resultArray,newLines);

	return resultArray;
}

function NowSoundWalkGen(randThing) {
	var resultArray = new Array();

	var newLines = g_now_begin[randThing.randInt(g_now_begin.length)];
	resultArray = AppendLines(resultArray,newLines);

	var driftIndex = randThing.randInt(g_now_drift.length)
	newLines = g_now_drift[driftIndex];
	resultArray = AppendLines(resultArray,newLines);

	resultArray.unshift(g_now_titles[driftIndex]);

	newLines = g_now_stop_lines[randThing.randInt(g_now_stop_lines.length)];
	resultArray = AppendLines(resultArray,newLines);

	newLines = g_now_game[randThing.randInt(g_now_game.length)];

	var lastLine = resultArray.pop();
	resultArray = AppendLines(resultArray, lastLine + ' ' + newLines);

	return resultArray;
}

function OnoSoundWalkGen(randThing) {
	var resultArray = new Array();
	var newLines = "";

	onoSoundWalkIndex = randThing.randInt(g_ono_titles.length);
	newLines = g_ono_titles[onoSoundWalkIndex];
	resultArray = AppendLines(resultArray,newLines);

	newLines = g_ono_begin[onoSoundWalkIndex];
	resultArray = AppendLines(resultArray,newLines);

	if (randThing.randInt(2))  {
		newLines = g_ono_optional_lines[randThing.randInt(g_ono_optional_lines.length)];
		resultArray = AppendLines(resultArray,newLines);
	}

	newLines = g_ono_last_lines[randThing.randInt(g_ono_last_lines.length)];
	resultArray = AppendLines(resultArray,newLines);

	return resultArray;
}

var g_lineIds = new Array();

function SoundWalkWrite_Gen(dateArray,styleText) {
	
	var randThing = new RandThing(dateArray);
	var genSoundWalk = null;

	switch (randThing.randInt(3)) {
		case 0:
			genSoundWalk = NowSoundWalkGen(randThing);
			break;
		case 1:
			genSoundWalk = OutsideSoundWalkGen(randThing);
			break;
		case 2:
			genSoundWalk = OnoSoundWalkGen(randThing);
			break;
	}
	
	//var indent = (genSoundWalk.length) * 12;
	var indent = 12;

	document.write('<div style="padding-left: ' + indent + 'pt" class="SubHeading">'+ genSoundWalk[0] + ' ' + dateArray.join('-') +'</div><br>');
	genSoundWalk = genSoundWalk.slice(1);
	indent +=  12; // zag left to right

	var lineIndex;
	for (lineIndex in genSoundWalk) {
		 /*indent = (genSoundWalk.length - lineIndex) * 12; // zag right to left */
		indent +=  12; // zag left to right
		var line = genSoundWalk[lineIndex];
		var lineNo = Number(lineIndex) + 1;
		var lineId = "line-" + lineNo;
		document.writeln('<div style="' + styleText + '" id="' + lineId + '">');
		g_lineIds.push(lineId);
		document.writeln('<p style="padding-left: ' + indent + 'pt">' + lineNo +") " + line + '</p>');
		document.writeln('</div>');
	}
}

function SoundWalkWrite_Composed(dateArray,styleText) {
	
 	var SoundWalkIndex = 0;

	var lineIndex;
	for (lineIndex in SoundWalks[soundWalkIndex]) {
		var line = SoundWalks[soundWalkIndex][lineIndex]
		if (lineIndex == 0) { // title
			document.write('<div class="SubHeading">' + line + '</div>');
		} else {
			var indent = (SoundWalks[soundWalkIndex].length - lineIndex) * 12;
			var lineId = "line-" + (lineIndex + 1);
			g_lineIds.push(lineId);
			document.writeln('<div style="' + styleText + '" id="' + lineId + '">');
			document.writeln('<p style="padding-left: ' + indent + 'pt">' + line + '</p>');
			document.writeln('</div>');
		}
	}
}

function SoundWalkWrite(type,dateString,styleText) {
	var dateArray = dateString.split("-");
	if (type == "comp") {
		SoundWalkWrite_Composed(dateArray,styleText);
	} else {
		SoundWalkWrite_Gen(dateArray,styleText);
	}
}

function View(type) {
	var d = new Date();
	var dString = "";
	var type='gen';
	dString =  d.getSeconds() + '-' + d.getMinutes() + '-' + d.getHours() + '-' + d.getDate() + '-' + d.getMonth() + '-' + d.getFullYear();
	window.location = "view.html?"+type+"&"+dString
	
}

function Print(type,dString) {
	window.location = "print.html?" + type + "," +dString;
}
