var timeoutHover;


/* Init Slider - Slick */
function InitSliderSlick1(selector)
{
  $(selector).slick({
    dots: true,
    vertical: false,
    centerMode: false,
    slidesToShow: 1,
    slidesToScroll: 1,
    autoplay: true,
    autoplaySpeed: 5000,
    arrows: false
  });
  
  /*$(selector+' .arrow-l').on('click', function(){
    $(selector).slick('slickPrev');
  });
  $(selector+' .arrow-r').on('click', function(){
    $(selector).slick('slickNext');
  });*/
}


function hideElement(id){
  if(document.getElementById(id).style.display != "none"){
    document.getElementById(id).style.display="none";
  }
}

function showElement(id){
  if(document.getElementById(id).style.display != "block"){
    document.getElementById(id).style.display="block";
  }
}

function bar(url)
{
  wasOpen  = false;
  win = window.open(url);    
  return (typeof(win)=='object')?true:false;
}

function scrollToId(id)
{
  if (id != '')
  {
    $("html, body").animate({ scrollTop: ($('#' + id).offset().top - 50) }, 1000);
  }
}

$(function() 
{
  $( ".dialogContent" ).dialog({
    autoOpen: false,
    width: 400,
    show: {
      effect: "blind",
      duration: 100
    },
    hide: {
      effect: "explode",
      duration: 100
    }
  });
});

$(document).ready(function() {
  
  //InitSliderSlick1('#mainBanner');
  
  $('.searchClose').click(function() {
    $('#searchTipsContainer').slideUp();
  });
  
  /* Cookies */
  //GoogleAnalytics('default');
  
  /*
  var stitekOpened = false;
  $('#rightFloatedFrame .openerContainer').hover(
    function() {
      var width = 40;
      var anim_time = 500;
      if (stitekOpened == false)
      {
        width = 200;
        stitekOpened = true;
      }
      else
      {
        anim_time = 200;
        stitekOpened = false; 
      }
      
      $(this).parent('#rightFloatedFrame').animate({
      width: width
      }, anim_time, function() {
        // Animation complete.
      });
    },
    function()
    {
      var width = 40;
      $(this).parent('#rightFloatedFrame').css('width',width);
      stitekOpened = false; 
    }
  );
  */
  
  $('a[rel*=lightbox]').venobox(); 
  $('.venobox').venobox({
    numeratio: true
  }); 
  
  
  $('.orderFrame .inner .selector .arrow').hover(function() {
    $(this).children('.subframe').slideToggle();
  },
  function () {
    $(this).children('.subframe').hide();
  }
  );
  
  $('#menuFrame LI').hover(
     function () 
     {
       $(this).children('.submenu').slideDown('fast');
     },
     function () 
     {
       $(this).children('.submenu').hide();
     }
  );
  
  $(function(){
    $(window).scroll(function()
    {
      var pos = $('#fullMenuFrame').position();
      var pos_top = pos.top + 90;
      //console.log(pos_top);
      if($(this).scrollTop()>=pos_top)
      {
        $('#fullStaticTopFrame').slideDown();
      }
      else
      {
        $('#fullStaticTopFrame').slideUp();
      }
    });
  });
  
  $('.kosikPopupContainer').hover(
     function () 
     {
       if (!timeoutHover)
       {
         timeoutHover = window.setTimeout(function(){
           timeoutHover = null;
           $.post('/jquery/scripts/load-top-kosik.php', {  },  function( data ) {
        	   $('.kosikPopupContainer').append(data);
        	   $('.kosikPopupFrame').slideDown('fast');
           });
         }, 300);
       }
     },
     function () 
     {
       if (timeoutHover)
       {
         window.clearTimeout(timeoutHover);
         timeoutHover = null;
       }       
       $('.kosikPopupFrame').hide();
       $('.kosikPopupFrame').remove();
     }
  );
  
  $('.userPopupContainer').hover(
     function () 
     {
       if (!timeoutHover)
       {
         timeoutHover = window.setTimeout(function(){
           timeoutHover = null;
           $('.userPopupFrame').slideDown('fast');
         }, 300);
       }
     },
     function () 
     {
       if (timeoutHover)
       {
         window.clearTimeout(timeoutHover);
         timeoutHover = null;
       }
       $('.userPopupFrame').hide();
     }
  );
  
  $('.main-submenu-li').hover(
     function () 
     {
       var elem = $(this);
       if (!timeoutHover)
       {         
         timeoutHover = window.setTimeout(function(){
           timeoutHover = null;           
           $.post('/jquery/scripts/load-main-submenu.php', { id_cat:elem.data('id') },  function( data ) {
        	   $('.submenu-container').html(data);
        	   elem.find('.submenu').slideDown('fast');
           });           
         }, 300);
       }
     },
     function () 
     {
       var elem = $(this);
       if (timeoutHover)
       {
         window.clearTimeout(timeoutHover);
         timeoutHover = null;
       }
       elem.find('.submenu').hide();
     }
  );
  
  $('.hamburger').unbind().click(function() {
    $(this).parent('DIV').find('.hamburgerContent').slideToggle('fast');
  });
  
  $('.hamburger.katalog').click(function() {
    
    $('.eshopLeftMenuFrame .catsFrame').slideToggle('fast');
  });
  
  $('#do_address').click(function() {
    if (this.checked) {
      $('.addressDo').slideDown();
    }
    else
    {
      $('.addressDo').slideUp();
    }
  });
  
  $('#reg').click(function() {
    if (this.checked) {
      $('.password-cont').slideDown();
    }
    else
    {
      $('.password-cont').slideUp();
    }
  });
  
  /* VALIDACE DIC */
  $('input[name=ic]').change( function() {
    var elem = $(this).parent('.formLine').children('.loading');
    elem.show();
    var ico = $(this).val();
    $.ajax({
      url: "/jquery/scripts/ares.php",
      contentType: "application/json; charset=utf-8",
      dataType: "json", 
      data: "ico="+ico,
      cache: false,
      success: function(data) {
        console.log(data);
        if (data.stav == 'ok') {
          $('input[name=dic]').val(data.dic);
          $('input[name=firm]').val(data.firma);
          $('input[name=ulice]').val(data.ulice);
          $('input[name=mesto]').val(data.mesto);
          $('input[name=psc]').val(data.psc);
          
          setCustAddress('dic', data.dic);
          setCustAddress('firm', data.firma);
          setCustAddress('ulice', data.ulice);
          setCustAddress('mesto', data.mesto);
          setCustAddress('psc', data.psc);
          
          if ($('#map_canvas').length > 0) 
          {
            getDistance();
          }
          
        } else {
          //alert(data.stav);
          console.log(data);
        }
        elem.hide();
    },
   });
  });
  
  $('.formInput').focus(function() {
    $(this).parent('.formLine').find('.tipContainer').slideDown();
  });
  $('.formInput').focusout(function() {
    $(this).parent('.formLine').find('.tipContainer').slideUp('fast');
  });
  
  $('.cntUp').click(function(){
    var cnt = $('#inputCnt').val();
    var new_cnt = parseInt(cnt) + 1;
    $('#inputCnt').val(new_cnt);
    if (new_cnt > 1)
    {
      $('.cntDown').removeClass('notAllowed');
    }
  });
  
  $('.cntDown').click(function(){
    var cnt = $('#inputCnt').val();
    var new_cnt = parseInt(cnt) - 1;
    if (new_cnt > 0)
    {
      $('#inputCnt').val(new_cnt);
    }
    if (new_cnt < 2)
    {
      $('.cntDown').addClass('notAllowed');
    }
  });
  
});


// funkce zjistí parametr z URL
function getUrlFilter() 
{
  var regex = /f=[0-9,whdo-]{1,}/g,
      matches,
      url = location.href;
      
  //console.log(url);
  //var regex = /f\=.*/;
  matches = url.match(regex);
  
  if (matches)
  {
    return matches[0]; 
  }  
  else
  {
    return '';
  }
  
  //return results == null ? null : results[1];
}

//Odstraneni prvku z pole pomoci regularniho vyrazu

function removeArrItem(arr, str)
{
  var i = arr.length;

  for (part in arr)
  {
    //console.log('SUBSTR['+part+']');
    if (arr[part].substring(0,2) == str)
    {
      arr.splice(part, 1);
    }
  }
  return arr;
}

function removeArrEmptyItem(arr)
{
  var k;
  for (k in arr)
  {
    if (!arr[k])
    {
      arr.splice(k, 1);
    }
  }
  return arr;
}