﻿/*
 * $Id: scroll.js,v 1.18 2011/02/21 13:05:29 michael Exp $
 * This is an unpublished work copyright (c) 2006 HELIOS Software GmbH
 * 30827 Garbsen, Germany
 */
 
/* global variable for submenu that got currently patched in scroll controls */
var _ws_ul;

var _ie = ((navigator.appVersion.indexOf("MSIE")!= -1) && !window.opera) ? true : false;
var _specialValuesRequired = navigator.appVersion.indexOf("Safari/4") > -1 || navigator.appVersion.indexOf("Safari/3") > -1;

/* scroll parameter */
var _WS_SCROLL_SPEED = _specialValuesRequired ? 10 : 5;
var _WS_SCROLL_INTERVAL = 5;

/* id's of scroll controls */
var _WS_UP_CTRL = "upControl";
var _WS_DOWN_CTRL = "downControl";
var _WS_SCROLL_BOX = "scrollBox";
var _WS_SCROLL_CONTAINER = "scrollContainer";
var _WS_VERTICAL = false;

/* add ws_scrollSubMenu to window onload event */
ws_addEvent(window, "load", ws_scrollSubMenu);

/* add ws_patchOutScrollControls to onresize event */
ws_addEvent(window, "resize", function() { ws_patchOutScrollControls(_ws_ul); });

/* register ws_mousepointerPositionHandler */
ws_addEvent(document, "mousemove", ws_mousePositionHandler);

function ws_scrollSubMenu() {
	if (document._WS_TB_VERTICAL) _WS_VERTICAL = true;
	var tbs = ws_getElementsByClassName("sTb1", document.getElementById("tb"));
	for (var i = 0; i < tbs.length; i++) {	
		ws_addScroll(ws_getParentNode(ws_getPreviousSibling(tbs[i]), 1), tbs[i]);
		ws_addEvent(ws_getPreviousSibling(tbs[i]), 'mouseover', function() {
				if (_ws_ul) ws_patchOutScrollControls(_ws_ul);
		});
	}
}

function ws_addScroll(elem, sub) {
	ws_addEvent(elem, 'mouseover', function() {
			sub.parentButton = elem;
			if (overflow(sub))
				ws_patchInScrollControls(sub);
			else
				sub.style.left = "";
		});
	ws_attachUnloadSwitch();
}

function ws_patchInScrollControls(e) {
	if (isDefined(_ws_ul) && _ws_ul != null && e.id != _ws_ul.id)
		ws_patchOutScrollControls(_ws_ul);
	else if (isDefined(_ws_ul) && _ws_ul != null && e.id == _ws_ul.id)
		return;	

	// IE only
	if (!e) e = window.event;
	if (e.timeout) clearTimeout(e.timeout);
			
	_ws_ul = e;
				
	_ws_ul.tmp = {
		parentObj: e.parentButton,
		tmpObj: ws_cloneNode(e, true)
	};

	_ws_ul.style.left = (_ie && !window.XMLHttpRequest && _WS_VERTICAL ? "0px" : "");
	
  	var scrollPos = ws_findPosition(_ws_ul);
	var scrollOffset = (_ie ? document.documentElement.scrollTop: window.pageYOffset);
	
	var scroller = document.createElement("div");
	with (scroller) {
		style.zIndex = "1";
		setAttribute("id", _WS_SCROLL_BOX);
		style.width = (parseInt(e.offsetWidth) * 2) - (_specialValuesRequired ? 8 : 0) + "px";
		style.height = (ws_getWindowProperties().height - ws_getHeight(e.parentButton)) + "px";
		style.display = "block";
		/* special position for Microsoft Internet Explorer */
		if (_ie) {
			style.top = ws_getHeight(e.parentButton) + "px";
			style.left = "-" + parseInt(ws_getWidth(e.parentButton) / 6) + "px";
		}
	}

	if (_WS_VERTICAL) {
		scroller.style.left = ws_getWidth(e.parentButton) + "px";
		scroller.style.top = "0px";
	}

	var container = document.createElement("div");
	with (container) {
		setAttribute("id", _WS_SCROLL_CONTAINER);
		style.zIndex = "2";
	}

	var toAppendTo = ws_getPreviousSibling(_ws_ul);
	container.appendChild(_ws_ul);
	scroller.appendChild(container);
	ws_getParentNode(toAppendTo, 1).insertBefore(scroller, ws_getNextSibling(toAppendTo));

	_ws_ul.position = {
		top: 0,
		bottom: (scrollPos.y + ws_getHeight(e)) - ws_getWindowProperties().height
	};

	var up = document.createElement("span");
	up.setAttribute("id", _WS_UP_CTRL);	
	var up_text = document.createTextNode("↑");
	up.style.display = "none";
	up.appendChild(up_text);
	
	var down = document.createElement("span");
	down.setAttribute("id", _WS_DOWN_CTRL);
	var down_text = document.createTextNode("↓");
	down.style.display = "none";
	down.appendChild(down_text);

	var b = document.getElementsByTagName("body")[0];
	b.appendChild(up);
	b.appendChild(down);
	
	var cntrlWidth = parseInt(e.offsetWidth);
	scrollPos = ws_findPosition(scroller);
	
	with (up) {
    	onmouseout = onScrollStop;
		style.zIndex = "1500";
	    style.width = (cntrlWidth - 2) + "px";
		/* up and down controlls have a fixed height of 12px + 2px border */
		style.top = scrollOffset + scrollPos.y  - (_specialValuesRequired ? 14 : 0) + "px";
		style.left = "-999em";
		style.display = "block";
	}

	if (_WS_VERTICAL)
		up.style.top = ((scrollOffset + scrollPos.y) - (_specialValuesRequired ? 14 : 0)) + "px";
	
	up.onmouseover = function() {
		_ws_ul.style.display = "block";
		onScrollUpStart();
	};
	
	with (down) {
  		onmouseout = onScrollStop;
		style.zIndex = "1500";
	    style.width = (cntrlWidth - 2) + "px";
		/* up and down controlls have a fixed height of 12px + 2px border
		 * but _WS_UP_CTRL is initialized already so calculate its height to avoid display of scrollbars */
 		style.top = ((scrollOffset + ws_getHeight(e.parentButton) + ws_getHeight(scroller)) - ws_getHeight(_WS_UP_CTRL)) + "px";
		style.left = (_specialValuesRequired ? scrollPos.x - 8 : scrollPos.x) + "px";
		style.display = "block";
   	}

	down.onmouseover = function() {
		_ws_ul.style.display = "block";
		onScrollDnStart();
	};
	
	_ws_ul.border = {
		top: parseInt(ws_getHeight(e.parentButton)) - parseInt(ws_getHeight(_WS_DOWN_CTRL)),
		left: parseInt(down.style.left),
		right: parseInt(down.style.left) + parseInt(ws_getWidth(e)),
		bottom: parseInt(down.style.top) + parseInt(ws_getHeight(_WS_DOWN_CTRL))
	};
	
	_ws_ul.upTop = up.style.top;
	_ws_ul.downLeft = down.style.left;

	ws_addEvent(_ws_ul, "mouseover", function() { this.hovered = true; } );
}

function ws_patchOutScrollControls(e) {
	if (!e) return;

	var b = document.getElementsByTagName("body")[0];

	/* remove scroll controls */
	var c = ws_getElementsById(_WS_UP_CTRL);

	if (c)
		b.removeChild(c);

	c = ws_getElementsById(_WS_DOWN_CTRL);
	if (c)	
		b.removeChild(c);

	/* remove scroll box and container */
	c = document.getElementById(_WS_SCROLL_BOX);
	if (c)
		c.parentNode.removeChild(c);

	c = document.getElementById(_WS_SCROLL_CONTAINER);
	if (c)
		c.parentNode.removeChild(c);

	/* append original nodes */
	_ws_ul.tmp.parentObj.appendChild(_ws_ul.tmp.tmpObj);
	_ws_ul.tmp.tmpObj.style.left = "-999em";
	_ws_ul = null;

	ws_showActiveButtons();
	ws_openSubMenu();
	ws_scrollSubMenu();
	
	if (_ie)
		fixSubmenus();
}

/*
 *		********** SCROLL HANDLER FUNCTIONS	**********
 */
 
var ws_scrollActive = false
var ws_scrollStop = true;
var ws_scrollActive = false;

function onScrollDnStart() {
  if (!ws_scrollActive) {
    ws_scrollStop = false;
    onScrollDn();
  }
}

function onScrollDn() {
  if (!ws_scrollStop) {
    ws_scrollActive = true;
    setTimeout('onScrollDn()', _WS_SCROLL_INTERVAL);
    var sc = ws_getElementsById(_WS_SCROLL_CONTAINER);
    var y = getTop(sc) - _WS_SCROLL_SPEED;
    if (y >= -(_ws_ul.position.bottom)) {
      getTop(sc, y);
      if (y < 0) {
		var scrollPos = ws_findPosition(ws_getElementsById(_WS_SCROLL_BOX));
		document.getElementById(_WS_UP_CTRL).style.left = (_specialValuesRequired ? scrollPos.x - 8 : scrollPos.x) + "px";
		if (_specialValuesRequired && !_WS_VERTICAL) {
			var scrollOffset = (_ie ? document.documentElement.scrollTop: window.pageYOffset);
			document.getElementById(_WS_UP_CTRL).style.top = _ws_ul.upTop;
		}
		
	  }
    } else {
      document.getElementById(_WS_DOWN_CTRL).style.left = "-999em";
      ws_scrollStop = true;
      ws_scrollActive = false;
    }
  }
}

function onScrollUpStart() {
  if (!ws_scrollActive) {
    ws_scrollStop = false;
    onScrollUp();
  }
}

function onScrollUp() {
  if (!ws_scrollStop) {
    ws_scrollActive = true;
    setTimeout('onScrollUp()', _WS_SCROLL_INTERVAL);
    var sc = ws_getElementsById(_WS_SCROLL_CONTAINER);
    var y = getTop(sc) + _WS_SCROLL_SPEED;
    if (y <= _ws_ul.position.top) {
      getTop(sc, y);
      if (y < 0) {
      	var scrollPos = ws_findPosition(ws_getElementsById(_WS_SCROLL_BOX));
		document.getElementById(_WS_DOWN_CTRL).style.left = _ws_ul.downLeft;
	  }
    } else {
      document.getElementById(_WS_UP_CTRL).style.left = "-999em";
      ws_scrollStop = true;
      ws_scrollActive = false;
    }
  }
}

function onScrollStop() {
  ws_scrollStop = true;
  ws_scrollActive = false;
}

function ws_mousePositionHandler (evt) {
	if (!evt)
		evt = window.event;

    if (!_ws_ul) return;

	var x, y;

    if (evt.pageX || evt.pageY) 	{
		x = evt.pageX;
		y = evt.pageY;
	} else if (evt.clientX || evt.clientY) 	{
		x = evt.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		y = evt.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
    
    x = parseInt(x);
    y = parseInt(y);

	if (isOut(x, y, _ws_ul.border, true))  {
	
		/* check if cursor is over parent button */
		if (isOver(x, y, _ws_ul.parentButton, false))
			return;
			
		/* check if cursor is over submenu */
		var subs = ws_getElementsByClassName("sTb2", _ws_ul, "ul");
		for (var i = 0; i < subs.length; i++) {
			if (subs[i] && getPageX(subs[i]) > 0 && isOver(x, y, subs[i], false))
				return;
		}
		
		ws_patchOutScrollControls(_ws_ul);
	}
}

/*
			********** SCROLL-LIB FUNCTIONS **********
*/

/* @return true if height + offset of <e> is more than screen height */
function overflow(e) {
	return (parseInt(ws_findPosition(e, true).y) + parseInt(e.offsetHeight)) > ws_getWindowProperties().height;
};

function isOut(x, y, obj, cords) {
	if (cords)
		return (
			x < obj.left
			|| x > obj.right
			|| y < obj.top
			|| y > obj.bottom
		);

	var scrollOffset = (_ie ? document.documentElement.scrollTop + document.body.scrollTop : window.pageYOffset);

	return (
		x < getPageX(obj)
		|| y < (scrollOffset + getPageY(obj))
		|| x > (getPageX(obj) + ws_getWidth(obj))
		|| y > (scrollOffset + getPageY(obj) + ws_getHeight(obj))
	);	
}

function isOver(x, y, obj, cords) {
	return !isOut(x, y, obj, cords);
}

function getScrollTop(e, bWin) {
	var offset=0;
	if (bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
		var w = window;
		if (bWin && e) w = e;
		if(w.document.documentElement && w.document.documentElement.scrollTop) offset=w.document.documentElement.scrollTop;
		else if(w.document.body && isDefined(w.document.body.scrollTop)) offset=w.document.body.scrollTop;
	}
	else {
		e = ws_getElementsById(e);
		if (e) offset = e.scrollTop;
	}
	return offset;
}

function moveTo(e,x,y) {
	getLeft(e,x);
	getTop(e,y);
}

function getTop(e, iY) {
	if(!(e=ws_getElementsById(e))) return 0;
	var css=isDefined(e.style);
	if(css && isString(e.style.top)) {
		if(isNumber(iY)) e.style.top=iY+'px';
		else {
			iY=parseInt(e.style.top);
			// USE ws_findPosition HERE - RECURSION PROBLEM
			// if(isNaN(iY)) iY = ws_findPosition(e).y;
			if(isNaN(iY)) iY=_getComputedStyle(e,'top',1);
			if(isNaN(iY)) iY=0;
		}
	}
	else if(css && isDefined(e.style.pixelTop)) {
		if(isNumber(iY)) e.style.pixelTop=iY;
		else iY=e.style.pixelTop;
	}
	return iY;
}

function getLeft(e, iX) {
	if(!(e=ws_getElementsById(e))) return 0;
	var css=e.style;
	if (css && e.style.left) {
		if(iX) e.style.left=iX+'px';
		else {
			iX=parseInt(e.style.left);
			// USE ws_findPosition HERE - IF RECURSION PROBLEM OCCURS
			// if(isNaN(iX)) iX = ws_findPosition(e).x;
			if(isNaN(iX)) iX=_getComputedStyle(e,'left',1);
			if(isNaN(iX)) iX=0;
		}
	}
	else if(css && isDefined(e.style.pixelLeft)) {
		if(iX) e.style.pixelLeft=iX;
		else iX=e.style.pixelLeft;
	}
	return iX;
}


function _getComputedStyle(e, p, i) {
	if (document.getElementById) e = document.getElementById(e);
	else if (document.all) e = document.all[e];
	else e = null;
	if(!(e = _getElementById(e))) return null;
	var s, v = 'undefined', dv = document.defaultView;
	if(dv && dv.getComputedStyle){
		s = dv.getComputedStyle(e,'');
		if (s) v = s.getPropertyValue(p);
	}
	else if(e.currentStyle) {
		v = e.currentStyle[getCamelized(p)];
	}
	else return null;
	return i ? (parseInt(v) || 0) : v;
}

function getPageX(e) {
	var x = 0;
	e = ws_getElementsById(e);
	while (e) {
		if (isDefined(e.offsetLeft)) x += e.offsetLeft;
		e = isDefined(e.offsetParent) ? e.offsetParent : null;
	}
	return x;
}

function getPageY(e) {
	var y = 0;
	e = ws_getElementsById(e);
	while (e) {
		if (isDefined(e.offsetTop)) y += e.offsetTop;
		e = isDefined(e.offsetParent) ? e.offsetParent : null;
	}
	return y;
}

