var current = null;
/* var all = false;*/
var details;
var team = new Array();
var sortby="id";
var all = 0;
var preLoad = new Array();
var preLoadIndex = 0;
var class_name = new Array;
class_name['canoe'] = 'Canoe';
class_name['kayak'] = 'Kayak';
class_name['voyageur'] = 'Voyageur';
class_name['single'] = 'Open Canoe';
class_name['double'] = 'Open Kayak';
class_name['solo'] = 'Buddied Solo';
class_name['K1'] = 'Solo Kayak';
class_name['C1'] = 'Solo Canoe';

	document.onmouseup= endresize;
	document.onmousemove=doresize;

function detail(id)
{
	var i = id - 1;
	details = '';
	display_team(i);
	document.getElementById('team_details').innerHTML = details;
	window.location.hash = id;
	all = 0;
	document.getElementById('content').style.height = 
		(document.getElementById('team_details').offsetTop
		+document.getElementById('team_details').offsetHeight+10) + 'px';
	resize2();
	
}
function display_all()
{
	var i;
	var obj;
	all = 1;
	obj = document.getElementById('hideall');
	obj.style.display = "inline";
	obj = document.getElementById('displayall');
	obj.style.display = "none";
  details = '';
	for (i = 0; i < team.length; i++) {
		display_team(i);
	}
	document.getElementById('team_details').innerHTML = details;
	document.getElementById('content').style.height = 
		(document.getElementById('team_details').offsetTop
		+document.getElementById('team_details').offsetHeight+10) + 'px';
	resize2();
}
function hide_all()
{
	var i;
	var obj;
	all = 0;
	obj = document.getElementById('hideall');
	obj.style.display = "none";
	obj = document.getElementById('displayall');
	obj.style.display = "inline";
	document.getElementById('team_details').innerHTML = '<br>';
	document.getElementById('content').style.height = 
		(document.getElementById('team_details').offsetTop
		+document.getElementById('team_details').offsetHeight+10) + 'px';
	resize2();
}
/*
function highlight(id)
{
	document.getElementById(id).style.backgroundColor = "#999999";
}
function unhighlight(number, oddity)
{
	if (oddity == 'odd') {
		document.getElementById(number).style.backgroundColor = "#a9b8c0";
	} else {
		document.getElementById(number).style.backgroundColor = "#cccccc";
	}
}
i*/
function Member(name, age, sex, location, bio, aim, reason, photo)
{
	this.name = name;
	this.bio = bio;
	this.sex = sex;
	this.bio = bio;
	this.aim = aim;
	this.age = age;
	this.location = location;
	this.reason = reason;
	this.photo = photo;
}

function dump_member(m)
{
  alert("member name="+m.name+" age="+m.age+" sex="+m.sex+" location="+m.location+" bio="+m.bio+" aim="+m.aim);
}

function dump_team(id)
{
  var i;
  var m;
  m = team[id].member;
  alert("team["+id+"] id="+team[id].id+" type="+team[id].type+" name="+team[id].name+ "#members="+m.length);
  for (i = 0; i < m.length; i++) {
    dump_member(m[i]);
  }

}
function Team(id, type, name, members, photo, rubric, sponsors, charities, provisional)
{
  var i;
  var m;
	this.id = id;
	this.type = type;
	this.name = name;
	this.photo = photo;
	this.rubric = rubric;
	this.sponsors = sponsors;
	this.charities = charities;
	this.provisional = provisional;
	this.member = new Array();
  while(members.length) {
    m = members.shift();
    this.member.push(m);
  }
}

function team_sort(m1, m2)
{
	var v1;
	var v2;
 	if (sortby == "class") {
		if (m1.type < m2.type) {
 			return -1;
		}
		if (m1.type > m2.type) {
 			return 1;
		}
		return m1.id - m2.id;
	}
	if (sortby == "name") {
		v1 = String(m1.name).toUpperCase();
		v2 = String(m2.name).toUpperCase();
		if (v1 < v2) {
			return -1;
		}
		if (v1 > v2) {
			return 1;
		}
		return m1.id - m2.id;
	}
	return m1.id - m2.id;
}
function sort_by(by)
{
	sortby = by;
  show_teams();
	if (all) {
		display_all();
	}
}
function do_init_teams()
{
	show_teams();
	teamlist_resize();
	var str=window.location.href;
	var i = str.lastIndexOf('#');
	if (i >= 0) {
		str = str.substr(i+1);
		i = parseInt(str, 10);
		if (isNaN(i) || i < 0 || i > team.length)
			window.location.hash = "Bdy";
		else {
			detail(i);
			window.location.hash = i;
		}
	}	
	document.getElementById('content').style.height = 
		(document.getElementById('team_details').offsetTop
		+document.getElementById('team_details').offsetHeight+10) + 'px';
	resize2();

}

function show_teams()
{
  var i;
  var j;
  var member;
  var evenodd = "odd1";
  var teamlist;
  var sep;
	team.sort(team_sort);
	
	teamlist =  '<table cellpadding="2" cellspacing="1">';
	teamlist += '<COL WIDTH=30><COL WIDTH=75><COL WIDTH=200><tr>'
	+ '<th width="30" class="heading" onclick="sort_by(\'id\')">Team</th>'
	+ '<th width="75" class="heading" onclick="sort_by(\'class\')">Class</th>'
	+ '<th width="200" class="heading"onclick="sort_by(\'name\')">Team Name</th>'
	+ '<th width="325">Members</th></tr>'
  for (i = 0; i < team.length; i++) {
	  j = i+1;
	  if (team[i].provisional) prov = 'p';
	  else prov = '';
/*		teamlist += '<tr class="'+evenodd+prov+'" onmouseover="highlight('+"'"+team[i].id+"'"+')" ';
		teamlist += 'onmouseout="unhighlight('+"'"+team[i].id+"', '"+evenodd+prov+"')"+'" id="'+team[i].id+'" onclick="detail('+"'"+j+"')"+'">';*/
		teamlist += '<tr class="'+evenodd+prov+' id="'+team[i].id+'" onclick="detail('+"'"+j+"')"+'">';
		teamlist += "<td width=30>"+team[i].id+"</td><td width=75>"+class_name[team[i].type]+"</td>";
		teamlist += '<td class="activ" width=200>'+team[i].name+'</td><td width=325>';
    member = team[i].member;
		sep = '';
		for (j = 0; j < member.length; j++) {
 			teamlist += sep+member[j].name;
			sep = '<br>';
		}
		teamlist += "</td></tr>";
		if (evenodd == 'even1') {
			evenodd = 'odd1';
		} else {
			evenodd = 'even1';
		}
	}
	teamlist += '</table>';
	document.getElementById('team_list').innerHTML = teamlist;
	document.getElementById('content').style.height = 
		(document.getElementById('team_details').offsetTop
		+document.getElementById('team_details').offsetHeight+10) + 'px';
	resize2();
}

var OldHeight = 0;
var OldMouseY = 0;

function startresize(ev)
{
		var elem = document.getElementById('team_list');
		OldHeight = elem.offsetHeight-17;

	e = ev || window.event;
	if (e.pageY) 	{
		OldMouseY = e.pageY;
	}
	else if (e.clientY) {
		OldMouseY = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

}
function endresize()
{
	OldHeight = 0;
	OldMouseY = 0;
}
function doresize(ev)
{
	if (OldHeight == 0) return;
	e = ev || window.event;
	if (e.pageY) 	{
		mousey = e.pageY;
	}
	else if (e.clientY) 	{
		mousey = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	if (OldMouseY == 0) {
		OldMouseY = mousey;
		return;
	}
	newHeight = OldHeight + (mousey - OldMouseY);
	if (newHeight < 30) newHeight = 30;
	document.getElementById('team_list').style.height = newHeight + 'px';
}

function display_team(id)
{
	var t = team[id];
	var prov;
	if (t.provisional == 1) prov = ' style="background-color:#ffccff"';
	else prov = '';
	var i = t.id +1;
	details += '<br>';
	details += '<div class="touter"'+prov+' id="'+t.id+'">';
	details += '  <table class="team"'+prov+'>';
	details += '  <COL WIDTH=17%><COL WIDTH=32%><COL WIDTH=51%>';
	details += '    <tr>';
	details += '      <td class="mb" style="color:#004080">Team: ';
	details += '        <span class="m">'+ t.id +'</span></td>';
	details += '      <td class="mb" style="color:#004080" valign="top">Class: <span class="m">'+ class_name[t.type] +'</span></td>';
	details += '      <td class="mb" style="color:#004080" valign="top">Team Name: <span class="m">'+ t.name +'</span></td>';
	details += '    </tr>';
  	details += '</table>';
	
	if (t.photo != '' || t.rubric != '' || t.sponsors != '' || t.charities != '') {
		details += '  <div class="tinner"'+prov+'>';
		if (t.photo != '') {
			details += '    <img src="../photos/'+t.photo+'" width="256" style="float:right;margin-left:25px;position:relative" alt="'+t.name+'">';
			preLoad[preLoadIndex] = new Image();
			preLoad[preLoadIndex].src = '../photos/'+t.photo;
			preLoadIndex++;
			}
		if (t.rubric != '')
			details += '    <p class="m">'+t.rubric+'</p>';
		if (t.sponsors != '') {
			details += '    <h3>Sponsors:</h3>';
			details += '      <p class="m" style="margin-left:20px">' + t.sponsors + '</p>';
		}
		if (t.charities != '') {
			details += '    <h3>Raising money for:</h3>';
			details += '      <p class="m" style="margin-left:20px">' + t.charities + '</p>';
		}
		
		details += '    <br clear="all">&nbsp;';
		details += '  </div>';
	}
	for (i = 0 ; i < t.member.length; i++) {
		details += '  <div class="tinner"'+prov+'>';
		if (t.member[i].photo == '') {
			if (isIE6) details += '  <table'+prov+'>';
			else details += '  <table width=100%'+prov+'>';
			details += '  <COL WIDTH=50%><COL WIDTH=50%>';
			details += '    <tr valign="top">';
			details += '      <td style="border-style:none;padding: 5px 5px 5px 5px" width=50%>';
		} else {
			details += '    <img src="../photos/'+t.member[i].photo+'" ';
			details += ' width="256" style="float:right;margin-left:25px;position:relative" alt="'+t.member[i].name+'">';
			preLoad[preLoadIndex] = new Image();
			preLoad[preLoadIndex].src = '../photos/'+t.member[i].photo;
			preLoadIndex++;
		}
		details += '        <h3>'+t.member[i].name+'</h3>';
		details += '        <h3>Age: <span class="m">'+t.member[i].age+'</span></h3>';
		details += '        <h3>Sex: <span class="m">'+t.member[i].sex+'</span></h3>';
		details += '        <h2>From: <span class="m">'+t.member[i].location+'</span></h2>';
		details += '        <h3>Bio:</h3>';
		details += '        <p class="m" style="margin-left:20px">'+t.member[i].bio+'</p>';
		if (t.member[i].photo == '') details += '</td><td>';
		details += '        <h3>Aims:</h3>';
		details += '        <p class="m" style="margin-left:20px">'+t.member[i].aim+'</p>';
		if (t.member[i].reason != '') {
			details += '        <h3>Reason for entering:</h3>';
			details += '        <p class="m" style="margin-left:20px">'+t.member[i].reason+'</p>';
		}
		if (t.member[i].photo == '') details += ' </td> </tr> </table>';
		else details += '<br clear="all">&nbsp;';
		details += '</div>';
	}
	details += '</div>';
}

