/* Functions */
function processPopups() {
	var links = $$("a.popup");
	links.each(function(link){
		//add vpos attribute to all icon links
		link.onclick = function() {
			var sWidth = ",width=600";
			var sHeight = '';
			var sModal = '';
			if(this.getAttribute('w')) 		sWidth = ',width='+ this.getAttribute('w');
			if(this.getAttribute('h')) 		sHeight = ',height='+ this.getAttribute('h');
			if(this.getAttribute('modal')) 	sModal = ',modal=1,dependant=1,chrome=0,alwaysRaised=1';
			//alert(sWidth+' '+sHeight);
			window.open(this.href,'','toolbar=0,resizable=1,menubar=0,scrollbars=1'+sWidth+sHeight+sModal);
			return false;
		}//onclick	
	});
	var links = $$("a.modal");
	links.each(function(link){
		if (!link.readAttribute("processPopupsObserved")) {
			link.observe('click', function(event){
				Event.stop(event);
				var params = {
					title: this.title,
					width: 600
				};
				if (this.getAttribute('h')) 
					params.height = this.getAttribute('h');
				if (this.getAttribute('w')) 
					params.height = this.getAttribute('w');
				//follow the href	
				Modalbox.show(this.href, params);			
				return false;
			});
			//to avoid observing the same thing again and again.
			link.writeAttribute("processPopupsObserved", true);
		}
	}

	);
}//end func


		/*	
		link.onclick = function(){
			var params = {title: this.title, width: 600, title: this.title};
			if(this.getAttribute('h')) 		params.height = this.getAttribute('h');
			if(this.getAttribute('w')) 		params.height = this.getAttribute('w');
			Event.stop();
			Modalbox.show(this.href, params);
			return false;
		}
		*/


/*
 * Doesn't actually open a true modal dialog, but who really cares eh?
 */
function openModal(url,winname){
	//feats is optional
	if(openModal.arguments.length > 2) feats = openModal.arguments[2];
	else feats = 'width=420,height=400';
	sModal = 'toolbar=0,resizable=1,menubar=0,scrollbars=1,modal=1,dependant=1,chrome=0,alwaysRaised=1';
	var wChild = window.open(url,winname,feats+sModal);
	wChild.onblur = function(){self.focus();}
}

/*
 * Simple callBack Func that pairs with openModal above
 */
function callBack(oRtn){
	//alert(oRtn.action);
	window.location.reload(false);
}

/*
 * Note that this function is also used when uploading files.
 */
function procRetdImages(url,winname){
	if(procRetdImages.arguments.length > 2) feats = procRetdImages.arguments[2];
	else feats = "";
	if(procRetdImages.arguments.length > 3) useImageHandler = procRetdImages.arguments[3];
	else useImageHandler = false;
	//dbg.h("procRetdImages");

	//set return function
	window.rtnFunc = function(retdValue){
		//dbg.h(retdValue.length + " : " + typeof retdValue);
		//dbg.p(DWRUtil.toDescriptiveString(retdValue,3));
		//dbg.p(Object.inspect(retdValue));
		var previewSrc = "";
		if(retdValue.length){
			//loop over returned values
			for(idx=0; idx < retdValue.length; idx++){
				//dbg.p(idx);
				if(typeof retdValue[idx] == "object"){
					//dbg.p("<strong>" + typeof retdValue[fld].theField + "</strong>");
					//alert(retdValue[idx].theField + " current : " + $F(retdValue[idx].theField) + " set : " + retdValue[idx].theValue + " folder : " + retdValue[idx].theFolder);
					//set the field and also the img using prototye funcs
					$(retdValue[idx].theField).value = retdValue[idx].theValue;
					if($(retdValue[idx].theField + "_thumb")) {
						//for thumbnail images only
						if(useImageHandler){
							//Call view image handler, forcing a reset
							previewSrc = "/content-img/reset/fb_preview/" + retdValue[idx].theImage + "?rnd=" + Math.floor(Math.random()*99999);
						}else{
							//Call the image directly
							previewSrc = "/" + retdValue[idx].theFolder + "/" + retdValue[idx].theImage;
						}
						$(retdValue[idx].theField + "_thumb").src = previewSrc;
					}
					//alert("all set");
				}//end if
			}//end for
		}//end if
	};
	//open the window
	openModal(url,winname,feats);
	return false;
}//end func()

// Removes all whitespace characters
// from start and end of a string
function trim(sString){
   sTrimmedString = "";
   if (sString != "")
   {
      var iStart = 0;
      var iEnd = sString.length - 1;
      var sWhitespace = " \t\f\n\r\v";
     
      while (sWhitespace.indexOf(sString.charAt(iStart)) != -1)
      {
         iStart++;
         if (iStart > iEnd)
            break;
      }
     
      // If the string not just whitespace
      if (iStart <= iEnd)
      {
         while (sWhitespace.indexOf(sString.charAt(iEnd)) != -1)
            iEnd--;
         sTrimmedString = sString.substring(iStart,++iEnd);
      }
   }
   return sTrimmedString;
}

function nicedate(theDate){
	if (theDate == undefined) {theDate =new Date();}
	var weekday=new Array("Sun","Mon","Tue","Wed","Thur","Fri","Sat");
	var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	document.write(weekday[theDate.getDay()] + " ");
	document.write(theDate.getDate() + ". ");
	document.write(monthname[theDate.getMonth()] + " ");
	document.write(theDate.getFullYear());
}

function setImage() {
	var pagename = document.title.substring(16,document.title.length);
	pagename = pagename.toLowerCase();
	pagename = pagename.replace(" ","");
	//alert("Feck (" + document.title +") = " +pagename);
	if (MM_findObj(pagename)!=null) { eval('document.'+pagename+'.src = "images/nav_'+pagename+'_on.gif"');}
}

function isArray(a) {
    return isObject(a) && a.constructor == Array;
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isFunction(a) {
    return typeof a == 'function';
}

function isString(a) {
    return typeof a == 'string';
}

function isBoolean(a) {
    return typeof a == 'boolean';
}

/*
	manages adding removing classes
	http://www.onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
	a : defines the action you want the function to perform.
	o : the object in question.
	c1 : the name of the first class
	c2 : the name of the second class 
 */
function jscss(a,o,c1,c2){
  switch (a){
    case 'swap':
      o.className=!jscss('check',o,c1)?o.className.replace(c2,c1): o.className.replace(c1,c2);
    break;
    case 'add':
      if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
  }
}

document.observe("dom:loaded", function(){
	processPopups();
}); //start the func on page load

