/*
Copyright (c) 2006, Dario Maggiorini (dario@dico.unimi.it) University of Milano,
ITALY.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met: Redistributions
of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

Neither the name of the University of Milano nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

--------------------------------------------------------------------------------

Original concept inspired by the Mozilla Foundation Developer site sidebar
http://developer.mozilla.org/ 

--------------------------------------------------------------------------------

version 0.9 beta
last edited: 2006 01 12
*/

/* 
/* This scrip manipulate drop down boxes inside your code; if the box
/* drops below the visible screen area, content is scrolled accordingly.
/*
/* function slideBox(who, on, off [, doNotScroll])
/* who: the object to slide
/* on : an object to make visible (the label to slide in the other direction)
/* off: an object to make invisible (invisible)
/* doNotScroll: if true prevents scrolling
/*
/* The script is truly generic, check the css for defined boxes and containers
/* 
/* use as follows:

<link rel="stylesheet" type="text/css" href="baloon.css">

<div class="dropbox">
	<div id="open"    class="dropbox_openlabel"  onclick="slideBox('content', 'close', 'open');">Open</div>
	<div id="close"   class="dropbox_closelabel" onclick="slideBox('content', 'open', 'close');" style="display: none;">Hide</div>
	<div id="content" class="dropbox_slider" style="display: none;">
		Put some content here
		<div class="box0">
			This content will get boxed
		</div>
		<div class="box1">
			Multiple boxes are possible
		</div>
	</div>
</div>

*/
 
function slideBox(who, on, off, doNotScroll) {		// original concept taken from the mozilla foundation
	var box = document.getElementById(who);
	var nsw = document.getElementById(on);
	var osw = document.getElementById(off);
	osw.style.display = 'none';
	nsw.style.display = 'block';
	if (box.style.display == 'none') {
		if (!doNotScroll) new Effect.SlideDown(box);
		else new Effect.Appear(box);
		setTimeout('checkScroll(\'' + who + '\')', 1200);
	}
	else {
		if (!doNotScroll) new Effect.SlideUp(box);
		else new Effect.Fade(box);
	}
}

function checkScroll(who) {
	var box = document.getElementById(who);
	var y = getRealY(box);
	var h = box.offsetHeight;
	var wh = ((document.all && !window.opera) ? __ietruebody().clientHeight : window.innerHeight - 20);
	var po = ((document.all) ? document.body.scrollTop + document.documentElement.scrollTop : window.pageYOffset);

	if (( y + h - po) > wh) {
		var dst;
		if (h < wh) dst = (y + h - po - wh) + 30;
		else dst = y - po - 15;
		smootScroll(dst);
	}
}

function getRealY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) curtop += obj.y;
	return curtop;
}

function __ietruebody() {
	return ((document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body);
}

function smootScroll(dst) {
	var spd = 50;
	if (dst <= spd) window.scrollBy(0,dst);
	else {
   		window.scrollBy(0, spd);
    	setTimeout('smootScroll('+ (dst - spd) +')', 10);
	}
}
