
Event.observe( window, 'load', onload_init_thumbs );
Event.observe( window, 'load', onload_videowire );
Event.observe( window, 'load', onload_change_category );

var hPEs = $H({});

function onload_change_category()
{
	$$('select.change-category').each(
		function(el)
		{
//alert('id = '+el.id);
			el.onchange = change_video_category;

		} );
}

function change_video_category()
{
//alert('id = ' + this.id);
	var iId = this.id.replace( /[^0-9]/g, '' );
//alert( 'iId = ' + iId );

	var cat = $F(this.id);
//alert( 'cat = ' + cat );

	if ( cat != 0 )
	{
		var args = 'f=change_category&i=' + iId + '&c=' + cat;
//alert( 'args = ' + args );
		send_request( '/change_video_category.php', args, cb_change_video_category );
	}
}
function cb_change_video_category()
{
}

function onload_init_thumbs()
{
//	$$('div.video-result div.left div.thumb').each(
	$$('#video-results div.video-result div.left div.thumb img',
		'#videowire-results div.videowire-results-item div.thumb img').each(
//	$$('#video-results div.video-result div.left').each(
		function(el)
		{
			el.onmouseover = onmouseover_thumb;
			el.onmouseout = onmouseout_thumb;
		} );

}

/*
function onmouseover_thumb()
{

	var aThumbs = this.getElementsBySelector('img');

	hPEs[this.id] = new PeriodicalExecuter(
		function(pe)
		{
			// Loop through thumbs. Move 'visible' style along one
			for ( var i=0; i < aThumbs.length; i++ )
			{
				var img = aThumbs[i];

				if ( img.hasClassName('visible') )
				{
					img.removeClassName('visible');
					var idx = (i+1) % aThumbs.length;
//alert('idx = ' + idx );
					aThumbs[idx].addClassName('visible');
					break;
				}
			}

		}, 1 );
//alert( 'a: hPEs = ' + hPEs[this.id] );
}
*/

function onmouseover_thumb()
{
	id = this.id;
	var iId = parseInt(this.id.replace( /^thumb-/, '' ));
//alert('aThumbSources[1] = ' + aThumbSources[1] );

	hPEs[this.id] = new PeriodicalExecuter(
		function(pe)
		{
			var idx = aThumbCurrent[iId];
//alert( 'iId = ' + iId + ', idx = ' + idx );
			idx++;
			idx %= aThumbSources[iId].length;
//alert( 'idx = ' + idx );

			$(id).src = aThumbSources[iId][idx];
//alert( 'src = ' + aThumbSources[iId][idx] );

			aThumbCurrent[iId] = idx;

		}, 0.85 );
//alert( 'a: hPEs = ' + hPEs[this.id] );
}

function onmouseout_thumb()
{
//alert( '1: hPEs = ' + hPEs.inspect() );
	hPEs[this.id].stop();
	hPEs.unset(this.id);
//alert( '2: hPEs = ' + hPEs.inspect() );

}

function onload_videowire()
{
	var aEls = $$('div.video-result h2 a', 'div.video-result div.thumb a');
//alert( 'aEls: ' + aEls );
	for ( var i=0; i < aEls.length; i++ )
	{
		aEls[i].onclick = video_search_result_onclick;
	}

	var aEls = $$('div.videowire-results-item div.headline a', 'div.videowire-results-item div.thumb a');
//alert( 'aEls: ' + aEls );
	for ( var i=0; i < aEls.length; i++ )
	{
		aEls[i].onclick = videowire_result_onclick;
	}


	$$('#videowire-results div.timeline-label').each(
		function(el)
		{
			el.onmouseover = timeline_onmouseover;
			el.onmouseout = timeline_onmouseout;
		});

	if ( $('videowire-timeline-icon') )
	{
		var hHook = { target: 'topRight', tip: 'topLeft' };
		new Tip( $('videowire-timeline-icon'), "h = hours<br/>d = days<br/>w = weeks<br/>m = months<br/>y = years", { title: "Timeline", hook: hHook } );
	}

	$$('#videowire-results div.videowire-results-item div.timeline-label').each(
		function(el)
		{
			var tiptext = el.up().getElementsByClassName('data-tip-text')[0].value;
//alert( 'tiptext = ' + tiptext );

			var hHook = { target: 'topRight', tip: 'topLeft' };
			new Tip( el, tiptext, { hook: hHook } );
		});


}

function timeline_onmouseover()
{
	this.addClassName( 'timeline-label-highlight' );
	var oInputs = this.getElementsByClassName('data-within');

	if ( oInputs.length == 0 )
		return;
//alert( 'within = ' + within );
	var within = $F(oInputs[0]);


	$$('#videowire-results div.timeline-'+within).each(
		function(el)
		{
			el.addClassName('highlight');
		});
}

function timeline_onmouseout()
{
	this.removeClassName( 'timeline-label-highlight' );
	var oInputs = this.getElementsByClassName('data-within');

	if ( oInputs.length == 0 )
		return;
//alert( 'within = ' + within );
	var within = $F(oInputs[0]);

	$$('#videowire-results div.timeline-'+within).each(
		function(el)
		{
//			el.style.backgroundColor = '#ffffff';
			el.removeClassName('highlight');
		});
}

function video_search_result_onclick()
{
	var args = 'url=' + this.href + '&qid=' + QID + '&t=vs';
//alert( 'args = ' + args );
	send_request( '/click.php', args, cb_video_search_result_onclick );
}

function cb_video_search_result_onclick()
{
}

function videowire_result_onclick()
{
	var args = 'url=' + this.href + '&qid=0' + '&t=vw';
//alert( 'args = ' + args );
	send_request( '/click.php', args, cb_videowire_result_onclick );
}

function cb_videowire_result_onclick()
{
}



