
// INIT 
function initTimeSlider(){
	
			//CurrentPosition of active highlighted element:
			var curPos;
			if($('.ts_href_active').position()){
			  	curPos = $('.ts_href_active').position().top;
			}
			else{
				if($('.themen_href_active').position()){
			 		curPos = $('.themen_href_active').position().top;
				}
				else{
					curPos = 0;	
				}	
			}
			
			var cHeight = parseInt($('#timeslidercontainer').height());
			var sHeight = parseInt($('#timeslidertable').height());
			var offSet = sHeight - cHeight;
			var scrollable = parseInt((curPos/cHeight)) * cHeight;
			
			if((scrollable + cHeight) > sHeight){
				$('#timeslidertable').css("top", "-"+ offSet +"px");
				end = offSet;
			}
			else{
				$('#timeslidertable').css("top", "-"+ scrollable +"px");
				end = scrollable;
			}
										   
			checkTSButtons(null,end,null,'init');
						
			$('#timeslideDown').mousedown(function(){tsdown();});
			$('#timeslideUp').mousedown(function(){tsup();});
	
}

//FUNCTIONS 
function tsdown(){
	
	var speed = 250;
	var cHeight = parseInt($('#timeslidercontainer').height());
	var sHeight = parseInt($('#timeslidertable').height());
	var sTop = parseInt($('#timeslidertable').position().top);
	var cTop = parseInt($('#timeslidercontainer').position().top);
	var tdHeight = parseInt($("td").height());
	var offSet = sHeight - cHeight;
	var endPos = offSet * (-1);
	
    var scrollable;
	if (offSet+sTop < cHeight){
		scrollable = offSet+sTop;
		//alert("here");
	}
	else{
		scrollable = cHeight;	
	}
	
	//alert(offSet+sTop);
	
	if (sTop > endPos){
    	$('#timeslidertable').animate({"top":"-=" + scrollable + "px"}, speed, function(){$('#timeslidertable').stop(); checkTSButtons(sTop, endPos, scrollable, 'down')} );
	}
}


function tsup(){
	var speed = 250;
	var cHeight = parseInt($('#timeslidercontainer').height());
	var sHeight = parseInt($('#timeslidertable').height());
	var sTop = parseInt($('#timeslidertable').position().top);
	var cTop = parseInt($('#timeslidercontainer').position().top);
	var tdHeight = parseInt($("td").height());
	var offSet = sHeight - cHeight;
	var endPos = 0;
	var scrollable;
	

	//alert(sTop);
		
	if (-(sTop) > cHeight){
		scrollable = cHeight;
	}
	else{
		scrollable = -(sTop);	
	}
	
	//alert(offSet+sTop - cHeight);
		
	if (sTop < endPos){
    	$('#timeslidertable').animate({"top":"+=" + scrollable + "px"}, speed, function(){$('#timeslidertable').stop(); checkTSButtons(sTop, 0, scrollable, 'up')}  );
	}
	
}


function checkTSButtons(top, end, tH, dir){

	$('#timeslideDown').show();
	$('#timeslideUp').show();
	
	if(dir == 'init'){
		//alert(end);
		top = parseInt($('#timeslidertable').position().top);
		cHeight = parseInt($('#timeslidercontainer').height());
		sHeight = parseInt($('#timeslidertable').height());
		
		if(top == 0){
			$('#timeslideUp').hide();
		}
		
		if((sHeight <= cHeight) || (sHeight-cHeight == end)){
			$('#timeslideDown').hide();	
		}
	}


	//slideUpCheck
	if((((top + tH)) >= end)&&(dir == 'up')){
		$('#timeslideUp').hide();
	}
	//slideDownCheck
	if(((top - tH) <= end) && (dir == 'down')){
		$('#timeslideDown').hide();
	}
}




