$(document).ready(function()
{
  $('.scrollable').scrollable({
    size: 7,
    item: '.item',
    clickable: false,
    speed: 1500,
    easing: 'linear',
    loop: false
  }).autoscroll({autoplay: true, interval: 1500});

  /*$('.items img').tooltip({
    tip: '#tooltip',
    effect: 'fade',
    position: 'top right',
    offset: [25, -60],

    onBeforeShow: function()
    {
      var text = this.getTip().html();
      this.getTip().html('<span>' + text +'</span>');

      return true;
    }
  });*/

  $('.fancybox').fancybox();

  $('.trigger_poll').fancybox({
    hideOnOverlayClick: false,
    titleShow: false,
    type: 'iframe',
    width: 580,
    height: 360,
    onStart: function() { if(GetCookie('hide_poll')!=false) return false; },
    onClosed: function() { SetCookie('hide_poll', 'true'); }
  });

  $('.trigger_poll').trigger('click');

  $('.box_handle').click(function()
  {
    var box_id = $(this).attr('rel');
    var box = $('#'+box_id);

    if(box.is(':visible')) box.slideUp('slow');
    else box.slideDown('slow');
  });

  var current_question = 1;

  $('#anketa_next').click(function()
  {
    if(current_question<6) var field = $('input[name="q_' + current_question +'"]:checked');
    else field = 'true';

    if(field.length>0)
    {
      if(current_question==6)
      {
        var vek = $('#field_vek').val();
        var mesto = $('#field_mesto').val();

        if(vek.length==0 || mesto.length==0)
        {
          alert('Prosím napíšte odpoveď');
          return false;
        }
      }

      current_question++;

      $('.question:visible').hide('slow', function() { $('#q_' + current_question).show('slow'); })
      $('#anketa_current_q').html(current_question);

      if(current_question==7)
      {
        $('#controls').hide();
        $.post('/actions/ajax.php?action=send_poll', $('#anketa').serialize(), function(a, b, c) { console.log(a); });
      }
    }
    else alert('Prosím vyberte jednu odpoveď');

    return false;
  });

  $('#sutaz_form').submit(function()
  {
    for(var q=1; q<9; q++)
    {
      var ac = $('input[name="q_' + q + '"]:checked');
      if(ac.length==0)
      {
        alert('Prosím vyplňte všetky polia...');
        return false;
      }
    }

    var x = 0;
    while((x < this.elements.length))
    {
      if(this.elements[x].value=='' && this.elements[x].className.indexOf('required')!=-1)
      {
        alert('Prosím vyplňte všetky polia...');
        this.elements[x].focus();
        return false;
      }

      x++;
    }

    return true;
  });

  $('#field_agree').click(function()
  {
    if($(this).is(':checked')) $('#button_sutaz').removeAttr('disabled');
    else $('#button_sutaz').attr('disabled', 'disabled');
  });

  $('#sutaz_button').each(function() { BlinkButton(); });
  $('.last_question').blur(function() { $('#osobne_udaje').show('slow'); });
});

function SetCookie(name, value, duration)
{
  var date = new Date();
  date.setTime(date.getTime() + (duration*24*60*60*1000));

	document.cookie = name + '=' + value + '; expires=' + date.toGMTString() + '; path=/';
}

function GetCookie(name)
{
	var nameEQ = name + '=';
	var ca = document.cookie.split(';');
	for(var i = 0; i<ca.length; i++)
  {
		var c = ca[i];
		while(c.charAt(0)==' ') c = c.substring(1, c.length);
		if(c.indexOf(nameEQ)==0) return c.substring(nameEQ.length, c.length);
	}

	return false;
}

function ValidateForm(form)
{
  var x = 0;

  while((x < form.elements.length))
  {
    if(form.elements[x].value=='' && form.elements[x].className.indexOf('required')!=-1)
    {
      alert('Prosím zadajte hodnotu v poli "'+form.elements[x].title+'"');
      form.elements[x].focus();
      return false;
    }

    x++;
  }

  form['submit'].value = 'Prosím čakajte';

  return true;
}

function BlinkButton()
{
  var b = $('#sutaz_button');

  if(b.hasClass('active'))
  {
    b.removeClass('active');
    b.css('background-position', 'left top');
  }
  else
  {
    b.addClass('active');
    b.css('background-position', 'left bottom');
  }

  setTimeout('BlinkButton()', 500);
}
