﻿var inner_w;
var inner_p;
var refresh_arch = '';
var td_data_image; //służy do przechowywania koloru boxa daty przed akcją hover
var t;
var refresh_arch;
var ajaxManagerWylot; 
var ajaxManagerPowrot;
var ajaxManagerWylot_Powrot;
var ajaxManager2;
//var ajaxManager3;
var ajaxManagerPojWylot;
var ajaxManagerPojPowrot;
 ajaxManagerPojWylot = $.manageAjax({manageType: 'normal', maxReq: 10, blockSameRequest: true}); ;
 ajaxManagerPojPowrot = $.manageAjax({manageType: 'normal', maxReq: 10, blockSameRequest: true}); ;
//menager do zadan pobierania wszystkich lotow
ajaxManagerWylot = $.manageAjax({manageType: 'normal', maxReq: 33, blockSameRequest: false});
//menager do zadan pobierania wszystkich lotow
ajaxManagerPowrot = $.manageAjax({manageType: 'normal', maxReq: 33, blockSameRequest: false}); 
//menager do zadan pobierania wszystkich lotow
ajaxManagerWylot_Powrot = $.manageAjax({manageType: 'queue', maxReq: 33, blockSameRequest: false}); 
//menager do zadan wyszukiwarki
ajaxManager2 = $.manageAjax({manageType: 'abortOld', maxReq: 1, blockSameRequest: true});
//menager do zadan pobierania pojedynczych lotow
ajaxManager3 = $.manageAjax({manageType: 'normal', maxReq: 31, blockSameRequest: false}); 
//menager do zadan pobierania czasu odswiezenia
ajaxManager4 = $.manageAjax({manageType: 'abortOld', maxReq: 1, blockSameRequest: true});
//menager do pobierania danych
ajaxManagerGetCity = $.manageAjax({manageType: 'abortOld', maxReq: 1, blockSameRequest: true});
$(function(){
	$('form#wyszukiwarka').click(function(){makeSearch();});
	$.datepicker.regional['pl'] = {clearText: 'Wyczyść', clearStatus: 'Wyczyść obecną datę',
		closeText: 'Zamknij', closeStatus: 'Zamknij bez zapisywania',
		prevText: '&#x3c;Poprzedni', prevStatus: 'Pokaż poprzedni miesiąc',
		nextText: 'Następny&#x3e;', nextStatus: 'Pokaż następny miesiąc',
		currentText: 'Dziś', currentStatus: 'Pokaż aktualny miesiąc',
		monthNames: ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec',
		'Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],
		monthNamesShort: ['Sty','Lu','Mar','Kw','Maj','Cze',
		'Lip','Sie','Wrz','Pa','Lis','Gru'],
		monthStatus: 'Pokaż inny miesiąc', yearStatus: 'Pokaż inny rok',
		weekHeader: 'Tydz', weekStatus: 'Tydzień roku',
		dayNames: ['Niedziela','Poniedziałek','Wtorek','Środa','Czwartek','Piątek','Sobota'],
		dayNamesShort: ['Nie','Pn','Wt','Śr','Czw','Pt','So'],
		dayNamesMin: ['N','Pn','Wt','Śr','Cz','Pt','So'],
		dayStatus: 'Ustaw DD jako pierwszy dzień tygodnia', dateStatus: 'Wybierz D, M d',
		dateFormat: 'mm,yy,dd', firstDay: 1, 
		initStatus: 'Wybierz datę', isRTL: false};
	$.datepicker.setDefaults($.datepicker.regional['pl']);
	
    $.datepicker.setDefaults({
        showOn: 'button',
        buttonImageOnly: true,
        buttonImage: './gfx/icon_calendar.gif',
        buttonText: 'Wybierz datę',
		numberOfMonths:2,
		closeAtTop:false,
		minDate: '0d', 
		maxDate: '1y',
		changeFirstDay:false,
//		mandatory:true,
		speed:'fast'

    });
	
	$('input#picker_wylot').datepicker({
	    onSelect: function(date){
			date = date.split(',');
			var m = date[0];
			var y = date[1];
			var d = date[2];
			var t = 32 - new Date(y, m-1, 32).getDate();
		    $('select#miesiac_wylot').val(parseInt(m, 10)+','+y+','+t); 
			var style_alfa_wylot = $('#dzien_wylot_alfa').attr('style');
			pobierzDni(parseInt(m, 10)+','+y+','+t,'dzien_wylot');
			if(d<10){
				d = parseInt(d.toString().replace('0',''))
			}else{
				 d = parseInt(d);
			}
			$('select#dzien_wylot').val(d);
			makeSearch();
		}
	});
	$('#picker_przylot').datepicker({
	    onSelect: function(date){
			date = date.split(',');
			var m = date[0];
			var y = date[1];
			var d = date[2];
			var t = 32 - new Date(y, m-1, 32).getDate();
		    $('#miesiac_powrot').val(parseInt(m, 10)+','+y+','+t);
			var style_alfa_powrot = $('#dzien_przylot_alfa').attr('style');
			$('#dzien_przylot_alfa').attr('style', style_alfa_powrot + ' display: none');
			pobierzDni(parseInt(m, 10)+','+y+','+t,'dzien_powrot');
		    if(d<10){
				d = parseInt(d.toString().replace('0',''))
			}else{
				 d = parseInt(d);
			}
			$('#dzien_powrot').val(d);
			makeSearch();
		}
	});
	$('#w_picker_najlepsze').datepicker({
		dateFormat: 'yy-mm-dd',
	    onSelect: function(date){
		    $('#datastart').val(parseInt(date));
		}
	});	
	$('#p_picker_najlepsze').datepicker({
		dateFormat: 'yy-mm-dd',
	    onSelect: function(date){
		    $('#datastart').val(parseInt(date));
		}
	});	
	
	if ($('#diagram_p').val() == 1){
		var licznik = 2;
	}
	else{
		var licznik = 1;
	}
	
	for(i = 0; i < licznik; i++) {
		inner_w = '';
		inner_p = '';
		
		var prefix = i > 0 ? 'p_' : 'w_';
		
		$("img." + prefix + "slupki").each(function(){
			var id = $(this).attr("id").split('_');
			var details = $('#details_'+id[1]+'_'+id[2]).val().split(',');

			if (prefix == 'w_') {
				var item = 'description_wylot';
			}
			else {
				var item = 'description_przylot';
			}
			
			$(this).attr("src", "./gfx/bar_dark_blue.gif");
			$(this).hover(function(){
				if ($(this).attr("src") != './gfx/bar_red.gif' ) {
						//Zapamiętywanie czasu odświeżenia dla słupka który jest obecnie kliknięty
						var checked = null;
						
						if (checked_pillar != '')
							{
								checked = $('#'+checked_pillar);
							}
						
					
						if ($(checked).attr('id') && $(checked).attr('id') != ''){
							
							var id_ = $(checked).attr('id').split('_');
							
							var refresh_arch_time =$('#ods_time_'+prefix+id_[2]).val();
							//alert(refresh_arch_time);
							if (typeof(refresh_arch_time) == 'string' && refresh_arch_time.length > 1){
								var tab_ods = new Array();
							    tab_ods = refresh_arch_time.split(' ');	
								if (tab_ods[1] == 'min.'){
									
									if (tab_ods[0] <= 20){
										var time = 	'dane aktualne';
									}
									else{
										var time = refresh_arch = tab_ods[0] + ' min.';
									}
								}
								else if (tab_ods[1] == 'godz.'){
									var time = 	tab_ods[0] + ' godz.'; 
								
								}
								else if (tab_ods[1] == 'dzień'){
									var time = tab_ods[0] +' '+ tab_ods[1];
								
								}								
								if (tab_ods[1] == 'dni' && parseInt(tab_ods[0]) > 5) {
									var time = '> 5 dni'
								}
								else if (tab_ods[1] == 'dni' && parseInt(tab_ods[0]) <= 5){
									var time = tab_ods[0] +' '+ tab_ods[1];
								}			
								if (time == 'dane aktualne'){
									refresh_arch = time;
								}					
								else{
									refresh_arch = '<span class="odswiezono ta-center">odświeżono</span><span class="odswiezono  ta-center"><strong style="color: rgb(44, 101, 195); font-size: 9px;">' + time + '</strong> temu</span>';			
								}
								
							}							
						    else if (refresh_arch_time.length == 1 || refresh_arch_time.length == 0 || refresh_arch_time == ''){
								refresh_arch = 'dane aktualne';
							}
	
						}					

					
					//wstawianie czasu odświeżenia
					val = obliczIleTemuOdswiezono($('#odswiezenie_' + id[1]+'_' + id[2]).val());
					
					$('#ods_time_' + id[1] + '_' + id[2]).val(val);
					var czas_ods = val;
					var val2= '';
						if (typeof(czas_ods) == 'string'){
							var tab_ods = new Array();
						    tab_ods = czas_ods.split(' ');	
							if (tab_ods[1] == 'minut'){
								czas_ods = tab_ods[0];
								val = 	czas_ods;
								
							}
							if (tab_ods[1] == 'dni' && tab_ods[0] > 5) {
								val2 = '> 5 dni'
							}								
							
						}
						if (parseInt(czas_ods) == 0){
							
						}		
									
					if(val != ''){
				
						if (czas_ods < 20) {
							
							$('#' + id[1] + '_time').css('padding-top', '5px');
							$('#' + id[1] + '_time').html('dane aktualne');
						}
						else {
							 if (val2 != ''){
							 	val = val2;
								$('#' + prefix + 'time').css('top','-3px');
							 }
							$('#' + id[1] + '_time').css('padding-top', '0px');
							$('#' + id[1] + '_time').html('<span class="odswiezono ta-center">odświeżono</span><span class="odswiezono  ta-center"><strong style="color: rgb(44, 101, 195); font-size: 9px;">' + val + '</strong> temu</span>');
						}
					}
					
					if (czas_ods == 0){
							$('#' + id[1] + '_time').css('padding-top', '5px');
							$('#' + id[1] + '_time').html('dane aktualne');						
					}
					
					
					details = $('#details_'+id[1]+'_'+id[2]).val().split(',');
					$(this).attr("src", "./gfx/bar_yellow.gif");
					td_data_image = $('#' + id[1] + '_' + id[2] + '_div').css('background-image');
					$('#' + id[1] + '_' + id[2] + '_div').css('background-image', 'url(./gfx/box_yellow.gif)');
					
					var info_data_loty = $('#' + id[1] + '_' + id[2] + '_td').attr('title').split(',');
					var info_data = info_data_loty[0];
					var ilosc_lotow = info_data_loty[1].split(':');
					ilosc_lotow = ilosc_lotow[1];
					
					if(id[1] == 'w')
						var info_kierunek = 'wylot';
					if(id[1] == 'p')
						var info_kierunek = 'powrót';		
					
					if ($('#' + id[1] + '_city_details').length > 0){
						var info_miasta = $('#' + id[1] + '_city_details').val().split(':');						
					}
					else{
						var info_miasta = '';					}
				//	detal = 'znaleziono ' +ilosc_lotow+ ' połączeń, najtańsza oferta: <br /> cena biletu <span class="bold red2"> ' + details[0] + ' ' + details[1] + ' </span> '+  info_kierunek + ' ' + info_data + ' <span class="bold cena"> ' + details[3] + '-' + details[4] + ' </span> <br /> linią <span class="cena"> ' + details[2] + '</span>, ' + info_miasta[0] + ' (' + details[5] + ') - ' + info_miasta[1] + ' (' + details[6] + ')'
					if(ilosc_lotow == 1)
					    var odmiana = 'połączenie';
					else if(ilosc_lotow < 5)
						var odmiana = 'połączenia';
					else	
						var odmiana = 'połączeń';
					var info_lotniska = $('#lotnisko_' + id[1] + '_' + id[2]).val().split(':');
					var lotnisko_wylot = info_lotniska[0].split('|');
					var det_z = lotnisko_wylot[1] + ', ' + lotnisko_wylot[0] + ' (' + details[5] + '), ' + lotnisko_wylot[2];
			
					var lotnisko_przylot = info_lotniska[1].split('|');
					var det_to = lotnisko_przylot[1] + ', ' + lotnisko_przylot[0] + ' (' + details[6] + '), ' + lotnisko_przylot[2];
					detal = 'znaleziono ' + ilosc_lotow + 
						' '+ odmiana +', najtańsza oferta:  cena biletu <span class="bold red2"> ' + 
						details[0] + ' ' + details[1] + '</span>/os. <br/> ' + 
						info_kierunek + ' ' + info_data + ' <span class="bold cena"> ' + 
						details[3] + '-' + details[4] + ' </span>   linią <span class="cena"> ' + 
						details[2] + '</span>,<br/>  ' + lotnisko_wylot[1] + ' ' + lotnisko_wylot[0] + ' (' + details[5] + ') - ' + 
						lotnisko_przylot[1] + ' ' + lotnisko_przylot[0]  + ' (' + details[6] + ')';
					
					$('#' + item).html(detal);
					clearTimeout(t);
				}
			}, function(){
				if ($(this).attr("src") != './gfx/bar_red.gif') {
					details = $('#details_'+id[1]+'_'+id[2]).val().split(',');
					$(this).attr("src", "./gfx/bar_dark_blue.gif");
					$('#' + id[1] + '_' + id[2] + '_div').css('background-image', td_data_image);
					
					t = setTimeout(function(){
						details = $('#details_' + id[1] + '_' + id[2]).val().split(',');
						var inner = (id[1] == 'w') ? inner_w : inner_p;
						$('#' + item).html(inner);
					}, 1000);
					var inner = (id[1] == 'w') ? inner_w : inner_p;
					$('#' + id[1] + '_' + id[2] + '_div').css('background-image', td_data_image);
					
					if (inner != ''){
						$('#' + id[1] + '_time').html(''+refresh_arch+'');
					}
					else{
						$('#' + id[1] + '_time').html('');
					}
					if ($('#' + id[1] + '_time').html() == 'dane aktualne'){
							$('#' + id[1] + '_time').css('padding-top','5px'); 	
					}
					else{
						$('#' + id[1] + '_time').css('padding-top','0px'); 	
					}
				}
			});
		});
 		$("td." + prefix + "data").each(function(){
			var id = $(this).attr("id").split('_');
			var details = $('#details_'+id[0]+'_'+id[1]).val().split(',');
			var ident = 'img_'+id[0]+'_'+id[1];
			
			if (prefix == 'w_') {
				var item = 'description_wylot';
			}
			else {
				var item = 'description_przylot';
			}
			if (($('#details_' + id[0] + '_' + id[1]).val()!='') ) {
				$(this).hover(function(){
					if ($("#" + ident).attr("src") != './gfx/bar_red.gif') {
						//wstawianie czasu odświeżenia
						details = $('#details_' + id[0] + '_' + id[1]).val() != '' ? $('#details_' + id[0] + '_' + id[1]).val().split(',') : '';
						if (details && details[0] != 0) {
							val = obliczIleTemuOdswiezono($('#odswiezenie_' + id[0] + '_' + id[1]).val());
							$('#ods_time_' + id[0] + '_' + id[1]).val(val);
							var czas_ods = val;
							var val2= '';
								if (typeof(czas_ods) == 'string'){
									var tab_ods = new Array();
								    tab_ods = czas_ods.split(' ');	
									if (tab_ods[1] == 'minut'){
										czas_ods = tab_ods[0];
										val = 	czas_ods;
									}
									if (tab_ods[1] == 'dni' && tab_ods[0] > 5) {
										val2 = '> 5 dni'
									}	
									
								}
								if (typeof(czas_ods) == 'number'){
									
								}							
							if (val != '') {
								refresh_arch = $('#' + id[0] + '_time').html();
								if (czas_ods < 20) {
									$('#' + id[0] + '_time').css('padding-top', '5px');
									$('#' + id[0] + '_time').html('dane aktualne');
								}
								else {
									 if (val2 != ''){
									 	val = val2;
										$('#' + prefix + 'time').css('top','-3px');
									 }
									$('#' + id[0] + '_time').css('padding-top', '0px');
									$('#' + id[0] + '_time').html('<span class="odswiezono ta-center">odświeżono</span><span class="odswiezono  ta-center"><strong style="color: rgb(44, 101, 195); font-size: 9px;">' + val + '</strong> temu</span>');
								}
							}
							
							$('#' + ident).attr("src", "./gfx/bar_yellow.gif");
							td_data_image = $('#' + id[0] + '_' + id[1] + '_div').css('background-image');
							$('#' + id[0] + '_' + id[1] + '_div').css('background-image', 'url(./gfx/box_yellow.gif)');
							
							var info_data_loty = $('#' + id[0] + '_' + id[1] + '_td').attr('title').split(',');
							var info_data = info_data_loty[0];
							var ilosc_lotow = info_data_loty[1].split(':');
							ilosc_lotow = ilosc_lotow[1];
							if (id[0] == 'w') 
								var info_kierunek = 'wylot';
							if (id[0] == 'p') 
								var info_kierunek = 'powrót';
							var info_lotniska = $('#lotnisko_' + id[0] + '_' + id[1]).val().split(':');
							var lotnisko_wylot = info_lotniska[0].split('|');
							var lotnisko_przylot = info_lotniska[1].split('|');
							//detal = 'znaleziono ' +ilosc_lotow+ ' połączeń, najtańsza oferta: <br /> cena biletu <span class="bold red2"> ' + details[0] + ' ' + details[1] + ' </span> '+  info_kierunek + ' ' + info_data + ' <span class="bold cena"> ' + details[3] + '-' + details[4] + ' </span> <br /> linią <span class="cena"> ' + details[2] + '</span>, ' + info_miasta[0] + ' (' + details[5] + ') - ' + info_miasta[1] + ' (' + details[6] + ')'
							if (ilosc_lotow == 1) 
								var odmiana = 'połączenie';
							else 
								if (ilosc_lotow < 5) 
									var odmiana = 'połączenia';
								else 
									var odmiana = 'połączeń';
							
							detal = 'znaleziono ' + ilosc_lotow +
							' ' +
							odmiana +
							', najtańsza oferta:  cena biletu <span class="bold red2"> ' +
							details[0] +
							' ' +
							details[1] +
							'</span>/os. <br/> ' +
							info_kierunek +
							' ' +
							info_data +
							' <span class="bold cena"> ' +
							details[3] +
							'-' +
							details[4] +
							' </span>   linią <span class="cena"> ' +
							details[2] +
							'</span>,<br/>  ' +
							lotnisko_wylot[1] +
							' ' +
							lotnisko_wylot[0] +
							' (' +
							details[5] +
							') - ' +
							lotnisko_przylot[1] +
							' ' +
							lotnisko_przylot[0] +
							' (' +
							details[6] +
							')';
							$('#' + item).html(detal);
						//$('#' + item).html('<b style="color:#CB0000;">' + details[0] + ' ' + details[1] + '</b>, linią lotniczą ' + details[2] + ', <b>' + details[3] + '-' + details[4] + '</b>, ' + details[5] + '>' + details[6]);
						}
						clearTimeout(t);
					}
				}, function(){
					if ($("#" + ident).attr("src") != './gfx/bar_red.gif') {
						details = $('#details_' + id[0] + '_' + id[1]).val().split(',');
						$('#' + ident).attr("src", "./gfx/bar_dark_blue.gif");
						data_hover = id[1].split('-');
						data_obj = new Date();
						data_obj.setFullYear(data_hover[0], data_hover[1] - 1, data_hover[2]);
						day_of_week = data_obj.getDay();
						if ((day_of_week == 0) || (day_of_week == 6)) 
							$('#' + id[0] + '_' + id[1] + '_div').css('background-image', "url(./gfx/box_dark_blue.gif)");
						else 
							$('#' + id[0] + '_' + id[1] + '_div').css('background-image', "url(./gfx/box_light_blue.gif)");
						//						$('#' + id[0] + '_' + id[1] + '_div').css('background-image', td_data_image);
						t = setTimeout(function(){
							details = $('#details_' + id[0] + '_' + id[1]).val().split(',');
							var inner = (id[0] == 'w') ? inner_w : inner_p;
							$('#' + item).html(inner);
						}, 1000);
						$('#' + id[1] + '_' + id[2] + '_div').css('background-image', 'url(./gfx/box_light_blue.gif)');
						$('#' + id[0] + '_time').html('' + refresh_arch + '');
						if ($('#' + id[0] + '_time').html() == 'dane aktualne') {
							$('#' + id[0] + '_time').css('padding-top', '5px');
						}
						else{
							$('#' + id[0] + '_time').css('padding-top', '0px');
						}
					}
				});
			}
		});
		
	}
	clearZDO();
	$('#z').autocomplete(
		"ajax/ajax_autocomplete.php",
		{
			minChars:3,
			cacheLength:1,
			selectFirst:1,
			matchSubset:false,
			scroll: false,
			scrollHeight:350
		}
	);
	$('#to').autocomplete(
		"ajax/ajax_autocomplete.php",
		{
			minChars:3,
			cacheLength:1,
			selectFirst:1,
			matchSubset:false,
			scroll: false,
			scrollHeight:350
		}
	);	
	$('#z').focus(function(){
		
		$("#z").flushCache();
		$("#z").setOptions({
			extraParams: {
				limit:  '0,0,0;' +
						$('#kraj_do').val() + ',' +
						$('#lotniska_do').val() + ',' +
						$('#miasto_do').val()
				}
		});
	});
	$('#to').focus(function(){
		$("#to").flushCache();
		$("#to").setOptions({
			extraParams: {
				limit:  $('#kraj_z').val() + ',' +
						$('#lotniska_z').val() + ',' +
						$('#miasto_z').val() + ';0,0,0'
					}
		});
//		eval('document.forms[0].to.focus()');
	});
    $('input#z').result(function(event, data, formatted){
		if ( data.length < 2) {
			$('#z').val('');
			$('#kraj_z').val('0');
			$('#lotniska_z').val('0');
			$('#miasto_z').val('0');
		}
		else {
			var extra = data[1].split(';');
			
			$('#kraj_z').val(extra[0]);
			$('#lotniska_z').val(extra[1]);
			$('#miasto_z').val(extra[2]);
			$('#odmiana_z').val(extra[3]);
			$('#odmiana_z_nazwa').val(extra[4]);
			$('#odmiana_z_lotnisko_nazwa').val(extra[5]);			
			
			$("#z").flushCache();
		}
		$("#to").focus(); 
		
    });
    $('input#to').result(function(event, data, formatted){
		if ( data.length < 2) {
			$('#to').val('');
			$('#kraj_do').val('0');
			$('#lotniska_do').val('0');
			$('#miasto_do').val('0');
		}
		else {
			var extra = data[1].split(';');
			
			$('#kraj_do').val(extra[0]);
			$('#lotniska_do').val(extra[1]);
			$('#miasto_do').val(extra[2]);
			$('#odmiana_do').val(extra[3]);
			$('#odmiana_do_nazwa').val(extra[4]);	
			$('#odmiana_do_lotnisko_nazwa').val(extra[5]);		
			
			$("#to").flushCache();
		}
		  
		$("#submit").focus(); 
    });
});

$(document).ready(function(){

	$('.pokaz_ukryj_zmien').toggle(function() {
						$('#filtruj_top').attr('src','./gfx/top_left4.png');
						$('.wyszukiwarka_chowana').slideDown();
						$('.pasek_boczny_chowany').slideDown();
						
					}, function() {
						$('.wyszukiwarka_chowana').slideUp(function(){
							$('#filtruj_top').attr('src','./gfx/top_left3.png');
						});
						$('.pasek_boczny_chowany').slideUp();
						
					});
});


