$(document).ready(function() {
    $('.comment .details .comment-text a').attr('target', '_blank');

    $('.time_counter').each(function() {
        var period = parseInt($(this).text());
        $(this).countdown({until: period, compact: true, description: ''});
    });

    $('.tabs').each(function() {
        var $tabs = $(this);       
        var $menu = $tabs.prev();

        var index = 0;
        if ($menu.find('li.active').length > 0)
        {
            index = $menu.find('li.active').index();
        }
        
        tabActivate($tabs, index);
        
        $menu.find('a').click(function(e) {
            e.preventDefault();

            tabActivate($tabs, $(this).parent().index());
        });
    });    
    
    $('.dots').each(function() {
    
        var $dots = $(this);
        var $menu = dotsGetMenu($dots); 
        
        dotsActivate($dots, 0);
        
        $menu.find('a').click(function(e) {
            e.preventDefault();

            if ($(this).hasClass('prev'))
            {
                index = $(this).parent().find('.active').index() - 1;
                index--;
                if (index >= 0)
                {
                    dotsActivate($dots, index);
                }
            }
            else if ($(this).hasClass('next'))
            {
                count = $(this).parent().find('a').length - 2;
                index = $(this).parent().find('.active').index() - 1;
                index++;
                if (index < count)
                {
                    dotsActivate($dots, index);
                }
            }
            else
            {
                dotsActivate($dots, $(this).index() - 1);
            }
        });
    });
    
    $('.news-banners .banner').hide();
    $('.news-banners').each(function() {
        newsBanerActivate(0);
    });
    
    $('.news-banners .banner-menu .elem').mouseover(function() {
        newsBanerActivate($(this).index());
    });
    
    $('.fancybox').fancybox({
        titlePosition: 'inside'
    });
    
    $('.photo-main .fullscreen').click(function(e) {
        e.preventDefault();
        
        $(this).closest('.photo-main').find('.fancybox:first').trigger('click');
    });
    
    $('.photo-main .image-gallery').each(function() {
        $pagination = $(this).parent().find('.image-pagination');
        
        count = $(this).find('> a').length;
        
        $pagination.append('<a href="" class="prev">&#9668;</a>');
        
        for (i = 1; i <= count; i++)
        {
            if (i == 1)
            {
                $pagination.append('<a class="number active" href="">' + i + '</a>');
            }
            else
            {
                $pagination.append('<a class="number" href="">' + i + '</a>');
            }
        }
        
        $pagination.append('<a href="" class="next">&#9658;</a>');
        
        $pagination.find('.next').click(function(e) {
            e.preventDefault();
            
            index = parseInt($(this).parent().find('.active').index()) - 1;
            
            len = $(this).parent().find('.number').length;
            
            if (index + 1 < len)
            {
                galleryShowImage(index + 1);
            }
        });
        
        $pagination.find('.prev').click(function(e) {
            e.preventDefault();
            
            index = parseInt($(this).parent().find('.active').index()) - 1;
            
            if (index - 1 >= 0)
            {
                galleryShowImage(index - 1);
            }
        });
        
        $pagination.find('.number').click(function(e) {
            e.preventDefault();
            
            index = parseInt($(this).text()) - 1;
            
            galleryShowImage(index);
        });
        
        function galleryShowImage(index)
        {
            $pagination.find('a').removeClass('active');
            $pagination.find('a').eq(index + 1).addClass('active');
            
            $('.photo-main .image-gallery a').hide();
            $('.photo-main .image-gallery a').eq(index).show();
        }
    });
    
    $('#weather-trigger').live('click', function(e) {
        e.preventDefault();
        
        if (parseInt($('.header-weather').css('height')) > 0)
        {
            $('.header-weather').stop().animate({
                height: 0
            }, 500);
        }
        else
        {
            $('.header-weather').stop().animate({
                height: 284
            }, 500);
        }
    });
    
    $('#weather-close').live('click', function(e) {
        e.preventDefault();
        
        $('.header-weather').stop().animate({
            height: 0
        }, 500);
    });
    
    $('body').click(function(e) {
        if ($(e.target).closest('.header-weather').length == 0 && $(e.target).attr('id') != 'weather-trigger' && $(e.target).closest('#weather-trigger').length == 0)
        {
            $('.header-weather').stop().animate({
                height: 0
            }, 500);
        }
    });    
    
    $('#header-weather-forecast-search').live('click', function(e) {
        e.preventDefault();

        reloadWeather();
    });
    
    $('#header-weather-forecast-location').live('keypress', function(e) {
        if (e.which == 13)
        {
            e.preventDefault();
            reloadWeather();
        }
    });
    
    $('.header-search-type').change(function() {
        if ($('#header-search-type-site').is(':checked'))
        {
            $('.header-search-site').show();
            $('.header-search-google').hide();
        }
        if ($('#header-search-type-google').is(':checked'))
        {
            $('.header-search-site').hide();
            $('.header-search-google').show();
        }
    });
    
    $('.classified-details-short .excerpt').each(function() {
        if ($(this).height() < 85)
        {
            $(this).parent().next().remove();
        }
    });
    
    $('.classified_more').click(function(e) {
        e.preventDefault();
        
        $(this).parent().find('.classified_less').show();
        $(this).hide();
        
        $(this).closest('.media').find('.classified-details').removeClass('classified-details-short');
    });
    
    $('.classified_less').click(function(e) {
        e.preventDefault();
        
        $(this).parent().find('.classified_more').show();
        $(this).hide();
        
        $(this).closest('.media').find('.classified-details').addClass('classified-details-short');
    });
});

function reloadWeather()
{
    var userLocation = $('#header-weather-forecast-location').val();

    userLocation = userLocation.replace(/ą/g, 'a').replace(/Ą/g, 'A')
        .replace(/ć/g, 'c').replace(/Ć/g, 'C')
        .replace(/ę/g, 'e').replace(/Ę/g, 'E')
        .replace(/ł/g, 'l').replace(/Ł/g, 'L')
        .replace(/ń/g, 'n').replace(/Ń/g, 'N')
        .replace(/ó/g, 'o').replace(/Ó/g, 'O')
        .replace(/ś/g, 's').replace(/Ś/g, 'S')
        .replace(/ż/g, 'z').replace(/Ż/g, 'Z')
        .replace(/ź/g, 'z').replace(/Ź/g, 'Z');
    
    $.ajax({
        url: "/home/pogoda/" + userLocation,
        success: function(data) {
            $('.header-weather').html(data);
        },
        complete: function() {
        
        },
        beforeSend: function() {
            $('.header-weather').html('<span class="loading" />')
        }
    });
    
    $.ajax({
        url: "/home/pogodaDzisiaj/" + userLocation,
        success: function(data) {
            $('#weather-trigger').html(data);
        },
        complete: function() {
        
        },
        beforeSend: function() {
            $('#weather-trigger').html('<span class="loading" />')
        }
    });
}

function newsBanerActivate(index)
{
    oldIndex = $('.news-banners .banner-menu .elem.active').index();
    $('.news-banners .banner-menu .elem').removeClass('active').eq(index).addClass('active');    
    if (oldIndex != index)
    {
        if (oldIndex >= 0)
        {    
            $('.news-banners .banner').eq(oldIndex).fadeOut(100);
        }
        $('.news-banners .banner').eq(index).fadeIn(100);
    }
}

function dotsGetMenu($dots)
{
    $menu = null;    
    if ($dots.parent().hasClass('tab'))
    {
        $menu = $dots.next().find('.dots-menu');
    }
    else
    {
        $menu = $dots.closest('.box').find('.dots-menu');
    }
    
    return $menu;
}

function dotsActivate($dots, index)
{
    $menu = dotsGetMenu($dots); 

    $dots.find('.dot').hide();
    $dots.find('.dot').eq(index).fadeIn(200);
    
    $menu.find('a').removeClass('active');
    $menu.find('a').eq(index + 1).addClass('active');
}

function tabActivate($tabs, index)
{
    $menu = $tabs.prev();

    $tabs.find('.tab').hide();
    $tabs.find('.tab').eq(index).show();
    
    $menu.find('li').removeClass('active');
    $menu.find('li').eq(index).addClass('active');
}
