$(document).ready(function(){

    // Gallery

    $("a[rel^='prettyPhoto']").prettyPhoto({
            animation_speed: 'fast', /* fast/slow/normal */
            slideshow: false, /* false OR interval time in ms */
            autoplay_slideshow: false, /* true/false */
            opacity: 0.6, /* Value between 0 and 1 */
            show_title: true, /* true/false */
            allow_resize: true, /* Resize the photos bigger than viewport. true/false */
            counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
            theme: 'facebook', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
            horizontal_padding: 20, /* The padding on each side of the picture */
            hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
            wmode: 'opaque', /* Set the flash wmode attribute */
            autoplay: true, /* Automatically start videos: True/False */
            modal: false, /* If set to true, only the close button will close the window */
            deeplinking: true, /* Allow prettyPhoto to update the url to enable deeplinking. */
            overlay_gallery: false, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
            keyboard_shortcuts: true, /* Set to false if you open forms inside prettyPhoto */
            social_tools: false,
            navigation: true
    });
    
    // Radio
    
    $('#vyhledavani .radio a').click(function() {
        var anchor = $(this);
        $('#vyhledavani input[name=e]').val(anchor.attr('rel'));
        $('#vyhledavani .radio .vybrano').appendTo(anchor);
        $('#vyhledavani .radio .sipka').appendTo(anchor);
        $('#vyhledavani.hp form').attr('action',anchor.attr('href'));
        return false;
    });
    
    // Select
    
    $('#vyhledavani.sp .select a[class!=selected]').click(function() {
        $('#vyhledavani.sp .select a.selected .content').text($(this).text());
        $('#vyhledavani.sp input[name=e]').val($(this).attr('rel'));
        $('#vyhledavani.sp form').attr('action',$(this).attr('href'));
        return false;
    });
    $('#vyhledavani.as .select.obor a[class!=selected]').click(function() {
        $('#vyhledavani.as .select.obor a.selected .content').text($(this).text());
        if($(this).attr('rel') == '') {
            $('#vyhledavani.as .select.obor a.selected .content').text($(this).attr('title'));
        }
        $('#vyhledavani.as input[name=o]').val($(this).attr('rel'));
        return false;
    });
    $('#vyhledavani.as .select.zeme a[class!=selected]').click(function() {
        $('#vyhledavani.as .select.zeme a.selected .content').text($(this).text());
        if($(this).attr('rel') == '') {
            $('#vyhledavani.as .select.zeme a.selected .content').text($(this).attr('title'));
        }
        $('#vyhledavani.as input[name=z]').val($(this).attr('rel'));
        return false;
    });
    
    // Form submitt
    
    $('#vyhledavani form input[type=text] + a').click(function() {
        $(this).parent('form').submit();
        return false;
    });
    
    $('.select a.selected').click(function() {
        var el, overflow;
        if($(this).parents('#vyhledavani.as').length > 0) {
            el = $(this).prev('div');
            overflow = 'scroll';
        } else {
            el = $(this).next('div');
            overflow = '';
        }
        el.animate(
            {
                'height': 'toggle', 
                'opacity':'toggle'
            },
            {
                duration : 300,
                complete : function() {
                    if($(this).css('display') != 'none') {
                        $(this)
                        .css('height','')
                        .css('opacity','1')
                        .css('display', 'block')
                        .css('overflow-y', overflow);
                    }
                }              
            }
        );
        return false;
    }); 
        
    $('.select a[class!=selected]').click(function() {
        $(this).parent('div')
            .animate({'height': 'toggle', 'opacity':'toggle'});        
        if(!$(this).is('#pocetzaznamu .select a')) return false;
    });
    $(document).click(function() {
        $('.select > div').slideUp(); 
    });    
    
    // Rozbalit
    
    $('.sbaleno').click(function() {
        $(this).next('.sbaleno_obsah').slideDown(); 
        $(this).remove();
        return false;
    });
    
    // QR
    
    $('#QR_napoveda a').click(function() {
        $('#QR_napoveda div').toggle();
        return false;
    });
    
    // Map
    
    if($('#mapa-obal input[name=adresa]').length>0) {
        var adresa = $('#mapa-obal input[name=adresa]').val();
        
        var volby = {
            zoom: 6,
            center: new google.maps.LatLng(50.085344,14.419556),
            mapTypeControl: false,
            streetViewControl: false,            
            zoomControl: false,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        
        var map = new google.maps.Map(document.getElementById("mapa"), volby);
        var geocoder = new google.maps.Geocoder();
        
        geocoder.geocode( {
            'address': adresa
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) { 
                if(results[0].geometry.viewport) {
                    map.fitBounds(results[0].geometry.viewport);    
                    var image = '/images/marker.png';
                    new google.maps.Marker({
                      position: results[0].geometry.location,
                      map: map,
                      icon: image,
                      clickable: false
                    });
                    var urlloc =  results[0].geometry.location.toUrlValue();
                    var urlmarker = adresa+'@'+results[0].geometry.location.toUrlValue();
                    $('#mapa-obal .zvetsit').attr('href','http://maps.google.com/maps?ll='+urlloc+'&z=15&t=m&q='+urlmarker);
                    return;
                }
            }
            $('#mapa-obal').prev('h2').remove();
            $('#mapa-obal').remove();
        });
    }

});
