//////////////////////////////////////////////////////////////////////////////
// [ABCO JS] AnimeScroll 
// Now every viewer of you site will know where you are taking him. No suprise
// scroll to a obscure part below in your site.
// Some code was searched and modify by me, most of those codes are related to
// across browser issues.
//
// Author: ABn3r CO
// Author URL: http://abnercordon.com
// URL: http://abcolive.com
//////////////////////////////////////////////////////////////////////////////
// If you would like to use this code you can if you email me, and tell me
// where are you going to use it, and leave this notice unchanged. This is a 
// contribution to the web development field, but I want the credits. :)
// Español? Deutsch? français? हिन्दी? 日本語? עברית/עִבְרִית?
// Translate this notice. (e.i. http://babelfish.yahoo.com/)
//////////////////////////////////////////////////////////////////////////////

var time = 18;
var speed = 1.1; //Velocity from 1.1 to 1.9 optimal

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	return [curleft,curtop];
	}
}

function abcoStopAnimation() { 		
	clearInterval(abcoIntScroll);
	clearTimeout(abcoTimScroll);
	//alert("Animation Stopped");
	return;
}

function abcoGetWebsiteSize() { //Works for all browsers, yuppy!
	return [document.getElementById('container').offsetHeight, document.getElementById('container').offsetWidth];
}

function abcoGetScreenSize() {
	if (window.innerHeight) { //Firefox, Opera, Safari, Chrome
		return [window.innerHeight, window.innerWidth];
	} else if (document.documentElement && document.documentElement.clientHeight){ //IE7, IE6
		return [window.document.documentElement.clientHeight, window.document.documentElement.clientWidth];
	} else { //Hopefully I get more browser support later
		//None
	}
}

function abcoGetPageOffset() {
	if (typeof(window.pageXOffset) == 'number') { //Firefox, Opera, Safari, Chrome
		return [window.pageXOffset, window.pageYOffset];
	} else if (document.documentElement){ //IE7, IE6
		return [document.documentElement.scrollLeft, document.documentElement.scrollTop];
	} else { //Hopefully I get more browser support later
		//None
	}
}

function abcoScrollAnimation (x,y) {
	website = abcoGetWebsiteSize();
	websiteHeight = website[0] + 8;
	websiteWidth = website[1];
	screenSize = abcoGetScreenSize();
	screenHeight = screenSize[0];
	screenWidth = screenSize[1];
	offset = abcoGetPageOffset();
	xOffset = offset[0];
	yOffset = offset[1];
	
	//Left and Up is no problem, is the Right and Down the causes problems
	// so we need to check for those.
	
	if (xOffset >= x && yOffset >= y) { //both greater than 
		x = Math.floor(xOffset / speed);
		y = Math.floor(yOffset / speed);
		//alert('left , up '+x+' '+y);
		//We should always be able to reach this point
		if (xOffset <= x && yOffset <= y) { abcoStopAnimation(); }
		window.scrollTo(x,y);
	} else if (xOffset >= x && yOffset <= y) {
		x = Math.floor(xOffset / speed);
		y = Math.floor(y +  Math.floor((yOffset - y) / speed));
		//alert('left, down '+x+' '+y);
		if (screenHeight > (websiteHeight - y)) {
			if (xOffset <= x) { abcoStopAnimation(); }
		} else {
			if (xOffset <= x && yOffset >= y) { abcoStopAnimation(); }
		}
		window.scrollTo(x,y);
	} else if (xOffset <= x && yOffset >= y) {
		x = Math.floor(x + Math.floor((xOffset - x) / (speed)));
		y = Math.floor(yOffset / (speed));
		//alert('right, up '+x+' '+y);
		if (screenWidth > (websiteWidth - x)) {
			if (yOffset <= y) { abcoStopAnimation(); }
		} else {				
			if (xOffset >= x && yOffset <= y) { abcoStopAnimation(); }
		}
		window.scrollTo(x,y);
	} else if (xOffset <= x && yOffset <= y) { //both less than
		x = Math.floor(x +  Math.floor((xOffset - x) / speed));
		y = Math.floor(y +  Math.floor((yOffset - y) / speed));
		//alert('right, down '+x+' '+y+ ' Cur Offset '+ xOffset + ' ' + yOffset);
		if (screenHeight > (websiteHeight - y)) { //There is no more to scroll down
			if (screenWidth > (websiteWidth - x)) {
				abcoStopAnimation();
			} else {
				if (xOffset >= x) { abcoStopAnimation(); }
			}
		} else if (screenWidth > (websiteWidth - x)) {
			if (screenHeight > (websiteHeight - y)) {
				abcoStopAnimation();
			} else {
				if (yOffset >= y) { abcoStopAnimation(); }
			}
		} else {
			if (xOffset >= x && yOffset >= y) { abcoStopAnimation(); }
		}
		window.scrollTo(x,y);
	} else {
		abcoStopAnimation();
	}
}

function abcoScroll(x,y) {
	clearInterval(abcoIntScroll);
	abcoIntScroll = setInterval("abcoScrollAnimation(x,y)",time);
	abcoTimScroll = setTimeout("abcoStopAnimation()",3000);
}

function abcoGoTop() {
	clearInterval(abcoIntScroll);
	abcoIntScroll = setInterval("abcoScrollAnimation(0,0)",time);		
	abcoTimScroll = setTimeout("abcoStopAnimation()",3000);
}

function abcoGoto(id) {
	var xandy = findPos(window.document.getElementById(id));
	x = xandy[0];
	y = xandy[1];
	//alert("Goto: " + id + " at [" + x+" "+y + "]");
	abcoScroll(x,y);
}