$(document).ready(function() {

    // Hover input - text out
    $('input#search').each(function() {
        var btntext = $(this).attr('value');
        $(this).focus(function() {
            text = $(this).attr('value');
            if (text == btntext) {
                $(this).attr('value', '');
            }
        }).blur(function() {
            var text2 = $(this).attr('value');
            if (text2 == '' || text2 == undefined) {
                $(this).attr('value', btntext);
            }
        });
    });

    // ZEBRA
    $('.table-list .table-wrap tr:odd').addClass('even');

    // TABLE LIST TRIGGERS
    $('.table-list .table-wrap tr')
      .click(function(e) {
          window.location = $('a', this).attr('href');
      })
      .hover(function() {
          $(this).addClass('hover');
      },
      function() {
          $(this).removeClass('hover');
      });


    $('.table-list .filter td[class!=free]')
    .hover(function() {
        $(this).addClass('hover');
    },
    function() {
        $(this).removeClass('hover');
    });

    // FILTER.DESC SHOW
    $('.filter .show-desc').removeClass('hidden');
    $('.filter table .desc').removeClass('desc').addClass('hidden');
    $('.filter .show-desc').click(function() {
        $('.filter .show-desc').hide();
        $('.filter .hide-desc').show();
        $('.filter table td span').removeClass('hidden').addClass('desc');
        return false;
    });
    $('.filter .hide-desc').click(function() {
        $('.filter .show-desc').show();
        $('.filter .hide-desc').hide();
        $('.filter table td span').addClass('hidden').removeClass('desc');
        return false;
    });

    //zobrazeni checkboxu hromadneho oznacovani/odznacovani pokud funguje JS
    $('label[for="allShow"]').show();

    $('label[for="allShow"] input').click(function() {
        var $this = $(this);
        var el = $(this).parent();
        $('.filter table td').each(function() {
            if ($this.is(':checked')) {
                $('input', this).attr('checked', true);
                $('.showAll', el).addClass('hidden');
                $('.hideAll', el).removeClass('hidden');
            }
            else {
                $('input', this).attr('checked', false);
                $('.showAll', el).removeClass('hidden');
                $('.hideAll', el).addClass('hidden');
            }
        });
    });


    $('.filter table td').click(function(e) {
        if (e.target.tagName != 'INPUT' && e.target.tagName != 'LABEL') {
            if ($('input', this).attr('checked')) {
                $('input', this).attr('checked', false);
            }
            else {
                $('input', this).attr('checked', true);
            }
        }

        var $this = $(this);
        $('.filter table').each(function() {
            if ($('input', this).size() == $('input:checked', this).size()) {
                $('label[for="allShow"] input').attr('checked', true);
                $('label[for="allShow"] .showAll').addClass('hidden');
                $('label[for="allShow"] .hideAll').removeClass('hidden');
            }
            else {
                $('label[for="allShow"] input').attr('checked', false);
                $('label[for="allShow"] .showAll').removeClass('hidden');
                $('label[for="allShow"] .hideAll').addClass('hidden');
            }
        });
        //return false;
    });


    // DATE PICKER
    $(function() {
        $('.date-pick').datePicker({ startDate: '01/01/1995' }).dpSetPosition($.dpConst.POS_TOP, $.dpConst.POS_RIGHT);
    });

    // SCROLLS
    $('.topic-list .paging, .services-list .paging').show();
    $('.topic-list').each(function() {
        var $this = $(this)
        var scroll = {
            el: $('ul', this)
            , elWidth: function() { return scroll.el.width() + 2; }
            , elScroll: function() { return scroll.el[0].scrollWidth; }
            , elScrollL: function() { return scroll.el[0].scrollLeft; }
            , itemWidth: function() { return $('li', scroll.el).width(); }
            , coef: 4
            , hover: function(e) {
                e = e || window.event;
                if (e.type == 'mouseenter') {
                    $(e.target).addClass('hover');
                }
                else if (e.type == 'mouseleave') {
                    $(e.target).removeClass('hover');
                }
            }
            , controlN: function() {
                if (left + scroll.elWidth() >= scroll.elScroll()) {
                    $('.next', $this).fadeTo("fast", 0.33).addClass('disable').removeClass('hover').unbind();
                }
                if ($('.prev', $this).is('.disable') && left > 0) {
                    $('.prev', $this).fadeTo("fast", 1).removeClass('disable').click(scroll.prev).bind('mouseenter mouseleave', scroll.hover);
                }
            }
            , controlP: function() {
                if (left <= 0) {
                    $('.prev', $this).fadeTo("fast", 0.33).addClass('disable').removeClass('hover').unbind();
                }
                if ($('.next', $this).is('.disable') && left + scroll.elWidth() < scroll.elScroll()) {
                    $('.next', $this).fadeTo("fast", 1).removeClass('disable').click(scroll.next).bind('mouseenter mouseleave', scroll.hover);
                }
            }
            , next: function() {
                scroll.el.stop();
                var length = scroll.itemWidth() * scroll.coef;
                left = left + length;
                scroll.controlN();
                scroll.el.animate({
                    scrollLeft: left + 'px'
                }, 250 * scroll.coef);
                actualNum++;
                actualUnit();
            }
            , prev: function() {
                scroll.el.stop();
                var length = scroll.itemWidth() * scroll.coef;
                left = left - length;
                scroll.controlP();
                scroll.el.animate({
                    scrollLeft: left + 'px'
                }, 300 * scroll.coef);
                actualNum--;
                actualUnit();
            }
            , clickA: function(value) {
                scroll.el.stop();
                var length = scroll.itemWidth() * scroll.coef;
                left = length * (value - 1);
                scroll.controlN();
                scroll.controlP();
                scroll.el.animate({
                    scrollLeft: left + 'px'
                }, 250 * scroll.coef);


            }
        };
        if (scroll.elWidth() < scroll.elScroll()) {
            // paging numbers get
            var activePage = '';
            scroll.el[0].scrollLeft = 0;
            var actualNum = 0;
            var paging = '';
            var sizeLi = $('li', scroll.el).size();
            var arrayLength = sizeLi / scroll.coef;
            for (var i = 0; i < arrayLength; i++) {
                paging = paging + '<a href="#">' + (i + 1) + '</a>';
            }
            $('.paging .pages', this).append(paging);

            var actualUnit = function() {
                $('.paging .pages a', $this).removeClass('active').css('cursor', 'pointer');
                $('.paging .pages a:eq(' + actualNum + ')', $this).addClass('active').css('cursor', 'default');
                activePage = $('.paging .pages a:eq(' + actualNum + ')', $this).text();
            };
            actualUnit();

            var clickUnit = function(el) {
                actualNum = $('.paging .pages a', $this).index(el);
                actualUnit();
            }

            // next prev get
            var left = scroll.elScrollL();
            scroll.el.css('overflow-x', 'hidden');
            scroll.controlN();
            scroll.controlP();
            $('.next:not(.disable)', this).click(scroll.next).bind('mouseenter mouseleave', scroll.hover);
            $('.prev:not(.disable)', this).click(scroll.prev).bind('mouseenter mouseleave', scroll.hover);

            $('.paging .pages a', this).bind('click', function() {
                var value = $(this).text();
                if (value > activePage) {
                    scroll.clickA(value);
                }
                else if (value < activePage) {
                    scroll.clickA(value);
                }

                clickUnit($(this));
                return false;
            });
        }
        else {
            $('.topic-list .in').addClass('no-scroll');
        }
    });

    $('.services-list').each(function() {
        var $this = $(this)
        var scroll = {
            el: $('.wrap', this)
            , elWidth: function() { return scroll.el.width(); }
            , elScroll: function() { return scroll.el[0].scrollWidth; }
            , elScrollL: function() { return scroll.el[0].scrollLeft; }
            , itemWidth: function() { return $('ul', scroll.el).width(); }
            , coef: 2
            , hover: function(e) {
                e = e || window.event;
                if (e.type == 'mouseenter') {
                    $(e.target).addClass('hover');
                }
                else if (e.type == 'mouseleave') {
                    $(e.target).removeClass('hover');
                }
            }
            , controlN: function() {
                if (left + scroll.elWidth() >= scroll.elScroll()) {
                    $('.next', $this).fadeTo("fast", 0.33).addClass('disable').removeClass('hover').unbind();
                }
                if ($('.prev', $this).is('.disable') && left > 0) {
                    $('.prev', $this).fadeTo("fast", 1).removeClass('disable').click(scroll.prev).bind('mouseenter mouseleave', scroll.hover);
                }
            }
            , controlP: function() {
                if (left <= 0) {
                    $('.prev', $this).fadeTo("fast", 0.33).addClass('disable').removeClass('hover').unbind();
                }
                if ($('.next', $this).is('.disable') && left + scroll.elWidth() < scroll.elScroll()) {
                    $('.next', $this).fadeTo("fast", 1).removeClass('disable').click(scroll.next).bind('mouseenter mouseleave', scroll.hover);
                }
            }
            , next: function() {
                scroll.el.stop();
                var length = scroll.itemWidth() * scroll.coef;
                left = left + length;
                scroll.controlN();
                scroll.el.animate({
                    scrollLeft: left + 'px'
                }, 250 * scroll.coef);
                actualNum++;
                actualUnit();
            }
            , prev: function() {
                scroll.el.stop();
                var length = scroll.itemWidth() * scroll.coef;
                left = left - length;
                scroll.controlP();
                scroll.el.animate({
                    scrollLeft: left + 'px'
                }, 300 * scroll.coef);
                actualNum--;
                actualUnit();
            }
            , clickA: function(value) {
                scroll.el.stop();
                var length = scroll.itemWidth() * scroll.coef;
                left = length * (value - 1);
                scroll.controlN();
                scroll.controlP();
                scroll.el.animate({
                    scrollLeft: left + 'px'
                }, 250 * scroll.coef);


            }
        };
        if (scroll.elWidth() < scroll.elScroll()) {
            // paging numbers get
            var activePage = '';
            scroll.el[0].scrollLeft = 0;
            var actualNum = 0;
            var paging = '';
            var sizeLi = $('ul', scroll.el).size();
            var arrayLength = sizeLi / scroll.coef;
            for (var i = 0; i < arrayLength; i++) {
                paging = paging + '<a href="#">' + (i + 1) + '</a>';
            }
            $('.paging .pages', this).append(paging);

            var actualUnit = function() {
                $('.paging .pages a', $this).removeClass('active').css('cursor', 'pointer');
                $('.paging .pages a:eq(' + actualNum + ')', $this).addClass('active').css('cursor', 'default');
                activePage = $('.paging .pages a:eq(' + actualNum + ')', $this).text();
            };
            actualUnit();

            var clickUnit = function(el) {
                actualNum = $('.paging .pages a', $this).index(el);
                actualUnit();
            }

            // next prev get
            var left = scroll.elScrollL();
            scroll.el.css('overflow-x', 'hidden');
            scroll.controlN();
            scroll.controlP();
            $('.next:not(.disable)', this).click(scroll.next).bind('mouseenter mouseleave', scroll.hover);
            $('.prev:not(.disable)', this).click(scroll.prev).bind('mouseenter mouseleave', scroll.hover);

            $('.paging .pages a', this).bind('click', function() {
                var value = $(this).text();
                if (value > activePage) {
                    scroll.clickA(value);
                }
                else if (value < activePage) {
                    scroll.clickA(value);
                }

                clickUnit($(this));
                return false;
            });
        }
        else {
            $('.services-list .in').addClass('no-scroll');
        }
    });







});