$(document).ready(function() {
	
 /*avia*/
 
 $("#country_a, #country_b").change( function() {
 	
 	
 	$("#calendar_note").html("");
 	var id = $(this).attr("id"); 
 	
 	$.getJSON(
 	'/ajax',
 	{
 		id: id,
 		city: $("#city_a").get(0).value,
 		category: $(this).get(0).value,
  		show_cities: true
 	},
 	function(obj)
 	{	
 		if(id=='country_a') $("#city_a").html(obj.html);	
 		else $("#city_b").html(obj.html);	

    }
    );	
 });

 $("#city_a").change( function() {
 	
 	$("#calendar_note").html("");
 	$.getJSON(
 	'/ajax',
 	{
 		city: $("#city_a").get(0).value,
 		country: $("#country_b").get(0).value,
  		show_city_b: true
 	},
 	function(obj)
 	{	
		$("#city_b").html(obj.html).attr("disabled","");	
    }
    );	
 });
 
 $("#city_b").change( function() {
 	$("#calendar_note").html("");
 	$("#city_b_indicator").html("<img src=\"/i/ajax/form.gif\" alt=\"\" width=\"16\" height=\"16\" style=\"float:left;margin: 0 3px 0 0\" />Идет подбор рейсов, подождите");
  	build_calendar();
 });
 
 $(".calendar_arrow").live("click", function() {
 	var direction = $(this).attr('id');
 	$("#calendar_snake").html("<p style=\"text-align: center;\"><img src=\"/i/ajax/form_blue.gif\" alt=\"\" width=\"16\" height=\"16\" /></p>");
  	$.getJSON(
 	'/calendar',
 	{
 		city_a: $("#city_a").get(0).value,
 		city_b: $("#city_b").get(0).value,
 		direction: direction,
  		show_routes: true
 	},
 	function(obj)
 	{	
		$("#calendar_site").html(obj.response);
    }
    );
 });
 
 $("#make_order").live("click", function () {
 	$("#make_order_indicator").html("<img src=\"/i/ajax/form.gif\" alt=\"\" width=\"16\" height=\"16\" style=\"float:left;margin: 0 3px 0 0\" />");
 	$.getJSON(
 	'/ajax',
 	{
 		city_a: $("#city_a").get(0).value,
 		city_b: $("#city_b").get(0).value, 		
  		show_request_form: true
 	},
 	function(obj)
 	{	
 		var top = $(window).scrollTop();
 		var windowheight = $(window).height();
 		top = top - 200 + windowheight/2;
 		
		$("#dynamic_content").html(obj.html).show();
		$("#request_form").css("top",top+"px");
		$("#make_order_indicator").empty();	
    }
    );
 });
 
 
  $(".wide_orderbutton").live("click", function () {
   	el = $(this);
 	var params = el.attr('id');
 	params = params.split("_");
  	var id = params[1]; 
  	var day = params[2];
  	var date = params[3];	
 	var count = parseInt($("#count_"+id).attr("value"));

 	if(count==0) {
 		alert("Укажите число пассажиров рейса");
 		return;
 	}
  	
 	$.getJSON(
 	'/ajax',
 	{
 		id: id,
 		day: day,
 		date: date,
 		count: count,
  		show_order_form: true
 	},
 	function(obj)
 	{	
 		/*
		$("#make_order_indicator").empty();	
		*/

 		
 		$("#dynamic_content").html(obj.html).show();
 		
 		var height = $("#request_form").height();
 		var top = $(window).scrollTop();
 		var windowheight = $(window).height();
 		top = top - height/2 + windowheight/2;
 		top = top<0 ? 0:top;
 		
 		$("#request_form").css("top",top+"px");
    }
    ); 	
  });
 
 
  $(".orderbutton").live("click", function () {
 	//$("#make_order_indicator").html("<img src=\"/i/ajax/form.gif\" alt=\"\" width=\"16\" height=\"16\" style=\"float:left;margin: 0 3px 0 0\" />");
  	el = $(this);
 	var params = el.attr('id');
 	params = params.split("_");
  	var id = params[1]; 
  	var day = params[2];	
 	var count = parseInt($("#count_"+id).attr("value"));

 	if(count==0) {
 		alert("Укажите число пассажиров рейса");
 		return;
 	}
  	
 	$.getJSON(
 	'/ajax',
 	{
 		id: id,
 		day: day,
 		date: $("#date").attr("value"),
 		count: count,
  		show_order_form: true
 	},
 	function(obj)
 	{	
 		/*
		$("#make_order_indicator").empty();	
		*/

 		
 		$("#dynamic_content").html(obj.html).show();
 		
 		var height = $("#request_form").height();
 		var top = $(window).scrollTop();
 		var windowheight = $(window).height();
 		top = top - height/2 + windowheight/2;
 		top = top<0 ? 0:top;
 		
 		$("#request_form").css("top",top+"px");
    }
    );
 });
 
 $("#closebtn").live("click", function() {
 	$("#dynamic_content").hide().html("");
 });
 
 
 $(".morebutton").click( function() {
 	el = $(this);
 	var params = el.attr('id');
 	params = params.split("_");
  	var id = params[1]; 
  	$.getJSON(
 	'/ajax',
 	{
  		id: id,
  		show_more: true
 	},
 	function(obj)
 	{	
 		var top = $(window).scrollTop();
 		var windowheight = $(window).height();
 		top = top - 125 + windowheight/2;
 		
		$("#dynamic_content").html(obj.html).show();
		$("#more_table").css("top",top+"px");
		//$("#make_order_indicator").empty();	
    }
    );
 })
 
 
  $(".flight-more-button").click( function() {
 	el = $(this);
 	var params = el.attr('id');
 	params = params.split("_");
  	var id = params[1]; 
  	var day = params[2];
  	$.getJSON(
 	'/ajax',
 	{
  		id: id,
  		day_id: day,
  		show_flight_more: true
 	},
 	function(obj)
 	{	
 		var top = $(window).scrollTop();
 		var windowheight = $(window).height();
 		top = top - 60 + windowheight/2;
 		
		$("#dynamic_content").html(obj.html).show();
		$("#more_table").css("top",top+"px");
		//$("#make_order_indicator").empty();	
    }
    );
 })
 
 
 $("#agree").live("click", function() {
 	$(".order_fields").attr("disabled", $(this).get(0).checked ? "" : "disabled");
 });
	
	
 $(".email").each(function() {
 	var e = $(this).attr("href") + "@" + $(this).text();
  	$(this).text(e);
  	$(this).attr("href", "mailto:" + e);
 	});	
});

function build_calendar() {
 	$.getJSON(
 	'/calendar',
 	{
 		city_a: $("#city_a").get(0).value,
 		city_b: $("#city_b").get(0).value,
  		show_routes: true
 	},
 	function(obj)
 	{	
		$("#calendar_site").html(obj.response);
		$("#city_b_indicator").html("");
		$("#calendar_note").html(obj.note);
    }
    );		
}


/* actions */

function loadform(alias)
{
 $('#'+alias).ajaxForm(
 { 
  dataType:  'json', 
  beforeSubmit: function(){
  		//$("#formdiv span.alert").empty();
  		$("#dynamic_content .input_alert").removeClass("input_alert");
 		$("#indicator").html("<img src=\"/i/ajax/form-order.gif\" width=\"16\" height=\"16\" alt=\"\" />").show();
 		$("#disabled").show();
  },
  success: function(data) {
  	//$("#inputresult").html(data.text);
  	$("#indicator").empty();
  	$("#disabled").hide();
  	
 	
  	if(data.items) $(data.items).addClass("input_alert");
  	if(data.note) $("#inputresult").html(data.note);
  	//выводим все алерты
  	if(data.notes) {
  		/*
  		for(var i in data.notes) {
  			if($("#"+i+"_alert").length) $("#"+i+"_alert").html("<span>"+data.notes[i]+"</span>");
  		}
  		*/
  	}
  	
 	if(data.done==1) {
  		$("#submit_data").attr("disabled","disabled");
  		// ставим задержку, чтобы пользователь мог прочитать надпись
  		setTimeout(function(){
  			$("#dynamic_content").html("").hide();
  			$("#calendar_note").html("");
  		}, 3000);
 	}
 	else {
  		$("#submit_data").attr("disabled","");
 	}
  }
 }
 );	
}

function check_length(param)
{
 //var tarea = new String($("#text_area").val());
 // alert(tarea.length);
 var length = $("#text_area").val().length;
 if(length>=param)
 {
  $("#text_area").val($("#text_area").val().substring(0,param));
   $("#ostatok").html("0");
 }
 else
 {
  var ostatok = param - length;
  $("#ostatok").html(ostatok);	
 }
}
