//-----------------------------------------------------------------------------
// FIRST 10 RECORDS IN LIST/SEARCH RESULTS

var first_page = new Array();

function addFirstPage( id_file )
{
	first_page.push( id_file );
}

//-----------------------------------------------------------------------------

function redirectTo( url )
{
	document.location.href = url; 
}

//-----------------------------------------------------------------------------
// Single Program Compare

function checkCompare( ignore )
{
	setCookie( 'compare_first_records', '#' + first_page.join( '#' ) );

	var url = '/compare.phtml?action=compare_selected';

	if( ignore == true )
		url = url + '&ignore=1';
	
	redirectTo( url );
}

//-----------------------------------------------------------------------------

function checkCompareAlert()
{
	var cookie_programs = getCookie( 'compare_programs' );
	var cookie_value	= cookie_programs.split( '#' );

	if( cookie_value.length <= 2 )
		alert( 'ĄDebes seleccionar al menos 2 programas para poder comparar!' );
	else
		checkCompare( true );
}

//-----------------------------------------------------------------------------
// Compare ALL records

function checkCompareAll()
{
	setCookie( 'compare_first_records', '#' + first_page.join( '#' ) );

	redirectTo( "/compare.phtml?action=compare_all&ignore=1" );
}

//-----------------------------------------------------------------------------
// Compare FREE records

function checkCompareFree()
{
	setCookie( 'compare_first_records', '#' + first_page.join( '#' ) );

	redirectTo( "/compare.phtml?action=compare_free&ignore=1" );
}

//-----------------------------------------------------------------------------
// Compare POPULAR records

function checkComparePopular()
{
	setCookie( 'compare_first_records', '#' + first_page.join( '#' ) );

	redirectTo( "/compare.phtml?action=compare_popular&ignore=1" );
}

//-----------------------------------------------------------------------------
// EXPLICITS IDS

var explicits_ids = new Array();

function checkCompareFromFile( id_file )
{
	var current_programs	= getCookie( 'compare_programs' );
	var current_ids			= current_programs.split( '#' );
		current_ids.shift();
		current_ids.push( id_file );

	if( current_ids.length < 2 )
		current_ids = current_ids.concat( explicits_ids );

	setCookie( 'compare_programs', '#' + current_ids.join( '#' ) );

	checkCompare( false );
}

//-----------------------------------------------------------------------------

function defineDisable( id_obj, disabled )
{
	try
	{
		document.getElementById( id_obj ).disabled = disabled;
	}
	catch (e)
	{
		// Object doesn't exists
	}
}

function changeClassName( id_obj, className )
{
	try
	{
		document.getElementById( id_obj ).className = className;
	}
	catch (e)
	{
		// Object doesn't exists
	}
}

function excludeFile( id_file )
{
	var value = getCookie( 'compare_exclude' );

	var ids	= value.split( '#' );

	var found = false;

	for ( var i = 0; ( i < ids.length ) && ( found == false ); i++ )
		if ( ids[i] == id_file )
			found = true;

	if ( found == false )
	{
		ids.push( id_file );

		value = ids.join( '#' );

		setCookie( 'compare_exclude', value );
	}
}

function adjustHeight( row_ids )
{
	var rows	= row_ids.split( '#' );
	var row		= '';
	var ids		= new Array();
	var img		= new Image();
	var max		= 0;

	for( var r = 0; r < rows.length; r++ )
	{
		ids = rows[r].split( '-' );
		max = 0;

		for( var i = 0; i < ids.length; i++ )
		{
			try
			{
				img.src = document.getElementById( 'screenshot_' + ids[i] ).src;
				
				if( img.height >= max )
					max = parseInt( img.height ) + 10;
			}
			catch (e)
			{
				// Program excluded
			}
		}

		for( var i = 0; i < ids.length; i++ )
		{
			try
			{
				document.getElementById( 'cell_' + ids[i] ).style.height = max + 'px';
			}
			catch (e)
			{
				// Program excluded
			}
		}
	}
}
