var expandingModule = 'right1', expandSpeed = 5, timerSpeed = 15, bottomPadding = 10;
var xx = new Array();
var hasLoaded = false;
var cancelclose = '';
var imageOnSuffix = '-selected', imageOffSuffix = '', imageTopOffSuffix = '-top';

//js work around to hide elements
//document.write('<link type="text/css" rel="stylesheet" href="/styles/jsonly.css" />');

function loadMe(args, openElement) {
	modulelinkidsarray = args.split(",");
	for (x=0;x<modulelinkidsarray.length;x++) {
		eleid = modulelinkidsarray[x];
		thisele = document.getElementById(eleid);
		if (openElement > 0 && x==openElement -1) {
			thisele.style.display = 'block'; // show, in order to get height
			thisele.style.height = getElementHeight(eleid) + bottomPadding + 'px';	
			thisele.originalHeight = getElementHeight(eleid);
			expandingModule = eleid;
		} else {
			thisele.style.display = 'block'; // show, in order to get height
			thisele.style.height = getElementHeight(eleid) + 'px'; // get height
			thisele.originalHeight = getElementHeight(eleid) + bottomPadding;
			if (eleid != cancelclose || cancelclose == '') thisele.style.display = 'none'; // hide again
		}
	}
	hasLoaded = true;
}
function getElementHeight(eleid) {
	thisele = document.getElementById(eleid);
	return thisele.offsetHeight;
}

function expandModule(moduleprefix, thisindex, maxindex, cancelifactive, changeImage) {
	if (hasLoaded == false) {
		cancelclose = moduleprefix + thisindex;
	} else {
		moduleid = moduleprefix + thisindex;
		thismodule = document.getElementById(moduleid);
		modulelinkidsarray = new Array();
		n = 0;
		for (x=1;x<=maxindex;x++) {
			if (x != thisindex) {
				modulelinkidsarray[n] = moduleprefix + x;
				n++;
			}
		}
		if (expandingModule == moduleid && cancelifactive > 0 && cancelifactive != 3) { // you've clicked on the current expanded module
			if (cancelifactive == 2) { // scroll active module back up
				startTimer(moduleid,'minus');
				expandingModule = '';
			} else {
			}
		} else { // expand this module
			//expandSpeed = Math.round((thismodule.originalHeight/100) * expandSpeedFactor);
			if (changeImage == true) {// change image based on moduleid and modulelinkids
				thismoduleimage = document.getElementById(moduleid + '-image');
				imageSrc = thismoduleimage.src;
				imageSrcNoExt = imageSrc.substr(0, imageSrc.length - 4);
				imageSrcExt = imageSrc.substr(imageSrc.length - 4, 4);
				re = new RegExp(imageOffSuffix, 'gi');
				imageSrcNoExt = imageSrcNoExt.replace(re, '');
				re = new RegExp(imageTopOffSuffix, 'gi');
				imageSrcNoExt = imageSrcNoExt.replace(re, '');
				imageSrcNew = imageSrcNoExt + imageOnSuffix + imageSrcExt;
				thismoduleimage.src = imageSrcNew;
				for (x=0;x<modulelinkidsarray.length;x++) {
					thismoduleimage = document.getElementById(modulelinkidsarray[x] + '-image');
					imageSrc = thismoduleimage.src;
					imageSrcNoExt = imageSrc.substr(0, imageSrc.length - 4);
					imageSrcExt = imageSrc.substr(imageSrc.length - 4, 4);
					
					re = new RegExp(imageOnSuffix, 'gi');
					imageSrcNoExt = imageSrcNoExt.replace(re, '');
					re = new RegExp(imageTopOffSuffix, 'gi');
					imageSrcNoExt = imageSrcNoExt.replace(re, '');
					thiscurrentindex = modulelinkidsarray[x].substr(5, modulelinkidsarray[x].length - 5);
					if (thiscurrentindex < thisindex) {
						imageSrcNew = imageSrcNoExt + imageTopOffSuffix + imageSrcExt;
					} else {
						imageSrcNew = imageSrcNoExt + imageOffSuffix + imageSrcExt;
					}
					thismoduleimage.src = imageSrcNew;
				}
			}
			if (thismodule.style.display != 'block') {
				thismodule.style.height = '0px';
				thismodule.style.display = 'block';
			}
			expandingModule = moduleid;
			startTimer(moduleid,'plus');
			
			document.getElementById('menu'+thisindex+'line').style.display='block';
			
			if (modulelinkidsarray.length > 0) {
				for (x=0;x<modulelinkidsarray.length;x++) {
					startTimer(modulelinkidsarray[x],'minus');
					document.getElementById(modulelinkidsarray[x] + 'line').style.display='none';
				}
			}
		}	
	}
}
function startTimer(eleid, direction) {	
	xx[xx.length] = setTimeout("expandContract('"+eleid+"', '"+direction+"')", timerSpeed);
}
function expandContract(eleid, direction) {
	restartTimer = 1;
	thisele = document.getElementById(eleid);
	thiseleheight = thisele.style.height;
	if (thiseleheight == 'auto') {
		thiseleheight = getElementHeight(eleid) + bottomPadding + 'px';
	}
	thiseleheight = thiseleheight.substring(0,(thiseleheight.length-2))*1;
	if (direction == 'plus') {	
		if (isNaN(parseInt(thisele.originalHeight)) == true) {
			maxHeight = 150;
		} else {
			maxHeight = thisele.originalHeight;
		}
		//document.title = maxHeight;
		if (thiseleheight + expandSpeed > maxHeight) {
			thisele.style.height = maxHeight + 'px';
			restartTimer = 0;
			// change image
			switch(eleid){
				case 'menu2':
					//document.getElementById('mainImage').src = 'images/phrenology.jpg';
					document.getElementById('flashgallery').innerHTML = '<img src="images/phrenology.jpg" width="474" height="398" alt="Phrenology By A.Coyote" title="Phrenology By A.Coyote" id="mainImage" />';
					break;
				case 'menu3':
					//document.getElementById('mainImage').src = 'images/who-we-do-it-for.jpg';
					document.getElementById('flashgallery').innerHTML = '<img src="images/who-we-do-it-for.jpg" width="474" height="398" alt="Who we do it for" title="Who we do it for" id="mainImage" />';
					break;
				case 'menu4':
					//document.getElementById('mainImage').src = 'images/contact.jpg';
					//document.getElementById('flashgallery').innerHTML = '';
					var so = new SWFObject('flash/contactForm.swf','flashgalleryswf','475','500','6','#000000');
					so.addVariable('postUrl','flash/email.php');
					so.write('flashgallery');
					break;
				case 'menu5':
					//document.getElementById('mainImage').src = 'images/selected-works/1/1.jpg';
					var so = new SWFObject('flash/flashgallery.swf','flashgalleryswf','475','500','6','#000000');
					so.addVariable('id',1);
					so.write('flashgallery');
					changeGallery(1);
					break;
				default:
					//document.getElementById('mainImage').src = 'images/coyote.jpg';
					document.getElementById('flashgallery').innerHTML = '<img src="images/coyote.jpg" width="474" height="398" alt="Coyote - design that bites" title="Coyote - design that bites" id="mainImage" />';
					break;
			}
		} else {
			thisele.style.height = thiseleheight + expandSpeed + 'px';
		}
	} else {
		//expandSpeed = Math.round();
		if (thiseleheight - expandSpeed < 0) {
			thisele.style.height = 0 + 'px';
			restartTimer = 0;
			thisele.style.display = 'none';
		} else {
			thisele.style.height = thiseleheight - expandSpeed + 'px';
		}
	}
	if (restartTimer == 1) {
		xx[xx.length] = setTimeout("expandContract('"+eleid+"', '"+direction+"')", timerSpeed);
	}
}
