//<![CDATA[
 var result = '';
 
function is_int(value){
  if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
      return true;
  } else {
      return false;
  }
}

function effectFadeIn(classname) {
$("#"+classname).fadeTo('slow', 0.4).fadeTo('slow', 1.0, effectFadeOut(classname))
}

function effectFadeOut(classname) {
$("#"+classname).fadeTo('slow', 1.0).fadeTo('slow', 0.4,  effectFadeIn(classname))
}

function addToCart(product_id, wait_msg, ok_msg, error_msg, oos_msg){
  $.blockUI({css: {border: 'none',padding: '15px',backgroundColor: '#000','-webkit-border-radius': '5px','-moz-border-radius': '5px',opacity: '.5',color: '#fff'}, message:'<h3 style="font-size: 14px;">'+wait_msg+'</h3>'});
  
  var count = $('#count').val();
  
  $.post(SITE_URL+"/add2cart.php",{ product_id: product_id, count: count },
    function(data){
      if(data=='ok') {
        $.blockUI({ css: {border: 'none',padding: '15px',backgroundColor: '#000','-webkit-border-radius': '5px','-moz-border-radius': '5px',opacity: '.5',color: '#fff'}, message:'<h3 style="font-size: 14px;">'+ok_msg+'</h3>', timeout: 1000 }); 
        
        $('#hc_text').hide();
        $('#hc_item').show();
        
        $.get(SITE_URL+"/shortcartinfo.php", 
          function(data) {
            var price = $(data).find('price').text();
            var items = $(data).find('items').text();
            
            $('#c_items span').text(items);
            $('#c_price span').html(price+' &euro;');
          },
          'xml');
        
      } else if (data=='OOS') {
        $.blockUI({ css: {border: 'none',padding: '15px',backgroundColor: '#000','-webkit-border-radius': '5px','-moz-border-radius': '5px',opacity: '.5',color: '#fff'}, message:'<h3 style="font-size: 14px;">'+oos_msg+'</h3>', timeout: 2000 });
      } else {
        $.blockUI({ css: {border: 'none',padding: '15px',backgroundColor: '#000','-webkit-border-radius': '5px','-moz-border-radius': '5px',opacity: '.5',color: '#fff'}, message:'<h3 style="font-size: 14px;">'+error_msg+'</h3>', timeout: 2000 });
      }
  });
}

function CartOperations(operation, cart_id, wait_msg, empty_msg){
  var conf = true;
  var msg = '';
	
  if (operation == 'remove'){
    if (confirm("Naozaj to chcete zmazať?")) {
      conf = true;
    } else { 
      conf = false;
    }
  } else {
    if (is_int(parseInt($('#qty'+cart_id).val())))
    {
      if (new Number($('#qty'+cart_id).val()) < 0) {
        conf = false;
        msg = 'Prosím zadajte kladné číslo.';
      } else {
        if (isNaN($('#qty'+cart_id).val())) {
        conf = false;
        msg = 'Prosím zadajte číslo.';
        }
      }
    } else {
      conf = false;
      msg = 'Prosím zadajte číslo.';
    }
  }
  
  if (conf) {
    $.blockUI({css: {border: 'none',padding: '15px',backgroundColor: '#000','-webkit-border-radius': '5px','-moz-border-radius': '5px',opacity: '.5',color: '#fff'}, message:'<h3 style="font-size: 14px;">'+wait_msg+'</h3>'});
    
    $.post(SITE_URL+"/cart_operations.php",{operation: operation, cart_id: cart_id, qty: parseInt($('#qty'+cart_id).val())}, 
    function(){
      $.post(SITE_URL+"/shortcartinfo.php", { operation: operation, cart_id: cart_id, qty: parseInt($('#qty'+cart_id).val())},
        function(data) {
          var price = $(data).find('price').text();
          var items = $(data).find('items').text();
          var qty = $(data).find('qty').text();
          var price_i = $(data).find('price_i').text();
          var price_sub = $(data).find('price_sub').text();
            
          $('#c_items span').text(items);
          $('#c_price span').html(price+' &euro;');
          
          if (qty == 0) {operation = 'remove'}
          
          if (operation == 'remove') {
            $('#pr'+cart_id).remove();
            $('#subtotal').html(price+' &euro;');
          }
          
          if (operation == 'update') {
            $('#qty'+cart_id).val(qty);
            $('#prp'+cart_id).html(price_i+' &euro;');
            $('#prs'+cart_id).html(price_sub+' &euro;');
            $('#subtotal').html(price+' &euro;');
          }
          
          if ($("table tr").length < 3)
          {
            $('.total').remove();
            $("#s_cart").last().append('<tr><td colspan="7"><strong>'+empty_msg+'</strong></td></tr>');

            $('.c_btn1').hide();
          }
        },
        'xml');
      });
    $.unblockUI();
  } else {
    if (operation != 'remove')
    {
      $.blockUI({ css: {border: 'none',padding: '15px',backgroundColor: '#920C23','-webkit-border-radius': '5px','-moz-border-radius': '5px',opacity: '.8',color: '#fff'}, message:'<h3 style="font-size: 12px;">'+msg+'</h3>', timeout: 2000 }); 
    }
  }
}

$(document).ready(function() {
  $("#contact").validate({
    rules: {
      name: { required: true },
      tel: { required: true },
      email: { email: true, required: true },
      message: { required: true },
      code: { required: true }
    }
  });
  
  $("#pd_mail").validate({
    rules: {
      name: { required: true },
      email: { email: true, required: true },
      message: { required: true },
      code: { required: true }
    }
  });
  
  $("#reg_form").validate({
    rules: {
      username: { required: true, minlength: 5 },
      password: { required: true, minlength: 5 },
      password_again: { required: true, equalTo: "#password", minlength: 5 },
      name: { required: true },
      surname: { required: true },
      street: { required: true },
      city: { required: true },
      zip: { required: true },
      state: { required: true },
      tel: { required: true },
      terms: { required: true },
      email: { email: true, required: true }
    }
  });
  
  $("#servis_form").validate({
    rules: {
      meno1: { required: true },
      model1: { required: true },
      motor1: { required: true },
      rok1: { required: true },
      text1: { required: true },
      mail1: { email: true, required: true }
    }
  });
  
  $("#reg_form1").validate({
    rules: {
      name: { required: true },
      surname: { required: true },
      street: { required: true },
      city: { required: true },
      zip: { required: true },
      state: { required: true },
      tel: { required: true },
      terms: { required: true },
      email: { email: true, required: true }
    }
  });
  
  $("#create_form").validate({
    rules: {
      nazov: { required: true },
      kategoria: { required: true },
      cena: { required: true },
      rok_vyroby: { required: true },
      stav: { required: true },
      city: { required: true },
      zip: { required: true },
      state: { required: true },
      tel: { required: true },
      terms: { required: true },
      email: { email: true, required: true }
    }
  });
  
  $("#login_form").validate({
    rules: {
      username: { required: true, minlength: 5 },
      password: { required: true, minlength: 5 }
    }
  });
  
  if ($("#images").length > 0){
    $('#images a').attr('rel', 'product').fancybox();
  }
  
  if ($("#gallery").length > 0){
    $('#gallery a').attr('rel', 'gallery').fancybox();
  }
  
  if ($("#n_image").length > 0){
    $('#n_image a').attr('rel', 'news').fancybox();
  }
  
  if ($(".aw_gallery").length > 0){
    $('.aw_gallery').each(function(){
      var id = $(this).attr('rel');
      $('a', this).attr('rel', id).fancybox();
    });
  }
  
  if ($(".a_image").length > 0){
    $('.a_image a').fancybox();
  }
    
  $('#sb').click(function () {
    if ($('#sb').is(':checked')) {
      $('#shipping_form').slideUp();
    } else {
      $('#shipping_form').slideDown();
    }
  });
  
  $('#isfirm0').click(function () {
    if ($('#isfirm0').is(':checked')) {
      $('#f_form').slideUp();
    }
  });
  
  $('#isfirm1').click(function () {
    if ($('#isfirm1').is(':checked')) {
      $('#f_form').slideDown();
    }
  });
  
  if ($("#top_fade").length > 0){
    $('#top_fade').cycle('fade');
  }
  
  if ($("#msg_hide").length > 0){
    setTimeout(function() {
      $('#msg_hide').fadeOut('fast');
    }, 3000);
  }
  
  if ($("#filter_form").length > 0){
    $('#kategoria').change(function(){
      $("#filter_form").submit();
    });
    
    $('#order').change(function(){
      $("#filter_form").submit();
    });
  }
  
  $('ul.sf-menu').superfish({ 
    delay: 1000,
    animation: {opacity:'show',height:'show'},
    speed: 'fast',
    autoArrows: true,
    dropShadows: false
  });
  
 /* $('#fade_img').bind('fade-cycle', function() {
    $(this).fadeOut('slow', function() {
        $(this).fadeIn('slow', function() {
            $(this).trigger('fade-cycle');
        });
    });
});

$('#fade_img').trigger('fade-cycle');*/


  //effectFadeIn('fade_img');

  /*$('.rc_items a').each(function() {
    $(this).animate({fontSize: "16px"}, 1500).delay(2000).animate({fontSize: "14px"}, 1500);
    $(this).delay(5000);
  });*/
});
//]]>

