window.addEvent('load', function(e){		
	initScrolling();	
	setTimeout(getCurrentPos1,1000);
});

function getCurrentPos1(){
	var scrollingContent = $('scrollingContent');
	//alert(Math.round(scrollingContent.getPosition().y*totalStep/scrollingContent.retrieve('maxScroll')));
	scrollingContent.store('curPos', Math.round(scrollingContent.getScroll().y*totalStep/scrollingContent.retrieve('maxScroll')));
}


function return2Top(){
	var scrollingContent = $('scrollingContent');
	scrollingContent.retrieve('slider').set(0);
}
function getCurentPos(link)
{
	var cur = $('scrollingContent').getElementById(link);
	if (cur != undefined)
	{
		// Kiem vi tri y cua cur trong div
		posy = cur.getPosition();
		h = cur.getSize();		
		pos = Math.round(posy.y/20) ;//- h.y;		
	}
	else 
	{
		pos = 0;
	}
	
	return pos;
	
}
	function ScrollLinkTo(link)
	{
		//initScrolling(link);
		setTimeout(getCurrentPos1,500);
	}
	
var posy ;
	var scrollingContent ;	
	var scrolling;
	var scrollingHeight ;
	var scrollingContentHeight ;
	var scrollingContentScroll ;		
	var scale ;
	var maxScroll ;
	var totalStep ;
function initScrolling(link){
	scrollingContent = $('scrollingContent');
	if(!scrollingContent) return;
	scrolling = scrollingContent.getNext();
	scrollingHeight = scrolling.getCoordinates().height;
	scrollingContentHeight = scrollingContent.getSize().y;
	scrollingContentScroll = scrollingContent.getScrollSize().y;		
	scale = scrollingHeight/scrollingContentScroll;
	maxScroll = scrollingContentScroll - scrollingContentHeight;
	totalStep = Math.round(maxScroll / 10);
	scrollingContent = $('scrollingContent');
	scrollingContent.store('maxScroll', maxScroll);
		
	if(link === undefined)
	{	
		scrollingContent.store('curPos', 0);
	}
	else 
	{		
		posy = getCurentPos(link);
		scrollingContent.store('curPos', posy);
	}	
	
	if(scrollingContentScroll - scrollingHeight <= 10){
		scrolling.setStyle('display', 'none');
		return;
	}
	
	var aLinks = scrolling.getElements('a'); 
	scrollingContent.store('slider', new Slider(aLinks[1], aLinks[1], {
		
//		onLoad: function(
		onChange: function(pos){			
			scrollingContent.scrollTo(0, scrollingContent.retrieve('maxScroll') * pos / totalStep);
			//alert(scrollingContent.retrieve('maxScroll') * pos / totalStep+"-"+scrollingContent.getScroll().y);
			scrollingContent.store('curPos', pos);			
			//alert(pos+":::"+Math.round(scrollingContent.getPosition().y*totalStep/scrollingContent.retrieve('maxScroll')));
		},
		mode: 'vertical',
		range: [0, totalStep]
	}).set(0));
		
	scrollingContent.addEvents({
		'mousewheel': function(e){
			e.stop();
			scrollingContent.retrieve('slider').set(scrollingContent.retrieve('curPos') - e.wheel);
			
		}
	});
	
	//Attemp to slideto	
	
	var scrolInterval = null;
	aLinks[0].addEvents({
		'mouseenter': function(e){
			e.stop();
			scrolInterval = setInterval(function(){
				scrollingContent.retrieve('slider').set(scrollingContent.retrieve('curPos') - 1);
			}, 100);
		},
		'mouseleave': function(e){
			clearInterval(scrolInterval);
		}
	});
	aLinks[2].addEvents({
		'mouseenter': function(e){
			e.stop();
			//alert(scrollingContent.retrieve('curPos'));
			scrolInterval = setInterval(function(){
				scrollingContent.retrieve('slider').set(scrollingContent.retrieve('curPos') + 1);
			}, 100);
		},
		'mouseleave': function(e){
			clearInterval(scrolInterval);
		}
	});
}