var TO_COUNTRY, TO_CITY, FROM_CITY;
function ucFirst(s)
{
	var c = s.charAt(0);

	if (parseInt(s.length)==1)
	{
		return c.toUpperCase();
	}
	else
	{
		return c.toUpperCase() + s.slice(1).toLowerCase();
	}
}
function initForm()
{
	var p = window.location.pathname;
	var g = p.split('/');
	if (2 <= g.length)
	{
		TO_COUNTRY = g[2];
		if (3 <= g.length)
		{
			TO_CITY = g[3];
		}
	}
	if (0 < p.indexOf('plecare-din'))
	{
		FROM_CITY = g[g.length - 1];
	}
}
function getCategories()
{
	 $('#service').bind('change', function ()
	 {
		getCountries( this.value );
	});
	 $('#service').trigger('change');
}

function getCountries( category )
{
	 $('#city').html('<option value="">Alege orasul</option>');//empty city select
	 
	  var countries = '<option value="">Alege tara</option>';


	  for ( key in cat[category] )//get key => value
	  {	
				var selected;
				if ( (typeof(defaultCountry) != 'undefined' &&  key == defaultCountry) ||  (typeof(TO_COUNTRY) != 'undefined' &&  key == TO_COUNTRY))
				{
					selected = 'selected';
				} else
				{
					selected = '';
				}

				countries += '<option value="'+  key + '" '  + selected + '>' + ucFirst(key) + '</option>';
	  }

	 $('#country').html( countries );	
	 
	 $('#country').bind('change', function ()
	 {
		getCities( $('#service').val(), this.value );
	});
	
	if(defaultCountry != null || TO_COUNTRY != null)
    {
		if ((defaultCountry == null || defaultCountry == '') && TO_COUNTRY != null)
		{
			defaultCountry = TO_COUNTRY;
		}
		getCities( $('#service').val(), defaultCountry );
	}
}

function getCities( category, country )
{

	 if ( country != '' )
	 {
		 var cities = '<option value="">Alege orasul</option>';

		 for ( key in cat[category][country] )//get key => value
		 {	
				var selected;
				if ( (typeof(defaultCity) != 'undefined' &&  cat[category][country][key] == defaultCity) || (typeof(TO_CITY) != 'undefined' &&  cat[category][country][key] == TO_CITY) )
				{
					selected = 'selected';
				} else
				{
					selected = '';
				}
				cities += '<option value="' + cat[category][country][key] + '" ' + selected + '>' + ucFirst(cat[category][country][key]) + '</option>';
		 }

		 $('#city').html( cities );//insert the option into the select
  	 }
	 
}
function formSubmit ()
{
	$('#mainSearch').bind("submit", function(e){
		if ('' == $('#mainSearch #service option[@selected]').val() && '' == $('#mainSearch #country option[@selected]').val() && '' == $('#mainSearch #city option[@selected]').val() && '' == $('#mainSearch #from option[@selected]').val())
		{
			alert('Alegeti macar o optiune!');
		}
		else
		{
			var loc = '', s = $('#mainSearch #service option[@selected]'), c = $('#mainSearch #country option[@selected]'), ci = $('#mainSearch #city option[@selected]'), f = $('#mainSearch #from option[@selected]');
			if ('' != s.val())
			{
				loc = '/'+ s.val().toLowerCase();
			}
			if ('' != c.val())
			{
				loc = loc +'/'+ c.val().toLowerCase();
			}
			if ('' != ci.val())
			{
				loc = loc +'/'+ ci.val().toLowerCase();
			}
			if ('' != f.val())
			{
				loc = loc +'/plecare-din/'+ f.val().toLowerCase();
			}
			if ('' != s.val() && '' == c.val() && '' == ci.val() && '' == f.val())
			{
				loc = loc +'/';
			}
			document.location = loc; 
		}
		
		return false;
	});
}
$(function()//on page load
{
	initForm();//init form imput values from _GET
	getCategories();//load all countries
	formSubmit ();//bind form submit
});


