/**
 * triage.js 
 * all utilities for the Triage project 2007
 * written by Red Bricks Media
 */

var BASE_PATH = "/";
var THEME_PATH = BASE_PATH + "themes/triage2007/";




/* MenuButton object - for menus - this function was written by Joey Chan, FunGroup in GPL - 2003 */
function MenuButton(sNormalImgPath, sOverImgPath) {
// public variables.
  this.normal = new Image();
  this.over = new Image();
  
// public functions.
  this.reset = fnReset;
  this.click = fnMouseClick;
  this.mouseOver  = fnMouseOver;
  this.mouseOut   = fnMouseOut;
  
// initial object.
  this.normal.src = sNormalImgPath;
  this.over.src = sOverImgPath;

// member functions.
  function fnReset(imgObj) {
    imgObj.src = this.normal.src;
  }

  function fnMouseClick(imgObj) {
    imgObj.src = this.over.src;
  }

  function fnMouseOver(imgObj) {
    imgObj.src = this.over.src;
    //window.defaultStatus = imgObj.title;
  }

  function fnMouseOut(imgObj) {
    imgObj.src = this.normal.src;
    //window.defaultStatus = "";
  }

  return this;
}

// Staff object, simply stores details of staff
function Staff(name, team, photo, details) {
	this.name = name;		// the name of the staff
	this.team = team;		// the team of the staff that belongs to
	this.photo = photo;	// the photo of the staff that to be loaded
	this.details = details;	// the details of the staff
}


// show bio
function showBio(name, obj) {
	if (!name) return false;

	var visible_width = 1024;
	var visible_height = 600;
	
	try {
		visible_width = document.body.offsetWidth;
		visible_height = document.body.offsetHeight;
	}
	catch (e) {
		visible_width = 1024;	// set to default
		visible_height = 600;
	}

	var horizontal_mid = visible_width * 0.5;	// horizontal middle point
	var vertical_mid = visible_height * 0.67;	// vertical middle point

	var bio = document.getElementById('bio');
	var bio_name = document.getElementById('bio_name');
	var bio_team = document.getElementById('bio_team');
	var bio_photo = document.getElementById('bio_photo');
	var bio_details = document.getElementById('bio_details');
     
	
	if (!bio) return false;
	/*
	var reName = new RegExp("^" + name + "$", "i");
	var bioPosition = -1;

	for (var i=0; i<staffs.length; i++) {
		if (reName.test(staffs[i].name)) {
			bioPosition = i;
			break;
		}
	}

	*/
	var bioPosition = -1;

	for (var i=0; i<staffs.length; i++) {
		if (name == (staffs[i].name)) {
			bioPosition = i;
			break;
		}
	}
	
	if (bioPosition < 0) return false;	// not found.

	bio_name.innerHTML = staffs[bioPosition].name;
	bio_team.innerHTML = staffs[bioPosition].team;
	bio_photo.src = staffs[bioPosition].photo;
	bio_details.innerHTML = staffs[bioPosition].details;

	bio.style.display = "block";
	bio_photo.style.display = "inline";
	bio.style.position = "absolute";

	bio_top = 0;
	bio_left = 0;

	try {
		obj_top = obj.offsetTop;	// the top position of the trigered object
		obj_left = obj.offsetLeft;	// the left position of the trigered object
				
		elem = obj;
				
		do {
			elem = elem.offsetParent;		
			obj_top += elem.offsetTop;
			obj_left += elem.offsetLeft;
		}
		while (elem != document.body);

		if (obj_left < horizontal_mid) {
			bio.style.left = (obj_left + obj.offsetWidth).toString() + "px";
		}
		else {
			bio.style.left = (obj_left - bio.offsetWidth).toString() + "px";
		}
		
		if (obj_top < vertical_mid) {
			bio.style.top = (obj_top).toString() + "px";
		}
		else {
			bio.style.top = (obj_top - bio.offsetHeight + obj.offsetHeight).toString() + "px";
		}
	}
	catch (e) {
		//bio.style.top = visible_height;
	}
}

// hide Bio
function hideBio() {
	var bio = document.getElementById('bio');
	if (!bio) return false;
	bio.style.display = "none";
}

// starts pulling staffs details
var staffs = new Array();


/* functions for popup windows */

function popupWindow(url, name, features) {
	var defaultFeatures = "channelmode=no, directories=no, fullscreen=no, height=600, location=no, menubar=no, resizable=yes, scrollbars=yes, toolbar=no, width=850";
	
	var win = window.open(url, ((name) ? name : "popup"), ((features) ? features : defaultFeatures) );
	return win;
}



function printFriendly() {
	window.print();
}

function sendToFriend() {
	
}

/**
 * button object
 */
//function Button(sNormalImgPath, sOverImgPath, sPressedImgPath) {
function Button(sNormalImgPath) {
// public variables.
  this.normal = new Image();
  this.over = new Image();
  this.pressed = new Image();
  
// public functions.
  this.reset = fnReset;
  this.click = fnMouseClick;
  this.mouseDown  = fnMouseDown;
  this.mouseOver  = fnMouseOver;
  this.mouseOut   = fnMouseOut;
  
// initial object.
  this.normal.src = sNormalImgPath;
  this.over.src = sNormalImgPath.replace(/(\.jpg|\.gif|\.png)$/, "_over$1");
  this.pressed.src = sNormalImgPath.replace(/(\.jpg|\.gif|\.png)$/, "_pressed$1");
// member functions.
  function fnReset(imgObj) {
    imgObj.src = this.normal.src;
  }

  function fnMouseClick(imgObj) {
    imgObj.src = this.over.src;
  }
  
  function fnMouseDown(imgObj) {
    imgObj.src = this.pressed.src;
  }

  function fnMouseOver(imgObj) {
    imgObj.src = this.over.src;
    window.defaultStatus = imgObj.title;
  }

  function fnMouseOut(imgObj) {
    imgObj.src = this.normal.src;
    window.defaultStatus = "";
  }

  return this;
}

var btnMore = new Button("/triage/images/more_arrow.gif");