/*
	Singleton для работы с интервалами дат
*/
var datepickerRange = {
  startDate:null, 
  endDate:null, 
  currentDate:new Date(), 
  selectCount:0,
	
  checkDays: function(){
    var self = this;
    if(this.startDate && this.endDate){
      $("#hcdatepicker").oneTime(1, function() {
        $('td>a.ui-state-default').each(function (i) {
          self.checkDay(this, i);
        });
      });
    }
  },
	
  checkDay: function(elem, currentPos){
    var currentDay=currentPos+1;
    var currentDate = new Date(this.currentDate.getFullYear(), 
    this.currentDate.getMonth(), currentDay);
    if(currentDate.getTime() >= this.startDate.getTime() &&
    currentDate.getTime() <= this.endDate.getTime()){
      $(elem).addClass('ui-state-active').removeClass('ui-state-highlight');	
    }
  },
	
  getSelectedDate: function(inst){
    return new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay);
  }
};

$(function() {
	$(".button, .big-button, .pager a.prev, .pager a.next").mousedown(function() {
		$(this).addClass("pushed");
	});
	$(".button, .big-button, .pager a.prev, .pager a.next").mouseup(function() {
		$(this).removeClass("pushed");
	});
	$(".button, .big-button, .pager a.prev, .pager a.next").mouseout(function() {
		$(this).removeClass("pushed");
	});
	
	$("#hbf-tabs .tabs a").click(function() {
		$("#hbf-tabs .t-item").show();
		$(this).parents(".t-item").hide();
		if ($(this).attr('id') == 'oneway') {
			$('#oneway_check').val(1);
		} else {
			$('#oneway_check').val(0);
		}
	});
	
	var checkinValueStart = null;
	var checkinValueEnd = null;
	var checkinValueCurrent = new Date();
	
	$(".cdatepicker").datepicker({
		minDate: 0,
		numberOfMonths: 2,
		buttonText: '',
		dateFormat: "yy-mm-dd",
		showOn: "both",
		buttonImage: "/i/ico_date.gif",
		buttonImageOnly: true,
//		beforeShow: function(input, instance) {
//				checkinValueCurrent = new Date(instance.selectedYear, instance.selectedMonth, instance.selectedDay);
//				
//				if (checkinValueStart && checkinValueEnd) {
//					if(checkinValueStart.getTime() > checkinValueEnd.getTime()) {
//						var tmp = checkinValueEnd;
//		        checkinValueEnd = checkinValueStart;
//		        checkinValueStart = checkinValueEnd;	
//					}
//					
//					var today = new Date();
//					$(".cdatepicker").oneTime(1, function() {
//		        $('td>a.ui-state-default').each(function (i) {
//		          var currentDay = today.getDate() + i;
//					    var currentDate = new Date(checkinValueCurrent.getFullYear(), checkinValueCurrent.getMonth(), currentDay);
//					    
//					    if( (currentDate.getTime() >= checkinValueStart.getTime()) && (currentDate.getTime() <= checkinValueEnd.getTime()) ){
//					      $(this).addClass('ui-state-active').removeClass('ui-state-highlight');	
//					    }
//		        });
//		      });
//				}
//		},
//		onChangeMonthYear: function(year, month, instance) {
//				checkinValueCurrent = new Date(instance.selectedYear, instance.selectedMonth, instance.selectedDay);
//				
//				if (checkinValueStart && checkinValueEnd) {
//					if(checkinValueStart.getTime() > checkinValueEnd.getTime()) {
//						var tmp = checkinValueEnd;
//		        checkinValueEnd = checkinValueStart;
//		        checkinValueStart = checkinValueEnd;	
//					}
//					
//					var today = new Date();
//					$(".cdatepicker").oneTime(1, function() {
//		        $('td>a.ui-state-default').each(function (i) {
//		          var currentDay = today.getDate() + i;
//					    var currentDate = new Date(checkinValueCurrent.getFullYear(), checkinValueCurrent.getMonth(), currentDay);
//					    
//					    if( (currentDate.getTime() >= checkinValueStart.getTime()) && (currentDate.getTime() <= checkinValueEnd.getTime()) ){
//					      $(this).addClass('ui-state-active').removeClass('ui-state-highlight');	
//					    }
//		        });
//		      });
//				}
//		},
//		onSelect: function(selectedDate, instance) {
//			switch(instance.id) {
//				case 'pickupDateTime':
//					date = $.datepicker.parseDate(
//						instance.settings.dateFormat ||
//						$.datepicker._defaults.dateFormat,
//						selectedDate, instance.settings );
//					$('#searchBoxContainer_Checkout').datepicker("option", "defaultDate", date );
//
//					checkinValueStart = new Date(instance.selectedYear, instance.selectedMonth, instance.selectedDay);
//				break;
//				case 'returnDateTime':
//					checkinValueEnd = new Date(instance.selectedYear, instance.selectedMonth, instance.selectedDay);
//				break;
//			}
//			
//			if (checkinValueStart && checkinValueEnd) {
//				if(checkinValueStart.getTime() > checkinValueEnd.getTime()) {
//
//					var tmp = checkinValueEnd;
//	        checkinValueEnd = checkinValueStart;
//	        checkinValueStart = checkinValueEnd;
//					
//	        $('#pickupDateTime').datepicker("setDate", checkinValueStart);
//	        $('#returnDateTime').datepicker("setDate", checkinValueEnd);
//				}
//			}
//		}
	});
	
	$(".ddatepicker").datepicker({
		minDate: 0,
//		numberOfMonths: 2,
		buttonText: '',
		dateFormat: "yy-mm-dd",
		showOn: "both",
		buttonImage: "/i/ico_date.gif",
		buttonImageOnly: true,
//		beforeShow: function(input, instance) {
//				checkinValueCurrent = new Date(instance.selectedYear, instance.selectedMonth, instance.selectedDay);
//				
//				if (checkinValueStart && checkinValueEnd) {
//					if(checkinValueStart.getTime() > checkinValueEnd.getTime()) {
//						var tmp = checkinValueEnd;
//		        checkinValueEnd = checkinValueStart;
//		        checkinValueStart = checkinValueEnd;	
//					}
//					
//					var today = new Date();
//					$(".ddatepicker").oneTime(1, function() {
//		        $('td>a.ui-state-default').each(function (i) {
//		          var currentDay = today.getDate() + i;
//					    var currentDate = new Date(checkinValueCurrent.getFullYear(), checkinValueCurrent.getMonth(), currentDay);
//					    
//					    if( (currentDate.getTime() >= checkinValueStart.getTime()) && (currentDate.getTime() <= checkinValueEnd.getTime()) ){
//					      $(this).addClass('ui-state-active').removeClass('ui-state-highlight');	
//					    }
//		        });
//		      });
//				}
//		},
//		onChangeMonthYear: function(year, month, instance) {
//				checkinValueCurrent = new Date(instance.selectedYear, instance.selectedMonth, instance.selectedDay);
//				
//				if (checkinValueStart && checkinValueEnd) {
//					if(checkinValueStart.getTime() > checkinValueEnd.getTime()) {
//						var tmp = checkinValueEnd;
//		        checkinValueEnd = checkinValueStart;
//		        checkinValueStart = checkinValueEnd;	
//					}
//					
//					var today = new Date();
//					$(".ddatepicker").oneTime(1, function() {
//		        $('td>a.ui-state-default').each(function (i) {
//		          var currentDay = today.getDate() + i;
//					    var currentDate = new Date(checkinValueCurrent.getFullYear(), checkinValueCurrent.getMonth(), currentDay);
//					    
//					    if( (currentDate.getTime() >= checkinValueStart.getTime()) && (currentDate.getTime() <= checkinValueEnd.getTime()) ){
//					      $(this).addClass('ui-state-active').removeClass('ui-state-highlight');	
//					    }
//		        });
//		      });
//				}
//		},
//		onSelect: function(selectedDate, instance) {
//			switch(instance.id) {
//				case 'dDate':
//					date = $.datepicker.parseDate(
//						instance.settings.dateFormat ||
//						$.datepicker._defaults.dateFormat,
//						selectedDate, instance.settings );
//					$('#searchBoxContainer_Checkout').datepicker("option", "defaultDate", date );
//
//					checkinValueStart = new Date(instance.selectedYear, instance.selectedMonth, instance.selectedDay);
//				break;
//				case 'rDate':
//				
//					checkinValueEnd = new Date(instance.selectedYear, instance.selectedMonth, instance.selectedDay);
//				break;
//			}
//			
//			if (checkinValueStart && checkinValueEnd) {
//				if(checkinValueStart.getTime() > checkinValueEnd.getTime()) {
//
//					var tmp = checkinValueEnd;
//	        checkinValueEnd = checkinValueStart;
//	        checkinValueStart = checkinValueEnd;
//					
//	        $('#dDate').datepicker("setDate", checkinValueStart);
//	        $('#rDate').datepicker("setDate", checkinValueEnd);
//				}
//			}
//		}
	});
	
//	$(".hcdatepicker").datepicker({
//		minDate: 0,
//		numberOfMonths: 2,
//		buttonText: '',
//		dateFormat: "yy-mm-dd",
//		showOn: "both",
//		buttonImage: "/i/ico_date.gif",
//		buttonImageOnly: true,
//		beforeShow: function(input, instance) {
//				checkinValueCurrent = new Date(instance.selectedYear, instance.selectedMonth, instance.selectedDay);
//				
////				console.debug('Start: ' + checkinValueStart);
////				console.debug('End: ' + checkinValueEnd);
////				console.debug('Current: ' + checkinValueCurrent);
//				
//				if (checkinValueStart && checkinValueEnd) {
//					if(checkinValueStart.getTime() > checkinValueEnd.getTime()) {
//						var tmp = checkinValueEnd;
//		        checkinValueEnd = checkinValueStart;
//		        checkinValueStart = checkinValueEnd;	
//					}
//						
//					var today = new Date();
//					$(".hcdatepicker").oneTime(1, function() {
//		        $('td>a.ui-state-default').each(function (i) {
//		          var currentDay = today.getDate() + i;
//					    var currentDate = new Date(checkinValueStart.getFullYear(), checkinValueStart.getMonth(), currentDay);
//
////					    console.debug(currentDate + ' | ' + currentDate.getTime());
//							
//					    if( (currentDate.getTime() >= checkinValueStart.getTime()) && (currentDate.getTime() <= checkinValueEnd.getTime()) ){
////					console.debug('currentDay: ' + currentDay + '; ' + 'currentDate: ' + currentDate);
////					console.debug('checkinValueStart: ' + checkinValueStart + '; ' + 'checkinValueEnd: ' + checkinValueEnd);
////		    	console.debug(currentDate.getTime() + ' ; ' + checkinValueStart.getTime() + ' ; ' + currentDate.getTime() + ' ; ' + checkinValueEnd.getTime() );
//					      $(this).addClass('ui-state-active').removeClass('ui-state-highlight');	
////					console.debug('==========================');
//					    }
//		        });
//		      });
//				}
//		},
//		onChangeMonthYear: function(year, month, instance) {
//				checkinValueCurrent = new Date(instance.selectedYear, instance.selectedMonth, instance.selectedDay);
//				
////				console.debug('Start: ' + checkinValueStart);
////				console.debug('End: ' + checkinValueEnd);
//				console.debug('Selected: ' + instance.selectedYear +' | '+ instance.selectedMonth +' | '+ instance.selectedDay);
//				
//				if (checkinValueStart && checkinValueEnd) {
//					if(checkinValueStart.getTime() > checkinValueEnd.getTime()) {
//						var tmp = checkinValueEnd;
//		        checkinValueEnd = checkinValueStart;
//		        checkinValueStart = checkinValueEnd;	
//					}
//					
//					var today = new Date();
//					$(".hcdatepicker").oneTime(1, function() {
//		        $('td>a.ui-state-default').each(function (i) {
//		          var currentDay = checkinValueCurrent.getDate() + i;
//		          
//		          if (checkinValueStart.getMonth() == checkinValueEnd.getMonth()) {
//					    	var currentDate = new Date(checkinValueStart.getFullYear(), (checkinValueStart.getMonth() - 1), currentDay);
//		          } else {
//		          	var currentDate = new Date(checkinValueStart.getFullYear(), (checkinValueEnd.getMonth() - 1), currentDay);
//		          }
//					    
//					    console.debug('i: ' + i + '; currentDay: ' + currentDay + '; currentDate: ' + currentDate + ' | ' + currentDate.getTime());
////					    console.debug('Current: ' + checkinValueCurrent);
////					    console.debug('Start: ' + checkinValueStart + ' | ' + checkinValueStart.getTime());
////							console.debug('End: ' + checkinValueEnd + ' | ' + checkinValueEnd.getTime());
//					    
//					    if( (currentDate.getTime() >= checkinValueStart.getTime()) && (currentDate.getTime() <= checkinValueEnd.getTime()) ){
//					    	console.debug('Apply');
//					      $(this).addClass('ui-state-active').removeClass('ui-state-highlight');	
//					    }
//					    console.debug('======================');
//		        });
//		      });
//				}
//		},
//		onSelect: function(selectedDate, instance) {
//			switch(instance.id) {
//				case 'searchBoxContainer_Checkin':
//					$('#searchBoxContainer_CheckinValue').val(selectedDate);
//					date = $.datepicker.parseDate(
//						instance.settings.dateFormat ||
//						$.datepicker._defaults.dateFormat,
//						selectedDate, instance.settings );
//					$('#searchBoxContainer_Checkout').datepicker("option", "defaultDate", date );
//					
//					checkinValueStart = new Date(instance.selectedYear, instance.selectedMonth, instance.selectedDay);
//				break;
//				case 'searchBoxContainer_Checkout':
//					$('#searchBoxContainer_CheckoutValue').val(selectedDate);
//					
//					checkinValueEnd = new Date(instance.selectedYear, instance.selectedMonth, instance.selectedDay);
//				break;
//			}
//			
//			if (checkinValueStart && checkinValueEnd) {
//				if(checkinValueStart.getTime() > checkinValueEnd.getTime()) {
//
//					var tmp = checkinValueEnd;
//	        checkinValueEnd = checkinValueStart;
//	        checkinValueStart = checkinValueEnd;
//					
//	        $('#searchBoxContainer_Checkin').datepicker("setDate", checkinValueStart);
//	        $('#searchBoxContainer_Checkout').datepicker("setDate", checkinValueEnd);
//				}
//			}
//			
//		}
//	});
	
	$("#tabs li a").click(function() {
		if (!$(this.parentNode).hasClass("act")) {
			$("#tabs li").removeClass("act");
			$(this.parentNode).addClass("act");
		}
		
		var selected = $(this.parentNode).attr('id');
		if (selected == 'dest_block') {
				$('#destOrHotel_dest_block').show();
				$('#destOrHotel_hotel_block').hide();
				$('#hotelSearchRadio').attr("checked","");
				$('#citySearchRadio').attr("checked","checked");
		}
		if (selected == 'hotel_block') {
				$('#destOrHotel_dest_block').hide();
				$('#destOrHotel_hotel_block').show();
				$('#citySearchRadio').attr("checked","");
				$('#hotelSearchRadio').attr("checked","checked");
		}
		
	});
	
	$(".i-ddl .down, .i-ddl em").click(function() {
		var list = $(".list", this.parentNode);
		if (list.is(":visible")) {
			list.hide();
			$(this.parentNode.parentNode).css("z-index", 1);
		}
		else {
			list.show();
			$(this.parentNode.parentNode).css("z-index", 99);
		}
	});
	$(".i-ddl .list li").click(function() {
		$(this).parents(".i-ddl").find("em").text($(this).text());
		$(this).parents(".list").hide();
	});
	$(".i-ddl").each(function() {
		var a = $(".down", this).get(0);
		var em = $("em", this).get(0);
		var ddl = this;
		
		addEvent(document, 'click', function(event){
			var event = event || window.event;
			var t = event.target || event.srcElement;
			
			if (t != a && t != em && !isChildNode(ddl, t)) {
				$(".list", ddl).hide();
				$(ddl).css("z-index", 1);
			}
		});
	});
	
	$(".cbx i, .cbx label").click(function() {
		var id = $("label", this.parentNode).attr("for");
		
		if ($(this.parentNode).hasClass("checked")) {
			$(this.parentNode).removeClass("checked");
			$("#" + id).removeClass("i-pas");
			$("#" + id).parents(".item").find(".lbl").removeClass("l-pas");	
		}
		else {
			$(this.parentNode).addClass("checked");
			$("#" + id).addClass("i-pas");
			$("#" + id).parents(".item").find(".lbl").addClass("l-pas");
		}
	})
	
	$(".btn-camera").click(showPopup);
	$("#overlay, #pop-close").click(hidePopup);
	
});

function showPopup() {
	$("#overlay, #popup").show();
}
function hidePopup() {
	$("#overlay, #popup").hide();
}

function setInpVal(a, fieldId) {
	document.getElementById(fieldId).value = a.innerHTML;
}

function addEvent(obj, type, fn) {
	if (obj.addEventListener)
		obj.addEventListener(type, fn, false);
	else if (obj.attachEvent)
		obj.attachEvent( "on"+type, fn );
}

function removeEvent(obj, type, fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(type, fn, false);
	else if (obj.detachEvent)
		obj.detachEvent( "on"+type, fn );
}

function isChildNode(elem, sell) {
	for (var childItem in elem.childNodes) {
		if (elem.childNodes[childItem].nodeType == 1) {
			if (elem.childNodes[childItem] == sell)
				return true;
			else if (isChildNode(elem.childNodes[childItem], sell))
				return true;
		}
	}
	return false;
}
