// Preload Images
img1 = new Image(16, 16);  
img1.src="images/spinner.gif";

img2 = new Image(220, 19);  
img2.src="images/loader-bar.gif";

var actualoffset=0;
var actualoffsetAlawar=0;
var reqespecialpage;
var reqespecialpageAlawar;
var reqlatestpage;
var reqcloud;
var reqbuscador;
var reqdetailbuscador;
var actualclave;
var actualclaveId;
var actualtiporel;

	window.addEvent('domready', function() {
		
		if($('nextgameblock')){
			$('nextgameblock').addEvent('click', function(){
				actualoffset++;
				reqespecialpage.send({method: 'post', data: 'clave='+actualclave+'&offset='+actualoffset})
			    //alert('clicked!');
			    
			});

		}
		
		if($('prevgameblock')){
			$('prevgameblock').addEvent('click', function(){
				actualoffset--;
				reqespecialpage.send({method: 'post', data: 'clave='+actualclave+'&offset='+actualoffset})
			    //alert('clicked!');
			    
			});

		}
		
		if($('nextgameblock_alawar')){
			$('nextgameblock_alawar').addEvent('click', function(){
				actualoffsetAlawar++;
				reqespecialpageAlawar.send({method: 'post', data: 'clave='+actualclaveId+'&tiporel='+actualtiporel+'&offset='+actualoffsetAlawar})
			    //alert('clicked!');
			    
			});

		}
		
		if($('prevgameblock_alawar')){
			$('prevgameblock_alawar').addEvent('click', function(){
				actualoffsetAlawar--;
				reqespecialpageAlawar.send({method: 'post', data: 'clave='+actualclaveId+'&tiporel='+actualtiporel+'&offset='+actualoffsetAlawar})
			    //alert('clicked!');
			    
			});

		}
		
		if($('nextlatestgameblock')){
			$('nextlatestgameblock').addEvent('click', function(){
				actualoffset++;
				reqlatestpage.send({method: 'post', data: 'offset='+actualoffset})
			    //alert('clicked!');
			});

		}
		
		if($('prevlatestgameblock')){
			$('prevlatestgameblock').addEvent('click', function(){
				actualoffset--;
				reqlatestpage.send({method: 'post', data: 'offset='+actualoffset})
			    //alert('clicked!');
			});

		}
		
		if($('refresh-esp')){
			$('refresh-esp').addEvent('click', function(){
				reqcloud.send()
			    //alert('clicked!');
			});
		}

		if($('login')){
			
			$('login').addEvent('submit', function(e) {
				// Prevents the default submit event from loading a new page.
				e.stop();
		
				// Show the spinning indicator when pressing the submit button...
				$('ajax_loading').setStyle('display','block');
		
				// Hide the submit button while processing...
				$('submit').setStyle('display','none');
		
				// Set the options of the form's Request handler.
				// ("this" refers to the $('login') element).
				this.set('send', { onComplete: function(response) {
					$('ajax_loading').setStyle('display','none');
		
					if(response == 'OK'){
						$('login_response').set('html', '');
						/*$('status').set('html', '<div id="logged_in">You are now logged it! <br />' +
			            '<img align="absmiddle" src="images/loader-bar.gif">' +
			            '<br /> Please wait while we redirect you to your private page...</div>');*/
			              
			            if($('contacto-page')){$('main-login-div').set('html','<div id="logged-user-div" class="titbox"><table border="0" cellpadding="0" cellspacing="0"><tr><td><img src="/games/images/identifiedusericon.png"></td><td class="mini_negro">&nbsp;&nbsp;'+$('user').get('value')+'</td></tr></table></div><div id="main-menu"><ul><li><a href="/games/micuenta.html">Mi Cuenta</a></li><li><a href="/games/juegos_puntuables">Puntuables</a></li><li><a href="/games/includes/logout.php">Desconectar</a></li></ul></div>');}
			            if($('micuenta-page')){window.location.reload();}
			            if($('user-aceptado')){
			              	$('status').set('html', '<div id="logged_in">Ya está logeado! <br />' +
			              		'<img align="absmiddle" src="images/loader-bar.gif">' +
			              		'<br /> Por favor espere mientras redirigimos el navegador a su cuenta personal en redribera...</div>');
			            	setTimeout('go_to_private_page()', 3000);
			            }
			            if($('playgame-page')){window.location.reload();}
			            //if($('indexgame-page')){$('main-login-div').set('html','<div id="logged-user-div" class="titbox"><table border="0" cellpadding="0" cellspacing="0"><tr><td><img src="/games/images/identifiedusericon.png"></td><td class="mini_negro">&nbsp;&nbsp;'+$('user').get('value')+'</td></tr></table></div><div id="main-menu"><ul><li><a href="/games/micuenta.html">Mi Cuenta</a></li><li><a href="/games/juegos_puntuables">Puntuables</a></li><li><a href="/games/includes/logout.php">Desconectar</a></li></ul></div>');}
			            if($('indexgame-page')){window.location.reload();}
			            if($('especial-page')){window.location.reload();}
						//setTimeout('go_to_private_page()', 3000);
					}
					else
					{
					  $('login_response').set('html', response);
					  // Show the login button
					  $('submit').setStyle('display','block');
					}
				}});
		
				// Send the form.
				this.send();
			});
		}
		
		
		if($('especial-games-page')){
			reqespecialpage = new Request({ 
				url: 'includes/especialpages.php',
				encoding: "ISO-8859-1",
				onRequest: function() {
 			    	$('especialpagesinprogress').setStyle('display','block');
 			    	$('especial-games-page').set('html', "");
      			},
      			onComplete: function(){
      				$('especialpagesinprogress').setStyle('display','none');
      			},
				onSuccess: function(txt,xml){
					$('especial-games-page').set('html', txt);
					if($('nextgameblock')){
						$('nextgameblock').addEvent('click', function(){
							actualoffset++;
							reqespecialpage.send({method: 'post', data: 'clave='+actualclave+'&offset='+actualoffset})
						    //alert('clicked!');
						    
						});

					}
					
					if($('prevgameblock')){
						$('prevgameblock').addEvent('click', function(){
							actualoffset--;
							reqespecialpage.send({method: 'post', data: 'clave='+actualclave+'&offset='+actualoffset})
						    //alert('clicked!');
						    
						});

					}
				},
				// Our request will most likely succeed, but just in case, we'll add an
				// onFailure method which will let the user know what happened.
				onFailure: function(xhr){
					$('especial-games-page').set('html', 'The request failed.');
				}
			});
		}
		
		if($('especial-games-page-alawar')){
			reqespecialpageAlawar = new Request({ 
				url: 'includes/especialpages_alawar.php',
				encoding: "ISO-8859-1",
				onRequest: function() {
 			    	$('especialpagesinprogressAlawar').setStyle('display','block');
 			    	$('especial-games-page-alawar').set('html', "");
      			},
      			onComplete: function(){
      				$('especialpagesinprogressAlawar').setStyle('display','none');
      			},
				onSuccess: function(txt,xml){
					$('especial-games-page-alawar').set('html', txt);
					if($('nextgameblock_alawar')){
						$('nextgameblock_alawar').addEvent('click', function(){
							actualoffsetAlawar++;
							reqespecialpageAlawar.send({method: 'post', data: 'clave='+actualclaveId+'&tiporel='+actualtiporel+'&offset='+actualoffsetAlawar})
						    //alert('clicked!');
						    
						});

					}
					
					if($('prevgameblock_alawar')){
						$('prevgameblock_alawar').addEvent('click', function(){
							actualoffsetAlawar--;
							reqespecialpageAlawar.send({method: 'post', data: 'clave='+actualclaveId+'&tiporel='+actualtiporel+'&offset='+actualoffsetAlawar})
						    //alert('clicked!');
						    
						});

					}
				},
				// Our request will most likely succeed, but just in case, we'll add an
				// onFailure method which will let the user know what happened.
				onFailure: function(xhr){
					$('especial-games-page-alawar').set('html', 'The request failed.');
				}
			});
		}
		
		if($('latest-games-page')){
			reqlatestpage = new Request({ 
				url: 'includes/latestgamespages.php',
				encoding: "ISO-8859-1",
				onRequest: function() {
 			    	$('latestpagesinprogress').setStyle('display','block');
 			    	$('latest-games-page').set('html', "");
      			},
      			onComplete: function(){
      				$('latestpagesinprogress').setStyle('display','none');
      			},
				onSuccess: function(txt,xml){
					$('latest-games-page').set('html', txt);
					if($('nextlatestgameblock')){
						$('nextlatestgameblock').addEvent('click', function(){
							actualoffset++;
							reqlatestpage.send({method: 'post', data: 'offset='+actualoffset})
						    //alert('clicked!');
						});

					}
					
					if($('prevlatestgameblock')){
						$('prevlatestgameblock').addEvent('click', function(){
							actualoffset--;
							reqlatestpage.send({method: 'post', data: 'offset='+actualoffset})
						    //alert('clicked!');
						    
						});

					}
				},
				// Our request will most likely succeed, but just in case, we'll add an
				// onFailure method which will let the user know what happened.
				onFailure: function(xhr){
					$('latest-games-page').set('html', 'The request failed.');
				}
			});
		}
		
		if($('cloud')){
			reqcloud = new Request({ 
				url: '/games/paneles/especiales-tagcloud.php',
				encoding: "ISO-8859-1",
				onRequest: function() {
 			    	$('relacionadosinprogress').setStyle('display','block');
 			    	$('cloud').set('html', "");
      			},
      			onComplete: function(){
      				$('relacionadosinprogress').setStyle('display','none');
      			},
				onSuccess: function(txt,xml){
					$('cloud').set('html', txt);
					if($('refresh-esp')){
						$('refresh-esp').addEvent('click', function(){
							reqcloud.send({method: 'post', data: 'r=1'})
						    //alert('clicked!');
						});

					}
				},
				// Our request will most likely succeed, but just in case, we'll add an
				// onFailure method which will let the user know what happened.
				onFailure: function(xhr){
					$('cloud').set('html', 'The request failed.');
				}
			});
		}
		
		if($('folder-search-link')){
			$('folder-search-link').addEvent('click', function(){
				$('category-search-gidiv').setStyle('display','block');
				var myMorph = new Fx.Morph($('category-search-gidiv'), {duration: 200,  transition: Fx.Transitions.Sine.easeOut});
			    myMorph.start({
			      'width': '250px',
			      'height': '470px',
			      'opacity': '0.95'
			    });
			    /*
				$('headerg').setStyle('opacity', 0.2);
				$('main-body').setStyle('opacity', 0.2);
				$('footer').setStyle('opacity', 0.2);
				$('logbar').setStyle('opacity', 0.2);
				*/
			    
			    reqbuscador = new Request({ 
					url: '/games/includes/buscador.php',
					encoding: "ISO-8859-1",
					onRequest: function() {
						$('category-search-gidiv').set('html', 'cargando datos...');
	      			},
	      			onComplete: function(){
	      				$('category-search-gidiv').set('html', '');
	      			},
					onSuccess: function(txt,xml){
						$('category-search-gidiv').set('html', txt);
						if($('hide-ppal')){
							$('hide-ppal').addEvent('click', function(){
								$('category-search-gidiv').setStyle('display','none');
								$('category-search-gidiv').setStyle('width','0px');
								$('category-search-gidiv').setStyle('height','0px');
								if($('detail-search-gidiv')){
									$('detail-search-gidiv').setStyle('display','none');
									$('detail-search-gidiv').setStyle('width','0px');
									$('detail-search-gidiv').setStyle('height','0px');
								}
							});

						}
						
					},
					// Our request will most likely succeed, but just in case, we'll add an
					// onFailure method which will let the user know what happened.
					onFailure: function(xhr){
						$('category-search-gidiv').set('html', 'The request failed.');
					}
				});
				
				reqbuscador.send();
			
			
			});
		}
});

function sh_ginfo(id,shortdesc,seccion,visitas,tiempo,titulo){
	if($('gidiv')){
		
		var pos = getElementAbsolutePos($('gi'+id));    
		
		$('gidiv').setStyle('display','block');
		$('gidiv').setStyle('left',pos.x+10);
		$('gidiv').setStyle('top',pos.y+100);
		$('gidiv').set('html', '<span class="gameBox_titulo">'+unescape(titulo)+'</span><br/><span style="color:red;">'+seccion+'</span><br/><span class="mini_negro" style="color:#69C;">'+visitas+' veces jugado en '+tiempo+'</span><br/>'+unescape(shortdesc)+'...');
		//alert(pos.x);
	}
}

function sh_ginfo_alawar(id,shortdesc,seccion,visitas,tiempo,titulo){
	if($('gidiv')){
		
		var pos = getElementAbsolutePos($('gialawar'+id));    
		
		$('gidiv').setStyle('display','block');
		$('gidiv').setStyle('left',pos.x+10);
		$('gidiv').setStyle('top',pos.y+100);
		$('gidiv').set('html', '<span class="gameBox_titulo">'+unescape(titulo)+'</span><br/>'+unescape(shortdesc)+'...');
		//alert(pos.x);
	}
}

function hide_ginfo(){
	$('gidiv').setStyle('display','none');
	$('gidiv').setStyle('left','0');
	$('gidiv').setStyle('top','0');
}

function showPpalDetail(id){
	if($('detail-search-gidiv')){
		//alert('hola');
		$('detail-search-gidiv').setStyle('display','block');
		$('detail-search-gidiv').setStyle('width','600px');
		$('detail-search-gidiv').setStyle('height','470px');
		if($('category-search-gidiv')){
			var ps=$('category-search-gidiv').getCoordinates();
			//$('detail-search-gidiv').setPosition({x:ps.x+200,y:ps.y});
			$('detail-search-gidiv').setStyle('left',(ps.left+ps.width-2)+'px');
		}
		
		reqdetailbuscador = new Request({ 
			url: '/games/includes/buscador-detail.php',
			encoding: "ISO-8859-1",
			onRequest: function() {
				$('detail-search-gidiv').set('html', 'cargando datos...');
  			},
  			onComplete: function(){
  				$('detail-search-gidiv').set('html', '');
  			},
			onSuccess: function(txt,xml){
				$('detail-search-gidiv').set('html', txt);
				if($('hide-ppal')){
					$('hide-ppal-detail').addEvent('click', function(){
						$('detail-search-gidiv').setStyle('display','none');
						$('detail-search-gidiv').setStyle('width','0px');
						$('detail-search-gidiv').setStyle('height','0px');
					});

				}
			},
			// Our request will most likely succeed, but just in case, we'll add an
			// onFailure method which will let the user know what happened.
			onFailure: function(xhr){
				$('detail-search-gidiv').set('html', 'The request failed.');
			}
		});
		
		reqdetailbuscador.send({method: 'post', data: 'clave='+id});
	}
}

function go_to_private_page()
{
window.location = 'micuenta.html'; // Members Area
}
