/* 
 * Copyright (c) 2003 Autometa Corporation
 * All Rights Reserved
 * License:  Closed
 * Platform:  Autometa DOM (LGPL)
 */
var gAM_dom = null;

var gGridTop 		=	0					
  , gGridLeft 		=	30					
  , gGridHeight		=	800					
  , gGridRight 		=	gGridLeft + 690
  ;

function JI_onload ()
{
    AM_domId.prototype  =   new AM_dom      ( 'JRE_video_hover' )   ;
    // FIXME: workaround for Konqueror setTimeout()
    gAM_dom             =   AM_domId.prototype                      ;
    var _oTimeline      =   new AM_timeline    ( 'JRE_timeline' )	;
}

function AM_bar ( p_szState , p_szCaucus, p_szOpen )
{
	var _aSegments				=	new Array(0)					;
	var _iTotalCount			=	0								;
	var _szState				=	p_szState						;
	var _szCaucus				=	p_szCaucus						;
	var _szOpen					=	p_szOpen						;

	this.AddSegment = function ( p_szName , p_iCount, p_szColor )
	{
		_aSegments.push( { 
								name 	: p_szName 
							,	count	: p_iCount 
							,	color	: p_szColor 
						 } )										;

		_iTotalCount			+=	p_iCount						;
	};

	var _f_createDiv = function ( p_background , p_color, p_posX, p_posY , p_fontPt, p_textAlign, p_width )
	{
		var _div				=	document.createElement ( 'div' )	;
		_div.style.border		=	'none'								;
		_div.style.position		=	'absolute'							;
		_div.style.color		=	p_color								;
		_div.style.background	=	p_background 						;
		_div.style.top			=	p_posY 								;
		_div.style.height 		=	8									;
		_div.style.fontSize 	=	p_fontPt 							;
		_div.style.padding 		=	'3px'								;
		_div.style.paddingLeft	=	'4px'								;
		_div.style.width 		=	p_width + 'px'						;
		_div.style.left 		=	p_posX								;
		_div.style.textAlign	=	p_textAlign							;
		return _div														;
	};

	this.render = function ( p_node, p_posY, p_tl, p_firstBar )
	{
		var _segment														;
		var _left			=	p_tl.screen.start + 4						;
		var _width															;
		var _div															;
		var _secondBarCounter = 0											;

		while ( _segment = _aSegments.pop() )
		{
			_width 		=	( _segment.count / _iTotalCount )
							  * ( p_tl.screen.end - p_tl.screen.start )		;

			_div 		=	_f_createDiv ( _segment.color, 'black'
											, _left, p_posY
											, '6pt', 'left', _width )		;
			_div.innerHTML			=	_segment.name 						;
			p_node.appendChild ( _div )										;

			if ( _width > 30 )
			{
				_div 		=	_f_createDiv ( 'midnightblue', 'silver'
												, _left, p_posY+20
												, '7pt', 'right', _width )		;
				if ( p_firstBar != 1)
				{
					_secondBarCounter 	+=	_segment.count						;
					_div.innerHTML		=	AddCommas( _secondBarCounter ) + '&nbsp;' 	;
				}
				else
				{
					_div.innerHTML		=	AddCommas( _segment.count ) + '&nbsp;' 	;
				}
				p_node.appendChild ( _div )										;
			}

			_left		+=		_width										;
		}
	};

	this.popup = function ()
	{
		alert ('popup for ' + _szState + ' with count = ' + _iTotalCount );
	}

	this.dump = function ()
	{
		return _iTotalCount 										;
	};
}

function AM_timeline ( p_domID )
{
	var		_dom                =   new AM_domId ( p_domID )        ;
	var		_video_grid			=	new AM_domId ( 'grid_video' )	;
	var		_video_node			=	_video_grid.node				;

	this.datetime				=   { 
										  start	: 0 
										, end	: 130 
									}								;

	this.screen					=	{ 
										  start : gGridLeft+4
										, end	: gGridRight
									}								;

    this.AM_DOM                 =   _dom                            ;
    this.node                   =   this.AM_DOM.node				;
    if ( ! this.node ) return null                                  ;

	var		self				=	this						
		, 	_dS					=	this.node.style					;

	var _f_appendBold = function ( 		p_posX , p_posY 
									,	p_szText , p_szColor
									,	p_fontPt , p_node , p_width)
	{
		var _b 					=	document.createElement ( 'b' )			;
		_b.style.position		=	'absolute'								;
		_b.style.left 			=	p_posX									;
		_b.style.top 			=	p_posY 									;
		_b.style.fontSize		=	p_fontPt								;
		_b.style.color			=	p_szColor 								;
		if ( p_szText )
			_b.innerHTML			=	p_szText 							;
		if ( p_width )
			_b.style.width			=	p_width								;
		p_node.appendChild ( _b )											;
		_b = null															;
	};

	var _f_appendGridLine = function ( p_posY , p_node )
	{
		var _img				=	document.createElement ( 'img' )		;
		_img.src				=	'sp_gray.gif'							;
		_img.height				=	1										;
		_img.width				=	770 + 130								;
		_img.style.position		=	'absolute'								;
		_img.style.left			=	gGridLeft - 20							;
		_img.style.top			=	p_posY + 57								;
		p_node.appendChild ( _img )											;
		_img					=	null									;
	};

	var 	_head
		, 	_list
		, 	_item
		, 	_bar					
	 	,	_iBarIndex	=	0											
		,	_yPos 		=	gGridTop
		, 	_grid 		=	document.getElementById ( "AM_grid" )			;

	_grid.style.display 		=	"none"									;
	_grid.style.visibility 		=	"hidden"								;
	var _row 					=	_grid.firstChild						;
		
	do
	{
		if ( _row.nodeType != 1 )
			continue														;

		_head 		=	_row.firstChild										;
		while ( _head.nodeType != 1 )
			_head 	=	_head.nextSibling									;
		
		if ( ! _head.y )
			_head.y 	=	0												;

		var _color																
		 , _aTitle	=	_head.innerHTML.split ( ',' )						;

		_f_appendGridLine ( _yPos , this.node )								;

		// State
		_f_appendBold ( gGridLeft-20	, _yPos+61, _aTitle[0], 'white', '9pt', this.node )			;

		// Caucus or Primary
		_color 	=	_aTitle[1].indexOf ('Caucus') != -1 ? 'orange' : 'thistle'	;
		_f_appendBold ( gGridRight+25	, _yPos+83, _aTitle[1], _color	, '7pt', this.node )		;

		// Open or Closed
		if 		( _aTitle[2] == 'Open' ) 		_color 		=	'mediumseagreen'					;
		else if ( _aTitle[2] == 'Closed' ) 		_color 		=	'tomato'							;
		else 									_color 		=	'thistle'							;
		_f_appendBold ( gGridRight+85	, _yPos+83, _aTitle[2], _color	, '7pt', this.node )		;

		// mm/dd
		_f_appendBold ( gGridRight+145	, _yPos+83, _aTitle[3], 'silver', '7pt', this.node )		;

		// low/high record
		if ( _aTitle[4] )
		{
			if ( _aTitle[4].indexOf ('Low') != -1 )
				_color	=	'mediumorchid' 								;
			else if ( _aTitle[4].indexOf ('High') != -1 )
				_color	=	'crimson' 									;
			else 
				_color	=	'silver' 									;
			_f_appendBold ( gGridRight+25	, _yPos+127, _aTitle[4], _color, '7pt', this.node , 60);
		}

		_list 		=	_head.nextSibling								;
		while ( _list.nodeType != 1 )
			_list 		=	_list.nextSibling							;
		_item 		=	_list.firstChild								;

		var yBarPos 	=   _yPos + 40									;
		var _firstBar 	=	1											;

		do
		{
			if ( _item.nodeType != 1 )
				continue												;

			var szParms													
			  , aSprites	=	new Array								
			  , oBar 		=	new AM_bar ( 	_aTitle[0] 
											,	_aTitle[1]
											,	_aTitle[2] )			;
			yBarPos			+=	45										;
			_bar			=	_item.firstChild						;

			do
			{
				if ( _bar.nodeType != 1 )
					continue											;

				szParms =  _bar.innerHTML.split( ',' )					;
				oBar.AddSegment ( 
									szParms [0]					// name
								 ,	parseInt ( szParms [1] )	// count
								 ,	szParms [2]					// color
								)										;
			}
			while ( _bar = _bar.nextSibling )

			oBar.render (  		_video_node
							,	yBarPos										
							,	this
							,	_firstBar
						)													;

			if ( _firstBar == 1 ) 
				 _firstBar = 0												;
		}
		while ( _item = _item.nextSibling )

		_yPos		=	_yPos + 130											;
		_iBarIndex	=	_iBarIndex + 2										;
	}
	while ( _row = _row.nextSibling )

	_f_appendGridLine ( _yPos, this.node )									;

} // AM_timeline()

function printObj ( testObj )
{
    var szObj       =   ''                                      ;
    for ( var i in testObj )
        szObj       += 'obj[' + i + ']=' + testObj[i]           ;
    return szObj    ;
}

// Credit: Boniface Lau, 2002-08-22, Boniface_Lau@compuserve.com
// http://groups.google.com/groups?selm=u8z2ys6vp.fsf%40tsohlacol.com
function AddCommas( p_num) 
{
	var g  = String ( p_num )									;

	g =		g.split('').reverse().join('').
         	replace(/(\d{3})/g,"$1,").
         	split('').reverse().join('').
         	replace(/^,/,"")									;

	return g													;
}
