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 - 5) {
					active = linkCount - 4;
				}
				if (active < 0) {
					active = 0;
				}

				table.style.marginLeft = '-' + (active * 153) + 'px';
				table.style.width = ' ' + (count * 153) + 'px';
				prevNode.onclick = function() {return false;}
				nextNode.onclick = function() {return false;}
				prevNode.onmouseover = function() {
					slideIsMouseOut = false;
					slideContainer = getSlideContainer(this);
					slideNaviMoveRight();
				}
				prevNode.onmouseout = function() {
					slideIsMouseOut = true;
					//slideContainer = null;
				}
				nextNode.onmouseover = function() {
					slideIsMouseOut = false;
					slideContainer = getSlideContainer(this);
					slideNaviMoveLeft();
				}
				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');
			}
			if (-scroll >= (countSub(getDeepSub(sContainer, 'TR', null), 'TD', null) - 4) * 153) {
				addClass(nextNode, 'inactive');
			} else {
				removeClass(nextNode, 'inactive');
			}
		}
	}
}

function slideNaviMoveLeft() {
	if(slideContainer) {
		slideNaviActivateButtons(slideContainer);
		var scroll = parseInt(slideContainer.style.marginLeft);
		if (!scroll) {
			scroll = 0;
		} 
		if (scroll % 153 == 0 && slideIsMouseOut) {
			slideNaviActivateButtons(slideContainer);
			slideContainer = null;
			return;
		}
		if (-scroll < (countSub(getDeepSub(slideContainer, 'TR', null), 'TD', null) - 4) * 153) {
			scroll = scroll - 9;
		}
		slideContainer.style.marginLeft = ' ' + scroll + 'px';
		if (-scroll < (countSub(getDeepSub(slideContainer, 'TR', null), 'TD', null) - 4) * 153 && (0-scroll) % 153 != 0) {
			window.setTimeout("slideNaviMoveLeft()", 15);
		} else if (-scroll < (countSub(getDeepSub(slideContainer, 'TR', null), 'TD', null) - 4) * 153 && (0-scroll) % 153 == 0 && !slideIsMouseOut) {
			window.setTimeout("slideNaviMoveLeft()", 500);
		} else {
			slideNaviActivateButtons(slideContainer);
			slideContainer = null;
		}
	}
}
function slideNaviMoveRight() {
	if(slideContainer) {
		slideNaviActivateButtons(slideContainer);
		var scroll = parseInt(slideContainer.style.marginLeft);
		if (!scroll) {
			scroll = 0;
		}
		if (scroll % 153 == 0 && slideIsMouseOut) {
			slideNaviActivateButtons(slideContainer);
			slideContainer = null;
			return;
		}
		if (scroll < 0) {
			scroll = scroll + 9;
		}
		slideContainer.style.marginLeft = ' ' + scroll + 'px';
		if (scroll < 0 && (0-scroll) % 153 != 0) {
			window.setTimeout("slideNaviMoveRight()", 15);
		} else if (scroll < 0 && scroll % 153 == 0 && !slideIsMouseOut) {
			window.setTimeout("slideNaviMoveRight()", 500);
		} else {
			slideNaviActivateButtons(slideContainer);
			slideContainer = null;
		}
	}
}

addInitFunction(initNaviSlide);