// scripts that build upon core.js

// Tabs
// hide all tabs but the one on top

function hideTabs(){
	// get all divs
	var alltabs = getTags(document, "div");
	
	for (var i = 0; i < alltabs.length; i++){
	
		// find all divs with the class "tabs"
		if (hasClass(alltabs[i],"tabs")){
		
			// get the child divs
			var c = getTags(alltabs[i],"div");
			
			for (var h = 0; h < c.length; h++){
			
				// hide all of them
				hideDisplayNone(c[h]);
			
				// but show the first one
				show(c[0]);			
			}
		}
	}
}

// show the tab you've clicked
function showTab(e, caller){
	if (!caller) caller = this;			
	
	var pt = getParent(caller);
	var dv = getTags(pt, "div");
	var lnk = getTags(pt, "a");
		
	for (var i = 0; i < dv.length; i++) {
				
		// make sure the divs not selected are hidden
		if (dv[i].id != caller.id + "_body") { hideDisplayNone(dv[i]); }
		
		// show the div you selected
		else { show(dv[i]); }
	}
	
	for (var h = 0; h < lnk.length; h++) {
		// turn off the anchor linking so that it doesn't interfere
		lnk[h].href = "javascript://";
		
		if (lnk[h].id != caller.id) { 
			removeClass(lnk[h],"on");
		}
		else { addClass(lnk[h],"on"); }
	}
}

function setTabs(obj){
	if (typeof(obj) == "string") obj = getObject(obj);

	if (obj.captureEvents)
		obj.captureEvents(Event.CLICK);
	
	obj.onclick = showTab;
}

// Manipulate sections of a page (panes): expand or collapse width, height

// incrementally collapse width
// parameters: obj = object id, minW = minimum width, increment = whole #

function togglePaneWidth(obj,minW,maxW,incrmt){
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	var w = getWidth(obj);
	
	if (maxW && maxW == w) { closePaneWidth(obj,minW,incrmt); }
	else { openPaneWidth(obj,maxW,incrmt); }	
}

function closePaneWidth(obj,minW,incrmt) {		
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	var w = getWidth(obj);
	var c = getChildren(obj);	
	
	if ((w>minW) && ((w-minW)>incrmt)) {
		w -= incrmt;
		obj.style.width = w + 'px';		
		var t = setTimeout(function(){closePaneWidth(obj,minW,incrmt);}, 0);
	}
	else {
		w = minW;
		obj.style.width = w + 'px';
		// stretch surrounding divs to fill up space
		resize();
	}
	
	// hide the contents, except for the toggle link	
	for (var i = 0; i < c.length; i++) { 
		if (!hasClass(c[i],'tglnk')) {
			hideDisplayNone(c[i]);
		}		
	}
}

// incrementally expand width
// parameters: obj = object id, maxW = maximum width, increment = whole #

function openPaneWidth(obj,maxW,incrmt){		
	if (typeof(obj) == 'string') obj = getObject(obj);

	var w = getWidth(obj);
	var c = getChildren(obj);
			
	if ((w<maxW) && ((maxW-w)>incrmt)) {
		w += incrmt;		
		obj.style.width = w + 'px';		
		var t = setTimeout(function(){openPaneWidth(obj,maxW,incrmt);}, 0);
	}
	else {
		w = maxW;
		obj.style.width = w + 'px';
		// contract surrounding divs
		resize();	
	}	
	
	// show the contents
	for (var i = 0; i < c.length; i++) { show(c[i]); }
}


// spindown functions

function spindown(e, caller) {
	if (!caller) caller = this;
	
	// get the bottom element
	var b = getObject(caller.id + "_body");
	
	// if we have both the top and bottom
	// set the appropriate styles
	if (b != null) {
		if (hasClass(caller, "spdn_open")) {
			// close it
			hideDisplayNone(b)
			removeClass(caller, "spdn_open");
			addClass(caller, "spdn_closed");
		}
		else {
			// open it
			show(b)
			removeClass(caller, "spdn_closed");
			addClass(caller, "spdn_open");
		}
	}
}

function setSpindown(obj) {
	if (typeof(obj) == "string") obj = getObject(obj);

	if (obj.captureEvents)
		obj.captureEvents(Event.CLICK);
	
	obj.onclick = spindown;
}

function spindownOnly(e, caller) {
		if (!caller) caller = this;
		
		var p = getParent(caller);
		var h = getTags(p,'h2');
		var d = getTags(p,'ul');
		var b = getObject(caller.id + "_body");
		
		if (b != null){
			if (hasClass(caller,"spdn_closed")){			
				for (i = 0; i < h.length; i++){
					hideDisplayNone(h[i].id + "_body");
					removeClass(h[i], "spdn_open");
					addClass(h[i], "spdn_closed");
				}
				show(b);
				removeClass(caller, "spdn_closed");
				addClass(caller, "spdn_open");
			}
			else{
				hideDisplayNone(b)
				removeClass(caller, "spdn_open");
				addClass(caller, "spdn_closed");
			}
		}			
	}	


// Alternating table row colors

// modified by Shaun to change classes rather than colors
// modified by Maggie to use our core scripts

function stripe() {
	
	// the flag we'll use to keep track of whether the current row is odd or even
	var even = false;
	var evenStyle = 'even';
	var oddStyle = 'odd';
	
	var tb = getTags(document, 'tbody');
	
	for (var h = 0; h < tb.length; h++) {
		// reset the even attribute
		even = false;
		
		if (hasClass(tb[h],'stripe')){
			// find all the tr elements... 
			var tr = getTags(tb[h], 'tr');
		
			// ... and iterate through them
			for (var i = 0; i < tr.length; i++) {
				// avoid rows that have a class attribute
				// or backgroundColor style
				//if (!hasAnyClass(trs[i]))					
					tr[i].className = even ? evenStyle : oddStyle;
		
				// flip from odd to even, or vice-versa
				even =  ! even;
			}		
		}			
	}
}


// go to new location onchange
// borrowed from quirksmode.org
function go(){
	nav = document.forms[0].displaytype;
	destination = nav.options[nav.selectedIndex].value;
	if (destination) location.href = destination;
}

function switchContent(obj){
	var d = document.forms[0].displaytype;
	var c = d.options[d.selectedIndex].value;
	if (c) getMyHTML(c,obj);
}

// get to the iframes
function updateFrame(obj,url){
	if (typeof(obj) == 'string') obj = getObject(obj);
	//if (obj) obj.location.href = url;
	
	if (obj) obj.src = url;
}

// open + close popups
function goTo(alink){
	var url = alink.href
	location.href = url;
}

function closeToolTip(obj){
	if (typeof(obj) == 'string') obj = getObject(obj);	
	setStyle(obj,'left','-1000px');
}

// show + hide tools tray
function hideTray(){
	hideDisplayNone('tray'); 
	setStyle('albumtray','width','1px'); 
	show('showtray');
}

function showTray(){
	hideDisplayNone('showtray'); 
	setStyle('albumtray','width','240px');
	show('tray');
}


function toggleChecked(obj) {
	// where obj is the form id
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	//get checkboxes
	var checks = getTags(obj, 'input');
	
	for(var i=0; i<checks.length; i++) {
	if (checks[i].checked) {
		checks[i].checked = false;
		}
	else {
		checks[i].checked = true;
		}
	}									
}

function checkAll(obj) {
	// where obj is the form id
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	//get checkboxes
	var checks = getTags(obj, 'input');
	
	for(var i=0; i<checks.length; i++) {
	checks[i].checked = true;
	}									
}


function checkAllVPhotos(obj,checkaction) {

	// where obj is the form id
	if (typeof(obj) == 'string') obj = getObject(obj);

	//get checkboxes

	var checks = getTags(obj, 'input');
	
	for(var i=0; i<checks.length; i++) {
	checks[i].checked = true;
	}


	document.getElementById("NumChecked").innerHTML=document.getElementById("TotImages").innerHTML;
	
		
	
									
}

function checkNone(obj) {
	// where obj is the form id
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	//get checkboxes
	var checks = getTags(obj, 'input');
	
	for(var i=0; i<checks.length; i++) {
	checks[i].checked = false;
	}								
}

function checkNoneVPhotos(obj,checkaction) {
	// where obj is the form id
	if (typeof(obj) == 'string') obj = getObject(obj);
	
	//get checkboxes
	var checks = getTags(obj, 'input');
	
	for(var i=0; i<checks.length; i++) {
	checks[i].checked = false;
	}	
	document.getElementById("NumChecked").innerHTML="0";
							
}


// highlight single element in a group, make sure the rest are not
function highlight(caller,className){
	if (!caller) caller = this;
	
	var container = getParent(caller);	
	var children = getChildren(container);
	
	for (var i = 0; i < children.length; i++){
		if (children[i] != caller){
			removeClass(children[i],className);
		}
	}
	addClass(caller,className);
}

function disableCheck(chk){
	if (typeof(chk) == 'string') chk = getObject(chk);
	
	var td = getParent(chk);
	var tr = getParent(td);
	var c = getTags(tr, 'input');	
	
	for(var i=0; i<c.length; i++){
		
		if (c[i] != chk){
			
			if (chk.checked){
				c[i].disabled = false;
			}
			else{
				c[i].disabled = true;
				c[i].checked = false;					
			}
		}	
	}						
}
