/*
 *	File: dc.js
 *	Author: Tyke Lewis
 *	Date: July 2007
 *	Purpose:  Creates slider up and down for divs
 *      modified by Forrest England 9/08 to work with cms forms
 */   


/* globals */
var interval = 1; // frequency SetTimeout calls SlideItUp/Down
var speed = 15;   // pixels to move up/down each pass
var tup;          // slide up timeout
var tdown;        // slide down timeout

var theDiv

/* window to print debugging messages */
var debug = false;
if (debug) {
    var dbgwin;
}

/* initialization */

addLoadEvent(setupDivs); // call setupDivs on page load

function setupDivs() {

    if (debug) {
	dbgwin = window.open ("", "debugwindow");
	dbgwin.document.write('<span style="color: red; font-weight: bold">debugging window</span><br /><br />'); 
	dbgwin.document.write('setupDivs called<br />');
    }

    // find slide controllers
    inputs = document.getElementsByTagName('input');
    for (i in inputs) {
	//	if (inputs[i].nodeType === 1) {
        if (debug) {
	    dbgwin.document.write("found input: slides = "+ inputs[i].classname + "<br />\n");
	}
	    if (inputs[i].className == "slidecontrol") {
		if (debug) {
		    dbgwin.document.write('found slide control for div '+
					  inputs[i].getAttribute('slides')+'<br />'); 
		}
		
		// find div slid by this control
		var div = document.getElementById(inputs[i].getAttribute('slides'));
		div.style.overflow = "hidden";
		div.setAttribute('theight', div.scrollHeight+10); // div height when fully open
		if (inputs[i].checked) {
		    div.style.height = div.getAttribute('theight')+"px";
		    div.setAttribute('canopen', false);
		    div.style.display= "block";
		} else {
		    div.style.height = "0px";
		    div.setAttribute('canopen', 'true');
		    div.style.display= "none";
		}
	    }
	    //	}
    }
}

// called when slide control is clicked
function slideMe(obj, what){

    theDiv = document.getElementById(what); // div to slide

    if (debug) {
	dbgwin.document.write('<br />slideMe called with arg '+theDiv.id+'<br />');
	dbgwin.document.write('&nbsp;&nbsp;&nbsp;&nbsp;canopen = '+theDiv.getAttribute('canopen')+'<br />');
	dbgwin.document.write('&nbsp;&nbsp;&nbsp;&nbsp;display = '+theDiv.style.display+'<br />');
    }

    if (theDiv.getAttribute('canopen') == 'true') {
	if (debug) {
	    dbgwin.document.write('<br>Initiating slide-down on '+theDiv.id+'<br />');
	}
	slideDown();
    } else {
	if (debug) {
	    dbgwin.document.write('<br>Initiating slide-up on '+theDiv.id+'<br />');
	}
	slideUp();
    }
}

function slideDown(){
    if (debug) {
        dbgwin.document.write('<br />slideDown called on '+theDiv.id+'<br />');
    }
    theDiv.setAttribute('canopen', 'false'); 
    theDiv.style.display= "block";
    clearInterval(tup);
    tup = setInterval("slideItDown()",interval);
}

function slideUp(){
    if (debug) {
        dbgwin.document.write('<br />slideUp called on '+theDiv.id+'<br />');
    }
    theDiv.setAttribute('canopen', 'true');
    clearInterval(tup);
    tup = setInterval("slideItUp()",interval);
}

function slideItDown(){
    if (debug) dbgwin.document.write('slideitDown called<br />');
    height = parseInt(theDiv.style.height);
        theight = parseInt(theDiv.getAttribute('theight'));
    if (debug) dbgwin.document.write('height = '+height+'<br />');
    if (debug) dbgwin.document.write('total height = '+theight+'<br />');
    if (height < theight) {
	theDiv.style.height = (height+speed) + "px";
	if (debug) {
	    dbgwin.document.write('slideitDown: setting height of '+theDiv.id+
				  ' to '+theDiv.style.height+'<br />');
	}
    } else {
	if (debug) {
	    dbgwin.document.write('slideitDown done: setting height of '+theDiv.id+
				  ' to '+theDiv.style.height+'<br />');
	}
/*	theDiv.setAttribute('canopen', 'false');*/
	clearInterval(tup);
    }
}
function slideItUp(){
    if (debug) dbgwin.document.write('slideitUp called<br />');
    height = parseInt(theDiv.style.height);
    if (height > speed) {
	theDiv.style.height = (height-speed) + "px";
	if (debug) {
	    dbgwin.document.write('slideitUp: setting height of '+theDiv.id+
				  ' to '+theDiv.style.height+'<br />');
	}
    } else {
	theDiv.style.height = "0px";
	theDiv.style.display= "none";
/*		theDiv.setAttribute('canopen', 'true'); */
		if (debug) {
		    dbgwin.document.write('slideitUp done: setting height of '+theDiv.id+
					  ' to '+theDiv.style.height+'<br />');
		}
	theDiv.setAttribute('canopen', 'true');
	clearInterval(tup);
    }
}


/* helper functions */

// register an onload function
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
	window.onload = func;
    } else {
	window.onload = function() {
	    if (oldonload) {
		oldonload();
	    }
	    func();
	}
    }
}

