$(function(){
    setTimeout(function(){ blink(); },700);
    
    var items = $('#homepage-offer div.offer');
    var itemh = items.eq(0).outerHeight();
    items.each(function () {
        var h = $(this).outerHeight();
        if (h > itemh) {
            itemh = h;
        }
    });
    items.height(itemh);
    
    //animacia hlavnych bannerov
    //carousel animation
    var banners1Timeout = null;
    var banners1ActiveIdx = 0;
    $('#banners').each(function () {
        displayNextBanners1Item.call(this, true);
    });
    
    function displayNextBanners1Item(init, itemidx) {
        if (banners1Timeout) {
            window.clearTimeout(banners1Timeout);
            banners1Timeout = null;
        }
        var $banners = $(this);
        var $bannerItems = $(this).find('li');
        var $progressbar = $('#banners-progressbar');
        var animationDuration = 700;
        if ($bannerItems.length > 1) {
            var bannerItemCur = $banners.find('li').eq(banners1ActiveIdx);
            var duration = 0;
            if (init) {
                duration = parseInt(bannerItemCur.find('input[name=duration]').val());
                if (!duration) {
                    duration = 3000;
                }
                //vytvorime bodkovadlo
                var progressItem = null;
                for (var i = 0 ; i < $bannerItems.length ; i++) {
                    progressItem = $('<span></span>');
                    progressItem.addClass('item');
                    if (i == 0) {
                        progressItem.addClass('item-active');
                    }
                    progressItem.text(i+1);
                    progressItem.click(function () {
                        displayNextBanners1Item.call($banners, false, $(this).index());
                    });
                    $progressbar.append(progressItem);
                }
                banners1Timeout = window.setTimeout(function () {
                    displayNextBanners1Item.call($banners[0]);
                },duration + animationDuration);
            } else {
                if (typeof(itemidx) == 'number') {
                    var nextItem = $bannerItems.eq(itemidx);
                } else {
                    var nextItem = bannerItemCur.next('li');
                }
                if (nextItem.length == 0) {
                    nextItem = $banners.find('li:first');
                }
                duration = parseInt(nextItem.find('input[name=duration]').val());
                if (!duration) {
                    duration = 3000;
                }
                banners1ActiveIdx = nextItem.index();
                bannerItemCur.fadeOut(animationDuration);
                nextItem.fadeIn(animationDuration, function () {
                    banners1Timeout = window.setTimeout(function () {
                        displayNextBanners1Item.call($banners[0]);
                    },duration + animationDuration);
                });
                //zobrazime bodku
                $progressbar.find('span.item')
                    .removeClass('item-active')
                    .eq(banners1ActiveIdx)
                    .addClass('item-active');
            }
        }
    }
});

function blink(){
    var self = $("#blink");
    if(self.length){
        if(self.hasClass('blink')){
            self.removeClass('blink').addClass('unblink');
        } else {
            self.removeClass('unblink').addClass('blink');
        }
    }
    setTimeout(function(){ blink(); },700);
}

