/************************************************************************************************************
(C) www.dhtmlgoodies.com, September 2005
************************************************************************************************************/	
var contentHeight = 0; 	// The total height of the content
var visibleContentHeight = 0;	
var scrollActive = false;

var scrollHandleObj = false; // reference to the scroll handle
var scrollHandleHeight = false;
var scrollbarTop = false;
var eventYPos = false;

var scrollbuttonActive = false;
var scrollbuttonDirection = false;
var scrollbuttonSpeed = 1; // How fast the content scrolls when you click the scroll buttons(Up and down arrows)
var scrollTimer = 5;	// Also how fast the content scrolls. By decreasing this value, the content will move faster	

var scrollMoveToActive = false;
var scrollMoveToYPosition = false;
function scrollDiv_startScroll(e)
{
	if(document.all)e = event;
	scrollbarTop = $('scrolldiv_theScroll1').offsetTop;
	eventYPos = e.clientY;
	scrollActive = true;
}

function scrollDiv_stopScroll()
{
	scrollActive = false;
	scrollbuttonActive = false;
	scrollMoveToActive = false;
}
function scrollDiv_scroll(e)
{
	if(!scrollActive)return;
	if(document.all)e = event;
	if(e.button!=1 && document.all)return;
	var topPos = scrollbarTop + e.clientY - eventYPos; 
	if(topPos<0)topPos=0;
	if(topPos/1>visibleContentHeight-(scrollHandleHeight)/1)topPos = visibleContentHeight-(scrollHandleHeight);
	$('scrolldiv_theScroll1').style.top = topPos + 'px';
	$('scrolldiv_content1').style.top = 0 - Math.floor((contentHeight) * ((topPos)/(visibleContentHeight-scrollHandleHeight)))+'px' 
}

/*
Click on the slider
Move the content to the this point
*/
function scrolldiv_scrollMoveToInit(e)
{		
	if(document.all)e = event;
	scrollMoveToActive = true;
	scrollMoveToYPosition = e.clientY - $('scrolldiv_scrollbar1').offsetTop;
	if($('scrolldiv_theScroll1').offsetTop/1 > scrollMoveToYPosition) scrollbuttonDirection = scrollbuttonSpeed*-2; else  scrollbuttonDirection = scrollbuttonSpeed*2;
	scrolldiv_scrollMoveTo();	
}

function scrolldiv_scrollMoveTo()
{
	if(!scrollMoveToActive || scrollActive)return;
	var topPos = $('scrolldiv_theScroll1').style.top.replace('px','');
	topPos = topPos/1 + scrollbuttonDirection;
	if(topPos<0){
		topPos=0;
		scrollMoveToActive=false;
	}
	if(topPos/1>visibleContentHeight-(scrollHandleHeight)/1){
		topPos = visibleContentHeight-(scrollHandleHeight);	
		scrollMoveToActiv=efalse;
	}
	if(scrollbuttonDirection<0 && topPos<scrollMoveToYPosition-scrollHandleHeight/2)return;	
	if(scrollbuttonDirection>0 && topPos>scrollMoveToYPosition-scrollHandleHeight/2)return;			
	$('scrolldiv_theScroll1').style.top = topPos + 'px';
	$('scrolldiv_content1').style.top = 0 - Math.floor((contentHeight) * ((topPos)/(visibleContentHeight-scrollHandleHeight)))+'px' 		
	setTimeout('scrolldiv_scrollMoveTo()',scrollTimer);		
}

function cancelEvent()
{
	return false;			
}

function scrolldiv_scrollButton()
{
	if(this.id=='scrolldiv_scrollDown1'){
		scrollbuttonDirection = scrollbuttonSpeed; 
		//alert("down")
	}
	else scrollbuttonDirection = scrollbuttonSpeed*-1;
	scrollbuttonActive=true;
	scrolldiv_scrollButtonScroll();
}
function scrolldiv_scrollButtonScroll()
{
	if(!scrollbuttonActive)return;
	var topPos = $('scrolldiv_theScroll1').style.top.replace('px','');
	topPos = topPos/1 + scrollbuttonDirection;
//	alert(topPos);
	if(topPos<0){
		topPos=0;
		scrollbuttonActive=false;
	}
	if(topPos/1>visibleContentHeight-(scrollHandleHeight)/1){
		topPos = visibleContentHeight-(scrollHandleHeight);	
		scrollbuttonActive=false;
	}	
	$('scrolldiv_theScroll1').style.top = topPos + 'px';
	$('scrolldiv_content1').style.top = 0 - Math.floor((contentHeight) * ((topPos)/(visibleContentHeight-scrollHandleHeight)))+'px' 			
	setTimeout('scrolldiv_scrollButtonScroll()',scrollTimer);
}
function scrolldiv_scrollButtonStop()
{
	scrollbuttonActive = false;
}


function scrolldiv_initScroll()
{
	visibleContentHeight = $('scrolldiv_scrollbar1').offsetHeight ;
	contentHeight = $('scrolldiv_content1').offsetHeight - visibleContentHeight;
	// alert (contentHeight);
	if (contentHeight < 50 ) {
		scrollObj = $('scrolldiv_slider1');
		scrollObj.style.display = "none";
		return;
	} else {
		scrollObj = $('scrolldiv_slider1');
		scrollObj.style.display = "block";
	}
	scrollHandleObj = $('scrolldiv_theScroll1');
	scrollHandleHeight = scrollHandleObj.offsetHeight;
	scrollbarTop = $('scrolldiv_scrollbar1').offsetTop;		
	$('scrolldiv_theScroll1').onmousedown = scrollDiv_startScroll;
	document.body.onmousemove = scrollDiv_scroll;
	$('scrolldiv_scrollbar1').onselectstart = cancelEvent;
	//$('scrolldiv_theScroll1').onmouseup = scrollDiv_stopScroll;
	if(document.all)document.body.onmouseup = scrollDiv_stopScroll; else document.documentElement.onmouseup = scrollDiv_stopScroll;
	$('scrolldiv_scrollDown1').onmousedown = scrolldiv_scrollButton;
	$('scrolldiv_scrollUp1').onmousedown = scrolldiv_scrollButton;
	$('scrolldiv_scrollDown1').onmouseup = scrolldiv_scrollButtonStop;
	$('scrolldiv_scrollUp1').onmouseup = scrolldiv_scrollButtonStop;
//	$('scrolldiv_scrollDown1').onmouseout = scrolldiv_scrollButtonStop;
//	$('scrolldiv_scrollUp1').onmouseout = scrolldiv_scrollButtonStop;
	$('scrolldiv_scrollUp1').onselectstart = cancelEvent;
	$('scrolldiv_scrollDown1').onselectstart = cancelEvent;
	//$('scrolldiv_scrollbar1').onmousedown = scrolldiv_scrollMoveToInit;
}
/*
Change from the default color
*/	
function scrolldiv_setColor(rgbColor)
{
	$('scrolldiv_scrollbar1').style.borderColor = rgbColor;
	$('scrolldiv_theScroll1').style.backgroundColor = rgbColor;
	$('scrolldiv_scrollUp1').style.borderColor = rgbColor;
	$('scrolldiv_scrollDown1').style.borderColor = rgbColor;
	$('scrolldiv_scrollUp1').style.color = rgbColor;
	$('scrolldiv_scrollDown1').style.color = rgbColor;
	$('scrolldiv_parentContainer1').style.borderColor = rgbColor;
}
/*
Setting total width of scrolling div
*/
function scrolldiv_setWidth(newWidth)
{
	$('dhtmlgoodies_scrolldiv1').style.width = newWidth + 'px';
	$('scrolldiv_parentContainer1').style.width = newWidth-30 + 'px';		
}

/*
Setting total height of scrolling div
*/
function scrolldiv_setHeight(newHeight)
{
	$('dhtmlgoodies_scrolldiv1').style.height = newHeight + 'px';
	$('scrolldiv_parentContainer1').style.height = newHeight - 0 +  'px';
	$('scrolldiv_slider1').style.height = newHeight - 0 + 'px';
	$('scrolldiv_scrollbar1').style.height = newHeight- 17 + 'px';		
}
/*
Setting new background color to the slider 
*/
function setSliderBgColor(rgbColor)
{
	$('scrolldiv_scrollbar1').style.backgroundColor = rgbColor;
	$('scrolldiv_scrollUp1').style.backgroundColor = rgbColor;
	$('scrolldiv_scrollDown1').style.backgroundColor = rgbColor;
}
/*
Setting new content background color
*/
function setContentBgColor(rgbColor)
{
	$('scrolldiv_parentContainer1').style.backgroundColor = rgbColor;
}

/*
Setting scroll button speed
*/
function setScrollButtonSpeed(newScrollButtonSpeed)
{
	scrollbuttonSpeed = newScrollButtonSpeed;
}
/*
Setting interval of the scroll
*/
function setScrollTimer(newInterval)
{
	scrollTimer = newInterval;
}

function voteScrollerInit(widthBlog,heightBlog)
{
	//scrolldiv_setColor('#bebebe');	// Setting border color of the scrolling content
	//setSliderBgColor('#bebebe');	// Setting color of the slider div
	//setContentBgColor('#FFFFFF');	// Setting color of the scrolling content
	setScrollButtonSpeed(1);	// Setting speed of scrolling when someone clicks on the arrow or the slider
	setScrollTimer(5);	// speed of 1 and timer of 5 is the same as speed of 2 and timer on 10 - what's the difference? 1 and 5 will make the scroll move a little smoother.
	scrolldiv_setWidth(widthBlog);	// Setting total width of scrolling div
	scrolldiv_setHeight(heightBlog);	// Setting total height of scrolling div
	scrolldiv_initScroll();	// Initialize javascript functions
	//
	//
	//
	$$("ul.nonSub a").each(function(a){
		a.addEvent("click", function(e){
			new Event(e).stop();
			var linkHref = $(this).getProperty("href").split("#");
			if (linkHref.length != 2) return;
			var target = $(linkHref[1]);
			if (target) {
				var windowLayer = target.getParent();
				if (windowLayer) {
					var pos = target.getPosition().y - windowLayer.getPosition().y;
					var scale = 364/windowLayer.getCoordinates().height+0.023;
					ScrollerGoto(pos*scale);
					//alert(scale);
				}
			}
		});
	});
}
//
function ScrollerGoto(pos_scroll){
	//alert(posscroll);
	//if(!scrollActive)return;
	//if(document.all)e = event;
	//if(e.button!=1 && document.all)return;
	var topPos = pos_scroll; 
	$('scrolldiv_theScroll1').style.top = topPos + 'px';
	$('scrolldiv_content1').style.top = 0 - Math.floor((contentHeight) * ((topPos)/(visibleContentHeight-scrollHandleHeight)))+'px' 
}
