// This library is intellectual property of Peters Web Solutions, 2004
// version 1.1.2
// except for the function
// dw_findObj which is intellectual property of Macromedia. Inc.
//
// and the function
// XBrowserAddHandler which was posted at http://weblogs.asp.net/asmith/archive/2003/10/06/30744.aspx by Andi Smith
//
// If you want to use or sell or copy or redistribute this library in any possible way, or extend the library please contact us at http://www.pwsol.com
//

Array.prototype.delay;
Array.prototype.timer;
Array.prototype.debug = false;
Array.prototype.wipe_start = function (delay) {
	if (this.wipe_start.arguments.length < 1) delay = 25;
	this.delay = (delay < 25) ? 25 : delay; 
	this.timer = setInterval("wipes.wipe_it()", this.delay);
}

Array.prototype.wipe_stop = function () {
	if (this.timer) {
		clearInterval(this.timer);
		this.timer = null;
	}
}

Array.prototype.wipe_restart = function() {
	if (this.delay < 25) this.delay = 25;
	this.timer = setInterval("wipes.wipe_it()", this.delay);
}

Array.prototype.wipe_it = function () {
	var stop_timer = true;
	for (var i = 0; i < wipes.length; i++) {
		stop_timer &= wipes[i].do_wipe();
	}
	if (stop_timer) this.wipe_stop();
}

Array.prototype.find_wipe = function (lnk)
{
	for (var i = 0; i < this.length; i++) {
		if (this[i].lnk != '' && this[i].lnk == lnk) return this[i]; 
	}
	return null;
}

Array.prototype.close_menues = function (openMenu) {
	for (var i = 0; i < this.length; i++) {
		if (this[i].lnk != '' && this[i].lnk != openMenu) {
			if (this[i].curr_height) this[i].action = 'close';
		}
	}
}

Array.prototype.on_error = function(msg, url, line)
{
	if (this.debug)	windows.status = msg + ' ' + url + ' ' + line;
	return false;
}

function wipe_obj(lnk, wipebox, action, is_menu, step_open, step_close) {
	this.lnk = lnk;
	this.wipebox = wipebox;
	if (lnk != '') this.l_el = dw_findObj(lnk);
	else this.l_el = null;
	this.w_el = dw_findObj(wipebox);
	if (this.w_el) this.calc_box();
	if (this.l_el) {
		// Since the link can be a differing object we can't prototype it.
		this.l_el.wipe_click = wipe_click;
		XBrowserAddHandler(this.l_el, 'click', 'wipe_click');
	}
	this.action = (action == 'open' || action == 'close' || action == 'opened') ? action : '';
	this.is_menu = is_menu;
	this.step_open = (step_open < 1) ? 1 : step_open;
	this.step_close = (step_close < 1) ? 1 : step_close;
	this.calc_box();
	this.curr_height = (action == 'close') ? this.height : 0;
	return this;
}

wipe_obj.prototype.calc_box = function () {
	if (!this.w_el || !typeof(this.w_el.clientHeight)=='undefined') return 'auto';
	if (!this.w_el.cloneNode) return 'auto';
	var clone = this.w_el.cloneNode(true);
	if (!clone) return 'auto';
	clone.style.visibility='hidden';
	clone.style.display='block';
	clone.style.height='auto';
	document.body.appendChild(clone);
	this.height = clone.clientHeight;
	document.body.removeChild(clone);
}

wipe_obj.prototype.do_wipe = function () {
	var finished = true;
	switch (this.action) {
		case 'open':
			if (this.height > this.curr_height) {
				this.curr_height += this.step_open;
				if (this.curr_height >= this.height) {
					this.curr_height = this.height;
					this.action='';
				}
				else {
					finished = false;
				}
				if (this.action == '') {
					this.w_el.style.height='auto';
					this.height=this.curr_height=this.w_el.clientHeight;
				}
				else {
					this.w_el.style.height=this.curr_height+'px';
				}
				this.w_el.style.visibility='visible';
				this.w_el.style.display='block';
			}
			break;
		case 'opened':
			this.w_el.style.height='auto';
			this.height=this.curr_height=this.w_el.clientHeight;
			this.action='';
			break;
		case 'close':
			if (this.curr_height) {
				this.curr_height -= this.step_close;
				if (this.curr_height <= 0) {
					this.curr_height = 0;
					this.w_el.style.visibility='hidden';
					this.w_el.style.display='none';
				}
				else {
					finished = false;
				}
				this.w_el.style.height= this.curr_height+'px';
			}
			break;
		default:
			break;
	}
	return finished;
}

function wipe_click(e)
{
	wipe = wipes.find_wipe(this.id);
	
	wipe.action = (wipe.curr_height == 0) ? 'open' : 'close';
//	alert(wipe.action + ' curr_height: ' + wipe.curr_height);
	wipe.calc_box();
	if (wipe.is_menu && wipe.action == 'open') {
		wipes.close_menues(this.id);
	}
	wipes.wipe_restart();
}


// Example: obj = findObj("image1");
function dw_findObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length) {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}

function XBrowserAddHandler(target,eventName,handlerName) { 
  if ( target.addEventListener ) { 
    target.addEventListener(eventName, function(e){target[handlerName](e);}, false);
  } else if ( target.attachEvent ) { 
    target.attachEvent("on" + eventName, function(e){target[handlerName](e);});
  } else { 
    var originalHandler = target["on" + eventName]; 
    if ( originalHandler ) { 
      target["on" + eventName] = function(e){originalHandler(e);target[handlerName](e);}; 
    } else { 
      target["on" + eventName] = target[handlerName]; 
    } 
  } 
}

