// history carousel
function historyScroll(period, e) {
    var scrollWidth = (period - 20) * 96;
    $('#historyWrapper').animate({scrollLeft: scrollWidth},500,'swing');
    $('#history .item').animate({'opacity': '.3'},100);
    $('#history .item .historyPrev, #history .item .historyNext').css('display','none');
    $('#h' + period).animate({'opacity': '1'},100);
    $('#h' + period + ' .historyPrev, #h' + period + ' .historyNext').css('display','block');
    $('#historyReel li').removeClass('active').animate({'padding-top': '0'}, 100);
    $('#historyReel li#h' + period + 'link').addClass('active').animate({'padding-top': '18px'}, 100);
    if (e) { e.preventDefault(); };
};

// display lightbox with wallpapers
function displayWallpapers() {
    if (($('#adidasCode').val() * 1) == 201100099) {
        $.fancybox({
                'transitionIn'   : 'elastic',
                'transitionOut'  : 'elastic',
                'overlayColor'   : '#000',
                'overlayOpacity' : 0.5,
                'href'           : '/download/vanoce/wallpapery.html'
        });   
    }
    if ($('#adidasCode').val() == '') {
        $.fancybox(
                '<p style="line-height:18px; font-weight:bold; color:#800">Nejprve prosím zadejte kód, který jste obdrželi při nákupu z kolekce adidas.</p>',
                {
                'transitionIn'   : 'elastic',
                'transitionOut'  : 'elastic',
                'padding'        : '20px',
                'overlayColor'   : '#000',
                'overlayOpacity' : 0.5,
                'href'           : ''
        });
    }
    if ($('#adidasCode').val() != '' && ($('#adidasCode').val() * 1) != 201100099) {
        $.fancybox(
                '<p style="line-height:18px; font-weight:bold; color:#800">Chybný kód. Zkontrolujte si prosím jestli jste jej opsal/a správně.</p>',
                {
                'transitionIn'   : 'elastic',
                'transitionOut'  : 'elastic',
                'padding'        : '20px',
                'overlayColor'   : '#000',
                'overlayOpacity' : 0.5,
                'href'           : ''
        });
    }
};

// Table cells wrapper
$.fn.wrapCell = function() {
    var $el;
    return this.each(function() {
        $el = $(this);
        var href = $el.find('a').attr('href');
        $('<a/>').attr('href', href).addClass('link').appendTo($el);
        var newDiv = $('<div/>', {
                'class': 'innerWrapper'
        });
        $el.wrapInner(newDiv);
    });
};

// Pavel's centering jQuery function
$.fn.centerElement = function() {
    var centerCoordinates = [
        Math.floor($(window).width()/2),
        Math.floor($(window).height()/2)
    ];
    function doCentering(elm) {
        var halfWidth = Math.floor(elm.outerWidth() / 2);
        var halfHeight = Math.floor(elm.outerHeight() / 2);
        elm.css({
            'left': centerCoordinates[0] - halfWidth + $(window).scrollLeft() + 'px',
            'top':  centerCoordinates[1] - halfHeight + $(window).scrollTop() + 'px'
        });
    }
    if ($(this).isArray) {
        var elms = $(this);
        $.each(elms, function(key, elm) {
            doCentering(elm);
        });
        return elms;
    }
    else {
        var elm = $(this);
        doCentering(elm);
        return elm;
    }
}
    
$(document).ready(function() {

    $('#jakFungujeContent h2, #jakZacitContent h2, #barSections li, #eshops h2, #page404 p, #kariera p.positionDescription, #kontakt p, #kontakt td, #kontakt th, #history .item h2, #boxTechnologies .tabs, #barSectionsOriginals li a, #originalsSections li a span,  .sectionsNavOriginals li a span, #shopfinderHeading h1, #page404 h2, #kariera h2, .buttonMailRed, #poselstvi p, #teamsport h1, #tym h1, #teamsport li h2, .sectionHeading p, #barSectionsPerformance li a, .sectionsNavPerformance li a span, #barSections li.active, #historyReel a, #poselstvi .active p, #page404 h1, #sectionName h1, #sectionName .section, #shopfinderHeading h1 span, #shopfinderHeading span.section, span#pageHeading, .sectionHeading a').css('wordSpacing', '-.3em');
    $('.sectionHeading h2').css('wordSpacing', '-.35em');

		if (typeof c !== 'undefined')
		{
			catalog = c;
		}

    /* ONCLICK INPUT/TEXTAREA DEFAULT VALUE CLEAR */
    $('input, textarea').click(
        function() { if (this.value == this.defaultValue) { this.value = ''; } }
    );
    $('input, textarea').blur(
        function() { if (this.value == '') { this.value = this.defaultValue; } }
    );

    // NAVIGATION BAR

    // sections hover
    $('.sectionsNavPerformance li').hover(function(){
       Cufon.replace('.sectionsNavPerformance li a span', { fontWeight: '400', textShadow: '0 1px 0 #000000', fontFamily: 'AdiHaus PS CE' }); 
    });
    $('.sectionsNavOriginals li').hover(function(){
       Cufon.replace('.sectionsNavOriginals li a span', { fontWeight: '400', fontFamily: 'AdiHaus PS CE' }); 
    });

    $('#barSections li.inactive').hover(
        function(){
            Cufon.replace(this, { fontWeight: '400', color: '#ffffff', textShadow: '0 1px 0 #000000', fontFamily: 'AdiHaus PS CE' });
        },
        function(){
            Cufon.replace(this, { fontWeight: '400', color: '#000000', textShadow: '0 1px 0 #ffffff', fontFamily: 'AdiHaus PS CE' });
        }
    );

    // MAIN MENU HOVER
    var mainMenuTimeout;

    function hideMenu() {
        $('#disablingLayer').fadeOut();
        $('.nav ul li').removeClass('active');
        $('.nav div.item').slideUp('fast');
    };
    
    $('.nav ul.items li.item').hover(
        function() {
            if (mainMenuTimeout) { clearTimeout(mainMenuTimeout) };
            if (!$(this).hasClass('active')) {
                $('.nav ul li').removeClass('active');
                $(this).addClass('active');
                $('.nav div.item').hide();
                var element = $(this).attr('id');
                $('div#' + element + 'Menu').slideDown('fast');
                $('#disablingLayer').fadeIn();
            }
        },
        function() {
            mainMenuTimeout = setTimeout(hideMenu,500);
        }
    );

    $('#disablingLayer').click(function() {
        hideMenu();
    });

    // NAVIGATION BAR SUBMENU ITEMS
    $('.nav div.item li a').hover(
        function() {
            $(this).stop(true, false).animate({
                'height': '86px'
            },200);
        },
        function() {
            $(this).stop(true, false).animate({
                'height': '26px'
            },200);
        }
    );

    // PAGINATION BAR DUPLICATION
    $('div.pagination').clone().attr('id', 'pagination-bottom').appendTo('#content');
    $('div.pagination#blackFilter').clone().attr('id', 'blackFilterBottom').appendTo('#content950');

    // FANCYBOX
    $('a.fancybox').fancybox({
        'transitionIn'   : 'elastic',
        'transitionOut'  : 'elastic',
        'overlayColor'   : '#000',
        'overlayOpacity' : 0.5
    });

    // PRODUCT CATEGORIES FILTERS
    $('#sectionTabs #performanceTab').click(function(e){
        if ($(this).hasClass('active')) {}
        else {
            $(this).addClass('active');
            $('#sectionTabs #originalsTab').removeClass('active');
            $('#sectionTabs').removeClass('originalsTab').addClass('performanceTab');
        }
				catalog.updateMainTab('performance');
        e.preventDefault();
    });
    $('#sectionTabs #originalsTab').click(function(e){
        if ($(this).hasClass('active')) {}
        else {
            $(this).addClass('active');
            $('#sectionTabs #performanceTab').removeClass('active');
            $('#sectionTabs').removeClass('performanceTab').addClass('originalsTab');
        }
				catalog.updateMainTab('originals');
        e.preventDefault();
    });

    $('#switchSex li').click(function(e){
				if (typeof c !== 'undefined')
				{
	 				c.update('gender', $(this).attr('id'));
				}
        if ($(this).hasClass('active')) {
            $(this).removeClass('active');
        }
        else {
            $(this).addClass('active');
        }

        if (typeof products == 'undefined')
        {
        	e.preventDefault();
				}
				else
				{
					var link = $('#' + $(this).attr('id') + ' a');
        	document.location.href = $(link).attr('href');
				}
    });

    // technologies lightboxes
    $(window).resize(function() {
        $('.technologiesPopup').centerElement();
    });
    $('.popupLink').click(function(e) {
        var elmid = $(this).attr('rel');
        $('#popupsDisablingLayer').fadeIn();
        $('#' + elmid).centerElement().fadeIn();
        e.preventDefault();
    });
    $('#popupsDisablingLayer, .technologiesPopup a.close, .technologiesPopup a.closelink').click(function(e) {
        $('.technologiesPopup, #popupsDisablingLayer').fadeOut();
        e.preventDefault();
    });

    // set flash height
    $('.flashArea').css('height', ($(window).height() - 92) + 'px');
    $(window).resize(function() {
        $('.flashArea').css('height', ($(window).height() - 92) + 'px');
    });

    // wrap all table cells to allow css hover fx
    $('.products td, .productsPerformance td, .productsOriginals td').wrapCell();

		if (typeof catalog !== 'undefined')
		{
			var current = '#' + catalog.hashGet('main') + 'Tab';
			$(current).click();
		}

        if (typeof Custom !== 'undefined')
        {
            Custom.init();
        }
    
    // history page
    $('#historyReel li').hover(
        function(){
            if (!$(this).hasClass('active')) {
                $(this).animate({
                    'padding-top': '10px'
                }, 100);
            }
        },
        function(){
            if (!$(this).hasClass('active')) {
                $(this).animate({
                    'padding-top': '0'
                }, 100);
            }
        }
    );

    // set initial carousel position
    function initializeCarousel() {
        var windowWidth = $('#historyWrapper').width();
        var initialPosition = Math.round((windowWidth - 940) / 2);
        $('.initialSpacer').css('width', initialPosition + 'px');
    }
    
    initializeCarousel();  
    $(window).resize(function() {initializeCarousel();});

    // set carousel size
    var historyWidth = 0;
    $('#history').children('div').each(function(index) {
        historyWidth += $(this).outerWidth();
    });
    $('#history').css('width', historyWidth + 'px');

    $('#h20 .historyPrev, #h100 .historyNext').click(function(e){return false});
    $('#h20link, #h30 .historyPrev').click(function(e){historyScroll(20,e);});
    $('#h30link, #h40 .historyPrev, #h20 .historyNext').click(function(e){historyScroll(30,e);});
    $('#h40link, #h50 .historyPrev, #h30 .historyNext').click(function(e){historyScroll(40,e);});
    $('#h50link, #h60 .historyPrev, #h40 .historyNext').click(function(e){historyScroll(50,e);});
    $('#h60link, #h70 .historyPrev, #h50 .historyNext').click(function(e){historyScroll(60,e);});
    $('#h70link, #h80 .historyPrev, #h60 .historyNext').click(function(e){historyScroll(70,e);});
    $('#h80link, #h90 .historyPrev, #h70 .historyNext').click(function(e){historyScroll(80,e);});
    $('#h90link, #h100 .historyPrev, #h80 .historyNext').click(function(e){historyScroll(90,e);});
    $('#h100link, #h90 .historyNext').click(function(e){historyScroll(100,e);});

    historyScroll(20);

    // poselstvi
    $('#poselstviEn').click(function(){
        if (!$(this).hasClass('active')) {
            $('#poselstviCz').removeClass('active');
            $(this).addClass('active');
            Cufon.replace('#poselstvi p', { fontWeight: '400', fontFamily: 'AdiHaus PS CE' });
            Cufon.replace('#poselstvi .active p', { fontWeight: '400', textShadow: '0 1px 0 #000000', fontFamily: 'AdiHaus PS CE' });
        };
    });
    $('#poselstviCz').click(function(){
        if (!$(this).hasClass('active')) {
            $('#poselstviEn').removeClass('active');
            $(this).addClass('active');
            Cufon.replace('#poselstvi p', { fontWeight: '400', fontFamily: 'AdiHaus PS CE' });
            Cufon.replace('#poselstvi .active p', { fontWeight: '400', textShadow: '0 1px 0 #000000', fontFamily: 'AdiHaus PS CE' });
        }
    });

    // micoach faq
    $('.faqContent').hide();
    $('#faq-obecne').show();
    $('.leftNav li a').click(function(){
        var topscroll = $(window).scrollTop();
        $('.faqContent').hide();
        var id = $(this).attr('id');
        var contentId = id.replace(/-link/g, '');
        $('#' + contentId).fadeIn('slow');
        $(window).scrollTop(topscroll);
        return false;
    });
    $('#faqMoveUp').click(function() {
        $('body, html, document').animate({scrollTop : 420}, 500,'swing');
        return false;
    })

    // vanocni kody
    $('a.buttonList3').click(function(e){
        displayWallpapers();
        e.preventDefault();
    });
    $('#wallpapersForm').submit(function(e){
        displayWallpapers();
        e.preventDefault();
    });

}); 
