// Skrypt - morph miniatur + wczytanie html Ajax

window.addEvent('domready', function(){
    var current = null,
        active_thumb = null;
    
    var panel = $('buttons'),
        glowny = $('main'),
        miniatury = $$('.miniatura'),
        galeria = $('galeria'),
        previous = $('previousPage'),
        next = $('nextPage'),
		xbutton = $('xbutton'),
		top = $('top');
    
    var min = miniatury[0].get('id').replace(/^min/,'').toInt(),
        max = miniatury.getLast().get('id').replace(/^min/,'').toInt();
    
    var topmar = new Fx.Morph('top', {duration: 700});
    var mapamargin = new Fx.Morph('mapa', {delay: 700, duration: 0});
	var mainmargin = new Fx.Morph('main', {delay: 700, duration: 0});
    var menusetfixed = new Fx.Morph('menu', {delay: 700, duration: 0});
	var galmargin = new Fx.Morph('galeria', {delay: 700, duration: 0});
	var xbmar = new Fx.Morph('xb', {delay: 700, duration: 0});
	var butmar = new Fx.Morph('buttons', {delay: 700, duration: 0});
	var menumar = new Fx.Morph('menu', {delay: 700, duration: 0});
    	
	var req = new Request.HTML({
	method: 'get',
        onSuccess: function(html){
            galeria.empty().adopt(html);
        },
        onFailure: function(){
            galeria.set('text', 'Wystapil blad przy wczytywaniu zdjecia.');
            //current = null;
        }
    });
    
    panel.set('opacity', 0);
    
	if (!window.sciezka_min) window.sciezka_min = '../mapa/minhtml/min';
	var sciezka_min = window.sciezka_min;
	
    var openPage = function(num){
        if (isNaN(num) || current === num) return;
        req.send({
            url:sciezka_min+num + '.html'
        });
        current = num;
    },
        openPrevious = function(event){
            if (event) event.stop();
            openPage(current > min ? current - 1 : min);
            
            // If nothing selected
            if(!active_thumb) {
                active_thumb = miniatury[miniatury.length-1];
                return activateDeactivate(active_thumb);
            }
            
            // Get previous thumb
            var newThumb = active_thumb.getPrevious('.miniatura');
            
            // If it doesn't exist, loop back to the last thumb
            if(!newThumb) newThumb = miniatury[miniatury.length-1];
            
            // Activate and de-activate accordingly
            activateDeactivate(newThumb,active_thumb);
        },
        openNext = function(event){
            if (event) event.stop();
            openPage(current < max ? current + 1 : max);
            
            if(!active_thumb) {
                active_thumb = miniatury[0];
                return activateDeactivate(active_thumb);
            }
            
            // Get next thumb
            var newThumb = active_thumb.getNext('.miniatura');
            
            // If it doesn't exist, loop back to the last thumb
            if(!newThumb) newThumb = miniatury[0];
            
            // Activate and de-activate accordingly
            activateDeactivate(newThumb,active_thumb);
        };
    
    var activateDeactivate = function(newThumb,oldThumb){
        newThumb.addClass('selected');
        
        if(oldThumb){
            oldThumb.removeClass('selected');
            oldThumb.morph({
                opacity: 0.3,
                width: 32,
                height: 24
            });
        }
        
        active_thumb = newThumb;
    };
    
    if (previous) previous.addEvent('click', openPrevious);
    if (next) next.addEvent('click', openNext);
	
	//xbutton start 
 xbutton.set('opacity', 0).addEvents({
     
            click: function(){
		    $('galeriabanner').style.display = 'none';
            
			    if(active_thumb === this) return;
				
				if(active_thumb && $type(active_thumb)==='element'){
                    active_thumb.removeClass('selected');
                    active_thumb.morph({
                        opacity: 0.3,
                        width: 32,
                        height: 24
                    });
				}
				$('galeria').set('text', '');
				panel.set('opacity', 0);
				xbutton.set('opacity', 0);
				
				glowny.morph ({
                    opacity: 1
                });
        galmargin.start ({'z-index': '0'});
        butmar.start ({'z-index': '0'});
        xbmar.start ({'z-index': '0'});
				
				if (Browser.Engine.trident4 || Browser.Engine.trident5) {
				topmar.start ({opacity: 1,'margin-top': '0px'});
                menusetfixed.start ({'position': 'relative'});
                mapamargin.start ({'margin-top': '0px'});
				mainmargin.start ({'margin-top': '0px'});
				xbmar.start ({'margin-top': '0px'});
				butmar.start ({'margin-top': '0px'});
				menumar.start ({'margin-top': '0px'});
					}
				else
                topmar.start ({
                    opacity: 1,
                    'margin-top': '0px'
                });
                menusetfixed.start ({
                    'position': 'relative'
                });
                mapamargin.start ({
                    'margin-top': '0px'
                });
				
				mainmargin.start ({'margin-top': '0px'});
				
				
            }
			
			
            });
        
    
    //xbutton end
    miniatury.each(function(el){ 
      		el.set('opacity', 0.3).addEvents({
            mouseenter: function(){
            	if(this===active_thumb) return;
                
            	this.morph({
                    opacity: 1,
                    width: 64,
                    height: 48
            	});
            },
            mouseleave: function(el){
                if(this===active_thumb) return;
                
                this.morph({
                    opacity: 0.3,
                    width: 32,
                    height: 24
                });
            },
            click: function(){
		$('galeriabanner').style.display = 'block';

                if(active_thumb === this) return;
                
                if(active_thumb && $type(active_thumb)==='element'){
                    active_thumb.removeClass('selected');
                    active_thumb.morph({
                        opacity: 0.3,
                        width: 32,
                        height: 24
                    });
                }
				
				xbutton.set('opacity', 0.3);
				panel.set('opacity', 0.3);
                glowny.morph ({opacity: 0.3});
				active_thumb = this;
                this.addClass('selected');
                openPage(el.get('id').replace(/^min/,'').toInt());
				galmargin.start ({'z-index': '4'});
				butmar.start ({'z-index': '5'});
				xbmar.start ({'z-index': '5'});
				
				 if (Browser.Engine.trident4 || Browser.Engine.trident5) {
					topmar.start ({opacity: 0,'margin-top': '-60px'});
                	mapamargin.start ({'margin-top': '-60px'});
					mainmargin.start ({'margin-top': '-60px'});
					galmargin.start ({'margin-top': '0px'});
					xbmar.start ({'margin-top': '0px'});
					butmar.start ({'margin-top': '0px'});
					menumar.start ({'margin-top': '-110px'});
					 }
					else
					topmar.start ({opacity: 0.3,'margin-top': '-110px'});
					menusetfixed.start ({'position': 'fixed'});
          mapamargin.start ({'margin-top': '50px'});
					mainmargin.start ({'margin-top': '50px'});
					
                
            	}
				
        	});
    	});
	});
	// mapa.js

window.addEvent('domready', function(){
$('galeriabanner').style.display = 'none';
	
var miasto = $$('.miasto'),
	main = $('main');
	
var req = new Request.HTML({
        method: 'get',
	onSuccess: function(html){
            main.empty().adopt(html);
        },
        onFailure: function(){
            main.set('text', 'Wystapil blad przy wczytywaniu danych.');
        }
    });
	
	if (!window.sciezka_miasta) window.sciezka_miasta = '../miasta/';
	var sciezka_miasta = window.sciezka_miasta;
	
var obclick = function(id){
	req.send({
            url:sciezka_miasta + id + '.html'
        })	
};
miasto.each(function(el){ 
      		el.set({opacity: 1,'background-position' : '-300px 0px'}).addEvents({
            
			mouseenter: function(el){
            	this.morph({
					opacity: 1,
					'background-position' : '0px 0px' 
					});
            },
           
		    mouseleave: function(el){ 
                this.morph({
					opacity: 1,
					'background-position' : '-300px 0px'
					});
            },
			click: function(id){
				obclick(el.get('id'));
			
			}
			
        	});
    	});
 
});
