/******************************************************************************/
/* mud_Scripts.js                                                               
/* REQUIRES mud_API.js
/* author: Takashi Okamoto mud(tm) - http://mudcorp.com
/******************************************************************************/

////////////////////////////////////////////////////////////////////////////////
// GLOBAL VARS

var selSection = new Array();
	selSection["level0"] = false;
	selSection["pradablossoms"] = false;
	selSection["mtv"] = false;
	selSection["212"] = false;
	selSection["iiwii"] = false;
	selSection["wyly"] = false;
	selSection["pradalibro"] = false;
	selSection["designusa"] = false;
	selSection["blik"] = false;
	selSection["nikenymarathon"] = false;
	selSection["nyco"] = false;
	selSection["clo"] = false;
	selSection["nike"] = false;
	selSection["moma"] = false;
	selSection["singapore"] = false;
	selSection["idea"] = false;
	selSection["proj"] = false;
	selSection["exhibits"] = false;
	selSection["book"] = false;
	selSection["cinema"] = false;
	selSection["reading"] = false;
	selSection["about"] = false;
	selSection["contact"] = false;

var selItems = new Array();
	selItems["proj"] = false;
	selItems["exhibits"] = false;
	selItems["about"] = false;
		
			
////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS

// toggle main and sub sections
function toggleSection(link, type) {
	//reset open sub items
	if (selItems[type]) {
		unsetItems(selItems[type], type);
		selItems[type] = false;
	}
	if (type == 'level0') {
		//reset all open sub items
		for (subItem in selItems) {
			if (selItems[subItem]) {
				unsetItems(selItems[subItem], subItem);
				selItems[subItem] = false;
			}
		}
		//reset all open sub sections
		for (sub in selSection) {
			if (selSection[sub] && sub != 'level0') {
				unsetSection(selSection[sub], sub);
				selSection[sub] = false;
			}
		}
	}
	var selectNum = link.id.substring(link.id.indexOf("-")+1);
	switch (selSection[type]) {
		// true if what was clicked is also selected
		case selectNum:
			//destroy movie for safari (in index.php)
			if (mov) {
				stopMovieSafari();
				mov = false;
			}
			selSection[type] = false;
			unsetSection(selectNum, type);
			break;
		// true if clicked isn't selected
		default:
			// turn off already selected
			if (selSection[type]) {
				if (mov) {
					stopMovieSafari();
					mov = false;
				}
				unsetSection(selSection[type], type);
			}
			selSection[type] = selectNum;
			setSection(selectNum, type);
			//initialize movie for safari (in index.php)
			if (link.id=='level0-cinema' || link.id=='level0-proj' || link.id=='level0-exhibits') {
				initMovieSafari();
			}
			break;
	}
}

// toggle sub items
function toggleItems(link, type, proj_id, exhibit_id) {
	var itemNum = link.id.substring(link.id.lastIndexOf("-")+1);
	if (selItems[type]) { // a sub item's open already
		if (itemNum != selItems[type]) { // clicked != open already
			unsetItems(selItems[type], type);
			selItems[type] = itemNum;
			setItems(itemNum, type);
		} else {
			selItems[type] = false;
			unsetItems(itemNum, type);
		}
	} else {
		selItems[type] = itemNum;
		setItems(itemNum, type);
	}
	// this is for projects, we need to load image sequences into iframes
	if (proj_id) {
		// if unset make project blank
		if (selItems[type] == false) {
			getRawObject(link.id.substring(0, link.id.length-2)+"-gallery").src = "proj-blank.html";
		}
		else {
			getRawObject(link.id.substring(0, link.id.length-2)+"-gallery").src = "proj-gallery.php?id=" + proj_id;
		}
	} 
	if (exhibit_id) {
		// if unset make project blank
		if (selItems[type] == false) {
			getRawObject(link.id.substring(0, link.id.length-2)+"-gallery").src = "exhibits-blank.html";
		}
		else {
			getRawObject(link.id.substring(0, link.id.length-2)+"-gallery").src = "exhibits-gallery.php?id=" + exhibit_id;
		}
	} 
}

// set/unset functions
function unsetSection(section, type) {
	var obj = getRawObject(type+"-"+section);
	obj.className = "notSelected";
	var inner = obj.innerHTML;
	switch (type) {
		case "level0":
			unsetDisplay(section+"-content");
			inner = inner.replace(/\+/g, "-");
			var selection = type+"-"+section;
			if (selection == "level0-book") {
			getRawObject("book-0-gallery").src = "proj-blank.html";
			}
			else if (selection == "level0-pradablossoms") {
			getRawObject("pradablossoms-0-gallery").src = "proj-blank.html";
			}
			else if (selection == "level0-clo") {
			getRawObject("clo-0-gallery").src = "proj-blank.html";
			}
			else if (selection == "level0-212") {
			getRawObject("212-0-gallery").src = "proj-blank.html";
			}
			else if (selection == "level0-iiwii") {
			getRawObject("iiwii-0-gallery").src = "proj-blank.html";
			}
			else if (selection == "level0-moma") {
			getRawObject("moma-0-gallery").src = "proj-blank.html";
			}
			else if (selection == "level0-wyly") {
			getRawObject("wyly-0-gallery").src = "proj-blank.html";
			}
			else if (selection == "level0-pradalibro") {
			getRawObject("pradalibro-0-gallery").src = "proj-blank.html";
			}
			else if (selection == "level0-nikenymarathon") {
			getRawObject("nikenymarathon-0-gallery").src = "proj-blank.html";
			}
			else if (selection == "level0-designusa") {
			getRawObject("designusa-0-gallery").src = "proj-blank.html";
			}
			else if (selection == "level0-blik") {
			getRawObject("blik-0-gallery").src = "proj-blank.html";
			}
			else if (selection == "level0-nyco") {
			getRawObject("nyco-0-gallery").src = "proj-blank.html";
			}
			else if (selection == "level0-mtv") {
			getRawObject("mtv-0-gallery").src = "proj-blank.html";
			}
			break;
		default:
			unsetDisplay(type+"-"+section+"-content");
			inner = inner.replace(/^\-/g, "+");
			var selection = type+"-"+section;
			if (selection == "idea-1") {
				getRawObject("idea-1-gallery").src = "proj-blank.html";
			}
			break;
	}
	obj.innerHTML = inner;
	// close right content area
	clearContent();
}

function setSection(section, type) {
	var obj = getRawObject(type+"-"+section);
	obj.className = "selected";
	var inner = obj.innerHTML;
	switch (type) {
		case "level0":
			setDisplay(section+"-content");
			inner = inner.replace(/\-/g, "+");
			var selection = type+"-"+section;
			if (selection == "level0-book") {
				getRawObject("book-0-gallery").src = "books.php";
			}
			else if (selection == "level0-pradablossoms") {
				getRawObject("pradablossoms-0-gallery").src = "pradablossoms.php";
			}
			else if (selection == "level0-mtv") {
				getRawObject("mtv-0-gallery").src = "mtv.php";
			}
			else if (selection == "level0-clo") {
				getRawObject("clo-0-gallery").src = "clo.php";
			}
			else if (selection == "level0-212") {
				getRawObject("212-0-gallery").src = "212.php";
			}
			else if (selection == "level0-moma") {
				getRawObject("moma-0-gallery").src = "moma.php";
			}
			else if (selection == "level0-iiwii") {
				getRawObject("iiwii-0-gallery").src = "iiwii.php";
			}
			else if (selection == "level0-wyly") {
				getRawObject("wyly-0-gallery").src = "wyly.php";
			}
			else if (selection == "level0-pradalibro") {
				getRawObject("pradalibro-0-gallery").src = "pradalibro.php";
			}
			else if (selection == "level0-nikenymarathon") {
				getRawObject("nikenymarathon-0-gallery").src = "nikenymarathon.php";
			}
			else if (selection == "level0-designusa") {
				getRawObject("designusa-0-gallery").src = "designusa.php";
			}
			else if (selection == "level0-blik") {
				getRawObject("blik-0-gallery").src = "blik.php";
			}
			else if (selection == "level0-nyco") {
				getRawObject("nyco-0-gallery").src = "nyco.php";
			}
			break;
		default:
			setDisplay(type+"-"+section+"-content");
			inner = inner.replace(/^\+/g, "-");
			var selection = type+"-"+section;
			if (selection == "idea-1") {
				getRawObject("idea-1-gallery").src = "idea.php";
			}
			break;
	}
	obj.innerHTML = inner;
}

function unsetItems(section, type) {
//alert ("unset " +type+"-"+selSection[type]+"-"+section);
	var obj = getRawObject(type+"-"+selSection[type]+"-"+section);
	obj.className = "notSelected";
	unsetDisplay(type+"-"+selSection[type]+"-"+section+"-content");
	var inner = obj.innerHTML;
	inner = inner.replace(/^\-/, "+");
	obj.innerHTML = inner;
}

function setItems(section, type) {
	var obj = getRawObject(type+"-"+selSection[type]+"-"+section);
	obj.className = "selected";
	setDisplay(type+"-"+selSection[type]+"-"+section+"-content");
	var inner = obj.innerHTML;
	inner = inner.replace(/^\+/, "-");
	obj.innerHTML = inner;
}

function showEssay(texthtml) {
	// picture obj in parent frame
	var obj = parent.getRawObject('content-right');
	var tag = '<iframe id="txt-' + Math.floor(Math.random() * 100) + '" src="_txt/' + texthtml + '" width="400" height="435" frameborder="0"></iframe>';
	// add close button
	tag += '<p><a href="#" onclick="clearContent();">--&gt; close</a></p>';
	obj.style.display = "block";
	obj.innerHTML = tag;
}

function showImage(texthtml) {
	// picture obj in parent frame
	var obj = parent.getRawObject('content-right');
	var tag = '<iframe id="image" src="_txt/' + texthtml + '" width="400" height="435" frameborder="0"></iframe>';
	// add close button
	tag += '<p></p>';
	obj.style.display = "block";
	obj.innerHTML = tag;
}

function clearContent() {
	var obj = getRawObject('content-right');
	var tag = '<a style="background-color:#FFF;" href="http://www.iiwii.org" target="_blank"><img src="images/IIWII-violator.gif" border="0"></a>';
	obj.innerHTML = tag;
//	unsetDisplay('content-right');
}

////////////////////////////////////////////////////////////////////////////////
// INIT

function mudInit() {
	initDHTMLAPI();
	// first off, we need to weed out NN4
	if (isNN4) {
		location.href = "notsupported.html";
	}
	// increase width for IE PC
	if (isIEWin) {
		getObject('content-left').width = "560px";
		getObject('content-right').left = "570px";
		getObject('body').fontSize = "11px";
	}
	// increase width even more for FF PC
	if (!isMac && !isIEWin) {
		getObject('content-left').width = "640px";
		getObject('content-right').left = "650px";
		getObject('body').fontSize = "11px";
	}
	
	// show content-wrapper
	getObject('loading').visibility = "hidden";
	getObject('content-wrapper').visibility = "visible";
}

// events
// gecko, safari, konqueror and generic
if (typeof window.addEventListener != 'undefined') {
	window.addEventListener('load', mudInit, false);
}
// opera 7
else if (typeof document.addEventListener != 'undefined') {
	document.addEventListener('load', mudInit, false);
}
// win/ie
else if (typeof window.attachEvent != 'undefined') {
	window.attachEvent("onload", mudInit);
}
// rest
else {
	window.onload = mudInit;
}