// Use the vars object to store data without creating errors when properties don't exist, eg.
// if (vars.lastSearchText==undefined) vars.lastSearchText = 'Type here';
var undefined;
var vars=new Object();


/** Quick version of document.getElementById()
*/
function get(name)
{
	return document.getElementById(name);
}

/** Returns true if the passed value is found in the
*   array.  Returns false if it is not.
*/
Array.prototype.inArray = function (value) {
    var i;
    for (i=0; i < this.length; i++) {
        if (this[i] === value) {
            return true;
        }
    }
    return false;
};

/** Add an onLoad event to the window
*/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


/** Take a group of divs all with the same class, find out which is the tallest, and make them all that height.
	@param group	string	Name of the class shared by all the divs
*/
function matchHeight(group)
{

	var divs,contDivs,maxHeight,d;

	divs=document.getElementsByTagName('div');

	contDivs=[];
	maxHeight=0;

	for (var i=0; i<divs.length; i++){
		var d=divs[i];
		if(d.className.split(' ').inArray(group)  ){
			contDivs.push(d);
			maxHeight=Math.max(maxHeight,getDivHeight(d));
		}

	}

	for(var i=0;i<contDivs.length;i++){
		contDivs[i].style.height=maxHeight+"px";
	}
}


/** get the current height of a div object
	@param d	object	DOM Object
*/
function getDivHeight(d)
{
	if(d.offsetHeight){
		 return d.offsetHeight;
	}
	else if(d.style.pixelHeight){
		return d.style.pixelHeight;
	}
	return 0;
}



/** Get all the elements in a page which have a particular class.
	@param strClass		string	The class to search for
	@param strTag		string	restrict the search to a certain type of tag, eg. div? Defaults to all tags.
	@param objContElm	object	A DOM object, defaults to document.
	@return array	List of found elements.
*/
function getElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  objContElm = objContElm || document;
  var objColl = (strTag == '*' && document.all && !window.opera) ? document.all : objContElm.getElementsByTagName(strTag);
  var arr = new Array();
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
  var arrClass = strClass.split(delim);
  for (var i = 0, j = objColl.length; i < j; i++) {
    var arrObjClass = objColl[i].className.split(' ');
    if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (var k = 0, l = arrObjClass.length; k < l; k++) {
      for (var m = 0, n = arrClass.length; m < n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
          arr.push(objColl[i]);
          break comparisonLoop;
        }
      }
    }
  }
  return arr;
}

/** IE 5.0 does not support the push method, so here goes */
if(Array.prototype.push == null){
	Array.prototype.push = function(){
		var i = 0, index = this.length, limit = arguments.length;
		while(i < limit){
			this[index++] = arguments[i++];
		};
		return this.length;
	};
};



/** Track the number of audio players which have been added
	(used by flash's localConnection to enable communication between players)
*/
var numAudioButtons = 0;

/** Add a music player to the html */
/*
function addAudioButton(p) {
	p.id = numAudioButtons++;
	var so = new SWFObject(base_url+"flash/musicplayer.swf", "musicplayer", "200", "17", "7", "#f9f9f9");
	so.addParam("wmode", "transparent");
	so.addParam("quality", "best");
	so.addParam("scale", "noScale");
	so.addParam("salign", "top");
	so.useExpressInstall(base_url+"flash/expressinstall.swf");
	so.addVariable("song_url", p.url);
	so.addVariable("song_title", p.title);
	so.addVariable("playerid", p.id);
	so.write("musicplayer"+p.id);
};
*/

function addAudioPlayer(p) {
	p.playerid = numAudioButtons++;
	var so = new SWFObject(base_url+"flash/standalone_mp3_player.swf", "audioplayer_"+p.id, "100", "20", "8", "#f9f9f9");
	so.addParam("wmode", "transparent");
	so.addParam("quality", "best");
	so.addParam("scale", "noScale");
	so.addParam("salign", "top");
	so.useExpressInstall(base_url+"flash/expressinstall.swf");
	so.addVariable("song_url", p.url);
	so.addVariable("song_title", p.title);
	so.addVariable("playerid", p.playerid);
	so.addVariable("base_url", base_url);
//alert("adding sound. "+base_url);
	so.write("audioplayer_"+p.id);
};

function addTimeline(info) {
	var so = new SWFObject(base_url+"flash/timeline.swf", "timeline", "718", "230", "7", "#f9f9f9");
	so.addParam("wmode", "transparent");
	so.addParam("quality", "best");
	so.addParam("scale", "noScale");
	so.addParam("salign", "top");
	so.useExpressInstall(base_url+'flash/expressinstall.swf');
	so.addVariable("base_url", base_url);
	so.addVariable("decade", info.decade);
	so.addVariable("item", info.itemid);
	// Set the expiry date for the cached timeline data (0==no cache at all)
	so.addVariable("cookieexpirydays", 0);
	so.write("timeline");
}

function addImage(info) {
   var so = new SWFObject(base_url+"flash/imageviewer.swf", "imageviewer", info.width, info.height, "7", "#f9f9f9");
   so.addParam("wmode", "transparent");
   so.addParam("quality", "best");
   so.addParam("scale", "noScale");
   so.addParam("salign", "top");
   so.useExpressInstall(base_url+'flash/expressinstall.swf');
   so.addVariable("base_url", base_url);
   so.addVariable("url", base_url+"images/diaries/"+info.projectid+"/"+info.itemid+".jpg");
   so.write("imageviewer_"+info.itemid);
}
