/**
 * Wise Up Online Portal
 * 
 * @since Version 1.0
 * @author MindSet Films Dev Team
 * @copyright Copyright (c) 2008, Ometz Group.
 * @author Ronie Herbert Neubauer.
 * 
 */

var formSlide;
var infoSlide;


function sleep(milliseconds) {
	  var start = new Date().getTime();
	  for (var i = 0; i < 1e7; i++) {
	    if ((new Date().getTime() - start) > milliseconds){
	      break;
	    }
	  }
	}


/**
 * Funcao para pegar os paises que possuam unidades.
 */
function getCountries()
{
	 var carregando='<select onchange="getStates();" id="country_cb">';
		carregando+='<option value=0>Carregando.</option>';
		carregando+='</select>';
		$('country').innerHTML = carregando;
		
	var enviaAjax = new Ajax(baseurl+"/ajax/get.unit.countries/", 
		{
			method: 'post',
			postBody: {},
			onComplete: function (r)
			{
				var html='<select onchange="getStates();" id="country_cb">';
					html+=r;
					html+='</select>';
					$('country').innerHTML = html;				
			}
		});
	
	enviaAjax.request();
	
}

/**
 * Funcao para pegar os estados que possuam unidades.
 */
function getStates()
 {
	 var carregando='<select onchange="getCities();" id="state_cb">';
		carregando+='<option value=0>Carregando.</option>';
		carregando+='</select>';
		$('state').innerHTML = carregando;
		
	
	var country = $('country_cb').value;

	var enviaAjax = new Ajax(baseurl+"/ajax/get.unit.states/", 
		{
			method: 'post',
			postBody: {country: country},
			onComplete: function (r)
			{
				var html='<select onchange="getCities();" id="state_cb">';
					html+=r;
					html+='</select>';
				$('state').innerHTML = html;
			}
		});
	
	enviaAjax.request();
	
}

/**
 * Funcao para pegar as cidades que possuam unidades.
 */
function getCities()
{
	 var carregando='<select onchange="getUnits();" id="city_cb">';
		carregando+='<option value=0>Carregando.</option>';
		carregando+='</select>';
		$('city').innerHTML = carregando;
		
	var state = $('state_cb').value;
	
	var enviaAjax = new Ajax(baseurl+"/ajax/get.units.cities/", 
		{
			method: 'post',
			postBody: {state: state},
			onComplete: function (r)
			{
				var html='<select onchange="getUnits();" id="city_cb">';
				html+=r;
				html+='</select>';
				
				$('city').innerHTML = html;
			}
		});
	
	enviaAjax.request();
}

/**
 * Funcao que lista as unidades baseada na cidade escolhida.
 */
function getUnits()
{
	
	var city = $('city_cb').value;

	var enviaAjax = new Ajax(baseurl+"/ajax/getunitsbycity/", 
		{
			method: 'post',
			postBody: {city: city},
			onComplete: function (r)
			{
				//remove todos os eventos dos botoes, down e up, para corrigir problemas da mootools.
				$('down').removeEvents();
				$('up').removeEvents();
				
				//Chama a funcao slideUnits
				slideUnits();
				
				//Esconde a div Form e mostra a div Info
				$('info').style.display = "block";
				formSlide.toggle();
				
				infoSlide.toggle();
				
				$('units').innerHTML = r		
			}
		});
	
	enviaAjax.request();
}

/**
 * Funcao que recolhe a lista de unidades e volta para as cidades, ativada pelo botao voltar.
 */
function backDiv()
{
	formSlide.toggle();
	infoSlide.toggle();
}

/**
 * Funcao para rolagem das unidades
 */
function slideUnits()
{
	// reseta a variavel current
	var current = 0;
	
	var scroll = new Fx.Scroll('units', {
	wait: false,
	duration: 1000,
	offset: {'x': 0, 'y': 0},
	transition: Fx.Transitions.Quad.easeInOut
	});
	
	//A cada nova pesquisa, volta para o 1 elemento
	scroll.toElement('unitsItem' + current);
	
	// Evento do botao baixo.
	
	$('down').addEvent('click', function(event){
	event = new Event(event).stop();
	if ($('unitsItem' + (current + 1)) != null) {
		current = current + 1;
		scroll.toElement('unitsItem' + current);
			}
		});
	
	
	// Evento do botao cima.
	$('up').addEvent('click', function(event)
	{
		event = new Event(event).stop();
		current = ((current - 1) <= 0) ? 0 : current - 1;
		scroll.toElement('unitsItem' + current);
	});
}

function init()
{
	// Form Slide
	formSlide = new Fx.Slide('formulario');
	
	//Info Skide
	infoSlide = new Fx.Slide('info').hide();
	
	getCountries();
}

window.addEvent('load', init);
