String.prototype.trim = function(){return this.replace(/^\s+/, "").replace(/\s+$/, "");}
String.prototype.append = function(e){return this.replace(/^\s+/, "").replace(/\s+$/, "");}

// scroll engine
var scrdiv, scriter, scrmin, scrmax;
var scrsteps=5.0;
var scrtimeout=null;
function scrinit(div, min, max) {
	if (scrdiv!=null)
		scrend();
	scrdiv=div;
	scriter=0;
	scrmin=min;
	scrmax=max;
	scrdo();
}
function scrend() {
	scrdiv.style.height = scrmax+"px";
	if (scrdiv.onfinish) {
		scrdiv.onfinish();
		scrdiv.onfinish=null;
	}
	clearTimeout(scrtimeout);
	scrdiv=null;
}
function scrdo() {
	if (scriter>=scrsteps) {
		scrend();
		return;
	}
	scrdiv.style.height = scrmin+((scrmax-scrmin)*scriter/scrsteps)+"px";
	scriter++;
	scrtimeout=setTimeout('scrdo()', 40);
}

function formatTime(timeInteger, part) {
	if(isNaN(Number(timeInteger)))
		return timeInteger;
  function zeropad( n ){ return n>9 ? n : '0'+n; }
  var t = new Date( Number(timeInteger) );
  var Y = t.getUTCFullYear();
  var M = t.getUTCMonth(); // month-1
  var D = t.getUTCDate();
  var d = t.getUTCDay(); // 0..6 == sun..sat
  var day = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'][d];
  var mon = ['Jan','Feb','Mar','Apr','May','Jun',
             'Jul','Aug','Sep','Oct','Nov','Dec'][M];
  var h = t.getUTCHours();
  var m = t.getUTCMinutes();
  var s = t.getUTCSeconds();
  if (part=="stime") {
  	if (h>0) return h+'h'+zeropad(m)+"'"
  	else return m+"'"+zeropad(s)+"''"
  } else if (part=="hour")
	  return zeropad(h)+':'+zeropad(m)
  else if (part=="hms")
	  return zeropad(h)+':'+zeropad(m)+":"+zeropad(s)
  else if (part=="simpledate")
	  return zeropad(D) +' '+ mon
  else if (part=="date")
	  return zeropad(Y-2000)+'/'+zeropad(M+1)+'/'+zeropad(D)
  else if (part=="DHS")
	  return zeropad(Y-2000)+'/'+zeropad(M+1)+'/'+zeropad(D)+' '+zeropad(h)+':'+zeropad(m)+":"+zeropad(s)
  else
	  return day +' '+ D +' '+ mon + ', '+ zeropad(h)+':'+zeropad(m)
}

function format (val, f) {
	if (val==null) return "";
	if (!f) return val;	
	if (f=="DST") return formatTime(val,"stime");
	if (f=="DHS") // Date hour and second
		return formatTime(val,"DHS");
	if (f=="DH") // date & hour
		return formatTime(val);
	if (f=="DS") // date & hour
		return formatTime(val, "hms");
	if (f=="SD") // dd mmm
		return formatTime(val, "simpledate");
	if (f=="DD") // yy/mm/dd
		return formatTime(val, "date");
	if (f=="d") // duration in hour
		return formatTime(val, "hour");
	if (f=="%")
		return val+" %";
	if (f=="M") {
		val = String(val);
		var res="";
		var pos=1;
		for(var i=val.length-1; i>=0; i--) {
			res=val.charAt(i)+res;
			if (val.charAt(i)=="."){
				pos = 0;
			} else {
			    if (pos%3==0)
				res=" "+res;
			}
			pos++;
		}
		return res;
	}
	if (f.charAt(0)=="D") {
		val=Number(val);
		var nb = f.charAt(1);
		if (f.charAt(2)=='%')
			return val.toFixed(nb)+" %";
		else
			return val.toFixed(nb);
	}
	return "?"+val+"?";
}

// ttips
var ttip = null;
function ttHide (e) {$("#tooltip").css("visibility","hidden");}
function ttMove(e) {
	if (!e) var e = window.event;
	var x=0, y=0;
	if (document.captureEvents) {
		x = e.pageX;
		y = e.pageY;
	} else if (window.event.clientX) {
		x = e.clientX+document.documentElement.scrollLeft;
		y = e.clientY+document.documentElement.scrollTop;
	}
	ttip.style.left=(x+15)+"px";
	ttip.style.top=(y+15)+"px";
}
function ttShow(e) {
	if (!e) var e = window.event;
	if (this.ttText) {
		var text = this.ttText.replace(/\[br\]/g,"<br>");
		ttip.innerHTML="<span class='ttnormal'>"+text+"<span>";
	} else
		ttip.innerHTML=_tt[this.ttKey];
	ttMove(e);
	ttip.style.visibility='visible';
}
function ttDefKOver(ev, obj, key) {
	obj.ttKey=key;
	obj.onmousemove=ttMove;
	obj.onmouseover=ttShow;
	obj.onmouseout=ttHide;
	obj.onmouseover(ev);
}

function ttDef(elem, txt) {$(elem).each(function(){this.ttText=txt}).mouseover(ttShow).mousemove(ttMove).mouseout(ttHide);}


function ttMoveScroll(e) {
	if (!e) var e = window.event;
	var x=0, y=0;
	if (document.captureEvents) {
		x = e.pageX;
		y = e.pageY;
	} else if (window.event.clientX) {
		x = e.clientX+document.documentElement.scrollLeft;
		y = e.clientY+document.documentElement.scrollTop;
	}
	var targ;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	while (targ.tagName!='TABLE')
		targ = targ.parentNode;
	var o = getOffset(targ)
	var width = targ.offsetWidth;
	var sx = x-o[0];
	var seconds = targ.getAttribute('seconds');
	var start = parseInt(targ.getAttribute('start'));
	var relSec = Math.round(seconds*sx/width);
	var startRel = start+relSec*1000
	ttip.innerHTML="<span class='ttnormal'>Browse log at "+formatTime(startRel,"hms")+"<span>";
	ttip.style.left=(x+15)+"px";
	ttip.style.top=(y+15)+"px";
}
function ttShowScroll(e) {
	ttMove(e);
	ttip.style.visibility='visible';
}
function ttDefScroll(elem) {
	elem.ttText = 'test';
	elem.onmouseover = ttShowScroll;
	elem.onmousemove = ttMoveScroll;
	elem.onmouseout = ttHide;
}

function schbxdef() {
	var schbx = document.getElementById('schbx');
	schbx.style.color='#666';
	schbx.value='Search WWS...';
	schbx.def=true;
}
function schbxfocus() {
	var schbx = document.getElementById('schbx');
	if (schbx.def==true) {
		schbx.style.color='#000';
		schbx.value='';
	}
}
function schbxblur() {
	var schbx = document.getElementById('schbx');
	if (schbx.value.trim()=='') {
		schbxdef();
	} else
		schbx.def = false;
}
function schbxsub() {
	var schbx = document.getElementById('schbx');
	if (schbx.value!='Search WWS...')
		document.schbxf.submit();
	return false;
}

$(function(){
	
	$(document.body).append("<div id='tooltip'>");
	ttip=document.getElementById("tooltip");
	
	for(var i=1;i<=3;i++) {
		var div = crel('div');
		div.id='drop'+i;
		div.className='menu';
		div.onmouseover=menuDontHide;
		div.onmouseout=menuHide;
		document.body.appendChild(div);
	}
	
	$("table.rep tr, table.dyn tr, table.sr tr")
		.mouseover(function() {$(this).css("backgroundColor", tabHlColor)})
		.mouseout (function() {if (this.hcolor) {$(this).css("backgroundColor", this.hcolor)} else {$(this).css("backgroundColor", "#000")}})
		.each     (function() {if (this.getAttribute('hcolor')=='true') {this.hcolor=tabHlColor;$(this).css("backgroundColor", tabHlColor)}});
	
	$("#schbx").focus(schbxfocus).blur(schbxblur);
	
	$(".tooltip").each(function(){ttDef(this,this.title);this.title="";});

	$("a.button").wrap("<div class='button borderlight'></div>");
	$("a.button").each(function(){
		$(this).before("<img src='"+prefix+"but.l.gif'> ");
		$(this).after(" <img src='"+prefix+"but.r.gif'>")});

	$(".confirm").each(function(){if (this.onclick!=null) {this.clicks=this.onclick;this.onclick=null;}});
	$(".confirm").click(function(){
		if (this.clicks!=null) {if (confirm(this.title)) return this.clicks(); else return false} 
		else return confirm(this.title)});

	$(".blscroll").each(function(){ttDefScroll(this);});

	schbxdef();
});


