function toggleLeft() {
    $('#left_side').toggle();
}

function toggleAdvancedSearch(e) {
    var $this = $(this), speed = 1000;
    e.preventDefault();
    if ($this.data('animating')) {
        return;
    }
    $this.data('animating', true);
    if ($this.data('switched')) {
        $this.data('form').fadeOut(speed, function(){
            $this.data('switch').fadeIn(speed, function (){
                $this.data('animating', false);
            });
        });
    } else {
        $this.data('switch').fadeOut(speed, function(){
            $this.data('form').fadeIn(speed, function (){
                $this.data('animating', false);
            });
        });
    }

    $this.data('switched', !$this.data('switched'));
}

function initializeMap(mapElem, mapDefaults) {
    var latlng = new google.maps.LatLng(mapDefaults['lat'], mapDefaults['lng']);
    var myOptions = {
        zoom: mapDefaults['zoom'],
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var gm = new google.maps.Map(mapElem.get(0), myOptions);

    var mpd = mapDefaults['points'];
    for (x in mpd) {
        new google.maps.Marker({
            position: new google.maps.LatLng(mpd[x][0], mpd[x][1]),
            map: gm,
            title: mpd[x][2]
        });
    }
}

function updateSearchCount() {
    var $this = $(this);
    var $form = $this.data('form');
    var data = $form.serializeArray();
    if (typeof this.url=='undefined') {
        this.url = searchCountUrl;
    }
    $form.data('searchCount').text('...')
    $.post(
        this.url,
        data,
        function(count) {
            $form.data('searchCount').text(parseInt(count));
        }
        );
}
function submitSearch(e) {
    var $this = $(this);
    var searchCountText = $this.data('searchCount').text();
    if (searchCountText=='0') {
        alert('Az aktuális keresésnek nincs eredménye, kérjük módosítson a feltételeken!');
        e.preventDefault();
    } else if (searchCountText=='...') {
        alert('Kis türelmét kérjük, a keresés még folyamatban van.');
        e.preventDefault();
    }
}
function updateCities(e) {
    var $this = $(this);
    if ($this.data('active')) {
        return;
    }
    var val = $this.val();
    if (!val) return;

    var region = $this.data('region')
    .attr('disabled', true)
    .addClass('disabled')
    .val('')
    ;

    $this.data('active', true);

    $.post(
        $this.data('url'),
        {
            'country': val
        },
        function(data) {
            var even = true, r = region, h = r.data('origHtml'), x, sr;
            r.removeAttr('disabled').removeClass('disabled');
            for (x in data) {
                h += '<option value="'+x+'"'+(even ? ' class="even"' : '')+'>'+data[x]+'</option>';
                even = !even;
            }
            r.html(h);
            $this.data('active', false);
            
            sr = $this.data('searchRegion');
            if (sr) {
                r.val(sr).change();
                $this.data('searchRegion', '');
            }
        },
        'json'
        );
}

$(document).ready(function(){
    // Search
    var container = $('#advanced-search-container');
    var $switch = $('#advanced-search-switch');
    var snowreportCountries = $('#middle > .snowreport_countries');
    if (snowreportCountries) {
        $switch = $switch.add(snowreportCountries);
    }
    if (container.length) {
        $('#advanced-search-switcher')
            .data('form', container)
            .data('switch', $switch)
            .data('switched', false)
            .data('animating', false)
            .click(toggleAdvancedSearch)
        ;
        var form = $('form', container)
            .data('searchCount', $('#search-count'))
            ;
        $('input, select', container).data('form', form);
        container.delegate('input, select', 'change', updateSearchCount);
        var region = $('#region');
        region.data('origHtml', region.html());
        $('#country')
            .data('region', region)
            .data('url', updateCitiesUrl)
            .data('active', false)
            .data('searchRegion', searchRegion)
            .change(updateCities)
            .change()
            ;
        $('form', container).submit(submitSearch);
    } else {
        $('#advanced-search-switcher').click(function(){
            return false
        });
    }
    // Datepicker
    if ($.datepicker) {
        $('#date').datepicker();
    }

    // Newsletter
    var newsletter = ('#hirlevel');
    if (newsletter.length) {
        var sn = $('.static', newsletter).addClass('relative');
        $('.email_static', newsletter).addClass('email_relative');
        var snl = $('label', sn);
        snl.each(function(){
            var $this = $(this);
            var inp = $('input', $this.parent().next());
            inp.data('label', $this);
            inp.focus(function(){
                var $this = $(this);
                $this.data('label').hide();
            });
            inp.blur(function(){
                var $this = $(this);
                if (!$this.val()) {
                    $this.data('label').show();
                }
            });
        })
    }

    // Contact
    var contact = $('#contact');
    if (contact.length) {
        var labels = $('label', contact).addClass('over_label');
        labels.each(function(){
            var label = $(this);
            var input = label.next();
            input
            .data('label', label)
            .focus(function(){
                $(this).data('label').hide();
            })
            .blur(function(){
                var $this = $(this);
                if (!$this.val()) {
                    $this.data('label').show();
                }
            })
            ;
            if (input.val()) {
                label.hide();
            }
        });
    }


    // Show errors
    var errors = $('#errors');
    if (errors.length) {

        errors.remove();
        $('#container').prepend(errors);
        errors.css({
            position: 'absolute',
            top: '120px',
            left: '272px',
            zIndex: 1000,
            width: '480px'
        });

        var errorsPosOriginal = 120;
        $(window).scroll(function(){
            var windowPos = $(window).scrollTop();
            if (windowPos < errorsPosOriginal) {
                errors.stop().css({
                    'top':errorsPosOriginal
                });
            } else {
                errors.stop().animate({
                    'top':windowPos
                },500);
            }
        }).scroll();

        $('.closer', errors)
        .click(function(){
            errors.remove();
        })
        .css({
            cursor: 'pointer'
        });
    }

    // Modal box
    if (typeof hs != 'undefined' && typeof hs.expand != 'undefined')
    {
        var hsw = $(document).width()-100;
        var hsh = $(window).height()-50;
        /*
        // Gallery
        $('.hs').click(function(e){
            e.preventDefault();
            $('.highslide-loading').text('Kép betöltése...');
            hs.htmlExpand(this, {
                objectType: 'iframe',
                headingText: 'Galéria',
                wrapperClassName: 'titlebar',
                allowWidthReduction: true,
                align: 'center',
                width: hsw,
                height: hsh
            });
        });*/
        
        $('#container').delegate('.pg', 'click', function(e){
            e.preventDefault();
            
            $('a', $(this).closest('.gallery').next()).eq(0).click();
        });

        // Map
        var oc = $('#online_catalog');
        $('.map', oc).each(function(){
            var $this = $(this);
            $this.attr('href', $this.attr('href') + '?hsw='+hsw+'&hsh='+hsh);
        });
        oc.delegate('.map', 'click', function(e){
            e.preventDefault();
            $('.highslide-loading').text('Térkép betöltése...');
            hs.htmlExpand(this, {
                objectType: 'iframe',
                width: hsw,
                height: hsh,
                headingText: 'Térkép',
                wrapperClassName: 'titlebar',
                allowWidthReduction: true,
                align: 'center'
            });
        });
    }

    // Google maps
    if (typeof google !='undefined') {
        initializeMap($("#map-container"), defaultMap);
    }

    // Left side min-height
    var ls = $('#left_side');
    var lsBottom = parseInt(ls.offset().top + ls.height());
    var content = $('#content_ambient .content_left');
    if (!content.length)
    {
        content = $('#content_ambient');
    }
    var contentBottom = parseInt(content.offset().top + content.height());
    if (lsBottom > contentBottom) {
        content.height(parseInt(lsBottom-content.offset().top));
    };

    // Print
    $('a.print, .print a').click(function(e){
        e.preventDefault();
        window.print();
    });
    
});

// FAQ
$(document).ready(function(){
    var q = $('.kerdes'), q2, a, i;
    if (q.length > 0) {
        for (i=0; i<q.length; i++) {
            q2 = q.eq(i).css('cursor', 'pointer');
            a = q2.next().hide().data('visible', false);
            q2.data('a', a);
        }
        $('.gyakori_kerdesek').delegate('.kerdes', 'click', function(){
            var a = $(this).data('a');
            
            if (a.data('visible')) {
                a.fadeOut('fast', function(){
                    a.data('visible', false);
                });
            } else {
                a.fadeIn('slow', function(){
                    a.data('visible', true);
                });
            }
        });
    }
});

// Places
$(document).ready(function(){
    var pb = $('#place-body, #region-body'), tables;
    tables = $('table', pb);
    if (tables.length) {
        tables.filter(function(){
            return $(this).width()>515;
        }).wrap('<div class="scroller" />');
    }
});

$(document).ready(function(){
    if (typeof $.prettyPhoto != 'undefined') {
        $("a[rel^='prettyPhoto']").prettyPhoto({
            social_tools: false
        });
    }
});

$(window).resize(function () {
    var windowWidth = $(window).width();
    if (typeof this.resizing == 'undefined') {
        this.resizing = false;
        this.container = $('#container');
    }
    if (windowWidth < 1024) {
        if ( ! this.resizing) {
            this.container.width(1024);
            this.resizing = true;
        }
    } else if (this.resizing) {
        this.resizing = false;
        this.container.css('width', 'auto');
    }
}).resize();


