document.onmousedown 	= mousedown;
document.onmouseup 		= mouseup;
document.onmousemove 	= mousemove;
document.onselectstart 	= rfalse;

var BAR_SEARCH			=	0;
var BAR_PLAY			=	1;
var m_BarMode			=	BAR_PLAY;	

var activeElements = new Array();
var activeElementCount = 0;

var lTop, lLeft;
var doMove = true ;
var doResize = false;

var upper=5;
var dwPos = 0;
function mousedown() {
	var pr;
	var mp = findMoveable(window.event.srcElement);
	if ((mp)&&(mp.id!="Search")) return;
	
	if (mp) pr= mp.style.zIndex;
		else pr = 1;
	
	if (mp) mp.style.zIndex =upper;	
	upper++;
	
	if (!window.event.altKey) {
		for (i=0; i<activeElementCount; i++) {
		if (activeElements[i] != mp) {
		  activeElements[i].style.borderWidth = "0px";
		}
	 }
	
	 if (mp) {
	   activeElements[0] = mp;
	   activeElementCount = 1;
	   mp.style.borderWidth = "0px";
	 } else {
	   activeElementCount = 0;
	 }
	} else {
	 if (mp) {
	   if (mp.style.borderWidth != "4px") {
		 activeElements[activeElementCount] = mp;
		 activeElementCount++;
		 mp.style.borderWidth = "4px";
	   }
	 }
	}
	
	lLeft = window.event.x;
	lTop = window.event.y;
	dwPos = m_pChnlBar.m_dwPos;
}

function mouseup()
{
	activeElementCount = 0;
	if ( m_connected == false || m_Mode == MODE_MONITOR)
  		return;

	if ( m_BarMode == BAR_SEARCH )
	{		
		if ( dwPos != m_pChnlBar.m_dwPos)
		{
			SetSliderTime();
			window.setTimeout( "m_BarMode = BAR_PLAY;", 1000);
		}
		else m_BarMode = BAR_PLAY;
	}
}
function mousemove() {
  var i, dLeft, dTop;

  if (window.event.button == 1) {

	sx = window.event.x;
	sy = window.event.y;

	dLeft = sx - lLeft;
	dTop = sy - lTop;

	for (i=0; i<activeElementCount; i++) {
		moveElement(activeElements[i], dLeft, dTop);
	}

	lLeft = sx;
	lTop = sy;

	m_BarMode = BAR_SEARCH;
	m_pChnlBar.ChangePos( );
//	window.status = String(m_pChnlBar.piHour + ':' + m_pChnlBar.piSec);
	
	return false;
  }

}

function moveElement(mp, dLeft, dTop) {
	var e;

	e = mp;
	if (dLeft > 0)
		{
		if (e.style.posLeft + dLeft > Number(e.begin)+Number(e.MAX) ) e.style.posLeft = Number(e.begin)+Number(e.MAX);
		else	
			e.style.posLeft += dLeft;

	}
	else
		{
		if (e.style.posLeft + dLeft < Number(e.begin) ) e.style.posLeft = Number(e.begin);
		else	
			e.style.posLeft += dLeft;
		}
}


function findMoveable(e) { 

  if (e.moveable == 'true')
	return e;

  if (e.tagName == "BODY")
	   {return null;}

  return findMoveable(e.parentElement);
}

function rfalse() {
  return false;
}

function SetSliderTime()
{
	var iHour = 0, iSec = 0;
	m_pChnlBar.GetPos();	
	iHour 	= m_pChnlBar.piHour;
	iSec	= m_pChnlBar.piSec;

 	var dwTime = curDate + iHour*60*60 + iSec;
 	WebVw.SetTime( dwTime - m_UTC * 60);
}


function CSearchBar()
{
	this.m_dwCamNum		=	0;
	this.m_dwLength		=	500;

//	this.Update			=	Update_CSearchBar;
	this.OnPaint		=	OnPaint_CSearchBar;
	this.OnPaint2		=	OnPaint_CSearchBar2;	
	this.Create			=	Create_CSearchBar;
	this.Set			=	Set_CSearchBar;
	this.Clear			=	Clear_CSearchBar;

	// in 1 hour  = 16 interval  
	// abd 1 hour = 32 pixel
	// from this - mistake = 3.45 minute
	this.interval 		=	10*24;
	this.colorfon		=	"#808080";
	this.colorch		=	"#80FF80";

	this.alpha			=	true;
}

function Create_CSearchBar(ch)
{
//var top		=	Number(document.SearchTD_Top)  + 16;
//var left		=	Number(document.SearchTD_Left) + 18;
//alert(left + "  "  + top);
//var top		=	 t + 22;
//var left		=	 l + 16;
//var s = "<span style=\"position:absolute;left:"+left+"px;top:"+top+"px;\">";
var s = "";
for(var i=0;i<this.interval;i++)
{
	s += "<span id=sb"+Number(i+ch*240)+" style=\"position:relation;background-color:"+this.colorfon+";width:2;height:6;border-width:0;font-size:0;"+ (this.alpha ? "filter:progid:DXImageTransform.Microsoft.Alpha(opacity=50);" : "") +"\"></span>"
}
return s;
}


function Clear_CSearchBar( ch)
{
	for(var i=0;i<this.interval;i++)	
		eval("document.all['sb"+Number(i+ch*240)+"'].style.backgroundColor=\""+this.colorfon+"\";");
}
/*
function Update_CSearchBar(dw)
{
	return 0;
}
*/
function Set_CSearchBar(n,color, ch)
{
//	eval("document.all['sb"+Number(n+ch*240)+"'].style.backgroundColor=\""+color+"\";");
	document.all['sb'+(n+ch*240)].style.backgroundColor=color;
}

function OnPaint_CSearchBar(ch)
{
	for (var i = 0; i < 24; i++) 
	{
		for (j = 0; j < 10; j++)
		{
			var b = m_ptrLog[ ch*240 + i*10 + j];	
			this.Set((i*10) + j,(b ? this.colorch : this.colorfon), ch);
		}
	}
}

function OnPaint_CSearchBar2(rch, ch)
{
	for (var i = 0; i < 24; i++) 
	{
		for (j = 0; j < 10; j++)
		{
			var b = m_ptrLog[ ch*240 + i*10 + j];	
			this.Set((i*10) + j,(b ? this.colorch : this.colorfon), rch);
		}
	}
}

function POINT()
{
	this.x 	= 0;
	this.y	= 0;
}


function CArrowCtrl(search) 
{
	this.m_dwPos		=	0;
	this.m_dwBegPos		=	0;
	this.m_dwEndPos		=	0;
	this.m_bMoving		=	false;
	this.m_ptMoving		=	new POINT();
	//HRGN m_hrgn;
	this.m_bEnable		=	false;
	
	this.m_iHour		=	0;
	this.m_iSec			=	0;

	this.piHour			=	0;
	this.piSec			=	0;

	this.m_Arrow		=	search;

//	this.OnPaint		=	OnPaint_CArrowCtrl;
//	this.OnCreate		=	OnCreate_CArrowCtrl;
	this.SetPos			=	SetPos_CArrowCtrl;
	this.GetPos			=	GetPos_CArrowCtrl;
	this.ChangePos		=	ChangePos_CArrowCtrl;
	
//	this.Update			=	Update_CArrowCtrl;
	this.GetEnable		=	GetEnable_CArrowCtrl;
	this.SetEnable		=	SetEnable_CArrowCtrl;
	this.OnResize		=	OnResize_CArrowCtrl;
};

function ChangePos_CArrowCtrl( )
{
  with(this)
	{
		m_dwPos = Search.style.posLeft - Search.begin;
		m_iHour = Math.floor( m_dwPos/20);
		m_iSec = (m_dwPos - (m_iHour*20)) * 180;

		m_Time = curDate + m_iHour * 60*60 + m_iSec;
  	}
}

function GetEnable_CArrowCtrl()
{ return this.m_bEnable;}

function SetEnable_CArrowCtrl()
{this.m_bEnable = true;}
/*
function Update_CArrowCtrl(dw)
{
with(this)
{
	if (dw == BAR_SEARCH){
		m_bEnable = true;
		//SetRange(158,758);
		SetPos(m_iHour,m_iSec);
	}

	if (dw == BAR_PLAY){
		m_bEnable = true;
		//SetRange(158,648);
		SetPos(m_iHour,m_iSec);
	}
	if (dw == 2)
	{
		//SetRange(158,758);
		m_bEnable = true;
		SetPos(0,0);
		m_bEnable = true;
	}
}
}
*/
function SetPos_CArrowCtrl(iHour,iSec)
{
with(this)
{
	//RECT rc;
	//GetWindowRect(m_hWnd,&rc);
	//ScreenToClient( &rc);
	
	if (m_BarMode == BAR_SEARCH && m_bEnable)
	{
		m_dwPos = Math.floor(iHour)*20 +  Math.round(iSec/180);
//		if (!m_Arrow.moving) 
//			m_Arrow.style.posLeft = m_Arrow.begin + m_dwPos;
	}

	if (m_BarMode == BAR_PLAY)
	{
		m_dwPos = Math.floor(iHour)*20 +  Math.round(iSec/180);
//		window.status = ( iHour + " " + iSec + " " + m_dwPos);
		if (!Search.moving) 
			Search.style.posLeft = Number( Search.begin) + m_dwPos;
	}

	m_iHour = iHour;
	m_iSec = iSec;
}
}

function OnResize_CArrowCtrl()
{
	Search.style.posLeft = Number( Search.begin) + this.m_dwPos;
}

function GetPos_CArrowCtrl()
{
with(this)
{
	if (m_BarMode == BAR_SEARCH)
		{
		  piHour = Math.floor( m_dwPos/20);
		  piSec =  (m_dwPos - (m_iHour*20)) * 180;
		}
	if (m_BarMode == BAR_PLAY)
		{
			piHour = m_iHour;
			piSec = m_iSec;
		}
}		
}


