if (xInclude('/x/x_core.js', '/x/x_dom.js')) {
  window.onload = winOnload;
}

function winOnload() {
  setupCollapsibles(false);
  if (gallery==true){
	setupThumbs();
  }
}

function H_OnClick() {
	var d, t, s, a;
	
	if (this.collapsed) {
		d = 'block';
		t = 'hide';
		s = '/images/collapse' + this.xShowHideImgSfx + '.gif';
		a = '[-]';
	}
	else {
		d = 'none';
		t = 'show';
		s = '/images/expand' + this.xShowHideImgSfx + '.gif';
		a = '[+]';
	}

	this.collapsibleEle.style.display = d;
	this.collapsed = !this.collapsed;
	this.xShowHideImgObj.setAttribute('title', 'Click to ' + t);
	this.xShowHideImgObj.setAttribute('alt', a);
	this.xShowHideImgObj.src = s;
	return false;
}

function H_OnMouseOver(revHover) {
	var p;
	
	p = xParent(this,true);
	
	if (p !== null) {
		if (this.revHover == false) {
			this.style.color = '#333366'
			}
		else {
			this.style.color = '#993333'
			}
	}
}

function H_OnMouseOut(revHover) {
	var p;
	
	p = xParent(this,true);
	
	if (p !== null) {
		if (this.revHover == false) {
			this.style.color = '#993333'
			}
		else {
			this.style.color = '#333366'
			}
	}
}

function setupCollapsibles(bMin) {
  setupCollapsibleHs('span', H_OnClick, bMin, 'menuItemHead', 'siteMenu', 'menuSubItems', 'showHideMenuSubItems', '-arrow', '-gray',true);
  setupMenuFlags('img', 'flagMenuCurrent', 'siteMenu', '-arrow')
  
}

function hideMenuLoad() {
	var h;

	h = xGetElementById('menuLoad');

	if (h) {
			h.style.display = 'none';
	}
}

function setupCollapsibleHs(sTag, fnMouseEvent, bMin, sTrigger, sParent, sChild, sImage, sImgStyle, sImgColor, revHover) {
  var h, i, c, list, lc, m, s, aH, aC, aM;
  
  lc = xGetElementById(sParent);

  aH = xGetElementsByClassName(sTrigger, lc, sTag);
  aC = xGetElementsByClassName(sChild, lc);
  aM = xGetElementsByClassName(sImage, lc);

  for (i = 0; i < aH.length; ++i) {
    h = aH[i]; // sTag element (trigger)
    c = aC[i]; // collapsible element (target)
    m = aM[i]; // image element (target)
    s = sImgStyle + sImgColor;

    if (h && c && m) {
		if ((c.id == secID) && (secID != "")) {
			h.collapsed = !bMin;
			s = sImgStyle + "-red";
		}
		else {
			if ((c.id == subSecID) && (subSecID != "")) {
				h.collapsed = !bMin;
				s = sImgStyle + "-red";
			}
			else {
				if  (h.id == "open") {
					h.collapsed = true;
				}
				else {
					if (h.id == "closed") {
						h.collapsed = false;
					}
					else {
						h.collapsed = bMin;
					}
				}
			}
		}

		h.collapsibleEle = c;
		h.xShowHideImgObj = m;
		h.xShowHideImgSfx = s;

		h.revHover = revHover;
		h.onmouseover = H_OnMouseOver;
		h.onmouseout = H_OnMouseOut;
		h.onclick = fnMouseEvent;
		h.onclick();
    }
  }
}

function setupMenuFlags(sTag, sTarget, sParent, sImgStyle) {
  var h, i, s, lc ,aH;
  
  lc = xGetElementById(sParent);
  aH = xGetElementsByClassName(sTarget, lc, sTag);

  for (i = 0; i < aH.length; ++i) {
    h = aH[i];

    if (h) {
    
    	if ((h.id == flagMenu) && (flagMenu != "")) {
    		s = sImgStyle + "-red";

	h.xShowHideImgSfx = s;
        s = '/images/expand' + h.xShowHideImgSfx + '.gif';
        h.src = s;
    	}
	
   }
  }
}

//Photo Gallery

function setupThumbs(){
	var h, i, c, list, parentElem, thumbElem, thumbsColl;

	parentElem = xGetElementById("gallerySelect");
	thumbsColl = xGetElementsByClassName("galleryThumbs", parentElem);

	for (i = 0; i < thumbsColl.length; ++i) {
		thumbElem = thumbsColl[i];

		if (thumbElem) {
			thumbElem.tabIndex = thumbElem.id;
			thumbElem.onmouseover = fnThumbOver;
			thumbElem.onmouseout = fnThumbOut;
			thumbElem.onfocus = fnThumbFocus;
			thumbElem.onblur = fnThumbBlur;
		}
	}
}

function fnThumbOver(){
	this.style.border = "1px solid #333333";
}

function fnThumbOut(){
	this.style.border = "1px solid #cccccc";
}

function fnThumbFocus(){
	this.style.border = "1px solid red";
}

function fnThumbBlur(){
	this.style.border = "1px solid #cccccc";
}

function scrollToThumb(e){
var eParent;

	eParent = document.getElementById("gallerySelect");
	eParent.scrollLeft = e.offsetLeft - Math.round(eParent.clientWidth/2) + Math.round(e.clientWidth/2);
	e.focus();
	
}

function doSwap(targetID,picID) {
	swapPhoto(targetID,picID);
	setBackNextAction(targetID,picID);
}

function swapPhoto(targetID,picID) {
	var targetElem, thumbElem;
	
	targetElem = xGetElementById(targetID);
	thumbElem = document.getElementById("thumb"+picID);
	
	getPhoto(targetElem,picID);
	scrollToThumb(thumbElem);
}

function getPhoto(targetElem,picID) {
	var dimW, dimH, picFName;
	
	picFName = gAryPhotos[picID][1];
	dimW = gAryPhotos[picID][3];
	dimH = gAryPhotos[picID][4];
	
	//targetElem.width = dimW
	targetElem.height = dimH
	targetElem.src = "/gallery_images/" + picFName;
}

function setBackNextAction(targetID,picID) {
	var nextElem, backElem, iNext, iBack, ub;
	
	nextElem = xGetElementById("next");
	backElem = xGetElementById("back");
	ub = gAryPhotos.length-1;
	
	//First in list - back to last in list
	if(picID==0){
		iBack = ub;
		iNext = 1;
	}
	else {
		//End of list - next to first in list
		if(picID==ub){
			iBack = picID-1;
			iNext = 0;
		}
		else {
			iBack = picID-1;
			iNext = picID+1;
		}
	}
	
	nextElem.targetID = targetID;
	nextElem.picID = iNext;
	
	backElem.targetID = targetID;
	backElem.picID = iBack;
	
	nextElem.onclick = fnBackNextClick;
	backElem.onclick = fnBackNextClick;
}

function fnBackNextClick(){
	doSwap(this.targetID,this.picID);
}