/*global document, window, event */


  var milisec = 0;
  var seconds = 0;
  var scrolling = null;
  var displayTimeout = null;
  var inc = 1;
  var wait = 50;
  var defaultYPos = null;
  var defaultSize = 16;
  var size = defaultSize;

function reset(element){
  milisec = 0;
  seconds = 0;
  inc = 1;
  wait = 50;
  size = defaultSize;
  if (scrolling) {
  	window.clearTimeout(scrolling);
  	scrolling = null;
	window.clearTimeout(displayTimeout);
	pausedisplay();
  }
  scrolling = null;
  displayTimeout = null;
  element.style.top = defaultYPos;
  element.style.fontSize=defaultSize;
//  scroll(element);
}

function updateText(element){
 element.fontSize = size;
 element.innerHTML = document.d.dtext.value;
}

function scrollUp() {
 inc += 1;
}

function scrollDown() {
 if (inc>1) {
  inc -= 1;
 }
}

function scrollReset() {
 inc = 50;
}

function fontSizeUp(element) {
 size += 2;
 element.style.fontSize = size + 'px';
}

function fontSizeDown(element) {
 size -= 2;
 element.style.fontSize = size + 'px';
}

function fontSizeReset(element) {
 size=defaultSize;
 element.style.fontSize = defaultSize;
}

function display(){
  if (milisec>=9){
     milisec=0
     seconds+=1
  }
  else
     milisec+=1
     document.d.d2.value = Math.floor(seconds/3600%10)+""+Math.floor(seconds/3600%10)+":"+Math.floor(seconds%3600/60/10)+""+Math.floor(seconds%3600/60%10)+":"+Math.floor(seconds%3600%60/10)+""+seconds%3600%60%10

//seconds + "." + milisec;
     displayTimeout = setTimeout("display()",100);
  }

function pausedisplay(){
     document.d.d2.value = Math.floor(seconds/3600%10)+""+Math.floor(seconds/3600%10)+":"+Math.floor(seconds%3600/60/10)+""+Math.floor(seconds%3600/60%10)+":"+Math.floor(seconds%3600%60/10)+""+seconds%3600%60%10;
     
  }

var scroll = function(element) {
 	var getYpos = function() {
  		var ypos = element.offsetTop;
  		var thisNode = element;
  		while (thisNode.offsetParent &&  (thisNode.offsetParent != document.body)) {
  		 	thisNode = thisNode.offsetParent;
  			ypos += thisNode.offsetTop;
 		}
 		return ypos;
 	};

 	var doScroll = function() {
  		var y = parseInt(getYpos(),10);
  		y=y-inc;
  		y=y+"px";
  		element.style.top = y;
  		scrolling = window.setTimeout(doScroll,wait);
 	};

 	var toggleScrolling = function() {
  		if (scrolling) {
   			window.clearTimeout(scrolling);
   			scrolling = null;
			window.clearTimeout(displayTimeout);
			pausedisplay();

  		} else {
   			doScroll();
			display();
  		}
 	};

	toggleScrolling();

	 var keys = function(key) {
 	 	if (!key) {
  	 		key = event;
 	 	 	key.which = key.keyCode;
 		 }
		switch (key.which) {
 			case 221:	// ]
 			 	if (scrolling) {
 				  	inc++;
 			 	}
		 	break;
			 case 219:	// [
 			  if (scrolling && inc>1) {
	  				 inc--;
				  }
			 break;
			 case 10:	// return
			 case 13:	// enter
			  	toggleScrolling();
			 break;
		}
		  return false;
	 };
	 document.onkeyup = keys;
};

var init = function() {
 	if (document.getElementById && document.getElementById("speech")) {
//		  scroll(document.getElementById("speech"));
		  defaultYPos = document.getElementById("speech").offsetTop;
 	}
};

window.onload = init;
