﻿if (typeof document.attachEvent!='undefined') window.attachEvent('onload',onHTMLLoad);
else window.addEventListener('load',onHTMLLoad,false);
var interval_top  = null;
var interval_bottom  = null;
var scroll_hight = null;
var scroll_min_hight = 50;
var scroll_flag = 'yes';

var top_style = 0;
var path = 'img/';
var s_top = 63;

var img_top = null;
var img_bottom = null;
var a_top  = null;
var a_bottom  = null;
var div_scroll = null;
var scrollbar = null;

// all var
var i = 0;
var d = '';

//
function onHTMLLoad(){
	if(scroll_flag == 'yes'){
		a_top = document.getElementById('img_top');
		a_bottom = document.getElementById('img_bottom');
		div_scroll = document.getElementById('scroll');
		scrollbar = document.getElementById('scrollbar');
		img_top = a_top.getElementsByTagName('img')[0];
		img_bottom = a_bottom.getElementsByTagName('img')[0];
		//
		scroll_hight = div_scroll.offsetHeight;
		setEventsScroll();
	}
}
function setEventsScroll(){
	
	if(scroll_hight < scroll_min_hight){
		scrollbar.style.display = 'none';
		return;
	} else if(!scroll_hight){
		scrollbar.style.display = 'none';
		return;
	} else{
		scrollbar.style.display = 'block';
	}
	
	a_top.onclick = function(){
		stopIntervals();
		d=0;
		s_top = div_scroll.offsetTop;
		//var max = scroll_hight + top -50;
		scrolling(0);
		return false;
	}
	a_bottom.onclick = function(){
		stopIntervals();
		d=0;
		s_top = div_scroll.offsetTop;
			var max = scroll_hight + s_top + 100;
			if(max > scroll_min_hight){//
				scrolling(1);
		}
		return false;
	}
	img_top.onmouseout = function(){
		this.src = path+'scroll_top.jpg';
	}
	img_top.onmouseover = function(){
		this.src = path+'scroll_top_active.jpg';
	}
	img_bottom.onmouseout = function(){
		this.src = path+'scroll_bottom.jpg';
	}
	img_bottom.onmouseover = function(){
		this.src = path+'scroll_bottom_active.jpg';
	}
}
function stopIntervals(){
	if(interval_top){
		window.clearInterval(interval_top);
	}
	if(interval_bottom){
		window.clearInterval(interval_bottom);
	}
}

function scrolling(mov_flag){
	if(mov_flag == 0){
		interval_top = window.setInterval(moving_top,5);
	} else if(mov_flag == 1){
		interval_bottom = window.setInterval(moving_bottom,5);
	}
}
function moving_top(){
	if(!d) {d = 0;}
	var h = div_scroll.style.top.substring(1, div_scroll.style.top.length-3);
	if(h == 0){
		top_style = div_scroll.style.top = 0;
		stopIntervals();
		d = 0;
		return;
	}
	if(h >0 && h < 1){
		top_style = div_scroll.style.top = 0;
		stopIntervals();
		d = 0;
		return;
	}
	div_scroll.style.top = s_top + 25*Math.log(d/9+1)+'px';
	if(d == 90){
		top_style = div_scroll.style.top;
		stopIntervals();
		d = 0;
	}
	d++;
}
function moving_bottom(){
	if(!d) {d = 0;}
	div_scroll.style.top = s_top - 25*Math.log(d/9+1)+'px';
	val = 25*Math.log(d/9+1) - s_top;
	if(d == 90){
		top_style = div_scroll.style.top;
		stopIntervals();
		d = 0;
	}
	if(val > scroll_hight + -63){
		top_style = div_scroll.style.top;
		stopIntervals();
		d = 0;
	}
	d++;
}
