var ewniosekValidacja = new Class({
    Extends: ReservValidation,
    setError: function(oItem, s) {
        // ustawienie wiadomosci o bledzie
        
        if (oItem.customMsg) {
             if (!oItem.errorMsg)
            	oItem.errorMsg = oItem.customMsg;
            else
                oItem.errorMsg += ' ' + oItem.customMsg;
        } else {
            if (!oItem.errorMsg)
            	oItem.errorMsg = s;
            else
                oItem.errorMsg += ' ' + s;
        }
        // dodanie do tablicy bledow blednego elementu
        if (this.aErrors.length == 0) {
            this.aErrors.push(oItem);
            return;
        }
        // jezeli element jest juz w tablicy bledow to go pomijamy
        for (var i=0; i<this.aErrors.length; i++) {
        	if (this.aErrors[i] == oItem) {
        		return;
        	}
        }
        this.aErrors.push(oItem);
    },
    
    _getItems: function() {
        //stale
        var o, name;
        for (var i=0; i<this.aStale.length; i++) {
        	o = this.oForm[this.aStale[i].name];
            name = (o.name) ? o.name : o[0].name;
            if(!name) throw new Error('ReservValidation._getItems: pole nie ma ustawionego atrybutu NAME.');
            this.aItems.push({
                obj: o,
                name: name,
                rules: this.aStale[i].rules,
                customMsg: this.aStale[i].customMsg,
                clientName: this.aStale[i].clientName
            });
        }    

    }
});

/** 
    AdvSearch - klasa (odpalac przez 'new') do pokazywania i ukrywania zaawansowanego szukania
    korzysta z obiektu EVENT i klasy BASE
    autor: pio
    last edited: 18:11 2008-08-12 by pio
*/
var AdvSearch = new Class({
    initialize: function(oLink, aHiddenIds, oTxt) {
        this.oLink = oLink;
        this.aHiddenIds = aHiddenIds;
        this.oTxt = oTxt;
        this.aHidden = [];
        this.oSearchInput = document.getElementById('btn');
    },

    init: function() {
        var This = this;
        this._getHidden();
        if (Cookie.read('advSearch')) this._show(); 
        pioEvent.addEvent(this.oLink, 'click', function(e) {
            This._handleClick(e);
        });
    },
    
    _getHidden: function() {
    	for (var i=0; i<this.aHiddenIds.length; i++) {
            this.aHidden.push(document.getElementById(this.aHiddenIds[i]));
    	}
    },

    _handleClick: function() {
        var e = pioEvent.getEvent();
        e.preventDefault();
        this._hideShow();
    },

    _hideShow: function() {
        for (var i=0; i<this.aHidden.length; i++) {
            if (isClassName(this.aHidden[i], 'hidden') == true) {
            	this.aHidden[i].className = '';
                this.oSearchInput.className = 'right';
                this.oLink.innerHTML = this.oTxt.pokazane;
                Cookie.write('advSearch', '1');
            } else {
                this.aHidden[i].className = 'hidden';
                this.oSearchInput.className = '';
                this.oLink.innerHTML = this.oTxt.ukryte;
                Cookie.dispose('advSearch');
            }
        }
    },
    
    _show: function() {
         for (var i=0; i<this.aHidden.length; i++) {
            this.aHidden[i].className = '';
            this.oSearchInput.className = 'right';
            this.oLink.innerHTML = this.oTxt.pokazane;
        }   	
    }
   
});

var GalleryNav = new Class({
    initialize: function() {
    	this.oUpperNav = document.getElementById('gallery-nav');
        this.oLowerNav = document.getElementById('slideshow');
        this.oCurrentNuberHolder = document.getElementById('gallery-current');
        this.oAllNuberHolder = document.getElementById('gallery-all');
        this.oTarget = document.getElementById('placeholder');
        
        this.oNext = document.getElementById('gallery-next');
        this.oPrev = document.getElementById('gallery-prev');
        this.oLast = document.getElementById('gallery-last');
        this.oFirst = document.getElementById('gallery-first');
        
        this.oLeft = document.getElementById('gallery-left');
        this.oRight = document.getElementById('gallery-right');
        
        this.aPics = [];
        this.iCurrent = 0;
        this.oCurrent = {};
        this.aTumbnails = [];
        this.iAll;
        this.sInvisibleClassName = 'invisible';
        this.sPicIdName = 'pic_';
        this.sTumbnailHiddenCN = 'hidden';
        this.sTumbnailNormalBorderStyle = '2px solid #A6D110';
        this.sTumbnailActivBorderStyle = '2px solid #a64b93';
    },
    
    init: function() {
    	this._getPics();
        if (this.noImages == 1) return;
        this._addClickToUpperNav();
        this._addClickToLowerNav();
        this._actualizeUpperNav();
        this._actualizeLowerNav();
    },
 
    _getPics: function() {
    	this.aPics = this.oLowerNav.getElementsByTagName('img');
        this.iAll = this.aPics.length;
        this.oCurrent = this._getPicByIndex(this.iCurrent);
        if (!this.oCurrent) this.noImages = 1;
        for (var i=0; i<this.aPics.length; i++) {
            this.aTumbnails.push(i);
            if (i == 2) break;
        }
    },
 
    _addClickToUpperNav: function() {
    	var This = this;
        pioEvent.addEvent(this.oLast, 'click', function() {This._showPic(This.iAll-1);});
        pioEvent.addEvent(this.oFirst, 'click', function() {This._showPic(0);});
        pioEvent.addEvent(this.oPrev, 'click', function() {This._showPic(This.iCurrent-1);});
        pioEvent.addEvent(this.oNext, 'click', function() {This._showPic(This.iCurrent+1);});
    },
    
    _addClickToLowerNav: function() {
    	var This = this;
        pioEvent.addEvent(this.oLeft, 'click', function() {This._scroll(-1);});
        pioEvent.addEvent(this.oRight, 'click', function() {This._scroll(1);});
        
        for (var i=0; i<this.aPics.length; i++) {
            pioEvent.addEvent(this.aPics[i], 'click', function() {
                This._showPic(This._re(this.id));
            });
        }
    },
    
    _re: function(s) {
    	var res = s.match(/(\d+)/gi);
        return parseInt(res);
    },
    
    _showPic: function(i) {
        var img = this._getPicByIndex(i);
        if (img == null) return;
        this.oTarget.src = img.src;
        
        this.iCurrent = i;
        this.oCurrent = this._getPicByIndex(this.iCurrent);
        this._actualizeUpperNav();
        this._actualizeLowerNav();
    },
    
    _getPicByIndex: function(i) {
        var pic = document.getElementById(this.sPicIdName + i);
        //if (!pic) throw new Error('Zdjecie o numerze ' + i + ' nie istnieje');
    	return pic;
    },
    
    _actualizeUpperNav: function() {
    	this.oCurrentNuberHolder.innerHTML = this.iCurrent + 1;
        this.oAllNuberHolder.innerHTML = this.iAll;
       
        this.oNext.className = '';
        this.oLast.className = '';
        this.oPrev.className = '';
        this.oFirst.className = '';
       
       if (this.iCurrent == 0) {
        	this.oPrev.className = this.sInvisibleClassName;
        	this.oFirst.className = this.sInvisibleClassName;
        } else if (this.iCurrent == this.iAll-1) {
        	this.oNext.className = this.sInvisibleClassName;
        	this.oLast.className = this.sInvisibleClassName;
        }
    },
    
    _actualizeLowerNav: function() {
        this._updateThumbnailArr();  
        this._updateLeftRight();
        this._pokazMiniaturki();
        this._zmienObramowanie();
    },
 
    _scroll: function(i) {
        this._updateLeftRight2(i);        
        this._updateThumbnailArr2(i);
        this._pokazMiniaturki();
    },
    
    _pokazMiniaturki: function() {
        for (var i=0; i<this.aPics.length; i++) {
            this.aPics[i].className = this.sTumbnailHiddenCN;
        }    
        for (var i=0; i<this.aTumbnails.length; i++) {
        	this.aPics[this.aTumbnails[i]].className = ''; 
        }    	
    },
    
    _zmienObramowanie: function() {
        for (var i=0; i<this.aPics.length; i++) {
        	this.aPics[i].style.border = this.sTumbnailNormalBorderStyle;
        }  
        this.oCurrent.style.border = this.sTumbnailActivBorderStyle;        
    },
 
    _updateThumbnailArr: function() {
        var bTmp = false;
        
        for (var i=0; i<this.aTumbnails.length; i++) {
        	if (this.aTumbnails[i] == this.iCurrent) {
        		bTmp = true; 
                break;
        	}
        }   
        
        if (bTmp == false) {
            if (this.aTumbnails[2] < this.iCurrent) {
                this.aTumbnails = [this.iCurrent-2, this.iCurrent-1, this.iCurrent];
            } else {
                this.aTumbnails = [this.iCurrent, this.iCurrent+1, this.iCurrent+2];
            }
        }    	
    },

    _updateThumbnailArr2: function(i) {
        this.aTumbnails = [this.aTumbnails[0]+i, this.aTumbnails[1]+i, this.aTumbnails[2]+i];
    },
    
    _updateLeftRight: function() {
        this.oLeft.className = '';
        this.oRight.className = '';
        if (this.iAll > 3) {
            if (this.iAll - 1 - this.iCurrent < 1 || this.aTumbnails[2] == this.iAll-1) {
                this.oRight.className = this.sInvisibleClassName;
            }

            if (this.iCurrent < 1 || this.aTumbnails[0] == 0) {
                this.oLeft.className = this.sInvisibleClassName;
            }
        } else {
            this.oLeft.className = this.sInvisibleClassName;
            this.oRight.className = this.sInvisibleClassName;        	
        }
    },

    _updateLeftRight2: function(i) {
        this.oLeft.className = '';
        this.oRight.className = '';
        if (this.iAll > 3) {
            if (i > 0) {
                if (this.iAll - 1 - this.aTumbnails[2] < 2) { // przejscie na ostatni
                	this.oRight.className = this.sInvisibleClassName;
                }
            }    
            else if (i < 0) {
                if (this.aTumbnails[0] < 2) {
                	this.oLeft.className = this.sInvisibleClassName;
                }
            } else if (i == 0) {
            	this.oLeft.className = this.sInvisibleClassName;
            }
        } else {
            this.oLeft.className = this.sInvisibleClassName;
            this.oRight.className = this.sInvisibleClassName;        	
        }
    }
});


// var Search = Base.extend({
    // constructor: function(oForm, oConfig) {
    	// this.oForm = oForm;
        // this.oConfig = oConfig || {};
        // this.oTrans = oForm['tripTransport2'];
        // this.oKat = oForm['tripTransport'];
        // this.oKraj = oForm['tripCountryName'];
        // this.oWyj = oForm['tripFrom'];
        
        // this.aCountries = arrCountries;
        // this.aWyjazdy = arrWyjazdy;
        // this.aTransoprt = arrTransport;
        
        // this.aNewWyjazdy = [];
        // this.aNewCountries = [];
    // },
    
    // _dev: function(k) {
        // var m = (new Komunikat(
            // {parent: document.getElementById('container'), content: document.createTextNode(k)},
            // {css: {background: '#fff', padding: '1em', position: 'absolute', top: '0px'}}
        // )).flash(5000);    	
    // },
    
    // init: function() {
        // this._dev('dev enabled');
        // this._createTransAndCountries();
        // this._setFromUrl();
        // this._addEvents();
    // },
    
    // _addEvents: function() {
        // var This = this;
        
        // if (this.oTrans.type == 'select-one') { // Transport jset polem SELECT
        	// Event.addEvent(this.oTrans, 'change', function() {
        		// This._setWyjArray(this.value);
                // This._setKrajArray(this.value, This.oKat.value);
        	// });
            // Event.addEvent(this.oKat, 'change', function() {
                // This._setKrajArray(This.oTrans.value, this.value);
        	// });
        // } else { // Transport jset polem RADIO
        	// for (var i=0; i<this.oTrans.length; i++) {
        		// Event.addEvent(this.oTrans[i], 'click', function() {
                    // This._setWyjArray(this.value);
                    // This._setKrajArray(this.value, This.oKat.value);
        		// });
        	// }
            // Event.addEvent(this.oKat, 'change', function() {
                // This._setKrajArray(This._getRadioChecked(This.oTrans).value, this.value);
        	// });
        // }
    // },
    
    // _setFromUrl: function() {
    	// this.aVars = getUrlVars();
        
    // },
    
    // _createTransAndCountries: function() {
        // if (this.oTrans.type == 'select-one') { // Transport jset polem SELECT
            // this._setWyjArray(this.oTrans.value);
            // this._setKrajArray(this.oTrans.value);
        // } else { // Transport jset polem RADIO
            // this._setWyjArray(this._getRadioChecked(this.oTrans).value);
            // this._setKrajArray(this._getRadioChecked(this.oTrans).value);
        // }    	
    // },
    
    // _setKrajArray: function(transV, katV) {
	    // this.aNewCountries = [];
        // if (!katV) katV = '';
        
        // if (transV != '' && katV != '') { //wybrany transport i kategoria
	        // for (var i=0; i<this.aCountries.length; i++) {
		        // if (this.aCountries[i].transport[transV] == 1 && this.aCountries[i].categories[katV] == 1) {
			        // this.aNewCountries.push(this.aCountries[i]);
                // }		        
            // }	        
        // } else if (transV != '' && katV == '') { // wybrany tylko transport, kategoria dowolna
            // for (var i=0; i<this.aCountries.length; i++) {
		        // if (this.aCountries[i].transport[transV] == 1) {
			        // this.aNewCountries.push(this.aCountries[i]);
                // }		        
            // }	        
        // } else if (transV == '' && katV != '') { // transport dowolny, wybrana kategoria
	        // for (var i=0; i<this.aCountries.length; i++) {
		        // if (this.aCountries[i].categories[katV] == 1) {
			        // this.aNewCountries.push(this.aCountries[i]);
                // }		        
            // }	        
        // } else if (transV == '' && katV == '') { // wszystko dowolne
                // this.aNewCountries = this.aCountries;
        // }        
        // this._update(this.oKraj, this.aNewCountries, this.oConfig.customKraj);
    // },       

    // _setWyjArray: function(v) {
        // this.aNewWyjazdy = [];
    	// for (var i=0; i<this.aWyjazdy.length; i++) {
    		// if (this.aWyjazdy[i].transport[v] == 1) {
    			// this.aNewWyjazdy.push(this.aWyjazdy[i]);
    		// }
    	// }
        // this._update(this.oWyj, this.aNewWyjazdy, this.oConfig.customWyj);
    // },
    
    // _update: function(oSel, aSel, oCustom) {
    	// var opt = {};
        // oSel.length = 0;
        
        // if (oCustom) {
            // opt = document.createElement('option');
            // opt.value = oCustom.value;
            // opt.text = oCustom.text;
            // oSel.options.add(opt);
        // }
        
        // for (var i=0; i<aSel.length; i++) {
            // opt = document.createElement('option');
            // opt.value = (aSel[i].id) ? aSel[i].id : aSel[i].key;
            // opt.text = aSel[i].name;
            // oSel.options.add(opt);
    	// }    	
    // },
    
    // _getRadioChecked: function(a) {
         // for (i=0; i<a.length; i++) {
            // if (a[i].checked == true)
               // var b = a[i];
         // }
        // return b;         
    // }      
// });


/**
    * 
*/
var Validacja = new Class({
    Extends: ReservValidation,
    initialize: function(form, stale, zmienne, oConfig) {
    	this.oForm = form;
        this.aStale = stale;
        this.oZmienne = zmienne;
        this.oConfig = oConfig;
        this.aItems = []; // tablica przechowuje obiekty pol formularzy z rugulami
        this.aRooms = []; // index to index pokoju, wartosc to liczba osob dla danego pokoju
        this.aErrors = []; // przechowuje obiekty Items ktore byly blednie wypelnione
        this.branchContainer = {};
        this.errorClassName = this.oConfig.klasaBlednegoElementu || 'reservation-error';
        this.errorDivId = this.oConfig.branchErrorDivId || 'reservation-branch-error';
   },
    
    init: function() {
        this._getItems();
        this._addEvents();
        this.createBranchContainer();
    },
    
    _addEvents: function() {
        var This = this;
    	for (var i=0; i<this.aItems.length; i++) {
            pioEvent.addEvent(this.aItems[i].obj, 'blur', function() {
                This.checkItem(This._getItem(this.name), 1);
    		});            
    	}
        
        pioEvent.addEvent(this.oForm['booking'], 'click', function(e) {
            This.checkAll(e);
        });
        
    },
    
    createBranchContainer: function() {
    	var el = this.oForm['booking'];
        var fieldset = el.parentNode;
        this.branchContainer = document.createElement('div');
        this.branchContainer.id = this.errorDivId;
        fieldset.insertBefore(this.branchContainer, fieldset.firstChild);
    },
    
    _getItems: function() {
        //stale
        for (var i=0; i<this.aStale.length; i++) {
        	this.aItems.push({
                obj: this.oForm[this.aStale[i].name], 
                rules: this.aStale[i].rules,
                clientName: this.aStale[i].clientName
            });
        }        
        
    }
});




/* PRZESTARZALE - NIE STOSOWAC. JEST DO TEGO JUZ NAPISANA KLASA */
var op = /opera/i.test(navigator.userAgent);
var ie = !op && /msie/i.test(navigator.userAgent);	     
var mz = !op && /mozilla\/5/i.test(navigator.userAgent); 
function activateDetailView() {
  var arr = document.getElementsByTagName('A');
  for (var i=0; i<arr.length; i++) {
    if (arr[i].className.indexOf('detail') == -1) continue;
    arr[i].onclick = function () {
      var tr = findParentElement( this, 'TR' );
      if (tr) {
        tr = tr.nextSibling;
        while (tr.nodeType != 1) tr = tr.nextSibling;
        if (!tr) return true;
        var td = tr.cells[0];
        if (td.style.display != 'none' && td.style.display != '' ) {
          td.style.display = 'none';
          if ( ie ) tr.style.display = 'none';
        } else {
          if ( ie ) {
            td.style.display = 'block';
            tr.style.display = '';
          } else {
            td.style.display = 'table-cell';
          }
        }
      }
      return false;
    }
  }
}
// f dla activateDetailView
function findParentElement( node, tag ) {
  var res = null;
  tag = tag.toUpperCase();
  while (node && node.tagName != tag) {
    node = node.parentNode;
  } 
  if (node && node.tagName == tag) res = node;
  return res;
}
