/* Obtencion del XMLHttpRequest */

var ajax = createAJAXObject( );
var onrequest = false;

function createAJAXObject( urltogo )
{  
	var obj;
	if( window.XMLHttpRequest ) // no es IE
	{ 	  
		obj = new XMLHttpRequest( );
	}
	else // Es IE o no tiene el objeto 
	{ 	  
		try 
		{   
			obj = new ActiveXObject("Microsoft.XMLHTTP");  
		}
		catch ( e )
		{  
			alert( 'El navegador utilizado no está soportado' );
			if ( urltogo != null )
			{
				window.location = urltogo;
			}
		}  
	}  
	return obj;  
}

function getNextPromocionado( )
{
	if ( ajax != null && !onrequest )
	{
		var query = 'promocionados.php?time=' + new Date( ).getTime( );
		ajax.open( 'GET', query, true );
		ajax.onreadystatechange = getNextPromocionadoResponse;
		ajax.send( '' );
		onrequest = true;
	}
}

function getNextPromocionadoResponse( )
{
	if ( ajax != null && ajax.readyState == 4 )
	{
		if ( ajax.status == 200 )
		{
			var promobj = null;
			try
			{
				promobj = json_parse( ajax.responseText );
				if ( promobj.length > 0 )
				{
					actualizaPromocionados( promobj[ 0 ].nombre, promobj[ 0 ].imagen, promobj[ 0 ].videofile, promobj[ 0 ].descripcion );
				}
			}
			catch ( e )
			{
				/*alert( "Error = " + e.name + "\nMessage = " + e.message + "\nAt = " + e.at + "\nText = " + e.text );*/
			}
		}
		else
		{
			alert( "Error retrieving AJAX data: " + ajax.status );
		}
		onrequest = false;
	}
}

function getAfiliados( provinciaid, poblacionid, categoriaid, nombre, action )
{
	if ( ajax != null && !onrequest )
	{
		var query = 'ajax.php';
		var sep = '?';
		if ( provinciaid != null )
		{
			query += sep + 'provincia=' + provinciaid;
			sep = '&';			
		}
		if ( poblacionid != null )
		{
			query += sep + 'poblacion=' + poblacionid;
			sep = '&';			
		}
		if ( categoriaid != null )
		{
			query += sep + 'categoria=' + categoriaid;
			sep = '&';			
		}
		if ( nombre != null )
		{
			query += sep + 'nombre=' + nombre;
			sep = '&';			
		}
		if ( action != null )
		{
			query += sep + 'action=' + action;
			sep = '&';			
		}
		query += sep + 'dummy=' + new Date( ).getTime( );
		/*alert( query );*/
		ajax.open( 'GET', query, true );
		ajax.onreadystatechange = getAfiliadosResponse;
		ajax.send( '' );
		onrequest = true;
	}
}

function getAfiliadosResponse( )
{
	if ( ajax != null && ajax.readyState == 4 )
	{
		if ( ajax.status == 200 )
		{
			var afobj = null;
			try
			{
				afobj = json_parse( ajax.responseText );
				var action = afobj.action;
				if ( action == 1 ) populateMap( afobj.afiliados );
				else if ( action == 2 ) populateCategoryList( afobj.afiliados );
				else if ( action == 3 ) populateSearchResults( afobj.afiliados );
				else alert( 'unknown action: ' + action );
			}
			catch ( e )
			{
				/*alert( "Error = " + e.name + "\nMessage = " + e.message + "\nAt = " + e.at + "\nText = " + e.text );*/
			}
		}
		else
		{
			alert( "Error retrieving AJAX data: " + ajax.status );
		}
		onrequest = false;
	}
}

function populateMap( afarr )
{
	var mapa = document.getElementById( "mapa" );
	if ( mapa != null )
	{
		var parent = mapa.parentNode;
		clearchilds( parent );
		parent.appendChild( mapa );
		var il = afarr.length;
		var i;
		for ( i = 0; i < il; i++ )
		{
			var icono = afarr[ i ];
			if ( (icono.provinciaid == provsel) && (icono.poblacionid == poblsel) )
			{
				var a = document.createElement( 'a' );
				a.setAttribute( "href", "javascript:showVideo('" + icono.videofile + "');" );
				var img = document.createElement( 'img' );
				img.setAttribute( "galleryimg", "no" );
				img.setAttribute( "src", findcategoriabyid( icono.categoriaid ).icono );
				img.textoIcono = icono.descripcion;
				if ( img.addEventListener )
				{
					img.addEventListener( "mouseover", function( event ) { return overlib( event.target.textoIcono ); }, false );
					img.addEventListener( "mouseout", function() { return nd( ); }, false );
				}
				else if ( img.attachEvent )
				{
					img.attachEvent( "onmouseover", function( event ) { return overlib( event.srcElement.textoIcono ); } );
					img.attachEvent( "onmouseout", function() { return nd( ); } );
				}
				img.style.border = 0;
				a.appendChild( img );
				var div = document.createElement( 'div' );
				div.setAttribute( "style", "position:absolute;z-index:4;width:27px;height:27px;left:" + icono.x + "px;top:" + icono.y + "px;" );
				div.style.position = "absolute";
				div.style.zIndex = 4;
				div.style.left = icono.x;
				div.style.top = icono.y;
				div.style.width = 27;
				div.style.height = 27;
				div.appendChild( a );
				parent.appendChild( div );
			}
		}
	}
}

function populateCategoryList( afarr )
{
	var	descripcioncategorias = document.getElementById( "descripcioncategorias" );
	clearchilds( descripcioncategorias );
	var la = afarr.length;
	if ( la > 0 )
	{
		var i;
		for ( i = 0; i < la; i++ )
		{
			descripcioncategorias.appendChild( createAfiliadoItem( afarr[ i ] ) );
		}
	}
	else
	{
		descripcioncategorias.appendChild( createNoResultsMessage( "No se han encontrado resultados" ) );
	}
}

function populateSearchResults( afarr )
{
	var listaafiliados = document.getElementById( "listaafiliados" );
	clearchilds( listaafiliados );	
	var la = afarr.length;
	if ( la > 0 )
	{
		var i;
		for ( i = 0; i < la; i++ )
		{
			listaafiliados.appendChild( createAfiliadoItem( afarr[ i ] ) );
		}
	}
	else
	{
		listaafiliados.appendChild( createNoResultsMessage( "No se han encontrado resultados" ) );
	}
}

function createAfiliadoItem( afiliado )
{
	var p = document.createElement( 'p' );

	var img = document.createElement( 'img' );
	img.setAttribute( "galleryimg", "no" );
	img.src = afiliado.imagen;
	p.appendChild( img );

	var spantitulo = document.createElement( 'span' );
	spantitulo.className = "titulo";
	var a = document.createElement( 'a' );
	a.href = afiliado.direccion;
	a.appendChild( document.createTextNode( afiliado.nombre ) );
	spantitulo.appendChild( a );
	p.appendChild( spantitulo );
	p.appendChild( document.createElement( 'br' ) );
	var spansubtitulo = document.createElement( 'span' );
	spansubtitulo.className = "subtitulo";
	var a = document.createElement( 'a' );
	a.href = "javascript:showVideo('" + afiliado.videofile + "')";
	a.appendChild( document.createTextNode( "Mostrar video" ) );
	spansubtitulo.appendChild( a );
	p.appendChild( spansubtitulo );
	p.appendChild( document.createElement( 'br' ) );
	p.appendChild( document.createTextNode( afiliado.descripcion ) );
	return p;
}

function createNoResultsMessage( message )
{
	var p = document.createElement( 'p' );
	p.style.textAlign = 'center';
	var spantitulo = document.createElement( 'span' );
	spantitulo.className = "titulo";
	spantitulo.appendChild( document.createTextNode( message ) );
	p.appendChild( spantitulo );
	return p;
}

function clearchilds( toclear )
{
	var childs = toclear.childNodes;
	var lc = childs.length;
	var i;
	for ( i = 0; i < lc; i++ )
	{
		toclear.removeChild( childs.item( 0 ) );
	}
}
