var MultipleOpenAccordion = Fx.Elements.extend({
	options: {
		openAll: true,
		allowMultipleOpen: true,
		firstElementsOpen: [0],
		start: 'open-first',
		fixedHeight: false,
		fixedWidth: false,
		alwaysHide: true,
		wait: false,
		onActive: Class.empty,
		onBackground: Class.empty,
		height: true,
		opacity: true,
		width: false
	},
	initialize: function(togglers, elements, options){
		this.parent(elements, options);
		this.setOptions(options);
		this.previousClick = null;
		this.elementsVisible = [];
		togglers.each(function(tog, i){
			$(tog).addEvent('click', function(){this.toggleSection(i)}.bind(this));
		}, this);
		this.togglers = togglers;
		this.h = {};
		this.w = {};
		this.o = {};
		this.now = [];
		this.elements.each(function(el, i){
			el = $(el);
			this.now[i] = {};
			el.setStyle('overflow','hidden');
			if(!(this.options.openAll && this.options.allowMultipleOpen)) el.setStyle('height', 0);
		}, this);
		if(!this.options.openAll || !this.options.allowMultipleOpen) {
			switch(this.options.start){
				case 'first-open': this.showSection(this.options.firstElementsOpen[0]); break;
				case 'open-first': this.toggleSection(this.options.firstElementsOpen[0]); break;
			}
		}
		if (this.options.openAll && this.options.allowMultipleOpen) this.showAll();
		else if (this.options.allowMultipleOpen) this.openSections(this.options.firstElementsOpen);
	},
	hideThis: function(i){ //sets up the effects for hiding an element
		this.elementsVisible[i] = false;
		if (this.options.height) this.h = {'height': [this.elements[i].offsetHeight, 0]};
		if (this.options.width) this.w = {'width': [this.elements[i].offsetWidth, 0]};
		if (this.options.opacity) this.o = {'opacity': [this.now[i]['opacity'] || 1, 0]};
		this.fireEvent("onBackground", [this.togglers[i], this.elements[i]]);
	},

	showThis: function(i){ //sets up the effects for showing an element
		this.elementsVisible[i] = true;
		if (this.options.height) this.h = {'height': [this.elements[i].offsetHeight, this.options.fixedHeight || this.elements[i].scrollHeight]};
		if (this.options.width) this.w = {'width': [this.elements[i].offsetWidth, this.options.fixedWidth || this.elements[i].scrollWidth]};
		if (this.options.opacity) this.o = {'opacity': [this.now[i]['opacity'] || 0, 1]};
		this.fireEvent("onActive", [this.togglers[i], this.elements[i]]);
	},
/*	Property: toggleSection
		Opens or closes a section depending on its state and the options of the Accordion.

		Argumetns:
		iToToggle - (integer) the index of the section to open or close
	*/
	toggleSection: function(iToToggle){
		//let's open an object, or close it, depending on it's state
		//now, if the index to toggle isn't the previous click
		//or we're going to allow items to be closed (so that all of them are closed
		//or we're allowing more than one item to be open at a time, continue
		//otherwise, we're looking at an item that was just clicked, and it should already be open
		if(iToToggle != this.previousClick || this.options.alwaysHide || this.options.allowMultipleOpen) {
			//save the previous click
			this.previousClick = iToToggle;
			var objObjs = {};
			var err = false;
			//go through each element
			this.elements.each(function(el, i){
				var update = false;
				//set up it's now state
				this.now[i] = this.now[i] || {};
				//if the element is the one clicked
				if(i==iToToggle){
					//if the element is visible, hide it if we allow alwaysHide or multiple
					if (this.elementsVisible[i] && (this.options.allowMultipleOpen || this.options.alwaysHide)){
						//if ! wait and timer
						if(!(this.options.wait && this.timer)) {
							//hide it
							update = true;
							this.hideThis(i);
						} else {
							this.previousClick = null;
							err = true;
						}
					} else if(!this.elementsVisible[i]){
					//else if hidden, show it
						//if ! wait and timer
						if(!(this.options.wait && this.timer)) {
							//show it
							update = true;
							this.showThis(i);
						} else {
							this.previousClick = null;
							err = true;
						}
					}
				} else if(this.elementsVisible[i] && !this.options.allowMultipleOpen) {
				//else (not clicked) if it's visible, hide it, unless we allow multiple open
					//if ! wait and timer
					if(!(this.options.wait && this.timer)) {
						//hide it
						update = true;
						this.hideThis(i);
					} else {
						this.previousClick = null;
						err = true;
					}
				} //else it's not clicked, it's not open, so leave it alone because we allow multiples
				//set up the effect instructions
				if(update) objObjs[i] = $merge(this.h, $merge(this.o, this.w));
			}, this);
			//if there's an error, just stop
			if (err) return false;
			//execute the custom function, which resizes everything.
			return this.custom(objObjs);
		}
		return false;
	},
/*	Property: showSection
		Opens a section of the accordion if it's not open already.

		Arguments:
		i - (integer) the index of the section to show
		useFx - (boolean) open it immediately (false) or slide it open using the effects (true);  defaults to false;
	*/
	showSection: function(i, useFx){
		if($pick(useFx, false)) {
			if (!this.elementsVisible[i]) this.toggleSection(i);
		} else {
			this.setSectionStyle(i,$(this.elements[i]).scrollWidth, $(this.elements[i]).scrollHeight, 1);
			this.elementsVisible[i] = true;
			this.fireEvent("onActive", [this.togglers[i], this.elements[i]]);
		}
	},
/*	Property: hideSection
		Closes a section of the accordion if it's not closed already.

		Arguments:
		i - (integer) the index of the section to hide
		useFx - (boolean) close it immediately (false) or slide it closed using the effects (true);  defaults to false;
	*/
	hideSection: function(i, useFx){
		if($pick(useFx, false)) {
			if (this.elementsVisible[i]) this.toggleSection(i);
		} else {
			this.setSectionStyle(i,0,0,0);
			this.elementsVisible[i] = false;
			this.fireEvent("onBackground", [this.togglers[i], this.elements[i]]);
		}
	},
	//internal function; sets a section (i) to the width (w), height (h), and opacity (o) passed in
	setSectionStyle: function(i,w,h,o){
			if (this.options.opacity) $(this.elements[i]).setOpacity(o);
			if (this.options.height) $(this.elements[i]).setStyle('height',h+'px');
			if (this.options.width) $(this.elements[i]).setStyle('width',w+'px');
	},
/*	Property: showAll
		Opens all the elements in the accordion immediately; used on startup	*/
	showAll: function(){
		if(this.options.allowMultipleOpen){
			this.elements.each(function(el,idx){
					this.showSection(idx, false);
			}, this);
		}
	},
/*	Property: hideAll
		Closes all the elements in the accordion immediately; used on startup	*/
	hideAll: function(){
		if(this.options.allowMultipleOpen){
			this.elements.each(function(el,idx){
				this.hideSection(idx, false);
			}, this);
		}
	},
/*	Property: openSection
		Opens specific sections of the accordion immediately; used on startup.

		Arguments:
		sections - array of indexes to open.
	*/
	openSections: function(sections) {
		if(this.options.allowMultipleOpen){
			this.elements.each(function(el,idx){
				if(sections.test(idx)) this.showSection(idx, false);
				else this.hideSection(idx, false);
			}, this);
		}
	}
});
MultipleOpenAccordion.implement(new Options);
MultipleOpenAccordion.implement(new Events);

function imgMouseOverEvents(outString, overString, selector) {
	$$(selector).each(function(image) {
		image = $(image);
		if ($type(image.src)) {
			if (image.src.indexOf(outString) > 0) {
				image.addEvent('mouseover',function(){
					image.src = image.src.replace(outString, overString);
				}).addEvent('mouseout', function(){
					image.src = image.src.replace(overString, outString);
				});
			}
		}
	});
};
function searchsummarylinks() {
		   
		  var headth = $$('div#searchsummary thead th');  
		  headth.each(function(el) {
		  
		    el.addEvent('mouseover', function() {
					this.addClass('hover');			
        })
        el.addEvent('mouseout', function() {
					this.removeClass('hover');			
        })
        
		  })
		  
		  
		  $$('div#searchsummary tbody tr').forEach(function(wiersz){
		  
 		  var sstd = $ES('td', wiersz);
       
		  sstd.each(function(el,index) {

        if (el.getFirst()) {
	  
        var ind = index+1;
		    el.addEvent('mouseover', function() {
			    this.getParent().getFirst().addClass('hover')
 					headth[ind].addClass('hover');  
 					el.addClass('hover');  
				})
		    el.addEvent('mouseout', function() {
					this.getParent().getFirst().removeClass('hover')
  				headth[ind].removeClass('hover');				
 					el.removeClass('hover');  
				})
	
	      }
        
        		  })
		  })
		  
   		  var ssth = $ES('thead tr th a', 'searchsummary');
		    ssth.each(function(el,index) {
	    	if (el.getProperty('rel')) {		
			  el.addEvent('click', function(){
			    
			    if (this.getProperty('rel')=='all') {
			  	  $$('.opcja').setStyle('display','block');
			  	  alert('all');
			    }
			    else {
			  	  $$('.opcja').setStyle('display','none');
			  	  $$('.'+this.getProperty('rel')).setStyle('display','block');
          };
          });
          }
     });
	     
}


// window.addEvent('domready', function() {
window.onload = function() {

  searchsummarylinks();

	imgMouseOverEvents('_off', '_on', 'img.amo, input.amo');
	new SmoothScroll();
	var myToggler = $$('li.pokaz');
	new MultipleOpenAccordion(myToggler, $$('div.details'), { 
			  openAll: false,
			  opacity: false,
			  duration: 1000,
			  firstElementsOpen: [],
   			onActive: function(myToggler){
   			  if(myToggler) myToggler.addClass('active').removeClass('background');
				},
  			onBackground: function(myToggler){
   			  if(myToggler) myToggler.removeClass('active').addClass('background');
				}
	});
        
   if ($('topimg') || $('choosedrawer')) {
   if (Cookie.get("topimg")) $('topimg').setProperty('src', '../img/TRAVELNOW/PL/titles/'+Cookie.get("topimg")+'.jpg');
   else $('topimg').setProperty('src', '../img/TRAVELNOW/PL/titles/14.jpg');

   $('choosedrawer').injectBefore('mainwrap');   
      
    var chooseSlide = new Fx.Slide('choosedrawer');
	  chooseSlide.hide();
	      
    if ($('registerform')) {
  
      $('chooseyouskin').setOpacity(0);

    }
    else {

    $('chooseyouskin').addEvent('click', 
      function() {
      
      $('choosedrawer').setStyle('display','block');
      
      if (window.loadDrawer === undefined) {
      chooseSlide.slideIn()
        .chain(function() {
          loadDrawer = new Ajax('choosedrawer.html', {method: 'get', update: 'cdwrap', onComplete: function(){
		    chooseSlide.slideIn()
            $ES('div.thumbs img','cdwrap').forEach(function(element,index){
              var tinr = index+1;
              element.addEvent('click', function() {
                $('topimg').setProperty('src', element.getProperty('src').replace('_thumb',''));
                Cookie.set("topimg",tinr);
                Cookie.set("CSYshow","no");
              })
              element.addEvent('mouseover', function() {
                element.addClass('hover');
              })
              element.addEvent('mouseout', function() {
                element.removeClass('hover');
              })
            })
      
          $('hidedrawer').addEvent('click', function() { chooseSlide.slideOut() });
          $('cysloader').setStyle('display','none');
          chooseSlide.slideIn();
          }
          }).request();  
        });
      } else {
         chooseSlide.slideIn();
      }
    });
    
    }
   
   }
   //
   // MyLogin
   //
   if ($('mylogin')) {
   $('hdmllogin').addClass('withlabel').setProperty('value','Login');
   $('hdmlpass').addClass('withlabel').setProperty('value','Hasło');
   
    $('hdmllogin').addEvent('focus', function() {
      if (this.hasClass('withlabel')) {
        this.removeClass('withlabel').setProperty('value','');
    }}) 
    $('hdmllogin').addEvent('blur', function() {
      if (this.value=='') {
        this.addClass('withlabel').setProperty('value','Login');
    }})    
    $('hdmlpass').addEvent('focus', function() {
      if (this.hasClass('withlabel')) {
        this.removeClass('withlabel').setProperty('value','').setProperty('type','password');
    }})
    $('hdmlpass').addEvent('blur', function() {
      if (this.value=='') {
        this.setProperty('type','text').addClass('withlabel').setProperty('value','Hasło');
    }})
    }
    //
    // Validators start
    //
	  
	  if ($('form')) {
      new FormValidator('formularz');
    }
    
    //
    // Faktura
    //
    
    if ($('fakturaform')) {
      var fakturaSlide = new Fx.Slide('faktura');
      if ($('czy_faktura').checked==false) fakturaSlide.hide(); else {
       	fakturaSlide.slideIn();
       	  $$('div#faktura fieldset input').forEach(function(el){
        	    el.removeClass('ignoreValidation');
      	  });
        }
        $('czy_faktura').addEvent('click', function(e){
        if($('czy_faktura').checked) {
        	fakturaSlide.slideIn();
        	  $$('div#faktura fieldset input').forEach(function(el){
        	    el.removeClass('ignoreValidation');
        	  });
	        }
        	else { 
        	fakturaSlide.slideOut();
        	  $$('div#faktura fieldset input').forEach(function(el){
        	    el.addClass('ignoreValidation');
        	  });
        	}
      });
    }
    
    
    
    if ($('search')) {
    
    $('advanced').setOpacity(0).setStyle('display','none');
		
		var triplinks = $$('div#tripoption ul li');
		triplinks.each(function(el,index) 
			{
			el.addEvent('mouseover', function()
				{
				this.setStyle('cursor','pointer');
				});
	
			el.addEvent('click', function()
				{
				$$('div#tripoption ul li.active').removeClass('active');
				this.addClass('active');
	               
				switch (index) 
					{
					case 0:
						$('advanced').setOpacity(0).setStyle('display','none');
						$('standard').setOpacity(1).setStyle('display','inline');
						$$('div#standard div#back').setOpacity(1);
/*						$$('div#standard div#back select, div#standard div#back input').each(function(el) 
							{
							el.removeProperty('disabled');
							}); */
						$('trip').setProperty('value','return');
					break;
							
					case 1:
						$('advanced').setOpacity(0).setStyle('display','none');
						$('standard').setOpacity(1).setStyle('display','inline');
						$$('div#standard div#back').setOpacity(0.25);
/*						$$('div#standard div#back select, div#standard div#back input').each(function(el) 
							{
							if(!(el.getProperty('diasbled'))) 
								el.setProperty('disabled','disabled')
							});*/
						$('trip').setProperty('value','oneway');
					break;
									  
					case 2:
						$('advanced').setOpacity(1).setStyle('display','inline');
						$('standard').setOpacity(0).setStyle('display','none');
						$('trip').setProperty('value','advanced');
					break;
					} 
				});
			});
			
			}
    
};

