$(document).ready(function(){
$('table.s_container6').hide();

	var rm_flag = false;
	var page = 1;
	var aload = false;
	var get_load = false;
	var currency = {
	"51": "USD",
	"66": "EUR",
	"58": "USD",
	"83": "USD",
	"72": "EUR",
	"97": "USD",
	"68": "USD",
	"67": "EUR",
	"80": "EUR"
	};

	var hash = window.location.hash.toString();
	
	path = new String(window.location.pathname);
	path_pos = path.indexOf('/country/');
	if(path_pos != -1) {
		path_country = path.substr(path_pos + 9, path.length - path_pos - 9);
		$('#form').append('<div id="countryPriceTable"></div>');
		$('#countryPriceTable').load('/ajax/country.php?country='+path_country);
		//ajax/country.php?country=greece
		console.log(path_country);
	}

	if (hash.length > 1 && aload === false) {
		hash = hash.substr(1);
		var tmp = new Array();
		var tmp2 = new Array();
		var param = new Array();

		tmp = hash.split('&');
		for(var i=0; i < tmp.length; i++) {
			tmp2 = tmp[i].split('=');

			switch (tmp2[0]) {
				case 'country' : $('#country').val(tmp2[1]); country_change();  break;
				case 'tour' :  $('#tour').data('pre', tmp2[1]); break;
				case 'date1' :  $('#date1').val(tmp2[1]); break;
				case 'date2' :  $('#date2').val(tmp2[1]); break;
				case 'nights_from' :  $('#nights_from').val(tmp2[1]); break;
				case 'nights_till' :  $('#nights_till').val(tmp2[1]); break;
				case 'adult' :  $('#adult').val(tmp2[1]); break;
				case 'child' :  $('#child').val(tmp2[1]); break;
				case 'star' :  $('#star').val(tmp2[1]); break;
				case 'currency' :  $('#currency').val(tmp2[1]); break;
				case 'meal' :  $('#meal').val(tmp2[1]); break;
				case 'max_price' :  $('#max_price').val(tmp2[1]); break;
				case 'page' :  page = tmp2[1]; break;
			}
		}
	}
	else {
		$('#tour').attr('disabled', true);
		$('table.s_container2, table.s_container4').hide();
	}
	
	/*$('#date1').change(function(){ console.log('!');
	  ar1 = $('#date1').val().split('.');
	  ar2 = $('#date2').val().split('.');
    
      d1 = new Date();
      d2 = new Date();
      d1.setTime(Date.parse(ar1[1] + '/' + ar1[0] + '/' + ar1[2]));
      d2.setTime(Date.parse(ar2[1] + '/' + ar2[0] + '/' + ar2[2]));
      
	  if (d2.getTime() >= d1.getTime()) return;
	  
      d = new Date(d1.getTime() + 60*60*24*7*1000);
      y = d.getFullYear().toString();
      m = d.getMonth() + 1; 
      if (m < 10) m = '0' + m.toString();
      else m = m.toString();
      d = d.getDate();
      if (d < 10) d = '0' + d.toString();
      else d = d.toString();
      
      $('#date2').val(d+'.'+m+'.'+y);
	});*/

	$('#country').change(country_change);
	$('#tour').change(tour_change);

	function tour_change() {
		id_tour = $('#tour').val();
		data = $('#tour').data(id_tour);
		$('#date1').val(data['date1']);
		$('#date2').val(data['date2']);
	}

	function country_change() {
		id_country = $('#country').val();
		$('#reg').html('<img src="/ajax/17-0.gif" alt="" >');
		$('#currency option:gt(0)').remove();
		$('#currency').append('<option value="'+((currency[id_country] == 'USD') ? 2 : 3)+'">'+currency[id_country]+'</option>');
		$.getJSON('/ajax/jtour.php', 'id_country=' + id_country, cb_country);
		$('#res').html('');
	}

	$('#form').submit(function(){
		if (window.location.pathname.indexOf("/search") == -1) {
			location.href = '/search#'+$(this).serialize();
		}
		else {
			load_data();
		}
		return false;
	});

	function load_data() {
		get = $('#form').serialize() + '&page='+page;
		$('#res').html('<img src="/ajax/ajax-loader.gif" alt="" >');
		$('#res').load('/ajax/jres.php', get, cb_res);
		aload = true;
		location.href = '#' + get;
		page = 1;
	}

	function ch_currency() {
		if ($('#currency').val() == 1) {
			$('div#res span.price').hide();
			$('div#res span.rur').show();
		}
		else {
			$('div#res span.rur').hide();
			$('div#res span.price').show();
		}
		
		//get = $('#form').serialize() + '&page='+page;
		//location.href = '#' + get;
	}

	function cb_res() {

		ch_currency();
		$('#currency').change(ch_currency);

		$('#navi a').click(function(){
			page = $(this).html();
			load_data();
			return false;
		});
	}

	function cb_country(data) {
		id_tour = $('#tour').data('pre');

		$('#tour').attr('disabled', false);
		$('table.s_container2, table.s_container4').show();
		if (rm_flag == false) {
			$('#country option:first').remove();
			rm_flag = true;
		}

		$('#tour option').remove();

		$.each(data, function(index, value){
			$('#tour').append('<option value="'+index+'">'+value['tour']+'</option>').data(index, value);
		});

		if (id_tour >= 0) {
			$('#tour').val(id_tour);
			if (get_load === false) {
        load_data();
        get_load = true;
      }
	  else {
	  tour_change();
			$('#reg').html('');
	  }
		}
		else {
			//$('#tour option:first').attr('selected', 'selected');
			tour_change();
			$('#reg').html('');
		}


	}

});
