// Copyright 2006-2008 ClickTale Ltd.
var DateDiffVal=0;

function InitDateDiff(sd)
{
	//document.write(DateDiffVal+"<br>");
	//DateDiffVal=(new Date()).getTime()-sd;
	//if(DateDiffVal>50*60*1000) // we can't know if this is just a reload of the page or a real difference. need way to get CURRENT server time (AJAX??!)
	//	DateDiffVal=0; 
	//document.write(DateDiffVal);
}

// render/get full UTC time
function RenderUTCDate(d)
{
	document.write(new Date(d+DateDiffVal));
}

function GetUTCDate(d)
{
	return(new Date(d));
}

// get/render short date 'Mon dd (year) [time]'
// accepts number
function GetShortDateOnly(d)
{
	//alert(d);
	d=new Date(d+DateDiffVal);
	//alert(d);
	var n=new Date();
	var ShortMonthArray = new Array("Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec") ;
	var s=ShortMonthArray[d.getMonth()]+" "+d.getDate()
	if(d.getFullYear()!=n.getFullYear())
		s+=", "+d.getFullYear();
	return s;
}

// get/render short date 'Mon dd (year) [time]'
// accepts number
function GetShortDate(d)
{
	var s=GetShortDateOnly(d)+" ";
	d=new Date(d+DateDiffVal);
	s+=Get12HTime(d);
	return s;
}

function RenderShortDate(d)
{
	document.write(GetShortDate(d));
}

// M/D/Y
function GetUSDate(d)
{
	if(typeof d=="number")
		d=new Date(d+DateDiffVal);
	return (d.getMonth()+1)+"/"+d.getDate()+"/"+d.getFullYear();
}

// get time 'hh:mm am'
// accepts date
function Get12HTime(d)
{
  var curHour = d.getHours()
  var curMin = d.getMinutes()
  var curSec = d.getSeconds()
  var curAMPM = " AM"
  if (curHour >= 12){
    curHour -= 12
    curAMPM = " PM"
    }
  if (curHour == 0) curHour = 12
  return curHour + ":" 
	+ ((curMin < 10) ? "0" : "") + curMin /*+ ":" 
    + ((curSec < 10) ? "0" : "") + curSec */
    + curAMPM
}

// break timespan into parts
function BreakTimeLen(l)
{
	var o={s: Math.floor(l/1000), ms:l%1000 };
	o.m=Math.floor(o.s/60);
	o.s=o.s%60;
	o.h=Math.floor(o.m/60);
	o.m=o.m%60;
	o.d=Math.floor(o.h/24);
	o.h=o.h%24;
	return o;
}

// get/render compact time length string
function GetTimeLen(l)
{
	var o=BreakTimeLen(l);
	if(o.d>0)
		return o.d+(o.d==1?" day":" days")+(o.h>0?" "+o.h+" hr":"");
	if(o.h>0)
		return o.h+" hr"+ (o.m>0?" "+o.m+" min":"");
	if(o.m>0)
		return o.m+" min"+ (o.s>0?" "+o.s+" sec":"");
	if(o.s>=10)
		return o.s+" sec";
	var lh=Math.floor(o.ms/10);
	return o.s+"."+((lh < 10) ? "0" : "")+lh+" sec";
}

// get/render compact time length string
function GetTimeLenDOM(l,h)
{
	var o=BreakTimeLen(l);
	//return h.SPAN(GetTimeLen(o)); // no speedup here	
	if(o.d>0)
		return h.SPAN({'class':'TimeLen'}, h.SPAN({'class':'d'}, h.SPAN(o.d), (o.d==1?" day":" days") ) , o.h>0?[" ", h.SPAN({'class':'h'}, h.SPAN(o.h)," hr")]:null );
	if(o.h>0)
		return h.SPAN({'class':'TimeLen'}, h.SPAN({'class':'h'}, h.SPAN(o.h), " hr") , o.m>0?[" ", h.SPAN({'class':'m'}, h.SPAN(o.m), " min")]:null ) ;
	if(o.m>0)
		return h.SPAN({'class':'TimeLen'}, h.SPAN({'class':'m'}, h.SPAN(o.m), " min") , o.s>0?[" ", h.SPAN({'class':'s'}, h.SPAN(o.s), " sec")]:null );
	if(o.s>=10)
		return h.SPAN({'class':'TimeLen'}, h.SPAN({'class':'s'}, h.SPAN(o.s), " sec" ) );
	var lh=Math.floor(o.ms/10);
	return h.SPAN({'class':'TimeLen'}, h.SPAN({'class':'s'}, h.SPAN(o.s+ "."+ (lh<10?"0":"")+lh) ," sec" ) );
}

function RenderTimeLen(l)
{
	document.write(GetTimeLen(l));
}

// format short url
function FormatURL(url,a,b)
{
	if(url.substr(0,7)=="http://")
		url=url.substr(7);
	if(url.length>a+b)
		url=url.substr(0,a)+"..."+url.substr(url.length-b);
	return url;
}

// get time till next action (for playback page)
function GetTimeToNextLen(l)
{
	if(l<60000)
		return l/1000+" sec";
		
	var s=Math.floor(l/1000);
	l=l%1000;	
	if(s<10)
	{
		var lh=Math.floor(l/10);
		return s+"."+((lh < 10) ? "0" : "")+lh+" sec";
	}
	if(s<60)
		return s+" sec";

	var m=Math.floor(s/60);
	s=s%60;
	if(m<60)
		return m+" min"+ (s>0?" "+s+" sec":"");
		
	var h=Math.floor(m/60);
	m=m%60;
	if(h<24)
		return h+" hr "+ (m>0?" "+m+" min":"");

	var a=Math.floor(h/24);
	h=h%24;
	return a+(a==1?" day":" days")+(h>0?" "+h+" hr":"");
}


