    Event.observe( window, 'resize', function() { Gallery.fitImage(); Gallery.setup(); }, false );
    Event.observe( window, 'load', function() { Gallery.fitImage(); }, false );
    
    Gallery = {
        kep: null,
        w: null,
        h: null,
        
        fitImage: function() {
        
            this.kep = $( 'nagykep' );
            this.w = $( 'gal_txt' ).cumulativeOffset().left - $( 'pic' ).cumulativeOffset().left;
            this.h = $( 'thumbz' ).cumulativeOffset().top + $( 'thumbz' ).getHeight();
            
            var matches = originalSrc.match( /(.*_)[a-z]{1,3}(\.((jpg)|(gif)))/ );
            
            src = matches[1];
            extension = matches[2];
            
            if( info[currentImage] != undefined && info[ currentImage ].originalWidth && info[ currentImage ].originalHeight )
            {
                size = this.getIdealSize( info[ currentImage ].originalWidth, info[ currentImage ].originalHeight );
            }
            else
            {
                size = this.getIdealSizeOld( this.kep.width, this.kep.height );
            }
            
            this.updateLinksImageSize( size );
            
            this.bigpic.theresNoBigger = false;
            
            var bigSize = this.getBigSize( size, currentImage );
            
            if( this.bigpic.theresNoBigger )
            {
                $( 'imagezoom' ).style.display = 'none';
            }
            else
            {
                $( 'imagezoom' ).style.display = 'block';
                this.bigpic.src = src + bigSize + extension;
            }
            
            $( 'pic' ).setStyle({ width: ( this.w - 15 ) + 'px' });
            
            this.kep.src = src + size + extension;
        },
        
        getBigSize: function( size, imageNum ) {
			if (imageNum >= info.length) { return ''; }
        
            var bigSize = null;
        
            switch( size )
            {
                case 's':
                    if( hasMoreSizes )
                    {
                        bigSize = 'ms';
                    }
                    else if ( indexOf( info[ imageNum ].availableSizes, 'm' ) !== -1 )
                    {
                        bigSize = 'm';
                    }
                    else
                    {
                        if( info[ imageNum ].originalHeight >= 300 || info[ imageNum ].originalWidth >= 300 )
                        {
                            bigSize = 'm';
                        }
                        else
                        {
                            this.bigpic.theresNoBigger = true;
                        }
                    }
                    break;
                case 'ms':
                    if( indexOf( info[ imageNum ].availableSizes, 'm' ) !== -1 )
                    {
                        bigSize = 'm';
                    }
                    else
                    {
                        this.bigpic.theresNoBigger = true;
                    }
                    break;
                case 'm':
                    if( hasMoreSizes )
                    {
                        bigSize = 'ml';
                    }
                    else if( indexOf( info[ imageNum ].availableSizes, 'l' ) !== -1 )
                    {
                        bigSize = 'l';
                    }
                    else
                    {
                        if( info[ imageNum ].originalHeight >= 660 || info[ imageNum ].originalWidth >= 660 )
                        {
                            bigSize = 'l';
                        }
                        else
                        {
                            this.bigpic.theresNoBigger = true;
                        }
                    }
                    break;
                case 'ml':
                    if( indexOf( info[ imageNum ].availableSizes, 'l' ) !== -1 )
                    {
                        bigSize = 'l';
                    }
                    else
                    {
                        this.bigpic.theresNoBigger = true;
                    }
                    break;
                case 'l':
                    if( indexOf( info[ imageNum ].availableSizes, 'xl' ) !== -1 )
                    {
                        bigSize = 'xl';
                    }
                    else
                    {
                        if( info[ imageNum ].originalHeight >= 900 || info[ imageNum ].originalWidth >= 900 )
                        {
                            bigSize = 'xl';
                        }
                        else
                        {
                            this.bigpic.theresNoBigger = true;
                        }
                    }
                    break;
                case 'xl':
                    this.bigpic.theresNoBigger = true;
                    break;
            }
            
            return bigSize;
        },
        
        widthAdj: 10,
        heightAdj: 70,
        
        getIdealSize: function( width, height ) {
        
            var whRate = width / height;
            
            for( i in widths )
            {
                if( width < widths[ i ] && height < heights[ i ] )
                {
                    actWidth  = width;
                    actHeight = height;
                }
                else
                {
                    actWidth  = whRate > 1 ? widths[ i ]  : widths[ i ]  * whRate;
                    actHeight = whRate < 1 ? heights[ i ] : heights[ i ] / whRate;
                }
                
                if( actWidth < this.w - this.widthAdj && actHeight < this.h - this.heightAdj )
                {
                    return i;
                }
            }
            
            return 's';
        },
        
        getIdealSizeOld: function( width, height ) {
        
            if ( width < height * 1.05 )
            {
                if( this.h >= 1665 )
                {
                    return 'xl';
                }
                else if( this.h >= 865 && this.h < 1665 )
                {
                    return 'l';
                }
                else if( hasMoreSizes && this.h >= 785 && this.h < 865 )
                {
                    return 'ml';
                }
                else if( this.h >= 625 && this.h < 865 )
                {
                    return 'm';
                }
                else if( hasMoreSizes && this.h >= 585 && this.h < 625 )
                {
                    return 'ms';
                }
                else
                {
                    return 's';
                }
            }
            else
            {
                if( this.w >= 1610 )
                {
                    return 'xl';
                }
                else if( this.w >= 810 && this.w < 1610 )
                {
                    return 'l';
                }
                else if( hasMoreSizes && this.w >= 730 && this.w < 810 )
                {
                    return 'ml';
                }
                else if( this.w >= 570 && this.w < 810 )
                {
                    return 'm';
                }
                else if( hasMoreSizes && this.w >= 530 && this.w < 570 )
                {
                    return 'ms';
                }
                else
                {
                    return 's';
                }
            }
        }, 
        
        setup: function() {
            var pozCook = document.cookie;
    		var mutiCook = document.cookie;
            
            var adHeight = $( 'thumbz' ).cumulativeOffset().top + $( 'thumbz' ).getHeight();
            
            $('rightad').setStyle({
                height: adHeight - 10 + 'px'
            });
            
            var thumbwidth = $( 'rightad' ).cumulativeOffset().left;
            
            if( !thumbwidth )
            {
                thumbwidth = $( 'gal_txt' ).cumulativeOffset().left + $( 'gal_txt' ).getWidth();
            }
            
            $( 'thumbz' ).setStyle({ 
                width: thumbwidth + 'px' 
            });
            $( 'thumblist' ).setStyle({ 
                width: thumbwidth + 'px' 
            });
            
    		if(pozCook.indexOf("!~")!=0){
    			var intS = pozCook.indexOf("!~");
    			var intE = pozCook.indexOf("~!");
    			var strPos = pozCook.substring(intS+2,intE);
    			$("thumblist").scrollLeft = strPos;
    		}
    	    if(mutiCook.indexOf("2~")!=0){
    			var intS = mutiCook.indexOf("2~");
    			var intE = mutiCook.indexOf("~2");
    			var muti = mutiCook.substring(intS+2,intE);
    			if( muti == "show")
                {
                    $( 'thumbz' ).setStyle({ height: '129px' });
                    $( 'thumblist' ).setStyle({ display: 'block' });
                    $( 'kiskepek' ).setStyle({ color: "#d6d6d6" });
    			}
                else
                {
                    $( 'thumbz' ).setStyle({ height: '23px' });
                    $( 'thumblist' ).setStyle({ display: 'none' });
                    $( 'kiskepek' ).setStyle({ color: "#ffffff" });
    			}
    		}
        },
        
        updateLinksImageSize: function() {
            var oThis = this;
            $( 'image_thumbs' ).select( 'a' ).each( function( element, index ) {
                var size = oThis.getIdealSize( Number( info[ index ]['originalWidth'] ), Number( info[ index ]['originalHeight'] ) );
                element.href = '?current_image_num=' + index + '&image_size=' + size;
            });
            
            $( 'gal_navi' ).select( 'a' ).each( function( element, index ) {
                if( element.hasClassName( 'arrow_next' ) )
                {
                    var imageIndex = ( currentImage === info.length + 1 ) ? 0 : ( currentImage + 1 );
                    var size = oThis.getIdealSize( Number( info[ imageIndex ]['originalWidth'] ), Number( info[ imageIndex ]['originalHeight'] ) );
                    element.href = '?current_image_num=' + ( imageIndex ) + '&image_size=' + size;
                    if( currentImage < info.length +1 || cyclePaging )
                    {
                        $( 'picLink' ).href = '?current_image_num=' + ( imageIndex ) + '&image_size=' + size;
                    }
                }
                else if( element.hasClassName( 'arrow_prev' ) )
                {
                    var imageIndex = currentImage === 0 ? ( info.length +1 ) : ( currentImage - 1 );
                    var size = oThis.getIdealSize( Number( info[ imageIndex ]['originalWidth'] ), Number( info[ imageIndex ]['originalHeight'] ) );
                    element.href = '?current_image_num=' + ( imageIndex ) + '&image_size=' + size;
                }
            });
            
            actualSize = size;
        },
        
        bigpic: {
            src: null,
            theresNoBigger: false,
            open: function() {
            
                if( this.theresNoBigger )
                {
                    return;
                }
                
                $( document.documentElement ).setStyle({ overflow: 'auto' });
                $( 'bigPicImg' ).src = this.src;
                $('thumbz').style.display = "none";
                $('container_32').style.display = "none";
                $('bigpic').style.display = 'block';
            },
            hide: function() {
                $( document.documentElement ).style.overflow = 'hidden';
                $('bigpic').style.display = "none";
                $('thumbz').style.display = "block";
                $('container_32').style.display = "block";
            }
        },
        
        openGalleryOfTheWeek: function( select ) {
            var value = select.options[ select.selectedIndex ].value;
            
            if( value != "dont" )
            {
                document.location.href = value;
            }
        }
    };

	function SetDivPosition(){
        var intX = $("thumblist").scrollLeft;
	    document.cookie = "yPos=!~" + intX + "~!";
	}
				
	function move()
	{
        $( 'thumblist' ).visible() ? moveDivDown() : moveDivUp();
	}

	function moveDivUp()
	{
        $( 'thumblist' ).setStyle({ display: 'block' });
        new PeriodicalExecuter( function( pe ) {
            $( 'thumbz' ).setStyle({ height: ( $( 'thumbz' ).getHeight() + 4 ) + 'px' });
            if( $( 'thumbz' ).getHeight() >= 129 )
            {
                $( 'thumbz' ).setStyle({ height: "129px" });
                $( 'kiskepek' ).setStyle({ color: "#d6d6d6" });
                document.cookie = "show=2~show~2";	
                pe.stop();
            }
        }, 0.001 );
	}

	function moveDivDown()
	{
        new PeriodicalExecuter( function( pe ) {
            $( 'thumbz' ).setStyle({ height: ( $( 'thumbz' ).getHeight() - 4 ) + 'px' });
            if( $( 'thumbz' ).getHeight() <= 23 )
            {
                $( 'thumbz' ).setStyle({ height: "23px" });
                $( 'thumblist' ).setStyle({ display: 'none' });
                $( 'kiskepek' ).setStyle({ color: "#ffffff" });
                document.cookie = "show=2~hide~2";
                pe.stop();
            }
        }, 0.001 );
	}
    
    
	
    /* egyeb cuccok */	
	function webaudit( ucm, uct, where, name, auditid )
	{
        document.write( '<img' + ( ( auditid != undefined ) ? ' id="' + auditid + '"' : '' ) + ' class="pixel" src="http://audit.median.hu/cgi-bin/track.cgi?uc=' + ucm + '&amp;dc=' + ( ( where == 'top' ) ? 1 : 2 ) + '&amp;ui=' + audit_same + '" alt="" />' );
        if ( uct )
        {
            var params = new Array();
            params.push( 'uc='+ uct );
            params.push( 'sd='+ screen.width +'x'+ screen.height );
            if ( screen.colorDepth != 'undefined' ) { params.push( 'cd='+ screen.colorDepth ); }
            var referrer = encodeURIComponent ? encodeURIComponent( document.referrer ) : escape( document.referrer );
            if ( referrer != '' ) { params.push( 'rp='+ referrer ); }
            params.push( 'ui='+ same );
            document.write( '<img class="pixel" src="http://pixel.' + document.domain + '/12g?' + params.join( '&amp;' ) + '" alt="" />' );
        }
    }

    WEBAUDIT=function() {

      this.WACID=null;
      this.WACIDName="WACID";


      this.getCookie=function(name)  {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++)
        {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
      }

      this.setCookie=function(name,value,topDomain) {
        var date = new Date(2020,12,31,23,59,59);
        var expires = "; expires="+date.toGMTString();
        document.cookie = name+"="+value+expires+"; path=/; domain=" + topDomain;
      }

      this.generateID=function(splitter) {
        var sp=(splitter) ? splitter : 'A';
        var now=new Date();
        return Date.parse(now.toGMTString()) + sp + Math.floor(Math.random()*1000000000);
      }

      this.getTopDomain=function(fullDomain) {
        var darabok=fullDomain.split('.');
        return darabok[(darabok.length-2)] + '.' + darabok[(darabok.length-1)];
      }

      this.getDomain=function(url) {
        var urlDarabok=url.split('/');
        return urlDarabok[2];
      }

      this.WACID=this.getCookie(this.WACIDName);
    }

    var wa=new WEBAUDIT();
    var felbontas = "";
    var audit_same =  Math.floor(Math.random()*1000000);
    var wa_url = "@u=";
    var wa_referrer = "@r=";

    if(wa.WACID==null)
    {
      wa.WACID=wa.generateID('A');
      wa.setCookie(wa.WACIDName,wa.WACID,wa.getTopDomain(wa.getDomain(document.URL)));
    }

    audit_same = audit_same + "@c=" + wa.WACID;
    if(screen) felbontas='@s='+screen.width+'x'+screen.height;
    if(document.referrer) wa_referrer=wa_referrer+document.referrer;
    if(document.URL) wa_url=wa_url+document.URL;
    audit_same = audit_same + felbontas + wa_url + wa_referrer;
    
    function indexOf( object, value ) {
        for( var i in object ) {
            if( object.i === value ) {
                return true;
            }
        }
        return false;
    }
