﻿// Funkce spouštěné při načítání stránky


	// Fce pro vytvoreni bezpecneho odkazu
	inCMS.initLinks = function() {
		var mailList = $('span.link');
		mailList.each(function(){
			var mail = $(this).html().replace('(zavinac)','@');
			mail = mail.replace(/\(tecka\)/g,'.');
			$(this).html('<a href="mailto:'+mail+'" title="'+mail+'">' + mail +'</a>');
		});
	}
inCMS.initLinks();

$(document).ready(function () {

    /* Not used for enorm CPU usage
    // Resize background image
    var backgroundImage = $('#backgroundImage');    
    $(window).resize(function () {
    resizeBackground(backgroundImage);
    });
    resizeBackground(backgroundImage);
    */

    // Set head line width
    var head1Width = $('#mainContentBox h1').outerWidth(true);
    $('#mainContentBox .headLine').animate({ marginLeft: head1Width + 12 }, 1000);

    // Add isparent class to parent menu items
    $('#menuList-h1 li').each(function () {
        if ($(this).children('ul').size() >= 1) {
            $(this).addClass('isParent');
        }
    });

    // Animate submenu
    $('#menuList-h1 li').each(function () {
        $(this).children('ul').hide();
    });
    $('#menuList-h1 li').hover(function () {
        $(this).addClass('hover');

        $(this).parent().children('li').children('ul').hide().find('ul').hide();

        $(this).children('ul').stop(true, true);
        if ($(this).children('ul').css('display') != 'block') $(this).children('ul').fadeIn();
    }, function () {
        $(this).removeClass('hover');
        thisLi = $(this);

        setTimeout(function () {
            if (!$(thisLi).hasClass('hover')) {
                $(thisLi).children('ul').stop(true, true).fadeOut();
            }
        }, 2000);
    });

    // Add even class to table rows
    $('table tr:nth-child(even)').addClass('even');

    // Clear gallery rows
    //$('.gallery .item:nth-child(3n+1)').css({ clear: 'both' });

    // Erase brackets from actual page indicator
    function removeBracketsFromText(inObject) {
        text = $(inObject).text();
        text = text.replace("[", "");
        text = text.replace("]", "");
        $(inObject).text(text);
    }
    $('.gallery .pages .selected').each(function () {
        removeBracketsFromText(this);
    });
    $('.list .pages .selected').each(function () {
        removeBracketsFromText(this);
    });
    $('.list2 .pages .selected').each(function () {
        removeBracketsFromText(this);
    });

    // Center pictures in list
    $('.list .listItem').each(function () {
        var imageBorder = $(this).children('.image');
        var imageBorderWidth = $(imageBorder).width();
        var image = $(imageBorder).children('img');
        var imageWidth = $(image).width();
        $(image).css('left', imageBorderWidth / 2 - imageWidth / 2 + 'px');
    });
    $('.list2 .listItem').each(function () {
        var imageBorder = $(this).children('.image');
        var imageBorderWidth = $(imageBorder).width();
        var image = $(imageBorder).children('img');
        var imageWidth = $(image).width();
        $(image).css({
            left: imageBorderWidth / 2 - imageWidth / 2
        });
    });

    // Add list navigation to list type2
    $('.list2:nth-child(2n+1)').addClass('even');
    $('.list2').each(function () {
        var list = $(this);
        var listItems = $(this).find('.listItem');
        var listSize = $(listItems).size();
        var itemWidth = 994;
        var activeItem = 0;
        var actualItem = 0;

        $(list).children('.content').width(listSize * itemWidth);

        $(listItems).each(function () {
            var navigationLinks = $(this).find('.navigation a');
            $(navigationLinks).eq(actualItem).addClass('active');
            $(navigationLinks).click(function (event) {
                event.preventDefault();
                slideListContentToItem(list, this);
                return false;
            });
            actualItem++;
        });

        // autoanimation
        setInterval(function () {
            if (!$(list).hasClass('hover')) {
                //slideListContentToNextItem(list);
            }
        }, 5000)

        $(this).hover(function () {
            $(this).addClass('hover');

        }, function () {
            $(this).removeClass('hover');
        });
    });

    // Stretch footer line to page actions icons
    $('#footerBox-copy').css({
        marginRight: $('#upBack').size() > 0 ? $('#upBack').width() : 0
    });
		
		// Detect simple display of AudioPlayer
		$('.audioPlayer .content').each(function(){
			if( $(this).children().size() <= 1){
				$(this).parent().addClass('simple');
			}
		});

});

$(window).load(function () { 
    
});

function resizeBackground(bImage) {    
    var windowWidth = $(window).width();
    bImage.width(windowWidth);
}

function slideListContentToItem(list, link) {
    var linkHref = $(link).attr('href');
    var toPosition = parseInt(linkHref.substring(9));
    var itemWidth = 994;
    $(list).children('.content').animate({
        left: toPosition * itemWidth * (-1)
    },1000);
}

function slideListContentToNextItem(list) {
    var itemWidth = 994;
    var actualItem = parseInt($(list).children('.content').css('left')) / itemWidth * (-1);
    var itemCount = $(list).find('.listItem').size();
    if (++actualItem >= itemCount) {
        actualItem = 0;
    }
    $(list).children('.content').animate({
        left: actualItem * itemWidth * (-1)
    }, 2000);    
}

