/*
////////////////
	$Id: scripts.js 33 2007-11-21 17:16:22Z mcasey $
	$URL: file:///\Monolith/SVN_repositories/goenglish/development/www/site/js/scripts.js $
	$Rev: 33 $
	$Date: 2007-11-21 18:16:22 +0100 (Wed, 21 Nov 2007) $
	$Author: mcasey $
	
	Javascript for GoEnglish
////////////////
*/

function doPopupImage() {
	if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a");
	for (var i=0; i < links.length; i++) {
		if (links[i].className.match("popupimage")) {
			links[i].onclick = function() {
				var w = this.getAttribute('w');
				var h = this.getAttribute('h');
				//alert('w='+w+' h='+h);
				window.open(this.href,'','toolbar=0,resizable=1,menubar=0,scrollbars=1,width='+w+',height='+h);
				return false;
			}//end func
		}//end if
	}//end if
}//end func

String.prototype.trim = function() {
   return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
   return this.replace(/^\s+/g,"");
}
String.prototype.rtrim = function() {
   return this.replace(/\s+$/g,"");
}

function isArray(testObject) {   
    return testObject && !(testObject.propertyIsEnumerable('length')) && typeof testObject === 'object' && typeof testObject.length === 'number';
}

function matchHeight(classid){
	//dbg.h('matchHeight');
	var divs,contDivs,maxHeight,divHeight,d;
	contDivs=[];
	// initialize maximum height value
	maxHeight=0;
	// iterate over all <div> elements in the document
	divs = document.getElementsByClassName(classid);

	//dbg.p('Found : ' + divs.length);

	if(divs){
		for (var i=0; i < divs.length; i++) {
			var d=divs[i];
			contDivs[contDivs.length]=d; // copy object to contDivs
			// determine height for <div> element
			if(d.offsetHeight){
				divHeight=d.offsetHeight;
			}else if(d.style.pixelHeight){
				divHeight=d.style.pixelHeight;
			}
			// calculate maximum height
			maxHeight=Math.max(maxHeight,divHeight);
		}//end for
	}
	if(!maxHeight || maxHeight < 20) maxHeight = 20;
	//dbg.p(contDivs.length+' : '+maxHeight);
	// assign maximum height value to all of container <div> elements
	for(var i=0;i<contDivs.length;i++){
		contDivs[i].style.height=maxHeight+'px';
	}//end for
}//end func


/*
	Initialise
*/
function setupMatchedheight(){
	//set up all the select.linkedsel elements onchange event
	if (!document.getElementsByTagName) return false;
	var aMatch = new Array;
	var divs = document.getElementsByTagName("div");
	var j; //ctr
	var bFound; //exists
	
	if(divs.length > 0){
		for (var i=0; i < divs.length; i++) {
			//see if class name of div contains "matchedheight"
			if (divs[i].className.match("matchedheight")) {
				//dbg.p("got " + divs[i].id + " : " + divs[i].className);
				bFound = false;
				//check the array of matches to see if this class ID is already listed.
				for(j=0; j < aMatch.length; j ++){
					//check the done array to see if this class Id has aleady been heightmatched
					if(aMatch[j] == divs[i].className){
						bFound = true;
						break;
					}
				}
				if(!bFound){
					//add to array
					//dbg.p("add  " + divs[i].className);
					aMatch[aMatch.length] = divs[i].className;
				}
			}//end if
		}//end for
		
		//now loop the found divs and run match height
		if(aMatch.length > 0){
			//dbg.h("Matched");
			for(j=0; j < aMatch.length; j ++){
				//dbg.p(j + " : " + aMatch[j]);
				matchHeight(aMatch[j]);
			}		
		}
	}
}//end func

var dbg = {
	init: function() {
		var oBody = document.getElementsByTagName("body")[0];
		oDebug = document.createElement("div");
		oDebug.id = 'debug';
		oDebug.style.position='absolute';
		oDebug.rememberPos = dbg.storePos;
		oBody.appendChild(oDebug);
		dbg.clr();
		//Check to see if we have cookieLib, attempt to load it if not found
		this.cookieLib = true;
		if(typeof cookieLib=='undefined' || typeof cookieLib=='Scriptaculous'){
			dbg.p('cookieLib OR Scriptaculous not found');
			this.cookieLib = false;
		}
		if(this.cookieLib){
			new Draggable('debug',{revert:this.storePos,handle:this.firstChild,starteffect:null,reverteffect:null,endeffect:null}); //scriptaculous
			var posData = cookieLib.get('debugposdata');
			if(posData){
				//new Effect.Move(oDebug,{ x: posData.split(",")[0], y: posData.split(",")[1], mode: 'absolute',starteffect:null,reverteffect:null,endeffect:null});
				oDebug.style.left = posData.split(",")[0] + 'px';
				oDebug.style.top = posData.split(",")[1] + 'px';
			}
		}
	},
	h: function(str){
		//bold header
		var oDbg = $('debug');
		if(oDbg) oDbg.innerHTML += '<h2>' + str + '</h2>';
	},
	p: function(str){
		//paragraph
		var oDbg = $('debug');
		if(oDbg) oDbg.innerHTML += '<p>' + str + '</p>';
	},
	s: function(str){
		//string
		var oDbg = $('debug');
		if(oDbg) oDbg.innerHTML += str;
	},
	clr: function(){
		var oDebug = $('debug');
		oDebug.innerHTML = '<h1>Debug</h1>';
		var btnClr = document.createElement("a");
		//btnClr.onclick = function(){alert('yo');};
		//btnClr.onclick = dbg.clr;
		btnClr.innerHTML = 'CLR';
		btnClr.href = 'javascript:dbg.clr();';
		oDebug.firstChild.appendChild(btnClr);
	},
	storePos: function(){
		//store current position in a cookie for later recall
		//Keeps the debug panel where you left it!
		if(this.cookieLib){
			cookieLib.set('debugposdata',Position.cumulativeOffset($('debug')).toString());
		}
	}
};


/***** WINDOW ONLOADER ****/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(function() {
  /* more code to run on page load */ 
	//dbg.init();
	setupMatchedheight();
	doPopupImage();
});