var geocoder = new GClientGeocoder();
var element_id = '';
var search_input_opacity = '0.8';

var hm_current = 0;
var initial_fadein = 1000;
var fade_speed = 1000;
var interval = 5000;
var inmotion = true;

$(document).ready(function() {
    $('#articles').hover(function() {
        inmotion = false;
    }, function() {
        inmotion = true;
    });
    setInterval('next()', interval);
});

$(document).ready(function() {
    //$('#content').css({'min-height': $('#banners').height()});

    $('#searchinsearch').focus(function () {$(this).select()});
    $('form .search-submit').css({opacity: search_input_opacity});
    $('form .search-submit').hover(function() {
        $(this).css({opacity: '1.0'});
    }, function() {
        $(this).css({opacity: search_input_opacity});
    });
    $('#map-home-container').mousedown(function() {
        window.location = '/map';
    });
    $('#partneri-loga a').hover(function() {
        //$(this).find('img.gray').hide();
        //$(this).find('img.color').show();
        $('#partneri-loga .gray').hide();
        $('#partneri-loga .color').show();
    }, function () {
        //$(this).find('img.gray').show();
        //$(this).find('img.color').hide();
        $('#partneri-loga .gray').hide();
        $('#partneri-loga .color').show();
    });
    $('#banners a.ad-item').click(function (e) {
        e.preventDefault();
        //console.log('/banners/stats/'+$(this).attr('id').substring(2));
        $.ajax({
            url: '/banners/stats/'+$(this).attr('id').substring(2)
        });
        window.open(this.href);
    });
    $('#m-knihkupectvi').click(function(e) {
        e.preventDefault();
    });

    $('#map-cela-cr').click(function(e) {
        e.preventDefault();
        map.setCenter(new google.maps.LatLng(49.80254124506291, 15.457763671875));
        map.setZoom(7);
    });

    var hint = 'adresa nebo město';
    if (($('#map-goto').val() == '') || ($('#map-goto').val() == hint)) {
        $('#map-goto').val(hint);
        $('#map-goto').addClass('passive');
    }
    $('#map-goto').blur(function () {
        if (($('#map-goto').val() == '') || ($('#map-goto').val() == hint)) {
            $('#map-goto').val(hint);
            $('#map-goto').addClass('passive');
        }
    });
    $('#map-goto').focus(function() {
        if ($(this).hasClass('passive')) {
            $(this).removeClass('passive');
            $(this).val('');
        } else $(this).select();
    });
    $('#map-goto').keypress(function(e) {
         if (e.which == 13) //console.log('enter');
         $('#map-goto-submit').click();
    });
    $('#map-goto-submit').click(function() {
        if (($('#map-goto').val() != '') || ($('#map-goto').val() != hint)) {
            geocoder.getLocations($('#map-goto').val(), function (locations) {
            //console.log(locations);
            if (typeof(locations.Placemark) == 'undefined') {
                alert('nenalezeno');
                return;
            }
            var north = locations.Placemark[0].ExtendedData.LatLonBox.north;
            var south = locations.Placemark[0].ExtendedData.LatLonBox.south;
            var east  = locations.Placemark[0].ExtendedData.LatLonBox.east;
            var west  = locations.Placemark[0].ExtendedData.LatLonBox.west;

            var bounds = new google.maps.LatLngBounds(new google.maps.LatLng(south, west), new google.maps.LatLng(north, east));

            map.fitBounds(bounds);

            $('#map-goto').val(hint).addClass('passive').blur();
        });
        }
    });
});

function next() {
    if (inmotion) {
        if ($('#articles .article').size() == 1) return;
        if (hm_current == ($('#articles .article').size()-1)) hm_next = 0; else hm_next = hm_current + 1;
//        console.log(hm_next + ' ' + hm_current);
        $('#articles .article:eq('+(hm_next)+')').css({'z-index': '1'});
        $('#articles .article:eq('+(hm_next)+')').show();

        $('#articles .article:eq('+hm_current+')').css({'z-index': '2'});
        $('#articles .article:eq('+hm_current+')').fadeOut({duration: 1000, easing: 'easeOutExpo'});
        hm_current = hm_next;
    }
}

