/*
 * Sichtbare Links (Anzahl)
 */
var slideVisibleColumns = 3;

/*
 * Verschiebeintervall (ms)
 */
var slideTimeout = 15;
/*
 * Pauseintervall, wenn Links wieder "passend" stehen (ms)
 */
var slideStopTimeout = 500;
/*
 * Versatz pro Intervall (px)
 */
var slideOffsetPerTimeout = 3;


/*
 * Zwischenspeicher 
 */
var slideContainer = null;
var slideIsMouseOut = true;


function initNaviSlide() {
	try {
	  document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
	var node = document.getElementById('productNavi');
	if (node) {
		var table = getDeepSub(node,'TABLE', null);
		var prevNode = findFirstSub(node,'SPAN', 'prev');
		var nextNode = findFirstSub(node,'SPAN', 'next');
		
		if (table && prevNode && nextNode) {
			var tr =  getDeepSub(table,'TR', null);
			if (tr) {
				var linkCount = countSub(tr, 'TD', null);
				addClass(node, 'slideNaviHasJs');
				removeClass(prevNode, 'hidden');
				removeClass(nextNode, 'hidden');

				var count = 0;
				var active = 0;
				var sub = findFirstSub(tr, 'TD', null);
				while (sub) {
					if (hasDeepSub(sub, 'STRONG', null)) {
						active = count;
					}
					count = count + 1;
					sub = findNextSub(sub, 'TD', null);
				} 
				if (active > linkCount - (slideVisibleColumns + 1)) {
					active = linkCount - slideVisibleColumns;
				}
				if (active < 0) {
					active = 0;
				}
				sub = findFirstSub(tr, 'TD', null);

				table.style.marginLeft = '-' + (active * sub.offsetWidth) + 'px';
	
				prevNode.onclick = function() {return false;}
				nextNode.onclick = function() {return false;}
				prevNode.onmouseover = function() {
					slideIsMouseOut = false;
					slideContainer = getSlideContainer(this);
					slideNaviMoveRight(true);
				}
				prevNode.onmouseout = function() {
					slideIsMouseOut = true;
					//slideContainer = null;
				}
				nextNode.onmouseover = function() {
					slideIsMouseOut = false;
					slideContainer = getSlideContainer(this);
					slideNaviMoveLeft(true);
				}
				nextNode.onmouseout = function() {
					slideIsMouseOut = true;
					//slideContainer = null;
				}
				slideNaviActivateButtons(table);
			}
		}
	}
}

function getSlideContainer(node) {
	return getDeepSub(node.parentNode, 'TABLE',null);
}

function slideNaviActivateButtons(sContainer) {
	var container = sContainer.parentNode.parentNode;
	if (container) {
		var prevNode = findFirstSub(container,'SPAN', 'prev');
		var nextNode = findFirstSub(container,'SPAN', 'next');
		if (prevNode && nextNode) {
			var scroll = parseInt(sContainer.style.marginLeft);
			if (!scroll) {
				scroll = 0;
			} 
			if (scroll >= 0) {
				addClass(prevNode, 'inactive');
			} else {
				removeClass(prevNode, 'inactive');
			}

			td = getDeepSub(container, 'TD', null);
			
			if (-scroll >= (countSub(getDeepSub(sContainer, 'TR', null), 'TD', null) - slideVisibleColumns) * td.offsetWidth) {
				addClass(nextNode, 'inactive');
			} else {
				removeClass(nextNode, 'inactive');
			}
		}
	}
}

function slideNaviMoveLeft(isInit) {
	if(slideContainer) {
		td = getDeepSub(slideContainer, 'TD', null);
		slideNaviActivateButtons(slideContainer);
		var scroll = parseInt(slideContainer.style.marginLeft);
		if (!scroll) {
			scroll = 0;
		} 
		if (scroll % td.offsetWidth == 0 && slideIsMouseOut) {
			slideNaviActivateButtons(slideContainer);
			slideContainer = null;
			return;
		}
		if (-scroll < (countSub(getDeepSub(slideContainer, 'TR', null), 'TD', null) - slideVisibleColumns) * td.offsetWidth) {
			if ((scroll - slideOffsetPerTimeout) % td.offsetWidth < scroll % td.offsetWidth) {
				scroll = scroll - slideOffsetPerTimeout;
			} else {
				scroll = Math.ceil(scroll / td.offsetWidth - 1) * td.offsetWidth;
			}
		}
		slideContainer.style.marginLeft = ' ' + scroll + 'px';
		if (-scroll < (countSub(getDeepSub(slideContainer, 'TR', null), 'TD', null) - slideVisibleColumns) * td.offsetWidth && (0-scroll) % td.offsetWidth != 0) {
			window.setTimeout("slideNaviMoveLeft()", slideTimeout);
		} else if (-scroll < (countSub(getDeepSub(slideContainer, 'TR', null), 'TD', null) - slideVisibleColumns) * td.offsetWidth && (0-scroll) % td.offsetWidth == 0 && !slideIsMouseOut) {
			window.setTimeout("slideNaviMoveLeft()", slideStopTimeout);
		} else {
			slideNaviActivateButtons(slideContainer);
			slideContainer = null;
		}
	}
}
function slideNaviMoveRight(isInit) {
	if(slideContainer) {
		td = getDeepSub(slideContainer, 'TD', null);
		slideNaviActivateButtons(slideContainer);
		var scroll = parseInt(slideContainer.style.marginLeft);
		if (!scroll) {
			scroll = 0;
		}
		if (scroll % td.offsetWidth == 0 && slideIsMouseOut) {
			slideNaviActivateButtons(slideContainer);
			slideContainer = null;
			return;
		}
		if (scroll < 0) {
			if (((scroll + slideOffsetPerTimeout) % td.offsetWidth > scroll % td.offsetWidth) || isInit) {
				scroll = scroll + slideOffsetPerTimeout;
			} else {
				scroll = Math.ceil(scroll / td.offsetWidth) * td.offsetWidth;
			}
		}
		slideContainer.style.marginLeft = ' ' + scroll + 'px';
		if (scroll < 0 && (0-scroll) % td.offsetWidth != 0) {
			window.setTimeout("slideNaviMoveRight()", slideTimeout);
		} else if (scroll < 0 && scroll % td.offsetWidth == 0 && !slideIsMouseOut) {
			window.setTimeout("slideNaviMoveRight(true)", slideStopTimeout);
		} else {
			slideNaviActivateButtons(slideContainer);
			slideContainer = null;
		}
	}
}

addInitFunction(initNaviSlide);