﻿var m_num = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");	
var timer1 = new Array();
var timer2 = new Array();
var timer3;
var timeout;
var wyloty_fog = false;
var powroty_fog = false;
var diagram_w_data_start = null;	// od którego dnia jest wyświetlany diagram wylotu
var diagram_w_data_stop = null;	// do którego dnia jest wyświetlany diagram	wylotu
var diagram_p_data_start = null;	// od którego dnia jest wyświetlany diagram powrotu
var diagram_p_data_stop = null;	// do którego dnia jest wyświetlany diagram	powrotu
var czas_odsw_w;
var czas_odsw_p;
var array_tor = new Array(); 	// tablica w której zapisane są informacje czy w danym dniu jest jakaś linia kótra przechodzi przez Tora
var i_w=0;	//licznik dla odświeżania wylotu
var i_p=0;	//licznik dla odświeżania powrotu
var data_wylot;
var data_przylot;
var intervalW;
var intervalP;
var clicked;
var slupek_clicked_w = false;
var slupek_clicked_p = false;
var temp_wyszukiwarka = Array();
var first_check = true;
var checked_pillar = '';



function blockAll(pref){


  $('#'+pref+'info_sum').css('display','none');
  $('#'+pref+'progres').css('display','block');
  $('#'+pref+'mess2_box').fadeIn();

 var inputy = document.getElementsByTagName('input');
 var selecty = document.getElementsByTagName('select');
  
 for (i=0; i<inputy.length; i++){
  if ( ($(inputy[i]).attr('type')!= 'hidden') && ($(inputy[i]).attr('id') != 'w_refresh') && ($(inputy[i]).attr('id') != 'p_refresh') ){   
    $(inputy[i]).attr('disabled', 'true');   
  }   
 }
 for (i=0; i<selecty.length; i++){ 
    $(selecty[i]).attr('disabled', 'true');   
 }
 $('#submit_checked_day').attr('disabled','true');
 if (pref == 'w_'){
 	wyloty_fog = true;
 }
 else{
 	powroty_fog =true;
 }
 
 
}


function releaseAll(pref,mod){

  

 if (pref == 'w_'){
 	wyloty_fog = false;
 }
 else{
 	powroty_fog =false;
 }	
 var inputy = document.getElementsByTagName('input');
 var selecty = document.getElementsByTagName('select');
 if (pref == 'w_'){
 	var pref2 = 'p_';
 }
 else{
 	var pref2 = 'w_'; 
 } 
 

 
 if (wyloty_fog == false && powroty_fog == false) {
     for (i = 0; i < inputy.length; i++) {
         if (($(inputy[i]).attr('type') != 'hidden') && ($(inputy[i]).attr('id') != 'w_refresh') && ($(inputy[i]).attr('id') != 'p_refresh')) {
             $(inputy[i]).removeAttr('disabled');
         }
     }
     for (i = 0; i < selecty.length; i++) {
         if ($(selecty[i]).attr('type') != 'hidden') {
             $(selecty[i]).removeAttr('disabled');
         }
     }
    
 }

  $('#' + pref + 'mess2_box').fadeOut(function(){
  	$('#'+pref+'progres').css('display','none');
	  $('#'+pref+'cancel').css('display','none');	
	  if(mod != 'no_flight'){
		
	  	$('#'+pref+'info_sum').show();
	  }
	  else{
	  	$('#'+pref+'info_sum').hide();
	  }	  
		
  });
 
 

  	
  if (!$('#obie_strony').is(':checked')) {
  		$("#dzien_powrot").attr('disabled','disabled');
		$('#miesiac_powrot').attr('disabled','disabled');
		
  }	
 
 
}



//wstaw info o locie
function putDiagram(prefix, element, max, id, details, lotnisko_info, czas_odswiezenia, ilosc_lotow, tor){
	
	if (document.getElementById('img_' + prefix + element)) {
        document.getElementById('img_' + prefix + element).height = max;
    }
   
	czas = obliczIleTemuOdswiezono(czas_odswiezenia);

	$('#ods_time_' + prefix + element).val(czas); 
    $('#lotnisko_' + prefix + element).val(lotnisko_info);
	$('#odswiezenie_' +prefix + element).val(czas_odswiezenia);
	$('#' + prefix + element + '_td').unbind("click");
    $('#' + prefix + element + '_div').unbind("click");
	if(tor=='tak'){
		array_tor[prefix + element] = 'tak';
	}else{
		array_tor[prefix + element] = 'nie';
	}
    if ((document.getElementById('details_' + prefix + element)) && (typeof(details) != 'undefined')) {
		var click = $('#click_'+prefix+element).attr('value');
		
        $('#' + prefix + element + '_td').removeAttr('onclick');
        $('#' + prefix + element + '_td').click(function(){
			
		    eval(click);
			if (prefix == 'w_'){
				var month_source = 'miesiac_wylot';
				var day_source = 'dzien_wylot';
			}
			else{
				var month_source = 'miesiac_powrot';
				var day_source = 'dzien_powrot';					
			}
				var data_temp = $('#'+month_source).val().split(',');
				var miesiac = data_temp[0];
				var rok = data_temp[1];
				miesiac = odmienMiesiac(miesiac,'integer-toa');
				var data_slupka = $('#'+day_source).val() +' '+ miesiac +' ' + rok;
				
			if ($('#img_'+prefix+element).attr('src') == './gfx/bar_red.gif'){
				

				$('#'+prefix+'refresh').addClass('can_click');			
				$('#'+prefix+'info_sum').html('Wybrany lot '+data_slupka+' - odśwież słupek i/lub kliknij dalej');	
			}
			else{
				
				$('#' + prefix + 'miesiac_prawy').removeClass('bold');
				$('#' + prefix + 'miesiac_lewy').removeClass('bold');
				
				$('#'+prefix+'info_sum').html('Wybierz dzień');
				$('#'+prefix+'refresh').removeClass('can_click');
				
			}
        });
        $('#' + prefix + element + '_div').removeAttr('onclick');
        $('#' + prefix + element + '_div').click(function(){
            eval(click);
			
			if ($('#img_'+prefix+element).attr('src') == './gfx/bar_red.gif'){

				$('#'+prefix+'refresh').addClass('can_click');				
			}
			else{

				$('#'+prefix+'refresh').removeClass('can_click');
			}
        });
        $('#' + prefix + element + '_k').removeAttr('onclick');
        $('#' + prefix + element + '_k').click(function(){
            eval(click.getAttribute('value'));
        });
        $('#details_' + prefix + element).val(details);
		
		//dt = element;
		//alert(dt.getDay());
		data_slupka = element.split('-');
		dt = new Date();
		dt.setDate(data_slupka[2]);
		dt.setMonth(data_slupka[1]-1);
		dt.setFullYear(data_slupka[0]);

		$('#'+prefix+element+'_div').attr('title',data_slupka[2]+' '+nazwaMiesiaca2(data_slupka[1])+' '+data_slupka[0]+', ilość lotów: '+ilosc_lotow);
		$('#'+prefix+element+'_td').attr('title',data_slupka[2]+' '+nazwaMiesiaca2(data_slupka[1])+' ('+dayName(dt.getDay())+')'+' '+data_slupka[0]+', ilość lotów: '+ilosc_lotow);
    }

    var kolor = $('#img_' + prefix + element).attr('src');
    if (kolor != './gfx/bar_red.gif') {
        $('#img_' + prefix + element).attr("src", "./gfx/bar_dark_blue.gif");
    }
    
    //skalowanie słupków
    var rob_cena = 0;
    var max_cena = 0;
    var rob_details = '';
    $('.details_' + prefix + 'class').each(function(){
        rob_detail = $(this).val().split(',');
        rob_cena = rob_detail[0];
        if (parseInt(rob_cena, 10) > parseInt(max_cena, 10)) 
            max_cena = rob_cena;
		    
    });
    
    //zmiana cen w podziałce
    for (i = 1; i <= 5; i++) 
        $('#cena_' + prefix + i).html(Math.ceil(i / 5 * max_cena + 5) + ' PLN');
    
    var mnoznik = config.diagram_wysokosc / max_cena;
    var rob_id = '';
    $('.details_' + prefix + 'class').each(function(){
        rob_id = $(this).attr('id').split('_');
        
        rob_detail = $(this).val().split(',');
        rob_cena = rob_detail[0];
		if (rob_cena != 0 && $(this).attr('value') != ''){
			$('#img_' + prefix + rob_id[2]).attr('height', Math.ceil(mnoznik * rob_cena));
		}
        else if (rob_cena == 0 && $(this).attr('value') != ''){
			$('#img_' + prefix + rob_id[2]).attr('height', 1);
		}
    })
}

//wstaw czas ostatniego odswiezenia
function putTimestamp(prefix, data, response, spider){
		
    $('#' + prefix + 'refresh').removeClass('submit5');
    $('#' + prefix + 'refresh').addClass('submit3');
	val = obliczIleTemuOdswiezono($('#odswiezenie_' +prefix + data).val());
	
	if (spider){
		
			switch (prefix){
				case 'w_':
					if (czas_odsw_w == val) {
						releaseAll(prefix);
						
						if ($('#w_cancel').css('display') == 'block'){
							
							$('#w_cancel').css('display','none');
						}
						$('#description_wylot').empty();	
						inner_w = '';
						$('#w_info_sum').text('Nie wszystkie loty zostały odświeżone');
						$('#'+prefix+'time').html('Błąd').css({'padding-top':'5px','text-align':'right'});
						if ($('#diagram_p').attr('value') == 1) {
							$('#dummy').css('display', 'none');
						}
						else {
							$('#dalej_button').css('display', 'none');
						}
						$('#w_error_ok').css('display','block');
					}
					break;
				case 'p_':
					
					if (czas_odsw_p == val) {
						releaseAll(prefix);	
						inner_p = '';					
						if ($('#p_cancel').css('display') == 'block'){
							
							$('#p_cancel').css('display','none');
						}
						
						$('#dalej_button').css('display','none');	
						$('#description_przylot').empty();			
						$('#p_info_sum').text('Nie wszystkie loty zostały odświeżone');		
						$('#'+prefix+'time').html('Błąd').css({'padding-top':'5px','text-align':'right'});
						$('#p_error_ok').css('display','block');	
										
					}	
					break;					
			}
	}
	if(val == '14214 dni'){
		$('#' + prefix + 'time').html('');	
		return false;
	}
	var czas_ods = obliczIleTemuOdswiezono($('#odswiezenie_' +prefix + data).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;
				czas_ods = czas_ods + ' minut';
			}
			if (tab_ods[1] == 'dni' && tab_ods[0] > 5) {
				val2 = '> 5 dni'
			}
			czas_ods = czas_ods + ' ' + tab_ods[1];
			
		}
		if (typeof(czas_ods) == 'number'){
			
		}	
		
	$('#ods_time_' + prefix + data).val(val);
	
	
	
		if (val < 20) {
			$('#' + prefix + 'time').css('padding-top', '5px');
			$('#' + prefix + 'time').html('dane aktualne');
		}
		else {
			if (val2 != '') {
				val = val2;
				$('#' + prefix + 'time').css('top', '-3px');
			}
			if ($('#' + prefix + 'time').html() != 'Błąd') {
				$('#' + prefix + 'time').css('padding-top', '0px');
			}
			if (!slupek_clicked_p && !slupek_clicked_w) {
			
				$('#' + prefix + '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>');
			
			}
		}
		
	
}



function poprzedniTydzien(prefix, dataStart, dataStop, warunek, mnoznik, spider){
	ajaxManagerPoj = prefix == 'w_' ? (ajaxManagerPojWylot) : (ajaxManagerPojPowrot);
   	ajaxManagerPoj.abort();
	
	// strzałki dla następny tydzień wyświetl
	$('#diagram_'+prefix+'button_nastepnyTydzien').css('display','block');
	
	if (diagram_w_data_stop &&prefix=='w_') {
		dataStop = diagram_w_data_stop;
	}else if(diagram_p_data_stop &&prefix=='p_'){
		dataStop = diagram_p_data_stop;		
	}
	dataStop = dataStop.split('-');
	yearStop = parseInt(dataStop[0]);
	if(parseInt(dataStop[1])<10)
		monthStop = parseInt(dataStop[1].replace('0',''));
	else
		monthStop = parseInt(dataStop[1]);
	if(parseInt(dataStop[2])<10)
		dayStop = parseInt(dataStop[2].replace('0',''));
	else
		dayStop = parseInt(dataStop[2]);
	if (diagram_w_data_start&&prefix=='w_') {
		dataStart = diagram_w_data_start;
	}else if(diagram_p_data_start &&prefix=='p_'){
		dataStart = diagram_p_data_start;	
	}
	dataStart = dataStart.split('-');
	yearStart = parseInt(dataStart[0]);
	if(parseInt(dataStart[1])<10)
		monthStart = parseInt(dataStart[1].replace('0',''));
	else
		monthStart = parseInt(dataStart[1]);
	if(parseInt(dataStart[2])<10)
		dayStart = parseInt(dataStart[2].replace('0',''));
	else
		dayStart = parseInt(dataStart[2]);
	data_dzis = new Date();
	rok_dzis = data_dzis.getFullYear();
	msc_dzis = data_dzis.getMonth()+1;
	dzien_dzis = data_dzis.getDate();
	for (var i = 0; i < 7; i++) {
		first_month_day = '';
		first_mont_day_div = '';
		first_month_day_pusty = '';
		dayStart = dayStart - 1;
		if ((yearStart > rok_dzis) || (yearStart >= rok_dzis && monthStart > msc_dzis) || (yearStart >= rok_dzis && monthStart >= msc_dzis && dayStart >= dzien_dzis)) {
		
			// przejście do poprzedniego mięsiąca daty końcowej
			if (dayStop <= 0) {
				monthStop = monthStop - 1;
				// przejście do poprzedniego miesiąca
				if (monthStop <= 0) {
					yearStop = yearStop - 1;
					monthStop = 12;
				}
				ile_dni_w_msc = new Date(yearStop, monthStop - 1);
				ile_dni_w_msc = ile_dni_w_msc.getDaysInMonth();
				dayStop = ile_dni_w_msc;
				$('#' + prefix + 'miesiac_prawy').html(nazwaMiesiaca(monthStop) + ' ' + yearStop);
			}
			dayStop_show = (dayStop) < 10 ? '0' + (dayStop) : (dayStop);
			monthStop_show = monthStop < 10 ? '0' + monthStop : monthStop;
			dataStop_show = yearStop + '-' + monthStop_show + '-' + dayStop_show;
			$('#' + prefix + dataStop_show + '_td').remove();
			$('#' + prefix + dataStop_show + '_div').remove();
			$('#' + prefix + dataStop_show + '_pusty').remove();
			
			// przejście do poprzedniego mięsiąca daty startowej
			if (dayStart <= 0) {
				$('#' + prefix + yearStart + '-' + (monthStart < 10 ? '0' + monthStart : monthStart) + '-01_td').addClass('firt_day_of_month_td');
				$('#' + prefix + yearStart + '-' + (monthStart < 10 ? '0' + monthStart : monthStart) + '-01_div').addClass('first_day_od_month_div');
				$('#' + prefix + yearStart + '-' + (monthStart < 10 ? '0' + monthStart : monthStart) + '-01_pusty').addClass('fist_day_of_month_pusty');
				monthStart = monthStart - 1;
				// przejście do poprzedniego miesiąca
				if (monthStart <= 0) {
					yearStart = yearStart - 1;
					monthStart = 12;
				}
				ile_dni_w_msc = new Date(yearStart, monthStart - 1);
				ile_dni_w_msc = ile_dni_w_msc.getDaysInMonth();
				dayStart = ile_dni_w_msc;
				$('#' + prefix + 'miesiac_lewy').html(nazwaMiesiaca(monthStart) + ' ' + yearStart);
			}
			dayStart_show = (dayStart < 10 ? '0' + dayStart : dayStart);
			monthStart_show = monthStart < 10 ? '0' + monthStart : monthStart;
			dataStart_show = yearStart + '-' + monthStart_show + '-' + dayStart_show;
			if (i < 6 && dayStart == 1) {
				first_month_day = 'border-left: 6px solid rgb(236, 241, 248); border-bottom: 3px solid rgb(255, 255, 255); padding-left: 5px;';
				first_mont_day_div = 'border-left: 6px solid rgb(236, 241, 248); padding-left: 5px; background-position: 5px 0px;';
				first_month_day_pusty = 'border-left: 6px solid rgb(236, 241, 248); padding-left: 5px; background-position: 5px 0px;';
			}
			td_td = '<td id="' + prefix + dataStart_show + '_td" class="diagram_kreseczki" height="158" valign="bottom" align="center" onclick="showDetNew(this,\'' + prefix + '\',\'description_wylot\',\'' + dayStart_show + '\',30);" title="" rowspan="4" style=" ' + first_month_day + ' border-bottom:3px solid #FFF;">' +
			'<img id="img_' +
			prefix +
			dataStart_show +
			'" class="' +
			prefix +
			'slupki" height="0" width="9" alt="" style="cursor:pointer; margin-right:4px;" src="./gfx/bar_dark_blue.gif"/>' +
			'<input id="click_' +
			prefix +
			dataStart_show +
			'" type="hidden" value="showDetNew(this,\'' +
			prefix +
			'\',\'description_wylot\',\'' +
			dayStart_show +
			'\',30);"/>' +
			'</td>';
			
			img = './gfx/box_light_blue.gif';
			day_tmp = new Date(yearStart, monthStart - 1, dayStart);
			day_tmp = day_tmp.getDay();
			if (day_tmp == 0 || day_tmp == 6) {
				img = './gfx/box_dark_blue.gif';
			}
			
			td_div = '<td id="' + prefix + dataStart_show + '_div" class="' + prefix + 'data" align="center" style=" ' + first_mont_day_div + ' background-image: url(' + img + '); background-repeat: no-repeat; cursor: pointer;" title=" ">' +
			'<span style="font-size:9px;top:-1px;position:relative;">' +
			dayStart_show +
			'</span>&nbsp;' +
			'<input id="details_' +
			prefix +
			dataStart_show +
			'" class="details_' +
			prefix +
			'class" type="hidden" value="" name="details"/>' +
			'<input id="lotnisko_' +
			prefix +
			dataStart_show +
			'" type="hidden" value="" name="lotnisko"/>' +
			'<input id="odswiezenie_' +
			prefix +
			dataStart_show +
			'" type="hidden" value="" name="odswiezenie"/>' +
			'<input id="ods_time_' +
			prefix +
			dataStart_show +
			'" type="hidden" value="" name="ods_time"/>' +
			'</td>';
			
			td_pusty = '<td id="' + prefix + dataStart_show + '_pusty" align="center" </td>'
			
			$('#' + prefix + 'tr_slupki #' + prefix + 'td_wstaw_slupki_new').next('td').before(td_td);
			$('#' + prefix + 'tr_numerki #' + prefix + 'td_wstaw_numerki_new').next('td').before(td_div);
			$('#' + prefix + 'pusty_tr #' + prefix + 'pusty_new').next('td').before(td_pusty);
			
			// nakładanie efektów po najechaniu myszką dla nowo doładowanych słupków
			addHoverEfect(prefix,dataStart_show);
			
			dayStop = dayStop - 1;
		}
		else {
			$('#diagram_' + prefix + 'button_poprzdniTydzien').css('display', 'none');
			break;
		}
	}
	// potrzebne dla poprawnej zmiany miesiąca i roku 
	if (dayStop <= 0) {
		monthStop = monthStop - 1;
		// przejście do poprzedniego miesiąca
		if (monthStop <= 0) {
			yearStop = yearStop - 1;
			monthStop = 12;
		}
		ile_dni_w_msc = new Date(yearStop, monthStop - 1);
		ile_dni_w_msc = ile_dni_w_msc.getDaysInMonth();
		dayStop = ile_dni_w_msc;
		$('#' + prefix + 'miesiac_prawy').html(nazwaMiesiaca(monthStop) + ' ' + yearStop);
	}
	//chowanie strałki poprzedni dzień
	if (!((yearStart > rok_dzis) || (yearStart >= rok_dzis && monthStart > msc_dzis) || (yearStart >= rok_dzis && monthStart >= msc_dzis && dayStart > dzien_dzis))) {
		$('#diagram_' + prefix + 'button_poprzdniTydzien').css('display', 'none');
	}	
	dataGetNew_od = yearStart+'-'+monthStart+'-'+dayStart;
	dataGetNew_do = yearStart+'-'+monthStart+'-'+(dayStart+i);
	if (prefix == 'w_') {
		diagram_w_data_start = dataGetNew_od;
		diagram_w_data_stop = yearStop +'-'+monthStop+'-'+dayStop;
	}else{
		diagram_p_data_start = dataGetNew_od;
		diagram_p_data_stop = yearStop +'-'+monthStop+'-'+dayStop;
	}	
	
	//wyświetlenie 7 słupków na początku
	getLoty(prefix, dataGetNew_od, dataGetNew_do, warunek, mnoznik, 0, false);
}

function nastepnyTydzien(prefix, dataStart, dataStop, warunek, mnoznik, spider){
	ajaxManagerPoj = prefix == 'w_' ? (ajaxManagerPojWylot) : (ajaxManagerPojPowrot);
   	ajaxManagerPoj.abort();
	
	$('#diagram_' + prefix + 'button_poprzdniTydzien').css('display', 'block');

	if (diagram_w_data_stop &&prefix=='w_') {
		dataStop = diagram_w_data_stop;
	}else if(diagram_p_data_stop &&prefix=='p_'){
		dataStop = diagram_p_data_stop;		
	}
	dataStop = dataStop.split('-');
	yearStop = parseInt(dataStop[0]);
	yearGetNew_od = yearStop;
	dayStop = parseInt(dataStop[2]);
	if(dataStop[1]<10)
		monthStop = parseInt(dataStop[1].replace('0',''));
	else
		monthStop = parseInt(dataStop[1]);
	monthGetNew_od =monthStop;	
	if (dataStop[2] < 10) {
		dayStop = parseInt(dataStop[2].replace('0', ''));
		dayGetNew_od = dayStop;
	}
	else {
		dayStop = parseInt(dataStop[2]);
		dayGetNew_od = dayStop;	
	}
	if (diagram_w_data_start&&prefix=='w_') {
		dataStart = diagram_w_data_start;
	}else if(diagram_p_data_start &&prefix=='p_'){
		dataStart = diagram_p_data_start;	
	}
	dataStart = dataStart.split('-');
	yearStart = parseInt(dataStart[0]);
	if(dataStart[1]<10)
		monthStart = parseInt(dataStart[1].replace('0',''));
	else
		monthStart = parseInt(dataStart[1]);
	if(dataStart[2]<10)
		dayStart = parseInt(dataStart[2].replace('0',''));
	else
		dayStart = parseInt(dataStart[2]);
	for (var i = 0; i < 7; i++) {
		first_month_day = '';
		first_mont_day_div = '';
		first_month_day_pusty = '';
		dayStop = dayStop + 1;
			// przejście do następnego mięsiąca daty początkowej
			ile_dni_w_msc = new Date(yearStart, monthStart - 1);
			ile_dni_w_msc = ile_dni_w_msc.getDaysInMonth();
			if (dayStart > ile_dni_w_msc) {
				monthStart = monthStart + 1;
				// przejście do następnego miesiąca
				if (monthStart >12) {
					yearStart = yearStart + 1;
					monthStart = 1;
				}
				dayStart = 1;
				$('#' + prefix + 'miesiac_lewy').html(nazwaMiesiaca(monthStart) + ' ' + yearStart);
			}
			dayStart_show = (dayStart) < 10 ? '0' + (dayStart) : (dayStart);
			monthStart_show = monthStart < 10 ? '0' + monthStart : monthStart;
			dataStart_show = yearStart + '-' + monthStart_show + '-' + dayStart_show;
			$('#' + prefix + dataStart_show + '_td').remove();
			$('#' + prefix + dataStart_show + '_div').remove();
			$('#' + prefix + dataStart_show + '_pusty').remove();
			
			// przejście do następnego mięsiąca daty końcowej
			ile_dni_w_msc = new Date(yearStop, monthStop - 1);
			ile_dni_w_msc = ile_dni_w_msc.getDaysInMonth();
			if (dayStop > ile_dni_w_msc) {
				monthStop = monthStop + 1;
				// przejście do poprzedniego miesiąca
				if (monthStop >12) {
					yearStop = yearStop + 1;
					monthStop = 1;
				}
				dayStop = 1;
				$('#' + prefix + 'miesiac_prawy').html(nazwaMiesiaca(monthStop) + ' ' + yearStop);
				$('#' + prefix + yearStop + '-' + (monthStop < 10 ? '0' + monthStop : monthStop) + '-01_td').addClass('firt_day_of_month_td');
				$('#' + prefix + yearStop + '-' + (monthStop < 10 ? '0' + monthStop : monthStop) + '-01_div').addClass('first_day_od_month_div');
				$('#' + prefix + yearStop + '-' + (monthStop < 10 ? '0' + monthStop : monthStop) + '-01_pusty').addClass('fist_day_of_month_pusty');
				
			}
			dayStop_show = (dayStop < 10 ? '0' + dayStop : dayStop);
			monthStop_show = monthStop < 10 ? '0' + monthStop : monthStop;
			dataStop_show = yearStop + '-' + monthStop_show + '-' + dayStop_show;
			if ( dayStop == 1) {
				first_month_day = 'firt_day_of_month_td';
				first_mont_day_div = 'first_day_od_month_div';
				first_month_day_pusty = 'fist_day_of_month_pusty';
			}
			td_td = '<td id="' + prefix + dataStop_show + '_td" class="diagram_kreseczki ' + first_month_day + '" height="158" valign="bottom" align="center" onclick="showDetNew(this,\'' + prefix + '\',\'description_wylot\',\'' + dayStop_show + '\',30);" title="" rowspan="4" style="  border-bottom:3px solid #FFF;">' +
			'<img id="img_'+prefix+dataStop_show +'" class="' +prefix +
			'slupki" height="0" width="9" alt="" style="cursor:pointer; margin-right:4px;" src="./gfx/bar_dark_blue.gif"/>' +
			'<input id="click_'+prefix+dataStop_show+'" type="hidden" value="showDetNew(this,\'' +
			prefix+'\',\'description_wylot\',\''+dayStop_show+'\',30);"/>'+'</td>';
			
			img = './gfx/box_light_blue.gif';
			day_tmp = new Date(yearStop, monthStop - 1, dayStop);
			day_tmp = day_tmp.getDay();
			if (day_tmp == 0 || day_tmp == 6) {
				img = './gfx/box_dark_blue.gif';
			}
			
			td_div = '<td id="' + prefix + dataStop_show + '_div" class="' + prefix + 'data ' + first_mont_day_div + '" align="center" style=" background-image: url(' + img + '); background-repeat: no-repeat; cursor: pointer;" title=" ">' +
			'<span style="font-size:9px;top:-1px;position:relative;">' +
			dayStop_show +
			'</span>&nbsp;' +
			'<input id="details_' +
			prefix +
			dataStop_show +
			'" class="details_' +
			prefix +
			'class" type="hidden" value="" name="details"/>' +
			'<input id="lotnisko_' +
			prefix +
			dataStop_show +
			'" type="hidden" value="" name="lotnisko"/>' +
			'<input id="odswiezenie_' +
			prefix +
			dataStop_show +
			'" type="hidden" value="" name="odswiezenie"/>' +
			'<input id="ods_time_' +
			prefix +
			dataStop_show +
			'" type="hidden" value="" name="ods_time"/>' +
			'</td>';
			
			td_pusty = '<td id="' + prefix + dataStop_show + '_pusty" class="'+first_month_day_pusty+'" >&nbsp; </td>'
			
			$('#' + prefix + 'tr_slupki #'+prefix+'td_wstaw_slupki_last').before(td_td);
			$('#' + prefix + 'tr_numerki #'+prefix+'td_wstaw_numerki_last').before(td_div);
			$('#'+prefix+'pusty_tr #'+prefix+'pusty_last').before(td_pusty);
			// nakładanie efektów po najechaniu myszką dla nowo doładowanych słupków
			addHoverEfect(prefix,dataStop_show);
			dayStart = dayStart + 1;
	}
	// potrzebne dla poprawnej zmiany miesiąca i roku 
	ile_dni_w_msc = new Date(yearStart, monthStart - 1);
	ile_dni_w_msc = ile_dni_w_msc.getDaysInMonth();
	if (dayStart > ile_dni_w_msc) {
		monthStart = monthStart + 1;
		// przejście do następnego miesiąca
		if (monthStart > 12) {
			yearStart = yearStart + 1;
			monthStart = 1;
		}
		dayStart = 1;
		$('#' + prefix + 'miesiac_lewy').html(nazwaMiesiaca(monthStart) + ' ' + yearStart);
		if(dayStart==1){
					$('#' + prefix + yearStart + '-' + (monthStart < 10 ? '0' + monthStart : monthStart) + '-01_td').removeClass('firt_day_of_month_td');
					$('#' + prefix + yearStart + '-' + (monthStart < 10 ? '0' + monthStart : monthStart) + '-01_div').removeClass('first_day_od_month_div');
					$('#' + prefix + yearStart + '-' + (monthStart < 10 ? '0' + monthStart : monthStart) + '-01_pusty').removeClass('fist_day_of_month_pusty');
				
		}
	}
	dataGetNew_od = yearGetNew_od+'-'+monthGetNew_od+'-'+dayGetNew_od;
	dataGetNew_do = yearStop+'-'+monthStop+'-'+dayStop;
	if (prefix == 'w_') {
		diagram_w_data_start = yearStart +'-'+monthStart+'-'+dayStart;
		diagram_w_data_stop = yearStop +'-'+monthStop+'-'+dayStop;
	}else{
		diagram_p_data_start = yearStart +'-'+monthStart+'-'+dayStart;
		diagram_p_data_stop = yearStop +'-'+monthStop+'-'+dayStop;
	}	
	
	//wyświetlenie 7 słupków na początku
	getLoty(prefix, dataGetNew_od, dataGetNew_do, warunek, mnoznik, 0, false);
	sprawdzCzySaLoty(prefix,yearStop,monthStop,dayStop);		
}

function sprawdzCzySaLoty(prefix,year,month,day){
	if(prefix == 'w_'){
		last_data = $('#miesiac_wylot').children('option:last-child').val();  
		last_data = last_data.split(',');
		if (!((year < last_data[1]) || (year <= last_data[1] && month < last_data[0]) || (year <= last_data[1] && month <= last_data[0] && day < last_data[2]))) {
			$('#diagram_w_button_nastepnyTydzien').css('display','none');
		}
	}else{
		last_data = $('#miesiac_powrot').children('option:last-child').val();  
		last_data = last_data.split(',');
		if (!((year < last_data[1]) || (year <= last_data[1] && month < last_data[0]) || (year <= last_data[1] && month <= last_data[0] && day < last_data[2]))) {
			$('#diagram_p_button_nastepnyTydzien').css('display','none');
		}
	} 
}
function addHoverEfect(prefix,data){
	
    
    if (prefix == 'w_') {
        var item = 'description_wylot';
    }
    else {
        var item = 'description_przylot';
    }
	$('#img_' + prefix + data).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;
						var id = $(this).attr("id").split('_');
						
				
						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();
							if (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 (typeof(czas_ods) == 'number'){
							
						}		
									
					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';		
			
					var info_miasta = $('#' + id[1] + '_city_details').val().split(':');
					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(){
				var id = $(this).attr("id").split('_');
				var details = $('#details_'+id[1]+'_'+id[2]).val().split(',');
				var ident = 'img_'+id[1]+'_'+id[2];
				if ($("#" + ident).attr("src") != './gfx/bar_red.gif') {
					
					
					details = $('#details_' + id[1] + '_' + id[2]).val().split(',');
					$('#' + ident).attr("src", "./gfx/bar_dark_blue.gif");
					data_hover = id[2].split('-');
					data_obj = new Date();
					data_obj.setFullYear(data_hover[1], data_hover[2] - 2, data_hover[2]);
					day_of_week = data_obj.getDay();
					if((day_of_week == 1) || (day_of_week == 6))
						$('#' + id[1] + '_' + id[2] + '_div').css('background-image', "url(./gfx/box_dark_blue.gif)");
					else
						$('#' + id[1] + '_' + id[2] + '_div').css('background-image', "url(./gfx/box_light_blue.gif)");
					var inner = '';
					t = setTimeout(function(){
						details = $('#details_' + id[1] + '_' + id[2]).val().split(',');
						inner = (id[1] == 'w') ? inner_w : inner_p;
						$('#' + item).html(inner);
					}, 1000);
					$('#' + id[1] + '_' + id[2] + '_div').css('background-image', 'url(./gfx/box_light_blue.gif)');
					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").hover(function(){
				var id = $(this).attr("id").split('_');
				var details = $('#details_'+id[0]+'_'+id[1]).val().split(',');
				var ident = 'img_'+id[0]+'_'+id[1];
				if ($("#" + ident).attr("src") != './gfx/bar_red.gif') {
						var checked = null;
						var id = $(this).attr("id").split('_');
				
						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();
							if (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';
									}
							}
						}	
					
					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(prefix == 'w_')
							var info_kierunek = 'wylot';
						if(prefix == 'p_')
							var info_kierunek = 'powrót';
						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[0] + '_' + id[1]).val().split(':');
						var lotnisko_wylot = info_lotniska[0].split('|');
						var lotnisko_przylot = info_lotniska[1].split('|');
			        	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(){
				var id = $(this).attr("id").split('_');
				var details = $('#details_'+id[0]+'_'+id[1]).val().split(',');
				var ident = 'img_'+id[0]+'_'+id[1];
				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)");
					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[0] + '_' + id[1] + '_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'); 	
					}
				}
			});
			
}

/**
 * funkcja wyłącza w wyszukiwarce kryteria zaawansowane
 */
function disableAdvancedSearch(data_wylot,data_powrot){
	var select_month = '';
	var wylot_month = data_wylot.split(',');
	$('#miesiac_wylot').children('option').each(function(){
		select_month = $(this).val().split(',');
		if(select_month[0] == wylot_month[0] && select_month[1] == wylot_month[1]){
			$(this).attr('selected','selected');
		}else{
			$(this).removeAttr('selected');
		}
	});
	if(data_powrot != 'false')
	{		
		var select_month_powrot = '';
		var powrot_month = data_powrot.split(',');
		$('#miesiac_powrot').children('option').each(function(){
			select_month = $(this).val().split(',');
			if(select_month[0] == powrot_month[0] && select_month[1] == powrot_month[1]){
				$(this).attr('selected','selected');
			}else{
				$(this).removeAttr('selected');
			}
		});
	}
	$('#zaawansowane').removeAttr('checked');
	$('#only_direct').removeAttr('checked');
	$('#submit').click();
}


// funkcja ustawia odpowiednie pola formularza i wyłącza opcję 'tylko bezpośrednie'
function changeToNotDirect(data_wylot,data_powrot){
	var select_month = '';
	var wylot_month = data_wylot.split(',');
	$('#miesiac_wylot').children('option').each(function(){
		select_month = $(this).val().split(',');
		if(select_month[0] == wylot_month[0] && select_month[1] == wylot_month[1]){
			$(this).attr('selected','selected');
		}else{
			$(this).removeAttr('selected');
		}
	});
	if(data_powrot != 'false')
	{		
		var select_month_powrot = '';
		var powrot_month = data_powrot.split(',');
		$('#miesiac_powrot').children('option').each(function(){
			select_month = $(this).val().split(',');
			if(select_month[0] == powrot_month[0] && select_month[1] == powrot_month[1]){
				$(this).attr('selected','selected');
			}else{
				$(this).removeAttr('selected');
			}
		});
	}
	$('#only_direct').removeAttr('checked');
	$('#submit').click();
}

//pobierz wiele lotow
function getLoty(prefix, dataStart, dataStop, warunek, mnoznik, spider, klik){
    ajaxManagerPoj = prefix == 'w_' ? (ajaxManagerPojWylot) : (ajaxManagerPojPowrot);
   	ajaxManagerPoj.abort();
	var w_brak_lotow = true;
	var p_brak_lotow = true;
	if (klik == true && !$('#'+prefix+'refresh').hasClass('can_click')){
		showInfo('Wybierz dzień w celu odświeżenia słupka z lotami','info');
		return false;
	}	
	blockAll(prefix);	
	var data_interval = dataStart;
    var ttl = new Date();
    var t_dataStart = dataStart.split("-");
    var t_dataStop = dataStop.split("-");
	var temp_dataStart = dataStart;
	var temp_dataStop = dataStop;
	

    var dataStart = new Date(t_dataStart[0], t_dataStart[1] - 1, t_dataStart[2]);
    var dataStop = new Date(t_dataStop[0], t_dataStop[1] - 1, t_dataStop[2]);
    var ile_dni = Math.ceil((dataStop.getTime() - dataStart.getTime()) / (1000 * 60 * 60 * 24)) + 1;
    $('#' + prefix + 'refresh').removeClass('submit3');
    $('#' + prefix + 'refresh').addClass('submit5');
    
    ttl = ttl.getFullYear() + ',' + ttl.getMonth() + ',' + ttl.getDate() + ',' + ttl.getHours() + ',' + ttl.getMinutes();

    
    var dataQueue = new Array();
    for (var i = 0; i < ile_dni; i++) {
        dataQueue[i] = dataStart.getTime() + 1000 * 60 * 60 * 24 * (i);
    }
    dataQueue = dataQueue.shuffle();
    if (prefix == 'w_') {
	
		id_linia_for_link = $('#wybierz_linie_link').val();
	
		
        for (key in dataQueue) {
			if(typeof(dataQueue[key]) != 'function'){	
                data = dataQueue[key];
                next_day = new Date(data);
                var rok = next_day.getFullYear();
                var miesiac = m_num[next_day.getMonth()];
                var dzien = next_day.getDate() < 10 ? "0" + next_day.getDate() : next_day.getDate();
                
                data_interval = rok + '-' + miesiac + '-' + dzien;
                //odswiez slupek
                ajaxManagerWylot.add({
                    url: 'index_ajax.php',
                    data: {
                        mod: 'main',
                        scr: 'wyszukiwarka',
                        evt: 'ajaxGetCheapestFlightDay',
                        action: "Diagram",
                        prefix: prefix,
                        data: data,
                        warunek: warunek,
                        mnoznik: mnoznik,
                        ttl: ttl,
                        aktualizuj: spider,
						id_linie_lot: id_linia_for_link
                    },
                    success: function(response){
					    //przetworz odpowiedz
						eval(response);
						
						var zm = new Array();
						 zm = response.split("(");
						 if (zm[0] == 'putDiagram' ){
						 	w_brak_lotow = false;
						 }
						
                    },
                    type: "GET",
                    queued: true
                });
            }

        }


        ajaxManagerWylot.add({
            url: 'ajax/ajax_wyniki.php',
            data: {
                action: "getTimestamp",
                prefix: prefix,
                data: data_interval
            },
            success: function(response){
                putTimestamp(prefix, data_interval, response);
				releaseAll(prefix);
				
				
				if(!slupek_clicked_w) {
					$('#w_time').html(' ');
				}
				
				if(!slupek_clicked_p) {
					$('#p_time').html(' ');
				}
				
				if (w_brak_lotow) {
					
					if (spradzCzyWidacJakisSlupek(prefix)) {

					}
					else{
						
						blockAll(prefix);
						
						from = $('#get_z').val();
						to = $('#get_do').val();
						data_wylot = $('#get_d_one').val();
						data_powrot = $('#get_d_two').val();
						if (prefix == 'w_'){
							
							temp_dataStart = temp_dataStart.split('-');
							var zero = '';
							
							if (temp_dataStart[1].length<2){
								zero = '0';
							}
							var data_wylot = zero+temp_dataStart[1]+temp_dataStart[0].substring(2,4);							
						}
					
//							data_powrot = temp_dataStart[2];
							
						 ajaxManagerWylot.add({
				            url: 'ajax/ajax_dataNajblizszegoLotu.php',
							dataType: 'json',
				            data: {
				                prefix: prefix,
				                data: data_interval,
								warunek: warunek,
								z : from,
								"do" : to,
								d_one : data_wylot,
								d_two : data_powrot,
								prefix : 'w'
				            },
				            success: function(response){
								var fly_from = $('#z').val().replace(' - wszystkie','') ;
								var fly_to =  $('#to').val().replace(' - wszystkie','') ;
								
								
								
								if (response['type'] == 'noFlightsInMonth_fromto'){

									if(response['only_direct'] == '1')
									{
										var div = ''+
										'<div style="margin:0px auto; padding-top:20px; text-align:center; color:#2663B8; width:530px;">'+
											'<p style="margin-top:0px;">Niestety w <b>'+response['this_month']+'</b> nie ma żadnych <b>bezpośrednich</b> lotów w bazie z  <span class="bold" id="error_from"> '+fly_from+' </span>'+
											'do <span class="bold" id="error_to"> '+fly_to+'</span>. <br/>'+
											'Najbliższe loty <b>z przesiadkami</b> w tym kierunku są w '+response['next_month']+'.</p>'+
											'<p><a style="font-weight:bold" href="'+response['link']+'" onclick="changeToNotDirect(\''+response['wylot_data']+'\',\''+response['powrot_data']+'\');return false;">Kliknij tutaj</a>, aby przejrzeć najbliższe loty dla tego kierunku lub wybierz inny kierunek w wyszukiwarce.</p>'+
											'<p>Nie znalazłeś odpowiedniego lotu? Zadzwoń do nas <b>022 451 00 50</b>. Znajdziemy dla Ciebie najtańszą opcję.</p>'+
										'</div>'; 										
									}
									else
									{
										var div = ''+
										'<div style="margin:0px auto; padding-top:20px; text-align:center; color:#2663B8; width:530px;">'+
											'<p style="margin-top:0px;">Niestety w <b>'+response['this_month']+'</b> nie ma żadnych lotów w bazie z  <span class="bold" id="error_from"> '+fly_from+' </span>'+
											'do <span class="bold" id="error_to"> '+fly_to+' </span>. <br/>'+
											'Najbliższe loty w tym kierunku są w '+response['next_month']+'.</p>'+
											'<p><a style="font-weight:bold" href="'+response['link']+'" >Kliknij tutaj</a>, aby przejrzeć najbliższe loty dla tego kierunku lub wybierz inny kierunek w wyszukiwarce.</p>'+
											'<p>Nie znalazłeś odpowiedniego lotu? Zadzwoń do nas <b>022 451 00 50</b>. Znajdziemy dla Ciebie najtańszą opcję.</p>'+
										'</div>'; 										
									}
								}								
								$('#diagram_wylot').fadeOut(function(){
									$('#w_time, #description_wylot').html(' ');
									$('#diagram_wylot').remove();
									$('#'+prefix+'main_main').append(div);
									releaseAll(prefix,'no_flight');
									$('#chose_return').css('visibility','hidden');
									$('#w_refresh_holder').css('visibility','hidden');
								});
								
								
								
				            },
				            type: "GET",
				            queued: true
				        });
					}
					
				}
            },
            type: "GET",
            queued: true
        });
		    
			$('#' + prefix + 'info_cancel').click(function(){	
				
				$('#' + prefix + 'info_cancel').attr('id');
				ajaxManagerWylot.abort();	
			    $('#' + prefix + 'refresh').removeClass('submit5');
			    $('#' + prefix + 'refresh').addClass('submit3');	
				releaseAll(prefix);	
			});	
    }
    else {
	
        for (key in dataQueue) {
            //sprawdz czy data jest timestampem ( bo pojawia sie inna wartosc w dataQueue )		
            numRegExp = /^[0-9]+$/
            if (numRegExp.test(dataQueue[key])) {
                data = dataQueue[key];
                next_day = new Date(data);
                
                var rok = next_day.getFullYear();
                var miesiac = m_num[next_day.getMonth()];
                var dzien = next_day.getDate() < 10 ? "0" + next_day.getDate() : next_day.getDate();
                
                data_interval = rok + '-' + miesiac + '-' + dzien;
                //odswiez slupek
                ajaxManagerPowrot.add({
                    url: 'index_ajax.php',
                    data: {
                        mod: 'main',
                        scr: 'wyszukiwarka',
                        evt: 'ajaxGetCheapestFlightDay',
                        action: "Diagram",
                        prefix: prefix,
                        data: data,
                        warunek: warunek,
                        mnoznik: mnoznik,
                        ttl: ttl,
                        aktualizuj: spider
                    },
                    success: function(response){
                        //przetworz odpowiedz
                        eval(response);
						
						var zm2 = new Array();
						 zm2 = response.split("(");
						 if (zm2[0] == 'putDiagram'){
						 	p_brak_lotow = false;
							
						 }
						
                    },
                    type: "GET",
                    queued: true
                });
                
            }
			
        }
        
        ajaxManagerPowrot.add({
            url: 'ajax/ajax_wyniki.php',
            data: {
                action: "getTimestamp",
                prefix: prefix,
                data: data_interval
            },
            success: function(response){
                putTimestamp(prefix, data_interval, response);
				releaseAll(prefix);
				
				if(!slupek_clicked_w) {
					$('#w_time').html(' ');
				}
				
				if(!slupek_clicked_p) {
					$('#p_time').html(' ');
				}
				
				if (p_brak_lotow) {
					if (spradzCzyWidacJakisSlupek(prefix)) {

					}else{
						blockAll(prefix);
						
						from = $('#get_z').val();
						to = $('#get_do').val();
						data_wylot = $('#get_d_one').val();
						data_powrot = $('#get_d_two').val();
						
						 ajaxManagerWylot.add({
				            url: 'ajax/ajax_dataNajblizszegoLotu.php',
							dataType: 'json',
				            data: {
				                prefix: prefix,
				                data: data_interval,
								warunek: warunek,
								z : from,
								"do" : to,
								d_one : data_wylot,
								d_two : data_powrot,
								prefix : 'p'
				            },
				            success: function(response){
				               
								var fly_from = $('#z').val().replace(' - wszystkie','') ;
								var fly_to =  $('#to').val().replace(' - wszystkie','') ;
								if (response['type'] == 'noFlightsInMonth_fromto'){ 
									

									if(response['only_direct'] == '1')
									{
										var div = ''+
										'<div style="margin:0px auto; padding-top:20px; text-align:center; color:#2663B8; width:530px;">'+
											'<p style="margin-top:0px;">Niestety w <b>'+response['this_month']+'</b> nie ma żadnych <b>bezpośrednich</b> lotów w bazie z  <span class="bold" id="error_from"> '+fly_from+' </span>'+
											'do <span class="bold" id="error_to"> '+fly_to+'</span>. <br/>'+
											'Najbliższe loty <b>z przesiadkami</b> w tym kierunku są w '+response['next_month']+'.</p>'+
											'<p><a style="font-weight:bold" href="'+response['link']+'" onclick="changeToNotDirect(\''+response['wylot_data']+'\',\''+response['powrot_data']+'\');return false;">Kliknij tutaj</a>, aby przejrzeć najbliższe loty dla tego kierunku lub wybierz inny kierunek w wyszukiwarce.</p>'+
											'<p>Nie znalazłeś odpowiedniego lotu? Zadzwoń do nas <b>022 451 00 50</b>. Znajdziemy dla Ciebie najtańszą opcję.</p>'+
										'</div>'; 										
									}
									else
									{
										var div = ''+
										'<div style="margin:0px auto; padding-top:20px; text-align:center; color:#2663B8; width:530px;">'+
											'<p style="margin-top:0px;">Niestety w <b>'+response['this_month']+'</b> nie ma żadnych lotów w bazie z  <span class="bold" id="error_from"> '+fly_from+' </span>'+
											'do <span class="bold" id="error_to"> '+fly_to+' </span>. <br/>'+
											'Najbliższe loty w tym kierunku są w '+response['next_month']+'.</p>'+
											'<p><a style="font-weight:bold" href="'+response['link']+'" >Kliknij tutaj</a>, aby przejrzeć najbliższe loty dla tego kierunku lub wybierz inny kierunek w wyszukiwarce.</p>'+
											'<p>Nie znalazłeś odpowiedniego lotu? Zadzwoń do nas <b>022 451 00 50</b>. Znajdziemy dla Ciebie najtańszą opcję.</p>'+
										'</div>'; 										
									}
																	

								}	
														
								$('#diagram_przylot').fadeOut(function(){
									$('#p_time, #description_przylot').html(' ');
									$('#diagram_przylot').remove();
									$('#'+prefix+'main_main').append(div);
									releaseAll(prefix,'no_flight');									
									$('#p_refresh_holder').css('visibility','hidden');
									
									
								});
								
				            },
				            type: "GET",
				            queued: true
				        });
					}
				}				
            },
            type: "GET",
            queued: true
        });
			$('#' + prefix + 'info_cancel').click(function(){	
				
				$('#' + prefix + 'info_cancel').attr('id');
				ajaxManagerPowrot.abort();	
			    $('#' + prefix + 'refresh').removeClass('submit5');
			    $('#' + prefix + 'refresh').addClass('submit3');	
				releaseAll(prefix);	

			});	
    }
	if ( $('#w_mess2_box').css('display')== 'none' && $('#p_mess2_box').css('display')== 'none'){
		releaseAll(prefix);
	}

} 


function spradzCzyWidacJakisSlupek(prefix){
	// to do sprawdzanie czy widać jakiś słupek jak tak return true else return fasle
	var zm = 0;
	$('.'+prefix+'slupki').each(function(){
		var h = new String();
		h = $(this).css('height');
		h = h.replace('px','');
		if (h != 0 ){
			zm+=1;
		}
	});
	//alert(zm);
	if (zm > 0){
		return true;
	}
	else{
		return false;
	}
	
}

function canGetLot(prefix, dataStart, warunek, mnoznik, spider){

	var info_data_loty = $('#' + prefix + dataStart + '_td').attr('title').split(',');
	var ilosc_lotow = info_data_loty[1];
	ilosc_lotow = ilosc_lotow.split(':');
	ilosc_lotow = ilosc_lotow[1];
	var info_data = info_data_loty[0];

		getLot(prefix, dataStart, warunek, mnoznik, spider);

}
function pobierzMiasta(warunek,select_nr){
			
	//ustawienie zmiennych
	data_wylot = 0;
	day = $('#dzien_wylot').val();
	if(day=='0'){
		day=1;
	}		
	msc = $('#miesiac_wylot').val();
	msc = msc.split(',');
	data_wylot = msc[1]+'-'+msc[0]+'-'+day;
	
	data_powrot = 0;
	if ($('#obie_strony').is(':checked')) {
		day = $('#dzien_powrot').val();
		if(day=='0'){
			day=1;
		}		
		msc = $('#miesiac_powrot').val();
		msc = msc.split(',');
		data_powrot = msc[1]+'-'+msc[0]+'-'+day;
	}		
	
	ajaxManagerGetCity.add({
		url: 'index_ajax.php',
		dataType: 'json',
		data: {
			mod: 'main',
			scr: 'wyszukiwarka',
			evt: 'ajaxGetCity',
			'warunek': warunek,
			'data_wylot' : data_wylot,
			'data_powrot' : data_powrot,
			'select_nr' : select_nr					
			},
			beforeSend: function(){ 
				if (select_nr == 1){
					$('#wylot_loader').css('display','inline');
				}
				if (select_nr == 2){
					$('#przylot_loader').css('display','inline');
				}				
			},
			success: function(response){
				insertSelect(response,warunek,select_nr);	
				$('#wylot_loader').css('display','none');
				$('#przylot_loader').css('display','none');
			},
			type: "GET"
	});	
}

function showSelect(text,warunek){
	
	temp_wyszukiwarka['kraj_z']=$('#kraj_z').val();
	temp_wyszukiwarka['miasto_z']=$('#miasto_z').val();
	temp_wyszukiwarka['lotniska_z']=$('#lotniska_z').val();
	temp_wyszukiwarka['z']=$('#z').val();	

	temp_wyszukiwarka['kraj_do']=$('#kraj_do').val();
	temp_wyszukiwarka['miasto_do']=$('#miasto_do').val();
	temp_wyszukiwarka['lotniska_do']=$('#lotniska_do').val();
	temp_wyszukiwarka['to']=$('#to').val(); 	
	
	if (!($('#miasto_z').val() != 0 || $('#lotniska_z').val() != 0)) {
		pobierzMiasta(warunek, 1);
	
	}
	else {
		$('#select_wylot').css('display','none');
		$('#select_wylot').prev().css('display','none');
		pobierzMiasta(warunek, 2);
		
	}

		$('#select_message_text').html(text);
		$('#select_buttons_box').css('display', 'block');
		$('#select_cancel').unbind('click');
		$('#select_ok').unbind('click');
		$('#select_cancel').css('display', 'block');
		$('#select_cancel').click(function(){
			$('#select_wylot').html('');
			$('#select_przylot').html('');
			$('#select_przylot').removeAttr('disabled');
			$('#kraj_z').val(temp_wyszukiwarka['kraj_z']);
			$('#miasto_z').val(temp_wyszukiwarka['miasto_z']);
			$('#lotniska_z').val(temp_wyszukiwarka['lotniska_z']);
			$('#z').val(temp_wyszukiwarka['z']);	
		
			$('#kraj_do').val(temp_wyszukiwarka['kraj_do']);
			$('#miasto_do').val(temp_wyszukiwarka['miasto_do']);
			$('#lotniska_do').val(temp_wyszukiwarka['lotniska_do']);
			$('#to').val(temp_wyszukiwarka['to']); 			
			hideInfo();
		});
		$('#select_ok').css('display', 'block');
		$('#select_ok').click(function(){
			//			eval(action);			
			hideInfo();
		});		
		$('#select_przylot').attr('disabled','disabled');
		$('#select_przylot').css('color','#E1E1E1');
		$('#select_box').css('height','160px');
		$('#select_box').css('background','url(./gfx/fog_message2.png)');
		$('#select_box').fadeIn();
}

function fromUpdate(warunek,el){
	

			warunek_temp = warunek.split(';');
			var option = $(el).children('option:selected');
			zm = $(el).val();
			
			zm = zm.split(';');
			warunek2 = zm[0] + ',' + zm[2] + ',' + zm[1] + ';' +warunek_temp[1];
			$('#kraj_z').val(zm[0]);
			$('#miasto_z').val(zm[1]);
			$('#lotniska_z').val(zm[2]);
			
			$('#odmiana_z').val(zm[3]);
			$('#odmiana_z_nazwa').val(zm[4]);
			$('#odmiana_z_lotnisko_nazwa').val(zm[5]);			
			$('#z').val($(option).html());
			//if (!($('#miasto_do').val() != 0 || $('#lotniska_do').val() != 0)) {
			pobierzMiasta(warunek2, 2);
			//}

}



function insertSelect(miasta,warunek,select_nr){
	
    
	if (select_nr == 1){
		
			if (!$('#select_wylot').attr('onchange')){
				
				$('#select_wylot').change(function(){
					fromUpdate(warunek,this);
					
				})
			}		
	
		select = $('#select_wylot');		
	}	
	else{
		select = $('#select_przylot');

	}	
	select.html('<option value="">Wybierz miasto</option>');

	for(var i = 0; i < miasta.length; i++){
			
			option = document.createElement('option');
			miasata_splice = miasta[i].split('|');
			$(select).append('<option value="'+miasata_splice[1]+'" >'+miasata_splice[0]+'</option>');


	}
	if (select_nr == 2){
		$('#select_przylot').css('color','#2C405F');
		$('#select_przylot').removeAttr('disabled');
		
	$('#select_przylot').change(function(){
		var option = $(this).children('option:selected');
		zm = $(this).val();
		zm = zm.split(';');
		$('#kraj_do').val(zm[0]);
		$('#miasto_do').val(zm[1]);
		$('#lotniska_do').val(zm[2]);
		$('#odmiana_do').val(zm[3]);
		$('#odmiana_do_nazwa').val(zm[4]);	
		$('#odmiana_do_lotnisko_nazwa').val(zm[5]);	
		$('#to').val($(option).html()); 		
	});

	}
	$('#select_ok').unbind("click");
	$('#select_ok').click(function(){
		$('#dzien_wylot').val(0);
		if ($('#obie_strony').attr('checked')){
			$('#dzien_powrot').val(0);
		}
		document.forms["wyszukiwarka"].submit.click();	
		hideInfo();	
	});	

}




//pobierz jeden lot
function getLot(prefix, dataStart, warunek, mnoznik, spider){


   	ajaxManagerPoj = prefix == 'w_' ? (ajaxManagerPojWylot) : (ajaxManagerPojPowrot);
   	ajaxManagerPoj.abort();
	if ( $('#ods_time_' + prefix + dataStart).val() < 20 && $('#ods_time_' + prefix + dataStart).val() != '') {
	
		return false;
	}
	else {
		if ( !(( $('#lotniska_z').val() != 0  || $('#miasto_z').val() != 0 ) && ( $('#lotniska_do').val() != 0  || $('#miasto_do').val() != 0)) ) {
			if (( $('#lotniska_z').val() == 0  && $('#miasto_z').val() == 0 ) && ( $('#lotniska_do').val() == 0  && $('#miasto_do').val() == 0 )){
				showSelect('Niestety zapytanie jest zbyt ogólne. W celu uaktualnienia ceny, proszę zawęzić wyniki wyszukiwania, precyzując lotniska wylotu oraz przylotu.',warunek);
			}
			else if (( $('#lotniska_z').val() == 0  && $('#miasto_z').val() == 0 ) && ( $('#lotniska_do').val() != 0  || $('#miasto_do').val() != 0 )){
				showSelect('Niestety zapytanie jest zbyt ogólne. W celu uaktualnienia ceny, proszę zawęzić wyniki wyszukiwania, precyzując lotnisko wylotu.',warunek);
				$('#select_przylot').css('display','none');
				$('#select_przylot').prev().css('display','none');	
			}
			else if (( $('#lotniska_z').val() != 0  || $('#miasto_z').val() != 0 ) && ( $('#lotniska_do').val() == 0  && $('#miasto_do').val() == 0 )){
				showSelect('Niestety zapytanie jest zbyt ogólne. W celu uaktualnienia ceny, proszę zawęzić wyniki wyszukiwania, precyzując lotnisko przylotu.',warunek);
			}			
			
			//showSelect('Proszę zawęzić wyniki wyszukiwania, precyzując lotniska wylotu oraz przylotu.',warunek);
			//showInfo('Proszę zawęzić wyniki wyszukiwania, precyzując przynajmniej jedno miasto.','info');
		}
		else {
			$('#'+prefix+'error_ok').hide();
			var info_data_loty = $('#' + prefix + dataStart + '_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 (ilosc_lotow < 5|| array_tor[prefix + dataStart] == 'nie' ) {
				if ($('#diagram_p').attr('value') == 1) {
					if (prefix == 'w_') {
						$('#dummy').css('display', 'none');
						$('#' + prefix + 'cancel').css('display','block');
						
					}
					if (prefix == 'p_') {

						$('#dalej_button').css('display','none');
						$('#' + prefix + 'cancel').css('display','block');
					}
				}
				else {

						$('#dalej_button').css('display','none');
						$('#' + prefix + 'cancel').css('display','block');					
				}
				if (spider) {
					if (prefix == 'w_') {
						intervalW = setInterval('timerWylot();', 1000);
						data_wylot = dataStart;
						i_w = 0;
						
					}
					else {
						intervalP = setInterval('timerPrzylot();', 1000);
						data_przylot = dataStart;
						i_p = 0;
					}
				}
				var data_interval = dataStart;
				var t_dataStart = dataStart.split("-");
				var data_new = new Date(t_dataStart[0], t_dataStart[1] - 1, t_dataStart[2]);
				var ttl = new Date();
				
				$('#' + prefix + 'time').css('padding-top', '5px');
				$('#' + prefix + 'time').html('odświeżam');
				$('#' + prefix + 'refresh').removeClass('submit3');
				$('#' + prefix + 'refresh').addClass('submit5');
				
				data = data_new.getTime(); // + 1000*60*60*24*(i-len);
				//reset poprzedniego zadania o odswiezenie
//				ajaxManager3.abort(timeout);
				
				//odswiez slupek
				ajaxManagerPoj.add({
					url: 'index_ajax.php',
					data: {
						mod: 'main',
						scr: 'wyszukiwarka',
						evt: 'ajaxGetCheapestFlightDay',
						action: "Diagram",
						prefix: prefix,
						data: data,
						warunek: warunek,
						mnoznik: mnoznik,
						//				ttl: ttl,
						aktualizuj: spider
					},
					success: function(response){
						//przetworz odpowiedz i wyczysc miganie
						
						
						if (prefix == 'w_') {
							clearInterval(timer1[data_interval]);
						}
						else {
							clearInterval(timer2[data_interval]);
						}
						
						 if (response != 'brak lotow'){
						 	eval(response);
						 }
						 else{
						 	clearSlupek(prefix,data);
							//$('#'+prefix+'info_sum').html('Brak lotów w wybranym dniu.');
						 }
						 
						releaseAll(prefix);
						
						if ($('#diagram_p').attr('value') == 1) {
							if (prefix == 'w_') {
								$('#' + prefix + 'cancel').css('display','none');
								$('#dummy').css('display', 'block');
							}
							else 
								if (prefix == 'p_') {

									    $('#' + prefix + 'cancel').css('display','none');
										if (response != 'brak lotow') {
											$('#dalej_button').fadeIn();
										}										
								}
						}
						else {

							$('#' + prefix + 'cancel').css('display','none');
								$('#' + prefix + 'cancel').hide();
								$('#'+prefix+'error_ok').hide();
								$('#dalej_button').show();
								$('#'+prefix+'info_sum').hide();							
							
						}
						$("#img_" + prefix + data_interval).stop();
						$("#img_" + prefix + data_interval).css("opacity", 1);
						$('#' + prefix + data_interval + '_k').stop();
						$('#' + prefix + data_interval + '_k').css("opacity", 1);
						if (prefix == 'w') {
							i_w = 0;
							clearInterval(intervalW);
						}	
						if (prefix == 'p') {
							i_p = 0;
							clearInterval(intervalP);	
						}	
						
						if (spider) {
							if (prefix == 'w_') {
								var item = 'description_wylot';
							}
							else {
								var item = 'description_przylot';
							}
							przerwijSucces(prefix,dataStart,spider)
							if ($('#details_' + prefix + dataStart).val() != '') {
								details = $('#details_' + prefix + dataStart).val().split(',');
//								$(this).attr("src", "./gfx/bar_yellow.gif");
								
								var info_data_loty = $('#' + prefix + dataStart + '_td').attr('title').split(',');
								var info_data = info_data_loty[0];
								var ilosc_lotow = info_data_loty[1].split(':');
								ilosc_lotow = ilosc_lotow[1];
								info_kierunek = 'wylot';
								
								
								var info_miasta = $('#' + prefix + 'city_details').val().split(':');
								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_' + prefix + dataStart).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);
								//if (response != 'brak lotow') {
								
									putTimestamp(prefix, dataStart, response, spider);
								//}
							}
							else{
								$('#'+prefix+'cancel').css('display','none');
								$('#'+prefix+'info_sum').html('Brak lotów w bazie dla wybranego dnia.')
							}
						}
						
					},
					type: "GET",
					beforeSend: function(){
						//inicjalizuj miganie
						blockAll(prefix);
						var mig_slupek = $('#img_' + prefix + data_interval);
						var mig_kal = $('#' + prefix + data_interval + '_k');
						
						if (prefix == 'w_') {
								timer1[data_interval] = setInterval(function(){
									mig_slupek.fadeTo(1000, 0.2);
									mig_kal.fadeTo(1000, 0.2);
									mig_slupek.fadeTo(1000, 1);
									mig_kal.fadeTo(1000, 1);
								}, 2000);
						}
						else {
								timer2[data_interval] = setInterval(function(){
									mig_slupek.fadeTo(1000, 0.2);
									mig_kal.fadeTo(1000, 0.2);
									mig_slupek.fadeTo(1000, 1);
									mig_kal.fadeTo(1000, 1);
								}, 2000);
						}
						
						mig_slupek.fadeTo(1000, 0.2);
						mig_kal.fadeTo(1000, 0.2);
						mig_slupek.fadeTo(1000, 1);
						mig_kal.fadeTo(1000, 1);
					}
				});
				if (spider) {
					$('#' + prefix + 'cancel').click(function(){
						$('#' + prefix + 'refresh').removeClass('submit5');
						$('#' + prefix + 'refresh').addClass('submit3');
						releaseAll(prefix);
						if (prefix == 'w_') {
							ajaxManagerPoj.abort();
							clearInterval(timer1[data_interval]);
							clearInterval(intervalW);
							i_w = 0;
						}
						if (prefix == 'p_') {
							ajaxManagerPoj.abort();
							clearInterval(timer2[data_interval]);
							clearInterval(intervalP);
							i_p = 0;
						}
						refresh_arch = $('#' + prefix + 'time').html();
						$('#' + prefix + 'time').css('padding-top', '5px');
						$('#' + prefix + 'time').html('anulowano');
						if ($('#diagram_p').attr('value') == 1) {
							if (prefix == 'w_') {
								$('#' + prefix + 'cancel').css('display','none');
							}
							if (prefix == 'p_') {
								$('#' + prefix + 'cancel').css('display','none');
								$('#dalej_button').fadeIn();
								
							}
						}
						else {

							$('#' + prefix + 'cancel').css('display','none');
							$('#dalej_button').fadeIn();
							
						}
					});
				}

		}
	}		
}

function timerWylot(){
	if ($('#w_time').html() == 'dane aktualne') {
			clearInterval(intervalW);
   			ajaxManagerPojWylot.abort();
			i_w = 0;
	}
	else {
		if (i_w >= 50) {
			clearInterval(intervalW);
			i_w = 0;
			przerwijOds('w_', data_wylot);
			ajaxManagerPojWylot.abort();
			$('#w_info_sum').html('Nie wszystkie loty zostały odświeżone');
			$('#'+prefix+'time').html('Błąd').css({'padding-top':'5px','text-align':'right'});
		}
		else {
			i_w++;
		}
	}
}

function timerPrzylot(){
	if ($('#p_time').html() == 'dane aktualne') {
		clearInterval(intervalW);
		ajaxManagerPojPowrot.abort();
		i_w = 0;
	}
	else {
		if (i_p >= 50) {
			
			clearInterval(intervalP);
			$('#p_info_sum').html('Nie wszystkie loty zostały odświeżone');
			$('#'+prefix+'time').html('Błąd').css({'padding-top':'5px','text-align':'right'});
			przerwijOds('p_', data_przylot);
			ajaxManagerPojPowrot.abort();
			i_p = 0;
		}
		else {
			i_p++;
		}
	}
}

function przerwijSucces(prefix,data_interval,spider){
	if (prefix == 'w_') {
  		    clearInterval(timer1[data_interval]);
			clearInterval(intervalW);
	}
	if (prefix == 'p_') {
			clearInterval(timer2[data_interval]);
			clearInterval(intervalP);
	}
	$('#' + prefix + 'cancel').hide();
	putTimestamp(prefix, data_interval,null,spider);
}

function przerwijOds(prefix,data_interval){
	$('#' + prefix + 'refresh').removeClass('submit5');
	$('#' + prefix + 'refresh').addClass('submit3');
	releaseAll(prefix);
	if (prefix == 'w_') {
  		    clearInterval(timer1[data_interval]);
			clearInterval(intervalW);
	}
	if (prefix == 'p_') {
			clearInterval(timer2[data_interval]);
			clearInterval(intervalP);
	}
	refresh_arch = $('#' + prefix + 'time').html();
	$('#' + prefix + 'cancel').hide();
	$('#dalej_button').hide();
	
	val = obliczIleTemuOdswiezono($('#odswiezenie_' +prefix + data_interval).val());
	if(val == '14214 dni'){
		return false;
	}
	$('#' + prefix + 'time').css('padding-top', '0px');
	$('#' +prefix + 'time').html('odświeżono<br/><strong style="color: rgb(44, 101, 195); font-size: 9px;">' + val + '</strong> temu');
	$('#'+prefix+'error_ok').css('display','block');					
}
//pobierz czas ostatniego odswiezenia
function getTimestamp(prefix, data){
    if (prefix.length > 0) {
        $.get('ajax/ajax_wyniki.php', {
            action: "getTimestamp",
            prefix: prefix,
            data: data
        }, function(response){
            putTimestamp(prefix, data, response);
        });
    }
} 
function clearSlupek(prefix, data){
     //$('#'+prefix+element+'_div').attr('title',data_slupka[2]+' '+nazwaMiesiaca(data_slupka[1])+' '+data_slupka[0]+', ilość lotów: '+ilosc_lotow);

		$('#' + prefix + data + '_td').unbind("click");
   	 	$('#' + prefix + data + '_div').unbind("click");
		var click = $('#click_'+prefix+data).attr('value');
		$('#' + prefix + data + '_div').removeAttr('onclick');
        $('#' + prefix + data + '_div').click(function(){
            eval(click);
			if ($('#img_'+prefix+data).attr('src') == './gfx/bar_red.gif'){
				
				
				$('#'+prefix+'refresh').addClass('can_click');				
			}
			else{
				
				$('#'+prefix+'refresh').removeClass('can_click');
			}
        });	 
	
	var data_s = data.split('-');
	  
	$('#'+prefix+data+'_div').attr('title',data_s[2]+' '+nazwaMiesiaca2(data_s[1])+' '+data_s[0]+', ilość lotów: 0');
	 
	$('#details_' + prefix + data).val('');
    $('#lotnisko_' + prefix + data).val('');
    $('#img_' + prefix + data).attr('height', 0);
}
