

window.addEvent('domready', function() {
    var tabs = 'tabs';
    if (document.getElementById(tabs)) {    
        var tabSheet = new TabNavigation({
            sNavId: 'tabs',
            sActiveClassName: 'active',
            sDisplayNoneClassName: 'hide',
            bAddClassToParentElement: 0
        });
        tabSheet.init();
    }		
});

window.addEvent('domready', function() {
    var manuMain = 'manuMain';
    if (document.getElementById(manuMain)) {    
		var xX = $('manuMain').getElements('li');
		
		
		for (var i=0; i<xX.length; i++) {
			if (xX[i].getChildren('.sub-items')[0]) {
				xX[i].addClass('submenu');
			}
		}
    }		
});

window.addEvent('domready', function() {

	if(typeof(ep3_current_step) == 'number' && ep3_current_step == 1){
		if ($('moreCriteria')){
			$('moreCriteria').setProperty('style', 'display:none');			
			$('lessCriteriaLink').setProperty('style', 'display:none');
			$('moreCriteriaLink').setProperty('style', '');
		}		
		if ($('udogodnienia')){
			$('udogodnienia').setProperty('style', 'display:none');			
			$('udogodnieniaLinkClose').setProperty('style', 'display:none');
			$('udogodnieniaLink').setProperty('style', '');
		}	
	}

});


/* modyfikacja wyszukiwarki*/
window.addEvent('domready', function() {

	if (typeof(ep3_current_step) == 'number'){
		if ($('menupath')){
			var tabm = $('menupath');
			if($('ep3_place_step3')){
				$('ep3_place_step3').addEvent('click', function(e) {
					window.location.href ='index.php';
				});	
			}
			if(tabm){
				var search = tabm.getElement('tbody tbody td');			
				search.addEvent('click', function(e) {
					window.location.href = document.URL.split('?')[0];
				});	
			
				if(ep3_current_step == 2){
				var tabs = tabm.getElement('tbody').getChildren();
					tabs.each(function(tab) {
						if(tab.hasClass('color1bot')){
							var img = tab.getElement('img');								
							img.addEvent('click', function(e) {
								alert('bb');
								window.location.href ='/';
							});
						}
					});	
				}	
				
			}	
		}	
	}
});

var ModalWindow = new Class({
    initialize: function(o) {
        this.oContent = o.oContent;
        this.sContent = o.sContent;
        this.oWindowCss = o.oWindowCss;
    	this.sWindowClassName = o.sWindowClassName || 'modal-window';
        this.sHiddenClassNameForSelects = o.sHiddenClassNameForSelects || 'hide';
        this.bBackground = o.bBackground || 0;
        this.iOpacity = o.iOpacity || 60;
        this.bgColor = o.bgColor || '#000';
        this.position = o.position;
        
        this.window = {};
        this.bg = null;
        this.width = 0;
        this.height = 0;

        this.createWindow();
    },
    
    init: function() {
        this.appendWindow();
        this.getPosition();
        this.setPosition();
        if (Browser.Engine.trident4) {this.selects = $$('select'); this._handleSelects();}
        
        if (this.bg) {
            var This = this;
            
            this.scrollEvent = window.addEvent('scroll', function() {
                This.setPosition();
                This.setBackgroundPosition();
            });
            this.scrollEvent = window.addEvent('resize', function() {
                This.setPosition();
                This.setBackgroundPosition();
            });
        }
    },
    
    /**
        * Metoda do zamykania okna
    */
    close: function() {
    	if (this.bg)
    		this.bg.dispose();

        this.insertContent('');
        this.window.dispose();
        window.removeEvent('scroll', this.scrollEvent);
        window.removeEvent('resize', this.resizeEvent);
        
        if (Browser.Engine.trident4) this._showSelects();
    },
    
    flash: function(i) {
        this.init();
        var This = this;
    	setTimeout(function() {This.close();}, i);
    },
    
    /**
        * Jak widac, wstawia html'a do okna
    */
    insertContent: function(content) {
    	this.window.innerHTML = content;
    },
    
    insertObject: function(o) {
    	this.window.innerHTML = '';
        this.window.appendChild(o);
    },
      
    getPosition: function() {
        this.width = this.window.getStyle('width').toInt();
    	this.height = this.window.getStyle('height').toInt();
        
        if (isNaN(this.width) || isNaN(this.hieght)) {
        	var size = this.window.getSize();
            this.width = size.x;
            this.height = size.y;
        }
    },
    
    setPosition: function() {
        if (!this.position) {
            this.placeCenter();
            return;
        }       
            
        if (this.position.absolute) this.placeAbsolute(this.position.absolute);
        if (this.position.center) this.placeRelativeCenter(this.position.center);
    },
    
    placeAbsolute: function(o) {
    	this.window.style.top = o.y + 'px';
        this.window.style.left = o.x + 'px';
    },
    
    placeRelativeCenter: function(o) {
    	this.window.style.top = o.y - (this.height / 2) + 'px';
        this.window.style.left = o.x - (this.width / 2) + 'px';
    },
    
    placeCenter: function() {
    	var obszar = window.getSize();
        var scroll = window.getScroll();
        
        var l = Math.ceil(obszar.x / 2 - (this.width/2));
        var t = Math.ceil(obszar.y / 2 - (this.height/2));
             
        this.window.style.top = t + scroll.y + 'px';
        this.window.style.left = l + scroll.x + 'px';
    },
    
    createWindow: function() {
    	this.window = new Element('div', {'class': this.sWindowClassName});
        if (this.oContent) this.window.appendChild(this.oContent);
        else if (this.sContent) this.insertContent(this.sContent);
        else this.insertContent('');
        
        if (this.oWindowCss) {
            for (var key in this.oWindowCss) {
                this.window.style[key] = this.oWindowCss[key];
            }
        }
    },
    
    appendWindow: function() {
    	var place = document.body;
        if (this.bBackground) this.createBackground();
        place.insertBefore(this.window, place.firstChild);
    },
    
    createBackground: function() {
    	this.bg = new Element('div', {
            'styles': {
                'position' : 'absolute',
                'background-color' : this.bgColor,
                'z-Index' : '9998',
                'overflow' : 'hidden'
            }
        });
        
        this.setBackgroundPosition();
        
        if (Browser.Engine.trident)
        	this.bg.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+this.iOpacity+')';
        else
            this.bg.style.opacity = this.iOpacity / 100;
        
        this.bg.inject(document.body, 'top');
    },
    
    setBackgroundPosition: function() {
        var zxc = window.getScrollSize();
        this.bg.setStyles({
            'top': 0,
            'left': 0,
            'width': zxc.x + 'px',
            'height': zxc.y + 'px'
        });
    },
   
    _handleSelects: function() {
        if (this.bBackground) {
            this._hideSelects();
            return;
        }
        var c = this.window.getCoordinates(), sc, s;
        var w = {x1: c.left,x2: c.left+c.width,y1: c.top,y2: c.top+c.height};
        this.selects.each(function(select, i) {
            sc = select.getCoordinates();
            s = {x1: sc.left,x2: sc.left+sc.width,y1: sc.top,y2: sc.top+sc.height};
            if ((s.x1 > w.x2) || (s.x2 < w.x1) || (s.y1 > w.y2) || (s.y2 < w.y1)) {
            	select.removeClass(this.sHiddenClassNameForSelects);
            } else {
                select.addClass(this.sHiddenClassNameForSelects);
            }
        }, this);
        
    },
   
    _hideSelects: function() {
        this.selects.each(function(select, i) {
        	select.addClass(this.sHiddenClassNameForSelects);
        }, this);
    },
    
    _showSelects: function() {
        this.selects.each(function(select, i) {
        	select.removeClass(this.sHiddenClassNameForSelects);
        }, this);
    }
});

/* modal z info o nowej ofercie kiedyœ tam */
window.addEvent('domready', function() {
    if (document.getElementById('toplayer-mainmodal') && !Cookie.read('mainmodal')) {	    
		
        window.addEvent('load', function() {
		
			var content = $('toplayer-mainmodal');
			if (!content) { return false; }
			
			var newcontent = new Element('div'),
				nodes = content.clone().getChildren(),
				len = nodes.length,
				i = 0;
				
			while (i < len) {
				newcontent.grab(nodes[i]);
				i = (i + 1);
			}
				
			var m = new ModalWindow({
				oContent: newcontent,
				sWindowClassName: 'main-modal',
				sHiddenClassNameForSelects: 'hide',
				bBackground: 1
			});
			
			m.init();
            Cookie.write('mainmodal', '1');
			
			var a = content.getPrevious('div');
            var b = $$('.zamknij-to')
			a.addEvent('click', function() { m.close(); });
            b.addEvent('click', function(e) { e.stop(); m.close(); });
		});		
    }
});

/*
window.addEvent('domready', function() {
    if (document.getElementById('content-top') 
        && document.getElementById('menu-top')) {
        
        var y = $('content-top').offsetHeight;                       
        $('menu-top').style.minHeight = y-20+'px';
            
    } else if (document.getElementById('content-top') 
        && document.getElementById('menu-top-incoming')) {
        
        var y = $('content-top').offsetHeight;                       
        $('menu-top-incoming').style.minHeight = y-20+'px';
        
    }
});*/
