﻿document.createElement("header");
document.createElement("footer");
document.createElement("section");
document.createElement("article");
document.createElement("time");
document.createElement("figure");
document.createElement("nav");
document.createElement("aside");

$(function () {
    InitSearching();
    SmallSlider.init();
    Slider.init();
    Marquee.init();
    InitFileInputs();

    $('.ui-tabs').tabs();
    $('.ui-tabs').each(function () {
        var $this = $(this);
        $this.find('ul').eq(0).find('li').each(function (idx) {
            if ($(this).hasClass('default'))
                $this.tabs('select', idx);
        });
    });

    $('li.inblank a').attr('target', '_blank');

    $('.fancybox').fancybox({
        titlePosition: 'over'
    });

    $('.logoSliderList span').tooltip({
        track: true,
        showURL: false
    });

    setTimeout("LogoSliderInit()", 2000);

    $('.NextFormSwitch').next().hide();
    $('.NextFormSwitch').click(function () {
        $(this).next().slideToggle("slow");
    });
});

function LogoSliderInit() {

    if ($(".logoSliderList").size()) {
        var $list = $('.logoSliderList li');
        var w = 0;
        for (var i = 0; i < $list.size(); i++)
            w += $list.eq(i).width();

        if (w > $(".logoSliderList").width() || true) {
            $(".logoSliderList").carouFredSel({
                width: 779,
                circular: true,
                infinite: true,
                auto: false,
                prev: {
                    button: ".logoSliderPrev"
                },
                next: {
                    button: ".logoSliderNext"
                }
            });
            $('.logoSliderPrev').removeClass('hidden');
            $('.logoSliderNext').removeClass('hidden');
        }
    }
}

function InitSearching() {
    var label = $('.searchBox label').text();
    $('.searchBox .text').focus(function () {
        if (this.value == label)
            this.value = '';
    }).blur(function () {
        if (this.value == '')
            this.value = label;
    }).blur();
}

function InitFileInputs() {
    $('.UploaderInputFile').each(function () {
        var $this = $(this);

        var sel = ''
      + '<a href="javascript:;" title="' + ($('body').hasClass("CSCZ") ? 'vybrat soubor' : 'chosse file') + '" class="niceFileButton">'
        + ($('body').hasClass("CSCZ") ? 'vybrat soubor' : 'chosse file')
        + '<span class="overlay"></span>'
      + '</a>';

        $this.wrap('<div class="niceFile"></div>');
        $this.before('<input type="text" value="" class="niceFileInput" readonly="readonly">');
        $this.prev().after(sel);

        $this.change(function () {
            var m = $(this).val().match(/([^\/|\\]+)$/);
            $(this).prev().prev().val($(this).val() ? m[0] : ($('body').hasClass("CSCZ") ? 'Soubor nevybrán' : 'no file selected' ));
        }).change();
    });
}

var SmallSlider = {

    init: function () {

        $(".smallSlider").each(function () {

            var $this = $(this);
            var $children = $this.find("li");
            var $dots = $this.find(".dots");

            for (var i = 0; i < $children.size(); i++) {
                $dots.append('<a href="javascript:;" onclick="SmallSlider.showSlide(this,' + i + ')" class="dot"></a>');
            }

            $dots.children().eq(0).addClass("active");

            $children.each(function (idx) {
                this.style.position = "absolute";
                this.style.zIndex = $children.size() - idx;

                if (idx > 0)
                    $(this).fadeOut(0);
            });


        });
    },

    showSlide: function (link, idx) {

        var $slider = $(link).parents("section").eq(0);
        var $children = $slider.find("li");
        var $dots = $slider.find(".dot");

        for (i = 0; i < $dots.size(); i++) {
            if ($dots.eq(i).hasClass("active"))
                var currentIdx = i;
        }

        var $current = $children.eq(currentIdx);
        var $next = $children.eq(idx);

        $current.fadeOut("slow");
        $next.fadeIn("slow");

        $slider.find(".dot").removeClass("active").eq(idx).addClass("active");
        return false;
    }
}

var Slider = {

  width: 0,
  count: 0,
  slides: {},
  activeIdx: 0,
  timer: null,
  pauseTimer: null,
  playDelay: 5,
  pauseDelay: 10,
  $slider: null,

  init: function () {

    this.$slider = $(".slider");

    if (!this.$slider.size())
      return;

    this.width = this.$slider.width();
    this.count = this.$slider.find("article").size();

    $(".slider header").css("display", "block");
    $(".slider article").removeClass("hidden");

    $(".slider header span").each(function () {

      var $this = $(this);
      var w = $this.width();
      var h = $this.height();

      $this.css({
        bottom: Math.round(55 + w / 2 - h / 2 + 7),
        left: -Math.round(w / 2 - h / 2 + 1)
      });
    });

    if (this.count > 1) {
      $(".slider article").each(function (idx) {
        var $this = $(this);

        if (idx == 0) {
          $this.find("header").attr("class", "minus");
        }
        else {
          $this.css("left", Slider.width - (Slider.count - idx) * 52);
        }

        $this.css("width", Slider.width - idx * 52);
        $this.find("a").append("<span></span>");
        $this.find(".text.right").css('right', $this.nextAll("article").size() * 52 + 30);

        this.id = "SliderSlide" + idx;
        Slider.slides[this.id] = {
          '$elm': $this,
          'idx': idx
        }

        $this.find("header").click(function () {
          if (Slider.$slider.find("article:animated").size() > 0)
            return false;

          var slide = Slider.slides[$(this).parent().attr("id")];

          if (slide.idx > Slider.activeIdx) {
            slide.$elm.prevAll("article").each(function (idx, elm) {
              $(elm).animate({ 'left': Slider.slides[elm.id].idx * 52 }, "slow");
              Slider.setClass($(elm), 'plus');
            });

            slide.$elm.animate({ 'left': slide.idx * 52 }, "slow");

            Slider.setClass(slide.$elm, 'minus');
            Slider.activeIdx = slide.idx;
          }
          else if (slide.idx < Slider.activeIdx) {
            slide.$elm.nextAll("article").each(function (idx, elm) {
              $(elm).animate({ 'left': Slider.width - (Slider.count - Slider.slides[elm.id].idx) * 52 }, "slow");
              Slider.setClass($(elm), 'plus');
            });

            Slider.setClass(slide.$elm, 'minus');
            Slider.activeIdx = slide.idx;
          }

          Slider.pause();
        });
      });
    } else {
      $(".slider article header").css("visibility", "hidden");
    }

    this.play();
  },

  setClass: function ($obj, cssClass) {
    $obj.find("header").removeClass("plus minus").addClass(cssClass);
  },

  play: function () {
    clearTimeout(Slider.timer);
    Slider.timer = setTimeout("Slider.showNext()", Slider.playDelay * 1000);
  },

  pause: function () {
    clearTimeout(Slider.timer);
    clearTimeout(Slider.pauseTimer);
    Slider.pauseTimer = setTimeout("Slider.play()", Slider.pauseDelay * 1000);
  },

  stop: function () {
    clearTimeout(Slider.timer);
    clearTimeout(Slider.pauseTimer);
  },

  showNext: function () {

    this.setClass(this.$slider.find("article"), "plus");

    if (this.activeIdx < this.count - 1) {

      var slide = this.slides["SliderSlide" + (this.activeIdx + 1)];

      slide.$elm.animate({ 'left': slide.idx * 52 }, "slow", function () { Slider.play(); });

      this.setClass(slide.$elm, "minus");
      this.activeIdx++;
    }
    else {

      var slide = this.slides["SliderSlide0"];

      slide.$elm.nextAll("article").each(function (idx, elm) {
        $(elm).animate({ 'left': Slider.width - (Slider.count - Slider.slides[elm.id].idx) * 52 }, "slow", function () { Slider.play(); });
      });

      Slider.setClass(slide.$elm, "minus");
      Slider.activeIdx = 0;
    }
  }
}

var Marquee = {

    count: 0,
    currentIdx: 0,
    playDelay: 5,
    timer: null,
    $items: null,
    $marquee: null,

    init: function () {
        this.$marquee = $('.marquee');

        if (!this.$marquee.size())
            return;

        this.$items = this.$marquee.find("li");
        this.count = this.$items.size();

        this.$items.each(function (idx) {
            this.style.position = "absolute";
            this.style.zIndex = Marquee.count - idx;

            if (idx > 0)
                $(this).fadeOut(0);

            $(this)
                .mouseover(function () { Marquee.pause(); })
                .mouseout(function () { Marquee.play(); });
        });

        this.play();
    },

    showNext: function () {

        var $current = this.$items.eq(this.currentIdx);
        var $next = (this.currentIdx < this.count - 1) ? this.$items.eq(this.currentIdx + 1) : this.$items.eq(0);

        $current.fadeOut("slow");
        $next.fadeIn("slow", function () { Marquee.play(); } );

        this.currentIdx = (this.currentIdx < this.count - 1) ? this.currentIdx + 1 : 0;
    },

    play: function () {
        clearTimeout(Marquee.timer);
        Marquee.timer = setTimeout("Marquee.showNext()", Marquee.playDelay * 1000);
    },

    pause: function () {
        clearTimeout(Marquee.timer);
    }
}

/*
var LogoSlider = {

    init: function () {

        var $slider = $('.logoSliderList');
        var clone = $slider.clone();

        var wrapper = document.createElement('div');
        wrapper.style.width = 2 * $slider.width(); 
    }
}*/

$(document).ready(function () {
  $('.box.form label').each(function (index, value) {
    $('#'+$(this).attr('for')).attr('title', $(this).text());
  });

  $('.watermark').blur(function () {
    if ($(this).val() == '') {
      $(this).val($(this).attr('title'));
    }
  });

  $('.watermark').focus(function () {
    if ($(this).val() == $(this).attr('title')) {
      $(this).val('');
    }
  });

  $('input[type=submit]').hover(
    function () {
      $('.watermark').each(function (index, value) {
        if ($(this).val() == $(this).attr('title')) {
          $(this).val('');
        }
      });
    },
    function () {
      $('.watermark').each(function (index, value) {
        if ($(this).val() == '') {
          $(this).val($(this).attr('title'));
        }
      });
    });
});
