
// -- Index

Indexer = function () {
}

Indexer.prototype.Index = function(f_id, unique) {
	if (this[f_id] == null) {
		this[f_id] = []
	}
	this[f_id].push(unique)
}

var ScrollerIndex = new Indexer()

var AnchorIndex = new Indexer()

// Fix for repaint problem in IE7 that sometimes causes layers to grow in width, eg when returning to page with browser back button
AnchorIndex.IndexOld=AnchorIndex.Index;
AnchorIndex.Index=function(f_id, unique){
	AnchorIndex.IndexOld(f_id, unique);
	if(s_iE7)
		_addOnload(function(){var o=_d.getElementById("s_"+f_id+"_"+unique);if(o)o.style.height="auto"})
}

// -- Size

function scrl_getContentWidth(aintScrollId){
	scrl_intContainerWidth[aintScrollId] = gW(scrl_objContainer[aintScrollId]);
	scrl_intContentWidth[aintScrollId] = gW(scrl_objContent[aintScrollId]);
//	scrl_intContentWidth[aintScrollId] = scrl_intContentWidth[aintScrollId]?scrl_intContentWidth[aintScrollId]:gW(scrl_objContent[aintScrollId]);
}

function scrl_getContentHeight(aintScrollId){
	scrl_intContainerHeight[aintScrollId] = gH(scrl_objContainer[aintScrollId]);
	scrl_intContentHeight[aintScrollId] = gH(scrl_objContent[aintScrollId]);
}

// -- Update

function scrl_automaticScroll(k) {
	var e,c0,c1,s0,s1,a0,a1,v0,v1
	
	e  = scrl_objContent[k]
	v0 = scrl_intSpeed0[k]
	v1 = scrl_intSpeed1[k]
	c0 = scrl_intContainerWidth[k]
	c1 = scrl_intContainerHeight[k]
	s0 = scrl_intContentWidth[k]
	s1 = scrl_intContentHeight[k]
	a0 = gX(e)
	a1 = gY(e)
	
	function sgn(x) {
		return x < 0 ? -1 : x > 0 ? 1 : 0
	}

	function cyc(x,v,s,t) {
		return (v<0 ? x>-s : x<t) ? (x + v) : sgn(-v) * (v<0 ? t : s)
	}

	sX(e, (v0 != 0) ? cyc(a0,v0,s0,c0) : 0)
	sY(e, (v1 != 0) ? cyc(a1,v1,s1,c1) : 0)

	scrl_automaticScrollTimeout[k] = setTimeout("scrl_automaticScroll(" + k + ")", 40)
}

// -- Interface

function scrl_showOrHideScrollButtons(k,hide) {
	
	var L,R,L2,R2,U,D
	L = gE("scrollMoveLeftButton_"+k)
	R = gE("scrollMoveRightButton_"+k)
	L2 = gE("scrollMoveLeftButton_"+k+"_2")
	R2 = gE("scrollMoveRightButton_"+k+"_2")
	U = gE("scrollMoveUpButton_"+k)
	D = gE("scrollMoveDownButton_"+k)

//alert("width: "+scrl_intContentWidth[k]+", "+scrl_intContainerWidth[k])
//alert("height: "+scrl_intContentHeight[k]+", "+scrl_intContainerHeight[k])
	if (hide || scrl_intContentWidth[k] <= scrl_intContainerWidth[k]) {
		if (L) hE(L)
		if (R) hE(R)
		if (L2) hE(L2)
		if (R2) hE(R2)
	} else {
		if (L) sE(L)
		if (R) sE(R)
		if (L2) sE(L2)
		if (R2) sE(R2)
	}
	if (hide || scrl_intContentHeight[k] <= scrl_intContainerHeight[k]) {
		if (U) hE(U)
		if (D) hE(D)
	} else {
		if (U) sE(U)
		if (D) sE(D)
	}
}

// - Navigation

function scrl_moveToAnchor(f_id,anchorId,xaxis,yaxis) {
	var t = ScrollerIndex[f_id]
	for (var i = 0; i < t.length; i++) {
		scrl_moveTo(t[i], "s_" + anchorId + "_" + AnchorIndex[anchorId][i], xaxis, yaxis)
	}
}

function scrl_moveTo(aintScrollId,anchorId,xaxis,yaxis) {
	var a = gE(anchorId)
	var b = scrl_objContent[aintScrollId]
	var pa = [gPos(a.id,"x"),gPos(a.id,"y")]
	var pb = [gPos(b.id,"x"),gPos(b.id,"y")]
	if (xaxis) {
		b.style.left = pb[0]-pa[0]
	}
	if (yaxis) {
		b.style.top = pb[1]-pa[1]
	}
	if (xaxis || yaxis) {
		scrl_justifyPosition(aintScrollId)
	}
}

function scrl_moveUp(aintScrollId){
	if (scrl_blnIeOrNs6 && parseInt(scrl_objContent[aintScrollId].style.top) < 0) {
		scrl_objContent[aintScrollId].style.top = parseInt(scrl_objContent[aintScrollId].style.top) + 5
	} else if (scrl_blnNs4 && scrl_objContent[aintScrollId].top < 0) {
		scrl_objContent[aintScrollId].top += 5
	}
	scrl_justifyPosition(aintScrollId)
	scrl_moveUpTimeout[aintScrollId] = setTimeout("scrl_moveUp(" + aintScrollId + ")", 50);
}

function scrl_moveDown(aintScrollId){
	if (scrl_blnIeOrNs6 && parseInt(scrl_objContent[aintScrollId].style.top) > (scrl_intContentHeight[aintScrollId] * (-1) + scrl_intContainerHeight[aintScrollId] - 10)) {
		scrl_objContent[aintScrollId].style.top = parseInt(scrl_objContent[aintScrollId].style.top) - 5
	} else if (scrl_blnNs4 && scrl_objContent[aintScrollId].top >= (scrl_intContentHeight[aintScrollId] * (-1) + scrl_intContainerHeight[aintScrollId])) {
		scrl_objContent[aintScrollId].top -= 5
	}
	scrl_justifyPosition(aintScrollId)
	scrl_moveDownTimeout[aintScrollId] = setTimeout("scrl_moveDown(" + aintScrollId + ")", 50);
}

function scrl_moveLeft(aintScrollId){
	if (scrl_blnIeOrNs6 && parseInt(scrl_objContent[aintScrollId].style.left) < 0) {
		scrl_objContent[aintScrollId].style.left = parseInt(scrl_objContent[aintScrollId].style.left) + 5
	} else if (scrl_blnNs4 && scrl_objContent[aintScrollId].left < 0) {
		scrl_objContent[aintScrollId].left += 5
	}
	scrl_justifyPosition(aintScrollId)
	scrl_moveLeftTimeout[aintScrollId] = setTimeout("scrl_moveLeft(" + aintScrollId + ")", 50);
}

function scrl_moveRight(aintScrollId){
//alert('contentWidth: '+scrl_intContentWidth[aintScrollId]);
	if (scrl_blnIeOrNs6 && parseInt(scrl_objContent[aintScrollId].style.left) > (scrl_intContentWidth[aintScrollId] * (-1) + scrl_intContainerWidth[aintScrollId])) {
		scrl_objContent[aintScrollId].style.left = parseInt(scrl_objContent[aintScrollId].style.left) - 5
	} else if (scrl_blnNs4 && scrl_objContent[aintScrollId].left > (scrl_intContentWidth[aintScrollId] * (-1) + scrl_intContainerWidth[aintScrollId])) {
		scrl_objContent[aintScrollId].left -= 5
	}
	scrl_justifyPosition(aintScrollId)
	scrl_moveRightTimeout[aintScrollId] = setTimeout("scrl_moveRight(" + aintScrollId + ")", 50);
}

function scrl_justifyPosition(aintScrollId){
	if (scrl_blnIeOrNs6 && parseInt(scrl_objContent[aintScrollId].style.left) < (scrl_intContentWidth[aintScrollId] * (-1) + scrl_intContainerWidth[aintScrollId])) {
		scrl_objContent[aintScrollId].style.left = scrl_intContentWidth[aintScrollId] * (-1) + scrl_intContainerWidth[aintScrollId];
	} else if (scrl_blnNs4 && scrl_objContent[aintScrollId].left < (scrl_intContentWidth[aintScrollId] * (-1) + scrl_intContainerWidth[aintScrollId])) {
		scrl_objContent[aintScrollId].left = scrl_intContentWidth[aintScrollId] * (-1) + scrl_intContainerWidth[aintScrollId];
	}
	if (scrl_blnIeOrNs6 && parseInt(scrl_objContent[aintScrollId].style.left) > 0) {
		scrl_objContent[aintScrollId].style.left = 0;
	} else if (scrl_blnNs4 && scrl_objContent[aintScrollId].left > 0) {
		scrl_objContent[aintScrollId].left = 0;
	}

	if (scrl_blnIeOrNs6 && parseInt(scrl_objContent[aintScrollId].style.top) < (scrl_intContentHeight[aintScrollId] * (-1) + scrl_intContainerHeight[aintScrollId] - 10)) {
		scrl_objContent[aintScrollId].style.top = scrl_intContentHeight[aintScrollId] * (-1) + scrl_intContainerHeight[aintScrollId] - 10;
	} else if (scrl_blnNs4 && scrl_objContent[aintScrollId].top < (scrl_intContentHeight[aintScrollId] * (-1) + scrl_intContainerHeight[aintScrollId] - 5)) {
		scrl_objContent[aintScrollId].top = scrl_intContentHeight[aintScrollId] * (-1) + scrl_intContainerHeight[aintScrollId] - 5;
	}
	if (scrl_blnIeOrNs6 && parseInt(scrl_objContent[aintScrollId].style.top) > 0) {
		scrl_objContent[aintScrollId].style.top = 0;
	} else if (scrl_blnNs4 && scrl_objContent[aintScrollId].top > 0) {
		scrl_objContent[aintScrollId].top = 0;
	}
}
