$(document).ready(function(){$('#from-city').bind('focus',function(){if(this.value==_('From'))
this.value='';$(this).addClass('selected-from');});$('#to-city').bind('focus',function(){if(this.value==_('To'))
this.value='';$(this).addClass('selected-to');$('#from-city').removeClass('selected-from');});$('#from-city').bind('blur',function(){if(this.value=='')
this.value=_('From');$(this).removeClass('selected-from');});$('#to-city').bind('blur',function(){if(this.value=='')
this.value=_('To');$(this).removeClass('selected-to');});$('#depart-month').bind('change',function(){changeMonth('none','depart');});$('#depart-year').bind('change',function(){changeMonth('none','depart');});$('#return-month').bind('change',function(){changeMonth('none','return');});$('#return-year').bind('change',function(){changeMonth('none','return');});$.ajax({url:'/ajax/loadAirports/',success:function(trunk){var airports=$.evalJSON(trunk);$('#from-city').autocomplete(airports,{delay:200,width:300,minChars:1,matchContains:"word",mustMatch:false,autoFill:false,selectFirst:false,formatItem:function(row,i,max){return row.name;},formatMatch:function(row,i,max){return row.name;},formatResult:function(row){return row.name;}});$("#from-city").result(function(event,data,formatted){$("#from-city").val(data.result);});$('#to-city').autocomplete(airports,{delay:200,width:300,minChars:1,matchContains:"word",mustMatch:false,autoFill:false,selectFirst:false,formatItem:function(row,i,max){return row.name;},formatMatch:function(row,i,max){return row.name;},formatResult:function(row){return row.name;}});$("#to-city").result(function(event,data,formatted){$("#to-city").val(data.result);});}});$.ajax({url:'/ajax/getAgencies/',success:function(content){agencies=$.evalJSON(content);agencies=$.shuffle(agencies)}});});selectDay=function(id,group,changeyear,changemonth,direction){if(!is_defined(id)){return false;}
var year=$('#'+group+'-year').val();var month=$('#'+group+'-month').val();if(is_defined(changeyear)&&is_defined(changemonth)){year=changeyear;month=changemonth;}
if(group=='return'){var depyear=$('#depart-year').val();var depmonth=$('#depart-month').val();if((depyear>=year&&parseInt(depmonth,10)>=parseInt(month,10))||changeyear<depyear){if(depmonth>month||parseInt($('#'+id).html(),10)<parseInt($('a.departactive').html(),10)||changeyear<depyear)
return false;}}else if(group=='depart'){if(date.year>=year&&parseInt(date.month,10)>=parseInt(month,10)){if(date.month>month||parseInt($('#'+id).html(),10)<parseInt(date.day,10)){return false;}}}
if(is_defined(changeyear)&&is_defined(changemonth)&&is_defined(direction)){changeMonth(direction,group,changeyear,changemonth,$('#'+id).html());}
if(group=='depart'){var returnyear=parseInt($('#return-year').val(),10);var returnmonth=parseInt($('#return-month').val(),10);var departday=parseInt($('#'+id).html(),10);if(returnyear<year||(returnyear==year&&returnmonth<month)){$('#return-calendar a.returnday').each(function(index){var wrongmonth=$(this).hasClass('returnwrongmonth');var returnday=parseInt($(this).html(),10);if(returnday>=departday&&month==(returnmonth+1)&&returnday>0&&returnday<8&&wrongmonth){$(this).removeClass("passed");}else{$(this).addClass("passed");$(this).removeClass("returnactive");}});$('#return-calendar a.wrongpassed').each(function(index){var returnday=parseInt($(this).html(),10);if(returnday>20&&returnday<departday){$(this).addClass("wrongpassed");}});}else if(returnyear==year&&returnmonth>month){$('#return-calendar a.passed').each(function(index){$(this).removeClass("passed");});$('#return-calendar a.wrongpassed').each(function(index){var returnday=parseInt($(this).html(),10);if((returnmonth-1)==month&&departday<=returnday){$(this).removeClass("wrongpassed");}});$('#return-calendar a.returnwrongmonth').each(function(index){var returnday=parseInt($(this).html(),10);if((returnmonth-1)==month&&departday>returnday&&returnday>20){$(this).addClass("wrongpassed");}});}else if(returnyear==year&&returnmonth==month){var selectedReturnday=parseInt($('a.returnactive').html());$('#return-calendar a.returnday').each(function(index){var returnday=parseInt($(this).html(),10);var wrongmonth=$(this).hasClass('returnwrongmonth');if(returnday<departday&&!wrongmonth){$(this).addClass("passed");$(this).removeClass("returnactive");if(returnday==selectedReturnday&&departday>selectedReturnday){$('#return-date').val('');}}else{$(this).removeClass("passed");$(this).bind('click',function(){selectDay(this.id,'return');});}});$('#return-calendar a.returnwrongmonth').each(function(index){var returnday=parseInt($(this).html(),10);if(returnday>20){$(this).addClass("wrongpassed");$(this).unbind('click');}});}
if(returnyear<year||(returnyear==year&&returnmonth<month)){changeMonth('jump','return',year,month,departday);$('#return-month option[value=\''+month+'\']').attr('selected','selected');$('#return-year option[value=\''+year+'\']').attr('selected','selected');}}
$('.'+group+'active').removeClass(group+'active');$('#'+id).addClass(group+'active');$('#'+group+'-date').val($('#'+id).attr('title'));}
changeMonth=function(direction,group,year,month,day){if(direction!='jump'){var year=$('#'+group+'-year').val();var month=$('#'+group+'-month').val();}
if(group=='return'||group=='jump'){var depyear=$('#depart-year').val();var depmonth=$('#depart-month').val();}
var lastyear=0;var firstyear=$('#'+group+'-year option[index=\'0\']').val();$('#'+group+'-year option').each(function(intIndex){lastyear=intIndex;});lastyear=$('#'+group+'-year option[index=\''+lastyear+'\']').val();var ok=false;if(direction=='jump'){ok=true;}else if(direction=='next'){if(year+1>lastyear&&month==12)
ok=false;else
ok=true;}else if(direction=='prev'){if(group=='return'){if((month-1)<depmonth&&depyear>=year)
ok=false;else
ok=true;}else{if(year==date.year&&(month-1)<date.month)
ok=false;else
ok=true;}}else if(direction=='none'&&(year>date.year||(year>=date.year&&month>=date.month))){ok=true;}else if(direction=='none'&&year==date.year&&month<date.month){var oldmonth=parseInt($('#'+group+'-date').val().split("-")[1],10);$('#'+group+'-month option[value=\''+oldmonth+'\']').attr('selected','selected');}
if(ok){$.ajax({type:'POST',url:'/ajax/changeMonth',data:'year='+year+'&month='+month+'&selected='+$('#'+group+'-date').val()+'&direction='+direction+'&group='+group+(is_defined(day)?'&day='+day:'')+(group=='return'?'&depyear='+depyear+'&depmonth='+depmonth+'&depday='+(direction=='jump'?day:$('#depart-calendar a.departactive').html()):''),success:function(trunk){$('#'+group+'-calendar').html(trunk);if(direction=='prev'){if(month==1){if(parseInt(date.year,10)==(parseInt(year,10)-1)){for(i=1;i<date.month;i++){$('#'+group+'-month option[value=\''+i+'\']').addClass('passed');}}
$('#'+group+'-month option[value=\'12\']').attr('selected','selected');$('#'+group+'-year option[value=\''+(parseInt(year,10)-1)+'\']').attr('selected','selected');}else{$('#'+group+'-month option[value=\''+(parseInt(month,10)-1)+'\']').attr('selected','selected');}}else if(direction=='next'){if(month==12){if(parseInt(date.year,10)!=(parseInt(year,10)+1)){$('#'+group+'-month option').removeClass('passed');}
$('#'+group+'-month option[value=\'1\']').attr('selected','selected');$('#'+group+'-year option[value=\''+(parseInt(year,10)+1)+'\']').attr('selected','selected');}else{$('#'+group+'-month option[value=\''+(parseInt(month,10)+1)+'\']').attr('selected','selected');}}else if(direction=='none'){if(parseInt(date.year,10)!=parseInt(year,10)){$('#'+group+'-month option').removeClass('passed');}else{for(i=1;i<date.month;i++){$('#'+group+'-month option[value=\''+i+'\']').addClass('passed');}}}
if(group=='depart'){selectDay($('a.departactive').attr('id'),'depart');}
clearCalenderHeight();checkCalenderHeight();}});}}
var displayingCalendars=false;displayCalendars=function(){if(!displayingCalendars){$('#return_date').remove();$('#depart_date').remove();$('#depart_calendar').attr('visibility','hidden');$('#return_calendar').attr('visibility','hidden');$('#depart_calendar').attr('display','block');$('#return_calendar').attr('display','block');$('#depart_calendar').fadeIn('normal');$('#return_calendar').fadeIn('normal');$('#gosearch').attr('overflow','visible');$('#search_travel_button').css({'display':'block'});$('#change_travel_button').css({'display':'none'});checkCalenderHeight();if($('#type_2').attr('checked')){var pos=$('#to-city').position();var height=$('#to-city').height();$('#hideReturnCalendar').css({'top':pos.top+height+30+'px','left':pos.left+'px'});$('#hideReturnCalendar').css({'display':'block'});}
displayingCalendars=true;}}
var firstTime=1;toggleReturnCalendar=function(){if(firstTime==1){var pos=$('#to-city').position();var height=$('#to-city').height();$('#hideReturnCalendar').css({'top':pos.top+height+30+'px','left':pos.left+'px'});firstTime=0;}
if($('#type_1').attr('checked'))
$('#hideReturnCalendar').css({'display':'none'});else
$('#hideReturnCalendar').css({'display':'block'});}
checkCalenderHeight=function(){var departCal=$('.depart').height();var returnCal=$('.return').height();if(departCal>returnCal)
$('.return').css({'height':departCal+'px'});else if(departCal<returnCal)
$('.depart').css({'height':returnCal+'px'});$('.gosearch').css({'height':(departCal>returnCal?departCal:returnCal)+'px'});}
clearCalenderHeight=function(){$('.return').css({'height':'auto'});$('.depart').css({'height':'auto'});}
