$(document).ready(function() {
    
    // google map   
    if ($('#map').length != 0) {
        var customStyle = [
        {
            stylers: [
            {
                visibility: "off"
            },
            {
                saturation: -100
            },
            {
                lightness: 49
            }
            ]
        },{
            featureType: "administrative",
            elementType: "geometry",
            stylers: [
            {
                visibility: "on"
            }
            ]
        }
        ];
    
    
        var customMapType = new google.maps.StyledMapType(customStyle, {
            name: "Custom"
        });
        
        var bounds = new google.maps.LatLngBounds();
        bounds.extend(new google.maps.LatLng(49.61071,19.46228));
        bounds.extend(new google.maps.LatLng(49.081062,22.565918));
        bounds.extend(new google.maps.LatLng(47.739323,17.830811));
        bounds.extend(new google.maps.LatLng(48.378145,16.842041));
    
        var myOptions = {
            zoom: 7,
            center: bounds.getCenter(),
            disableDefaultUI: true,
            mapTypeControlOptions: {
                mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'pink_parks']
            },
            scrollwheel: false,
            draggable: false,
            disableDoubleClickZoom: true
        
        };
        //    
        var map = new google.maps.Map(document.getElementById('map'), myOptions);
        
        map.mapTypes.set('custom', customMapType);
        map.setMapTypeId('custom');
        //map.fitBounds(bounds);
        
        // pobocky
        var bratislava = new google.maps.Marker({
            position: new google.maps.LatLng(48.19596,17.160923),
            map: map,
            icon: '/img/marker-tebau.png'
        });
        var sucany = new google.maps.Marker({
            position: new google.maps.LatLng(49.108236,19.005658),
            map: map,
            icon: '/img/marker-tebau.png'
        });
        var kosice = new google.maps.Marker({
            position: new google.maps.LatLng(48.747113,21.266763),
            map: map,
            icon: '/img/marker-tebau.png'
        });
        // bubliny k pobockam
        
        var contentBratislava = '<div><p><b>Centrála Bratislava</b></p>' +
        '<p>Adresa: <strong>Bojnická 18/C, 831 04, Bratislava</strong><br />'+
        'Telefon: <strong>02 496 839 00</strong><br />'+
        'Otváracia doba: <strong>Po-Pi 7.00 – 16.30</strong></p>'+
        '<p><a href="/stranka/centrala-bratislava">Viac informácií >> </a></p></div>';

        var contentSucany = '<div><p><b>Pobočka Sučany</b></p>' +
        '<p>Hrabinská cesta 31, 038 52, Sučany<br />'+
        'Telefon: <strong>043 42 93 275</strong><br />'+
        'Otváracia doba: <strong>Po-Pi 7.00 – 15.30</strong></p>'+
        '<p><a href="/stranka/pobocka-sucany">Viac informácií >> </a></p></div>';

        var contentKosice = '<div><p><b>Pobočka Košice</b></p>' +
        '<p>Adresa: </strong>Magnezitárska 5, 040 01, Košice</strong><br />'+
        'Telefon: <strong>055 46 65 604</strong><br />'+
        'Otváracia doba: <strong>Po-Pi 7.00 – 15.30</strong></p>'+
        '<p><a href="/stranka/pobocka-kosice">Viac informácií >> </a></p></div>';

        
        var infowindow = new google.maps.InfoWindow();
        
        google.maps.event.addListener(bratislava, 'click', function() {
            infowindow.setContent(contentBratislava);
            infowindow.open(map, bratislava);
        });
        
        google.maps.event.addListener(kosice, 'click', function() {
            infowindow.setContent(contentKosice);
            infowindow.open(map, kosice);
        });
        
        google.maps.event.addListener(sucany, 'click', function() {
            infowindow.setContent(contentSucany);
            infowindow.open(map, sucany);
        });
        
        google.maps.event.addListener(infowindow, 'closeclick', function() {
            map.panTo(bounds.getCenter());
        });
    }
            
    /*******************************************************************************************************************
    *  Navigation                                                                                                  
    *******************************************************************************************************************/
    $('#header ul > li').hover(function() {
        $(this).find('.submenu').slideDown(200);
    },function() {
        $(this).find('.submenu').slideUp(200);
    });
    
    /*******************************************************************************************************************
    *  Slideshow                                                                                                  
    *******************************************************************************************************************/
    slideshowInit();
    function slideshowInit() {
        var item = $('#slideshow > li');
        var count = item.length - 1;
        var interval = 3000;
        var duration = 2000;
        var i = 0;
        if(count != 0) {
            setInterval(slideshow, interval);
        }
        function slideshow() {            
            if(i == count) { 
                i = -1; 
                item.eq(count).fadeOut(duration);
                item.eq(0).fadeIn(duration);
            }else{
                item.eq(i).fadeOut(duration).next().fadeIn(duration);
            }        
            i++;
        }
    }
    
    /*******************************************************************************************************************
    *  Slider                                                                                                  
    *******************************************************************************************************************/
   
    var currentIndex = 0;
    var cl = 0;
    var wrap = $('#wrap');
    
    $('#wrap ul').width(($('#wrap ul li').length + 4) * 130);
    $('#wrap ul li:eq(0)').clone().appendTo('#wrap ul');
    $('#wrap ul li:eq(1)').clone().appendTo('#wrap ul');
    $('#wrap ul li:eq(2)').clone().appendTo('#wrap ul');
    $('#wrap ul li:eq(3)').clone().appendTo('#wrap ul');
   
    var mod = $('#wrap ul li').length;
   
    // po najeti na polozku pozastavit
    $('#wrap ul li').mouseover(function() {
        $('#slider .next').addClass('hover');
    });
    // po najeti na polozku pozastavit
    $('#wrap ul li').mouseout(function() {
        $('#slider .next').removeClass('hover');
    });
    // po najeti na sipku pozastavit
    $('#slider .next, #slider .prev').mouseover(function() {
        $(this).addClass('hover');
    });
    // po najeti na sipku pozastavit
    $('#slider .next, #slider .prev').mouseout(function() {
        $(this).removeClass('hover');
    });
    
    $('#slider .next').click(function(e) {
        e.preventDefault();
        if (currentIndex == mod - 4) {
            wrap.stop().scrollTo( 'li:eq(0)', 0 );
            currentIndex = 0;
        }
        ++currentIndex;
        wrap.stop().scrollTo( 'li:eq('+currentIndex+')', 800 );
        
    });
   
    $('#slider .prev').click(function(e) {
        e.preventDefault();
        if (currentIndex == 0) {
            currentIndex = mod - 4;
            wrap.stop().scrollTo( 'li:eq('+currentIndex+')', 0 );
        }
        --currentIndex;
        wrap.stop().scrollTo( 'li:eq('+currentIndex+')', 800 );
        
    });
    
    function scroll() {
        var hover = ($('#slider .prev').hasClass('hover') || $('#slider .next').hasClass('hover'));
        
        if (!hover) {
            if (currentIndex == mod - 4) {
                wrap.stop().scrollTo( 'li:eq(0)', 0 );
                currentIndex = 0;
            }
            currentIndex++;
            currentIndex = (currentIndex % mod);
            wrap.stop().scrollTo( 'li:eq('+currentIndex+')', 800 );
            
            setTimeout(scroll, 2000);
        } else {
            setTimeout(scroll, 2000);
        }
        
    }
    
    setTimeout(scroll, 2000);
    

   
   
   
//    sliderInit();
//    function sliderInit() {
//        /* Init */
//        var animationRun = false;
//        var slider = $('#slider ul');
//        var visibleCount = 4;
//        var item = new Array();
//        var itemWidth = $('#slider ul > li').outerWidth();
//        var sliderStepWidth = itemWidth + 10;       
//        $('#slider ul > li').each(function(index) {
//            item[index] = $(this).html();
//        });
//        var count = item.length;        
//        $(slider).prepend('<li>' + item[count - 1] + '</li>');         
//        var positionLeft = -130;
//        for(c=0;c<count;c++){
//            $('#slider ul > li').eq(c).css('left', positionLeft + 'px');
//            positionLeft +=  sliderStepWidth;
//        }                        
//        $('#slider ul > li:last').remove();        
//        var sliderWrap = $('#slider');                                
//        var visibleWidth = visibleCount * sliderStepWidth;        
//        var sliderWidth = (count + 1) * sliderStepWidth;
//        $(slider).css({
//            'width' : sliderWidth
//        });
//        var prev = $('#slider a.prev');
//        var next = $('#slider a.next');                
//        $('#slider ul > li').click(function() {
//            location.href = $(this).find('a').attr('href');
//        });
//        
//        /* Settings */
//        var interval = 2000; 
//        var duration = 500;                
//                        
//        if(sliderWidth > visibleWidth) {
//
//            function sliderAnimationNext() {                
//                animationRun = true;
//                position = slider.position().left;
//                step = position - sliderStepWidth;   
//                lastPosition = $(slider).find('li:last').position().left - step;
//                $(slider).append($(slider).find('li:first').clone().css('left', lastPosition));
//                $(slider).find('li:first').remove();                
//                $('#slider ul > li').each(function(index) {
//                    var itemLeft = $(this).position().left + step;
//                    $(this).stop(true,true).animate({
//                        left: itemLeft
//                    }, duration, function() {
//                        animationRun = false;
//                    });
//                });                                
//            }
//
//            function sliderAnimationPrev() {
//                animationRun = true;
//                position = slider.position().left;
//                step = position - sliderStepWidth;   
//                lastPosition = $(slider).find('li:first').position().left + step;
//                $(slider).prepend($(slider).find('li:last').clone().css('left', lastPosition));
//                $(slider).find('li:last').remove();
//                $('#slider ul > li').each(function(index) {
//                    var itemLeft = $(this).position().left - step;
//                    $(this).stop(true,true).animate({
//                        left: itemLeft
//                    }, duration, function() {
//                        animationRun = false;
//                    });
//                });
//            }
//
//            /* Prev */
//            $(prev).click(function(a) {
//                a.preventDefault();
//                if(animationRun == false){
//                    sliderAnimationPrev();
//                }
//            });
//
//            /* Next */
//            $(next).click(function(a) {
//                a.preventDefault();
//                if(animationRun == false){
//                    sliderAnimationNext();
//                }
//            });
//
//            // Add hover state
//            var isHover = false;
//
//            $(sliderWrap).hover(function() {
//                $(sliderWrap).addClass('hover'); // Debug
//                isHover = true;
//                clearInterval(sliderInterval);
//            }, function() {
//                $(sliderWrap).removeClass('hover'); // Debug
//                isHover = false;
//                sliderInterval = setInterval(sliderAnimationNext, interval);
//            });
//            
//            /* Auto animation */
//            var sliderInterval = setInterval(sliderAnimationNext, interval);
//            
//        }else{
//            next.addClass('disabled');
//        }        
//
//    }  
  
});

