
var $={
addOnload:function(f){var p=window.onload;if (typeof window.onload!='function'){window.onload=f;}else{window.onload=function(){p();f();}}},
getByClass:function(str,node,tag){var classEls=new Array();if(node==null)node=document;if(tag==null)tag='*';var els=node.getElementsByTagName(tag);var elsLen=els.length;var pattern=new RegExp('(^|\\s)'+str+'(\\s|$)');for(i=0,j=0;i<elsLen;i++){if(pattern.test(els[i].className)){classEls[j]=els[i];j++;}}return classEls;},
getById:function(){var els=new Array();for(var i=0;i<arguments.length;i++){var el=arguments[i];if(typeof el=='string')el=document.getElementById(el);if(arguments.length==1)return el;els.push(el);}return els;},
getByTag:function(str,node){if(node==null)node=document;return node.getElementsByTagName(str);}
};

var TARGET={
init:function(){var t=$.getByTag("a")[0];for(var i=0;i<t.length;i++){var a=t[i];if(a.getAttribute("href")&&a.getAttribute("rel")=="external")a.target="_ext";}}
};

var HTTPREQUEST={
init:function(){
	try{
	// if (XMLHttpRequest) {
		return new XMLHttpRequest;
	} catch(f){
	// } else {
		var types = [
		'Microsoft.XMLHTTP',
		'MSXML2.XMLHTTP.5.0',
		'MSXML2.XMLHTTP.4.0',
		'MSXML2.XMLHTTP.3.0',
		'MSXML2.XMLHTTP'
			];
		for (var i = 0; i < types.length; i++) { try { return new ActiveXObject(types[i]); } catch(e) {} }
		return false; // XMLHttpRequest not supported
		}
	
	}
};

var JSON={
baseDir:"",
init:function(directory){
	JSON.baseDir = "/carl/pgal";
	},
getData:function(id,callback,args){
    if (typeof args == "undefined") {
	  args = {"id":id};
	}
	var httpRequest;
	var jsonData;
	var url;
	httpRequestObj = HTTPREQUEST.init();
	if (httpRequestObj){
		url = location.hostname + JSON.baseDir + "/" + id + ".js";
		httpRequestObj.open("GET", url);
		httpRequestObj.onreadystatechange = function(){
		    if (httpRequestObj.readyState == 4) {
				if (httpRequestObj.status == 200) {
					jsonData = eval("(" + httpRequestObj.responseText + ")");
					callback(id,jsonData,args);
					}
				}
			}
		httpRequestObj.send(null);		

		}
	}
};

// var HOVER={
// init:function(){if(window.attachEvent){var m=$.getByTag("LI",$.getId("nav-menus"));for(var i=0;i<m.length;i++){m[i].onmouseover=function(){this.className+=" hover"};m[i].onmouseout=function(){this.className=this.className.replace(new RegExp(" hover\\b"),"")};}}}
// };

var VFORM={
validate:function(sf){var empty=function(ef,en,et){eo=ef.elements[en];if(et=="checkbox"){if(!eo.length){or=!eo.checked;}else{eb=true;for(ec=0;ec<eo.length;ec++)if(eo[ec].checked)eb=false;or=eb;}}else if(et=="radio"){eb=true;for(ec=0;ec<eo.length;ec++)if(eo[ec].checked)eb=false;or=eb;}else if(et=="select-one"){or=eo.options[eo.selectedIndex].value=="";}else if(et=="text"){or=eo.value.length<=0;}else if(et=="textarea"){or=eo.value.length<=0;}return or;};se=sf.elements;sl=se.length;sm="Please complete the following required fields:\n";if(sl>1){st="";for(sc=0;sc<sl-1;sc++){if((se[sc].name.indexOf("_req")!=-1)&&(st!=se[sc].name)&&(empty(sf,se[sc].name,se[sc].type)))sm=sm+"- '"+se[sc].title+"'\n";st=se[sc].name;}}if(sm.length>50)alert(sm);return sm.length<=50;},
init:function(){var ivfForms=$.getByTag("form");for(var ivfCounter=0;ivfCounter<ivfForms.length;ivfCounter++)ivfForms[ivfCounter].onsubmit=function(){return VFORM.validate(this);};}
};

var PGAL={
selectedIndex:-1,
selectedItem:null,
maximum:6,
init:function(){
	var pgalObject=$.getByClass("pgal");
	var pgalArgs;

	for(pgalCounter=0;pgalCounter<pgalObject.length;pgalCounter++){
		// Load JSON associated with this pgal
		pgalArgs={"pgalObject":pgalObject[pgalCounter]};
		JSON.getData(pgalObject[pgalCounter].id,PGAL.loadObject,pgalArgs);
		}
	},
loadObject:function(id,jsonData,args){
	var pgalPhotoObject;
	var pgalDescriptionObject;
	var pgalNextObject;
	var pgalPreviousObject;
	var currJsonItem;
	var currpgalItem;
	var pgalDiv=args.pgalObject;
	pgalDiv.items = jsonData.pgal.items;
	
	//  alert(jsonData["pgal01"].items[0].src);
	pgalPhotoObject=$.getByClass("pgal-photo",pgalDiv)[0];
	pgalDescriptionObject=$.getByClass("pgal-desc",pgalDiv)[0];
	var firstItem = null;
	
	for(var pgalItemCounter=0;pgalItemCounter<pgalDiv.items.length;pgalItemCounter++){
		currJsonItem=pgalDiv.items[pgalItemCounter];

		currpgalItem=$.getById(currJsonItem.id);
		if (pgalItemCounter == 0) firstItem=currpgalItem; 
		
		currpgalItem.onclick=function(){return PGAL.itemClick(this);};
		currpgalItem.pgalParent=pgalDiv;
		currpgalItem.pgalPhotoObject=pgalPhotoObject;
		currpgalItem.pgalDescriptionObject=pgalDescriptionObject;
		currpgalItem.pgalId=currJsonItem.id;
		currpgalItem.pgalIndex=pgalItemCounter;
	}
 	pgalNextObject=$.getByClass("pgal-next",pgalDiv,"a");
 	if(pgalNextObject){
 		try {
 		pgalNextObject[0].onclick=function(){return PGAL.nextClick(this);};
 		pgalNextObject[0].pgalParent=pgalDiv;
 		} catch(e) { };
 	}
 	pgalPreviousObject=$.getByClass("pgal-previous",pgalDiv,"a");
 	if(pgalPreviousObject){
 		try {
 		pgalPreviousObject[0].onclick=function(){return PGAL.previousClick(this);};
 		pgalPreviousObject[0].pgalParent=pgalDiv;
 		} catch(e) { };
 	}
 	// $.addOnload(PGAL.itemClick(firstItem));
	
 		
 		
 		// for(pgalItemCounter=0;pgalItemCounter<pgalItems.length;pgalItemCounter++){
 			// Get parent XML element for this item
 			// xmlPgalObject=xmlObject.getElement("image",pgalItemCounter);
 			// Get child elements. ex:
 			// pgalImageSrc=xmlObject.getChildElement(xmlPgalObject,"src");
 			// pgalItems[pgalItemCounter].pgalPhotoUrl=pgalArray[0][pgalItemCounter];
 			// pgalItems[pgalItemCounter].pgalDescriptionCopy=pgalArray[1][pgalItemCounter];
 			// }
 		// pgalNextObject=$.getByClass("pgal-next",args.pgalObject,"a");
 		// if(pgalNextObject)pgalNextObject[0].onclick=function(){return PGAL.nextClick(this);};
 		// pgalPrevObject=$.getByClass("pgal-prev",args.pgalObject,"a");
 		// if(pgalPrevObject)pgalPrevObject[0].onclick=function(){return PGAL.previousClick(this);};
	},	
itemDeselect:function(pgalItemAnchor){
	pgalItemAnchor.pgalParent.selectedItem=null;
	pgalItemAnchor.pgalParent.selectedIndex=-1;
	for(var pgalItemCounter=0;pgalItemCounter<pgalItemAnchor.pgalParent.items.length;pgalItemCounter++){
		currpgalItem=$.getById(pgalItemAnchor.pgalParent.items[pgalItemCounter].id);
		currpgalItem.className=currpgalItem.className.replace(new RegExp(" lit\\b"),"");
		}
	},
itemSelect:function(pgalItemAnchor){
	pgalItemAnchor.className+=" lit";
	pgalItemAnchor.pgalParent.selectedItem=pgalItemAnchor;
	pgalItemAnchor.pgalParent.selectedIndex=pgalItemAnchor.pgalIndex;
	},	
itemClick:function(pgalItemAnchor){
	var htmlStr = "";
	if (pgalItemAnchor.pgalParent.items[pgalItemAnchor.pgalIndex]){
		PGAL.itemDeselect(pgalItemAnchor);
		PGAL.itemSelect(pgalItemAnchor);
		currPgal=pgalItemAnchor.pgalParent.items[pgalItemAnchor.pgalIndex];
		
		var imgTitle = "Photo " + (pgalItemAnchor.pgalIndex + 1);
		if (pgalItemAnchor.pgalDescriptionObject){
			if (currPgal.description){
				pgalItemAnchor.pgalDescriptionObject.innerHTML=currPgal.description;
				imgTitle = currJsonItem.description;
			} else {
				pgalItemAnchor.pgalDescriptionObject.innerHTML="";
			}
		}

		if (pgalItemAnchor.pgalPhotoObject && currPgal.src){
			var splitSrc = currPgal.src.split(".");
			switch ( splitSrc[splitSrc.length - 1]){
			case "swf":
				htmlStr = '<object type="application/x-shockwave-flash" data="' + currPgal.src + '"';
				htmlStr+=' width="250" height="250">';
				htmlStr+='<param name="movie" value="' + currPgal.src + '" />';
		 		htmlStr+='<param name="wmode" value="transparent" />';
		 		if (currPgal.image){
		 			htmlStr+='<img src="' + currPgal.image + '" title="' + imgTitle + '" />';
		 		}
				htmlStr+='</object>';
				break;
			case "jpg","png","gif":
				htmlStr = '<img src="' + currPgal.src + '" title="' + imgTitle + '" />';	
				break;
			}
			pgalItemAnchor.pgalPhotoObject.innerHTML=htmlStr;
			 // pgalItemAnchor.pgalPhotoObject.src=currPgal.src;
		}
		pgalItemAnchor.blur();
		return false;
		}
	},
nextClick:function(pgalNextAnchor){
	var currJsonItem;
	var currpgalItem;
	if (! pgalNextAnchor.pgalParent.selectedIndex) pgalNextAnchor.pgalParent.selectedIndex = 0;
	if (pgalNextAnchor.pgalParent.selectedIndex < 0) pgalNextAnchor.pgalParent.selectedIndex = 0;
	var nextSel = pgalNextAnchor.pgalParent.selectedIndex + 1;
	if (nextSel >= pgalNextAnchor.pgalParent.items.length) nextSel = 0; 
	currJsonItem=pgalNextAnchor.pgalParent.items[nextSel];
	currpgalItem=$.getById(currJsonItem.id);
	PGAL.itemClick(currpgalItem);
	pgalNextAnchor.blur();
	return false;
	},
previousClick:function(pgalPreviousAnchor){
	var currJsonItem;
	var currpgalItem;
	if (! pgalPreviousAnchor.pgalParent.selectedIndex) pgalPreviousAnchor.pgalParent.items.length - 1;
	if (pgalPreviousAnchor.pgalParent.selectedIndex < 0) pgalPreviousAnchor.pgalParent.items.length - 1;
	var nextSel = pgalPreviousAnchor.pgalParent.selectedIndex - 1;
	if (nextSel < 0) nextSel = pgalPreviousAnchor.pgalParent.items.length - 1; 
	currJsonItem=pgalPreviousAnchor.pgalParent.items[nextSel];
	currpgalItem=$.getById(currJsonItem.id);
	PGAL.itemClick(currpgalItem);
	pgalPreviousAnchor.blur();
	return false;
	}
};

// $.addOnload(HOVER.init);
$.addOnload(JSON.init);
$.addOnload(PGAL.init);
// $.addOnload(TARGET.init);
$.addOnload(VFORM.init);