var menu_child_ul_timeout = 0;
var poradie = 0;
var mediaporadie = 0;

$(function () {
    $('div.firm_vip_menu ul li a').mouseenter(function () {
        $(this).parent().parent().children('ul').show();
    }).mouseleave(function () {
        var self = $(this);
        menu_child_ul_timeout = window.setTimeout(function () {
           self.parent().parent().children('ul').hide();
        }, 200);
    });
    
    $('div.firm_vip_menu ul li ul').mouseenter(function () {
        window.clearTimeout(menu_child_ul_timeout);
    }).mouseleave(function () {
        menu_child_ul_timeout = window.setTimeout(function () {
           clearTopSubmenu();
        }, 200);
    });
    
    
    widgetNewsScroll();
    mediaAnimate();
    
    
    
    if ($('.google-map').length){
        
        $('.google-map').each(function () {
            $(this).css({
                height: 400,
                zIndex: 100
            });
            
            var address = '';
            if ($('.google_description', this).val()){
                address = $('.google_description', this).val();
            }
            
            var coords = {
                lat: 48.715238,
                lng: 21.248798
            };
            
            if ($('.google_latitude', $(this).parent()).val() && $('.google_longitude', $(this).parent()).val()){
                coords.lat = $('.google_latitude' , $(this).parent()).val();
                coords.lng = $('.google_longitude', $(this).parent()).val();
            }
            
            
            
            var geocoder = new google.maps.Geocoder();
            var latlng = new google.maps.LatLng(coords.lat, coords.lng);
            
            var map = new google.maps.Map(document.getElementById($(this).attr('id')), {
                zoom: 15,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.SATELLITE,
                mapTypeControl: false
            });
            
            var marker = new google.maps.Marker({
                position: latlng,
                map: map,
                title: address
            });
            
            var infowindow = new google.maps.InfoWindow({ 
                content: address
            });
            
            google.maps.event.addListener(marker, 'click', function() {
                infowindow.open(map,marker);
            });
            
            google.maps.event.trigger(marker, 'click');
        });
        
            
    }
    
    
    
});

function clearTopSubmenu() {
    $('div.firm_vip_menu ul li ul').hide();
}


function widgetNewsScroll(){
    var current = $('div.widget-news-item:eq('+ (poradie) +')');
    
    poradie++;
    
    if ($('div.widget-news-item').length > 1){
        time = setTimeout(function (){
            if (poradie == $('div.widget-news-item').length){
                var next = $('div.widget-news-item:eq(0)');
                poradie = 0;
            } else {
                var next = $('div.widget-news-item:eq('+ (poradie) +')');
            }
            
            current.hide('visible');
            next.show('visible');
            
            widgetNewsScroll();
        }, 5000);
    }
}

function mediaAnimate() {
    var current = $('div.media-image:eq('+ (mediaporadie) +')');
    
    mediaporadie++;
    
    if ($('div.media-image').length > 1){
        time = setTimeout(function (){
            if (mediaporadie == $('div.media-image').length){
                var next = $('div.media-image:eq(0)');
                mediaporadie = 0;
            } else {
                var next = $('div.media-image:eq('+ (mediaporadie) +')');
            }
            
            next.css('position','absolute');
            next.css('top','0');
            current.fadeOut(2000);
            next.fadeIn(2000,function () {
                next.css('position','static');
            });
            
            
            mediaAnimate();
        }, 10000);
    }
}

