﻿$(document).ready(function(){

	//установка дат
	var date1 = $("#departureDateStr").val().split(" ");
	$("#day1").append(date1[0]);
	$("#month1").append(date1[1]);
	$("#year1").append(date1[2]);
	
	var date2 = $("#returnDateStr").val().split(" ");
	$("#day2").append(date1[0]);
	$("#month2").append(date1[1]);
	$("#year2").append(date1[2]);
	

	//прорисовка взрослых
	var html = generateHtml("numAdults");
	$("#adultsBlock").append(html);
	
	//прорисовка детей
	html = generateHtml("numChildren");
	$("#childrenBlock").append(html);
	
	
	//прорисовка младенцев
	html = generateHtml("numInfants");
    $("#infantsBlock").append(html);
	
	
	$("#tableAdults,#tableChildren,#tableInfants").delegate('div[id^="numAdults_"],div[id^="numChildren_"],div[id^="numInfants_"]',"click",function(){
		var current = this;
		if($(current).hasClass("free")){
			var index = current.id.lastIndexOf("_");
			var value = current.id.substring(index+1);
			
			if (current.id.indexOf("numAdults_")!=-1){
				$("#numAdults").val(value);
				//сделаем проверку, если младенцев получилось больше чем взрослых, то ставим количество младенцев равных количеству взрослых
				if ($("#numInfants").val()>value){
					$("#numInfants").val(value);
				}
			}else if (current.id.indexOf("numChildren_")!=-1){
				$("#numChildren").val(value);
			}else if(current.id.indexOf("numInfants_")!=-1){
				$("#numInfants").val(value);
			}
			
			//прорисовка взрослых
			$("#adultsBlock").empty();
			var html = generateHtml("numAdults");
			$("#adultsBlock").append(html);
			
			//прорисовка детей
			$("#childrenBlock").empty();
			html = generateHtml("numChildren");
			$("#childrenBlock").append(html);
			
			
			//прорисовка младенцев
			$("#infantsBlock").empty();
			html = generateHtml("numInfants");
			$("#infantsBlock").append(html);
			
	
		}
	});
});
//генерация html для младенцев и детей
function generateHtml(numType){
    var html = '';
	var numAdults = $("#numAdults").val();
	var numChildren = $("#numChildren").val();
	
	
	if (numType == "numAdults"){
		html += '';
		for (var i=1;i<10;i++){
			if (numAdults == i){
				html+= '<div id="numAdults_'+i+'" class="nums current"><img src="'+contextpath+'/images/num'+i+'r.png" alt="" /></div>';//текущий элемент
			}else if (i+parseInt(numChildren)<10){
				html+= '<div id="numAdults_'+i+'" class="nums free"><img src="'+contextpath+'/images/num'+i+'.png" alt="" /></div>';//свободный элемент
			}else{
				html+= '<div id="numAdults_'+i+'" class="nums blocked"><img src="'+contextpath+'/images/num'+i+'n.png" alt="" /></div>';//неразрешённый элемент			
			}
		}
	}
	
	if (numType == "numChildren"){
		for (var i=0;i<9;i++){
			if (numChildren == i){
				html += '<div id="numChildren_'+i+'" class="nums current"><img src="'+contextpath+'/images/num'+i+'r.png" alt="" /></div>';//текущий элемент
			} else if (i+parseInt(numAdults) <10){
				html+= '<div id="numChildren_'+i+'" class="nums free"><img src="'+contextpath+'/images/num'+i+'.png" alt="" /></div>';//свободный элемент
			} else{
				html+= '<div id="numChildren_'+i+'" class="nums blocked"><img src="'+contextpath+'/images/num'+i+'n.png" alt="" /></div>';//неразрешённый элемент			
			}	
	    }	
	}
	var numInfants = $("#numInfants").val();
	if (numType == "numInfants"){
		for (var i=0;i<10;i++){
				if (numInfants == i){
				html += '<div id="numInfants_'+i+'" class="nums current"><img src="'+contextpath+'/images/num'+i+'r.png" alt="" /></div>';//текущий элемент
			} else if (i<=parseInt(numAdults)){
				html+= '<div id="numInfants_'+i+'" class="nums free"><img src="'+contextpath+'/images/num'+i+'.png" alt="" /></div>';//свободный элемент
			} else{
				html+= '<div id="numInfants_'+i+'" class="nums blocked"><img src="'+contextpath+'/images/num'+i+'n.png" alt="" /></div>';//неразрешённый элемент			
			}	
		}
	}
	return html;
}

 /*
	Функция возвращает true, если направления присутствуют в списке, 
	false  в противном случае
  */
   function checkFlight(origin,destination){
	  var flights = {};
	  flights.DME = {YYZ:'YYZ', MIA:'MIA', JFK: 'JFK', LHR: 'LHR', IAH:'IAH', LAX:'LAX'};
	  flights.SVO = {YYZ:'YYZ', MIA:'MIA', JFK: 'JFK'};
	  flights.YYZ = {DME:'DME', SVO:'SVO'};
	  flights.MIA = {DME:'DME', SVO:'SVO'};
	  flights.JFK = {DME:'DME', SVO:'SVO'};
	  flights.LHR = {DME:'DME'};
	  flights.IAH = {DME:'DME'};
	  flights.LAX = {DME:'DME'};
	  
   
	  if (origin && origin === 'HKT'){
			return true;
	  }
	  
	  if (destination && destination === 'HKT'){
		    return true;
	  }
	  
	  if (flights[origin] && flights[origin][destination]){
		return true
	  }
	  
	  return false; 
   }
