var elements_count=10;
var czyselect=0;

window.addEvent('domready', function() {
	
});
window.addEvent('load', function() {	
	init();
});

function init()
{
	// pobieramy elementy strony
	var i;

	// checkboxy
	elements=$('start').getElements('a[class$=checkbox]');
	for (i=0;i<elements.length;i++)
		{
			checkbox(elements[i]);
		}
	elements=$('start').getElements('a[class$=checkbox_selected]');
	for (i=0;i<elements.length;i++)
		{
			checkbox(elements[i]);
		}
		
	// selecty
	elements=$('selekty').getElements('div[class$=select]');
	for (i=0;i<elements.length;i++)
		{
			select(elements[i]);
		}	

  // wylaczamy wszystkie okienka klikajac gdziestam.
  $('start').addEvent('click', function() {
    // chowamy selecty
    inneselecty=$('start').getElements('div[class$=select_open]');
			for (i=0; i<inneselecty.length; i++){
				if ( czyselect == '0'){
					inneselecty[i].toggleClass('select_open');
					inneselecty[i].toggleClass('select');
				}
			}
      czyselect = '0';
  });
  
/*  


<input type="text" class="miasto" id="wylotform" name="wylot" readonly=readonly value="2009-06-25" onclick="show_cal_wylot();" />
<input type="text" class="miasto" id="powrotform" name="powrot" readonly=readonly value="2009-07-02" onclick="show_cal_powrot();" />

<div id="cal_ico1" onclick="show_cal_wylot();"></div>
<div id="cal_ico2" onclick="show_cal_powrot();"></div>


// pokazujemy kalendarze wylot,powrot
function show_cal_wylot(){
  var status;
  status=document.getElementById('status_wylot').value;
  if (status == '0'){
    document.getElementById('przylot_kalendarz').style.display="none";
    document.getElementById('status_przylot').value='0'; 
    
    document.getElementById('wylot_kalendarz').style.display="block";
    document.getElementById('status_wylot').value='1';
  } else {
    document.getElementById('wylot_kalendarz').style.display="none";
    document.getElementById('status_wylot').value='0';  
  } 
}

function show_cal_powrot(){

  // blokada kalendarza dla lotu w jedna strone.
  ow=$('ow').value; if (ow == 'on'){ return false; }

  var status;
  status=document.getElementById('status_przylot').value;
  if (status == '0'){
    document.getElementById('wylot_kalendarz').style.display="none";
    document.getElementById('status_wylot').value='0';  

    document.getElementById('przylot_kalendarz').style.display="block";
    document.getElementById('status_przylot').value='1';
  } else {
    document.getElementById('przylot_kalendarz').style.display="none";
    document.getElementById('status_przylot').value='0';  
  } 
}
// pokazujemy kalendarze - koniec


*/
  
}

// checkboxy
function checkbox(pole)
{
	pole.addEvent('click', function() {
		if (this.hasClass('checkbox'))
			{
				this.removeClass('checkbox');
				this.addClass('checkbox_selected');


						if ((this.getProperty('rel'))=="cheep"){
							$(this.getProperty('rel')).setProperty('value','2');
						} else if ((this.getProperty('rel'))=="dir" ){
							$(this.getProperty('rel')).setProperty('value','on');
						} else {
							$(this.getProperty('rel')).setProperty('value','1');
						}

						
			}
			else
			{
				this.removeClass('checkbox_selected');
				this.addClass('checkbox');
					
					if ((this.getProperty('rel'))=="flight_type"){
						$(this.getProperty('rel')).setProperty('value','0');
					} else {
						$(this.getProperty('rel')).setProperty('value','');
					}

			}
		return false;
	});
}

// selecty

function select(pole)
{
	more=pole.getElement('a[class=more]');
	more.addEvent('click', function() {
		czyselect=1;
			this.parentNode.toggleClass('select_open');
			this.parentNode.toggleClass('select');			
			
			topole=this.parentNode.getProperty('title');
			inneselecty=$('start').getElements('div[class$=select_open]');
			for (i=0; i<inneselecty.length; i++){
				if ( topole != inneselecty[i].getProperty('title') ){
				inneselecty[i].toggleClass('select_open');
				inneselecty[i].toggleClass('select');
				}
			}

	});
	
	
	div=pole.getElement('div');
	div_suwak=div.getElement('div');
	links=div.getElements('a');
	span=pole.getElement('span');
	pole_width=pole.getStyle('width').toInt();

	div_suwak.addEvent('click', function() {
	// tutaj scroll.
		czyselect=1;
	});

	for (i=0;i<links.length-2;i++)
	{
		
	links[i].addEvent('click', function() {
 	
	
					// tutaj zalozenia daty/miesiaca.
					nazwapola=pole.getProperty('title');
					$(pole.getProperty('title')).setProperty('value',this.getProperty('rel'));
          
					

					links=this.parentNode.getElements('a');
					for (i=0;i<links.length;i++)
						{
							links[i].removeClass('selected');
						
						}
					
					this.addClass('selected');
					em=this.parentNode.parentNode.parentNode.getElement('em');
					em.setText(this.getText());
					
					this.parentNode.parentNode.parentNode.toggleClass('select');
					this.parentNode.parentNode.parentNode.toggleClass('select_open');
				});
				
				links[i].addEvent('mouseenter', function() {	
					this.addClass('hover');
			    });
				
				links[i].addEvent('mouseleave', function() {
					this.removeClass('hover');
			    });			
								
				if (links.length-2>elements_count)
					{
						links[i].setStyle('width',(pole_width-17)+'px');
					}
					else
					{
						links[i].setStyle('width',pole_width+'px');
					}									
			}
			
	if (links.length-2>elements_count)
		{

			height=elements_count * 16;
			div.setStyle('height',height + 'px');
			
			down=div.getElement('a[class=down]');
			down.removeEvents();
			down.addEvent('click', function() {
			czyselect=1;
				links=this.parentNode.parentNode.getElements('a');
				span=this.parentNode.parentNode.getElement('span');
				if ((links.length-2-elements_count)*16>-span.getStyle('top').toInt())
					{
						height=span.getStyle('top').toInt()-16;
						span.setStyle('top',height + 'px')
					}				
			});											
			
			up=div.getElement('a[class=up]');
			up.removeEvents();
			up.addEvent('click', function() {
			czyselect=1;
				links=this.parentNode.parentNode.getElements('a');
				span=this.parentNode.parentNode.getElement('span');
				if (0>span.getStyle('top').toInt())
					{
						height=span.getStyle('top').toInt()+16;
						span.setStyle('top',height + 'px');
					}				
			});
			
			
			span.setStyle('width',(pole_width-6)+'px');
			div.setStyle('width',(pole_width+10)+'px');
			div_suwak.setStyle('left',(pole_width-6)+'px');
			div_suwak.setStyle('height',((elements_count*16)-2)+'px');
		}
		else
		{
			//alert('test');

			span.setStyle('width',(pole_width+10)+'px');
			
			// normalne przegladarki
			div.setStyle('width',(pole_width+10)+'px');
			
			if (window.ie6){
			//explorer
			div.setStyle('width',(pole_width+0)+'px');
			}
			
			div.setStyle('height',((links.length-2)*16)+'px');
			div_suwak.setStyle('display','none');
		}
		
		pole.addEvent('mousewheel', function(event) {
			event = new Event(event).stop();
		 
			/* Mousewheel UP */
			if (event.wheel > 0) {
				mouse_up(this);
			} 
			/* Mousewheel DOWN*/
			else if (event.wheel < 0) {
				mouse_down(this);
			}
		});
		



}

function mouse_down(element)
{
	div=element.getElement('div');
	span=div.getElement('span');
	links=div.getElements('a');	
	links_count=links.length;
	for (i=0;i<links.length-2;i++)
		{
			if (links[i].hasClass('selected')) link_number=i;
		}
	if (link_number<links_count-3)
		{
		links[link_number].removeClass('selected');
		links[link_number+1].addClass('selected');
		em=element.getElement('em');
		em.setText(links[link_number+1].getText());
		}
	if ((link_number+1)*16>=elements_count*16-span.getStyle('top').toInt() && link_number!=links_count-3)
		{
			span.setStyle('top',(span.getStyle('top').toInt()-16) + 'px');
		}
}

function mouse_up(element)
{
	links=element.getElement('div').getElements('a');
	links_count=links.length;
	for (i=0;i<links.length-2;i++)
		{
			if (links[i].hasClass('selected')) link_number=i;
		}
	if (0<link_number)
		{
		links[link_number].removeClass('selected');
		links[link_number-1].addClass('selected');
		em=element.getElement('em');
		em.setText(links[link_number-1].getText());
		}
	if ((link_number+1)*16<=-span.getStyle('top').toInt()+16 && link_number!=0)
		{
			span.setStyle('top',(span.getStyle('top').toInt()+16) + 'px');
		}
}



// pokazujemy kalendarze wylot,powrot
function show_cal_wylot(){
  var status;
  status=document.getElementById('status_wylot').value;
  if (status == '0'){
    document.getElementById('przylot_kalendarz').style.display="none";
    document.getElementById('status_przylot').value='0'; 
    
    document.getElementById('wylot_kalendarz').style.display="block";
    document.getElementById('status_wylot').value='1';

  } else {
    document.getElementById('wylot_kalendarz').style.display="none";
    document.getElementById('status_wylot').value='0';  

  } 
}

function show_cal_powrot(){

  // blokada kalendarza dla lotu w jedna strone.
  ow=$('ow').value; if (ow == 'on'){ return false; }

  var status;
  status=document.getElementById('status_przylot').value;
  if (status == '0'){
    document.getElementById('wylot_kalendarz').style.display="none";
    document.getElementById('status_wylot').value='0';  

    document.getElementById('przylot_kalendarz').style.display="block";
    document.getElementById('status_przylot').value='1';


  } else {
    document.getElementById('przylot_kalendarz').style.display="none";
    document.getElementById('status_przylot').value='0';  


  } 
}
// pokazujemy kalendarze - koniec

function zamknij_wylot(){
				 $('wylot_kalendarz').setStyle('display','none');
         $('status_wylot').value='0';
}

function zamknij_powrot(){
				 $('przylot_kalendarz').setStyle('display','none');
         $('status_przylot').value='0'; 
}

function set_wylot_day(dzien,miesiac,rok){
				 przylot_ustawiony=document.getElementById('przylot_ustawiony').value;
				 aktualny_wylot=document.getElementById('wylotform').value;
				 aktualny_przylot=document.getElementById('powrotform').value;
				 xajax_xfend('ustaw_wylot',dzien,miesiac,rok,przylot_ustawiony,aktualny_wylot,aktualny_przylot);
}
function set_powrot_day(dzien,miesiac,rok){
				 przylot_ustawiony=document.getElementById('przylot_ustawiony').value;
				 aktualny_wylot=document.getElementById('wylotform').value;
				 aktualny_przylot=document.getElementById('powrotform').value;					 
				 xajax_xfend('ustaw_powrot',dzien,miesiac,rok,przylot_ustawiony,aktualny_wylot,aktualny_przylot);
}

function wylot_next_month(){
				 aktualny_wylot=document.getElementById('wylotform').value;
				 aktualny_przylot=document.getElementById('powrotform').value;
				 
  				 dzien=document.getElementById('wylot_lewy_widoczny').value;
  				 miesiac=document.getElementById('wylot_prawy_widoczny').value;
  				 rok=document.getElementById('przylot_lewy_widoczny').value;
  				 przylot_ustawiony=document.getElementById('przylot_prawy_widoczny').value;

      wylot_pokazany=document.getElementById('wylot_rok_pokazany').value;
      powrot_pokazany=document.getElementById('powrot_rok_pokazany').value;


var Today = new Date();      
var Year = Today.getFullYear() ;
var Month = Today.getMonth() ;
Year++;

if (Month == '1' || Month == '01') {Month='12';} else {Month--;}
//teraz = new Date();
//mies=(teraz.getMonth());
//alert(mies);

if (Year == wylot_pokazany){
  if (Month==dzien || Month==miesiac){
//    alert('stop!');
  return false;
  }
}


				 xajax_xfend('wylot_next_month',dzien,miesiac,rok,przylot_ustawiony,aktualny_wylot,aktualny_przylot,wylot_pokazany,powrot_pokazany);
}

function wylot_prev_month(){
				 aktualny_wylot=document.getElementById('wylotform').value;
				 aktualny_przylot=document.getElementById('powrotform').value;

//var tab = aktualny_wylot.split("-");
//if (dzien == tab['1'] || miesiac == tab['1']){
//  alert('wczesniej nie ma lotow - gwarantuje!');
//  return false;
//}
				 
  				 dzien=document.getElementById('wylot_lewy_widoczny').value;
  				 miesiac=document.getElementById('wylot_prawy_widoczny').value;
  				 rok=document.getElementById('przylot_lewy_widoczny').value;
  				 przylot_ustawiony=document.getElementById('przylot_prawy_widoczny').value;

      wylot_pokazany=document.getElementById('wylot_rok_pokazany').value;
      powrot_pokazany=document.getElementById('powrot_rok_pokazany').value;

var Today = new Date();      
var Year = Today.getFullYear() ;
var Month = Today.getMonth() ;

if (Year == wylot_pokazany){
  if (Month==dzien || Month==miesiac){
//    alert('stop!');
  return false;
  }
}


				 xajax_xfend('wylot_prev_month',dzien,miesiac,rok,przylot_ustawiony,aktualny_wylot,aktualny_przylot,wylot_pokazany,powrot_pokazany);
}



function powrot_next_month(){
				 aktualny_wylot=document.getElementById('wylotform').value;
				 aktualny_przylot=document.getElementById('powrotform').value;
				 
  				 dzien=document.getElementById('wylot_lewy_widoczny').value;
  				 miesiac=document.getElementById('wylot_prawy_widoczny').value;
  				 rok=document.getElementById('przylot_lewy_widoczny').value;
  				 przylot_ustawiony=document.getElementById('przylot_prawy_widoczny').value;

      wylot_pokazany=document.getElementById('wylot_rok_pokazany').value;
      powrot_pokazany=document.getElementById('powrot_rok_pokazany').value;



//var tab = aktualny_wylot.split("-");
//if (dzien == tab['1'] || miesiac == tab['1']){
//  alert('wczesniej nie ma lotow - gwarantuje!');
//  return false;
//}
var Today = new Date();      
var Year = Today.getFullYear() ;
var Month = Today.getMonth() ;
Year++;

if (Month == '1' || Month == '01') {Month='12';} else {Month--;}
//teraz = new Date();
//mies=(teraz.getMonth());
//alert(mies);

if (Year == powrot_pokazany){
  if (Month==rok || Month==przylot_ustawiony){
//    alert('stop!');
  return false;
  }
}


				 xajax_xfend('powrot_next_month',dzien,miesiac,rok,przylot_ustawiony,aktualny_wylot,aktualny_przylot,wylot_pokazany,powrot_pokazany);
}





function powrot_prev_month(){
				 aktualny_wylot=document.getElementById('wylotform').value;
				 aktualny_przylot=document.getElementById('powrotform').value;


  				 dzien=document.getElementById('wylot_lewy_widoczny').value;
  				 miesiac=document.getElementById('wylot_prawy_widoczny').value;
  				 rok=document.getElementById('przylot_lewy_widoczny').value;
  				 przylot_ustawiony=document.getElementById('przylot_prawy_widoczny').value;

      wylot_pokazany=document.getElementById('wylot_rok_pokazany').value;
      powrot_pokazany=document.getElementById('powrot_rok_pokazany').value;

var tab = aktualny_wylot.split("-");

if (rok.length == 1) { rok="0"+rok; }
if (przylot_ustawiony.length == 1) { przylot_ustawiony="0"+przylot_ustawiony; }

if (rok == tab['1'] || przylot_ustawiony == tab['1']){
//    alert('stop!');
  return false;
}


				 xajax_xfend('powrot_prev_month',dzien,miesiac,rok,przylot_ustawiony,aktualny_wylot,aktualny_przylot,wylot_pokazany,powrot_pokazany);
}



function szukaj_polaczenia(){
	 
	    document.getElementById('paneld').submit();
}



function xcityhelper(action,pole,helperfield,kod,miasto){
if (action=='search')
	{
		 var charCode = (kod.which) ? kod.which : event.keyCode
		 if (charCode=='9'){
			return false;
		 }
		 if (charCode=='13'){
			xcityhelper('complete',pole,helperfield);
		 } else {	
			fraza=$(pole).getProperty('value');
			if (fraza.length<3) { return false; }
			//document.getElementById(helperfield).style.visibility='visible';
			xajax_xch('search',pole,helperfield,fraza,'','');
		}
	} 
	else if (action == 'selekt') {
		xajax_xch('selekt',pole,helperfield,'0',kod,miasto);
	} 
	else if (action=='complete')
	{
		if (pole=="miasto_z"){
			fraza=document.getElementById('w_kod_z').value;
		} else {
			fraza=document.getElementById('w_kod_d').value;
		}
		
		if (fraza !=""){
			xajax_xch('kommit',pole,helperfield,fraza,'','');
		}
	}

}



function oneway()
{
	element=$('start').getElement('a[class$=dwie_strony]');
	element.removeClass('dwie_strony');
	element.addClass('dwie_stronyoff');
	element=$('start').getElement('a[class$=jedna_strona]');
	element.removeClass('jedna_strona');
	element.addClass('jedna_stronaoff');
	
	$('data_p').setStyle('opacity','0.2');
	$('powrotform').setStyle('opacity','0.2');
	$('cal_ico2').setStyle('opacity','0.2');
	
	$('ow').setProperty('value','on');
}
function twoway()
{
	element=$('start').getElement('a[class$=dwie_stronyoff]');
	element.removeClass('dwie_stronyoff');
	element.addClass('dwie_strony');
	element=$('start').getElement('a[class$=jedna_stronaoff]');
	element.removeClass('jedna_stronaoff');
	element.addClass('jedna_strona');

	$('data_p').setStyle('opacity','1');
	$('powrotform').setStyle('opacity','1');
	$('cal_ico2').setStyle('opacity','1');
	
	$('ow').setProperty('value','');
}

function pokaz_detale(id){
				 document.getElementById('dane_niewidoczne'+id).style.display="block";
				 document.getElementById('linkwiecej_'+id).style.display="none";
				 document.getElementById('linkwiecej0_'+id).style.display="block";
				 document.getElementById('cenawlasciwa_'+id).style.display="none";
				 document.getElementById('rezerwujmale_'+id).style.display="none";
				 document.getElementById('lot_widoczne_'+id).style.height="55px";
				 document.getElementById('dopisek_o_biletach_'+id).style.display="none";

				 document.getElementById('przewoznik_'+id).style.height="55px";
				 document.getElementById('przewoznik_'+id).style.paddingTop="5px";
				 document.getElementById('przwoznik_txt_'+id).style.display="block";
				 
}
function ukryj_detale(id){
				 document.getElementById('dane_niewidoczne'+id).style.display="none";
				 document.getElementById('linkwiecej_'+id).style.display="block";
				 document.getElementById('linkwiecej0_'+id).style.display="none";
				 document.getElementById('cenawlasciwa_'+id).style.display="block";
				 document.getElementById('rezerwujmale_'+id).style.display="block";
				 document.getElementById('lot_widoczne_'+id).style.height="85px";
				 document.getElementById('dopisek_o_biletach_'+id).style.display="block";

				 document.getElementById('przewoznik_'+id).style.height="40px";
				 document.getElementById('przewoznik_'+id).style.paddingTop="20px";
				 document.getElementById('przwoznik_txt_'+id).style.display="none";
}

function pokaz_ukryte(id){
				 document.getElementById('ukryte_'+id).style.display="block";
				 document.getElementById('dopisek_'+id).style.display="none";
				 document.getElementById('ddopisek_'+id).style.display="block";
}

function ukryj_ukryte(id){
				 document.getElementById('ukryte_'+id).style.display="none";
				 document.getElementById('dopisek_'+id).style.display="block";
				 document.getElementById('ddopisek_'+id).style.display="none";
}


