function disp_search() {

var s = document.getElementById('search_li');
if (s.style.display == "none") { s.style.display = "block"; 
var si = s.getElementsByTagName('input');
si = si[0];
si.focus();
} else { s.style.display = "none" }

var i = document.getElementById('search_img');
//Find out if the the current image is arrow_d or arrow_r
var place = i.src.length - 5;
var g_name = i.src[place];

if (g_name == "r") { i.src = "./graphics/arrow_d.png"; } else { i.src = "./graphics/arrow_r.png"; }
}

function parse_music() {
//Get the music data from the HTML document.
var span = document.getElementById('music_data');
var raw_data = span.innerHTML;

//Place the music files into the array split_data, and split the music file data within itself.
var split_data = raw_data.split("|");
var dat = new Array();
for (x in split_data) {
//.split() returns an array of the data, which is then stored as a music piece inside dat[]. 
//Music data is referenced by dat[x][y], for example. x returns the particular music piece; y returns that data inside that piece.
dat[x] = split_data[x].split(";");
}
//dat is now a multi-dimensional array organized first by music entry, then by that entry's data.
return dat;
}

function mobj(array) {
this.id = array[0];
this.title = array[1];
this.instrs = array[2];
this.key = array[3];
this.time = array[4];
this.lyrics = array[5];
}

function make_obj(dat) {
var obj = new Array();
for (x in dat) {
	var m = new mobj(dat[x]);
	obj[x] = "<div class='rd'><span id='m" + x + "'><ul style='display:block'><li class='title'>"+m.title+"</li><li>For "+m.instrs+"</li><li><a href=javascript:more('"+x+"') id='a"+x+"'>More...</a></li><li><a href='index.php?p=download&id=" + m.id + "'>Download</a></li><br><span style='display:none;' id='s"+x+"'><li>In the Key of "+m.key+"</li><li>With Time Sig "+m.time+"</li>";
	if (m.lyrics) { obj[x] = (m.lyrics == "Scottish Psalter") ? obj[x] + "<li>Lyrics from "+m.lyrics+"</li>" : obj[x] + "<li>Lyrics by "+m.lyrics+"</li>"; }
	obj[x] = obj[x] + "</span></ul></span></div>";
}
return obj;
}



function more(x) {
var sid = "s" + x;

var s = document.getElementById(sid);
if (s.style.display == "none") {
var aid = "a"+x;
var link = document.getElementById(aid);
// link.text = "Less...";
link.innerHTML = "Less...";

s.style.display = "block";

} else {
var aid = "a"+x;
var link = document.getElementById(aid);
// link.text = "Less...";
link.innerHTML = "More...";

var s = document.getElementById(sid);
s.style.display = "none";
}
}


function disp(cat, x) {
var win = document.getElementById('win');
win.innerHTML = "";

var dat = parse_music();
dat = disp_filter(dat, cat);

var obj = make_obj(dat);
if (!x) { 
var x = 0;
/*var now_x = document.getElementById('current_id');
var x = now_x.innerHTML;*/ }

//vars y and w represent the next and previous 10 pages.
var y = x + 10;
var w = x - 10;
//Only add 10 to the page!
var i = x;
while ((i < y ) && (obj[i])) { 
win.innerHTML += obj[i]; 
i++;
}

update_nav(obj, x);
}

function page(x) {
var now_filter = document.getElementById("current_filter");
var cat = now_filter.innerHTML;
disp(cat, x);
}

function next(x) {
if (typeof(x) == "number") { page(x); }
}

function prev(x) {
if (typeof(x) == "number") { page(x);  }
}

function update_nav(obj, x) {
//obj is the array of music. x is the current position in the music browsing. IE, if we're on page 2, x is 10, showing music 10 through 19.
var y = x + 10;
var w = x - 10;


//Now we will update the Prev and Next buttons.
if (obj[y]) { var to_go = y; } else { var to_go = ""; }
document.getElementById('a_next_0').href = "javascript:next(" + to_go + ")";
document.getElementById('a_next_1').href = "javascript:next(" + to_go + ")";

if (obj[w]) {
var to_go = w; } else { var to_go = ""; }
document.getElementById('a_prev_0').href = "javascript:prev(" + to_go + ")";
document.getElementById('a_prev_1').href = "javascript:prev(" + to_go + ")";

// var num_of_p = x / 10 - ((x % 10)/10);
var num_of_p = obj.length/10 - ((obj.length % 10)/10) + 1;
var i = 0;
var span0 = document.getElementById('nav_nums_0');
var span1 = document.getElementById('nav_nums_1');
//Remove all the existing navigation numbers...
span0.innerHTML = "";
span1.innerHTML = "";

//Create the navigation number links and add them to the page.
while ((i + 1 <= num_of_p) && (obj[i*10])) {
var num = document.createElement('a');
num.href = "javascript:page(" + i*10 + ");"
num.innerHTML = i+1;
if (i*10 == x) { num.style.fontWeight = "bold"; num.style.textDecoration = "underline"; }
span0.appendChild(num);

var num1 = document.createElement('a');
num1.href = "javascript:page(" + i*10 + ");"
num1.innerHTML = i+1;
if (i*10 == x) { num1.style.fontWeight = "bold"; num1.style.textDecoration = "underline"; }
span1.appendChild(num1);
i++;
}

}

function disp_filter(obj, cat) {
//var cat is actually the filter.

//Set the current_filter variable inside the page.
var now_filter = document.getElementById("current_filter");
//Get the old filter and set the new filter. We then pass the old and new filters to update_filter_links to change the links in the music navigation window to match the current display. If we are seeing cello music, the cello link should be bold.
var oldfilt = now_filter.innerHTML;
now_filter.innerHTML = cat;
update_filter_links(oldfilt, cat);

if (cat == 'all') { return obj; }

//This array will contain all the music that matches our filter.
var ret_obj = new Array();
switch (cat) {
	case 'Duets': var filter = 2;
			break;
	case 'Trios': var filter = 3;
			break;
	case 'Quartets': var filter = 4;
			break;
	case 'Quintets': var filter = 5;
			break;
	case 'Sextets': var filter = 6;
			break;
	case 'Septets': var filter = 7;
			break;
	default : var filter = cat;
}

if (typeof(filter) == "number") { 
	//Runs if we are sorting an ensemble
	for (x in obj) {
		instrs = obj[x][2].split(", ");
		if (instrs.length == filter) { ret_obj.push(obj[x]); }
	}
} else if ((filter != "Lyrics") && (filter.indexOf('?search?') == -1)) {
	//Runs if we're not sorting lyrics and if the string ?search? cannot be found.
	//Sorts by instrument.
	for (x in obj) {
		instrs = obj[x][2].split(", ");
		for (y in instrs) {
			if (instrs[y] == filter) { ret_obj.push(obj[x]); break; }
		}
	}

} else if ((filter.indexOf('?search?') == -1)&&(filter=="Lyrics")) {
	//Sorts music with lyrics
	for (x in obj) {
		if (obj[x][5]) { ret_obj.push(obj[x]); }
	}	
} else {
	//Sorts by search string
	str = filter.substr(8, filter.length);
	for (x in obj) {
		for (y in obj[x]) {
			if ((y != 0) && (obj[x][y].indexOf(str) != -1)) { ret_obj.push(obj[x]); break; }
		}
}
}
if ((ret_obj[0])&&(ret_obj.length != 0)) { return ret_obj; } else { 
var win = document.getElementById('win');
win.innerHTML = "<br><p align='center'><em>Sorry, no matches could be found for \"</em>" + str + "<em>\".<br>Try broadening your search.</p><br>";
}
}

function update_filter_links(oldfilt, newfilt) {
var of = document.getElementById("filter" + oldfilt);
var nf = document.getElementById("filter" + newfilt);

if (of) {
	of.style.fontWeight = "normal";
	of.style.fontSize = "1em";
	}

if (nf) {
	nf.style.fontWeight = "bold";
	nf.style.fontSize = "1.15em"; 
	}
}

function search() {
var search_txt = document.getElementById("search_txt");
var str = search_txt.value;
disp("?search?" + str);
}

function change_bg() {
var bgs = Array("horn_keys.png", "cello_bridge.png", "piano_keys.png", "sheet_music.png");
var mbody_text = document.getElementById("mbody_text");

var rand = Math.floor(Math.random()*4);
mbody_text.style.backgroundImage = "url(./graphics/" + bgs[rand] + ")";
}

