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

function ajaxFileUpload()
{
  //starting setting some animation when the ajax starts and completes
  $("#loading")
  .ajaxStart(function(){
    $.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;">Nahrávam súbor. Prosím čakajte...</h3>'});
  })
  .ajaxComplete(function(){
    $.unblockUI();
  });
  
  $.ajaxFileUpload
  (
    {
      url:SITE_URL+'/ajax/doajaxfileupload.php', 
      secureuri:false,
      fileElementId:'fileToUpload',
      dataType: 'json',
      success: function (data, status)
      {
        if(typeof(data.error) != 'undefined')
        {
          if(data.error != '')
          {
            //alert(data.error);
            $('#motive_file').val(data.error);
          }
          else
          {
            $('#motive_file').val(data.msg);
          }
        }
      },
      error: function (data, status, e)
      {
        //alert(e);
        $('#motive_file').val(e);
      }
    }
  )
  
  return false;
}

function addToCart(product_id, wait_msg, ok_msg, error_msg, oos_msg){
  var msg = '';
  var error = false, error_c = true, error_m = true, error_f = true, error_p = true;
  var color = $('input[name="color"]:checked', '#pd_form').val();
  var motive = $('input[name="motive"]:checked', '#pd_form').val();
  var position = $('#position option:selected').val();

  if ($(".pd_options").length > 0){
    if (color > 0) {
      error_c = false;
    } else {
      error_c = true;
      msg = 'Prosím vyberte farbu.';
    }
  } else {
    error_c = false;
  }
  
  if ($("#position").length > 0){
    if (position > 0) {
      error_p = false;
    } else {
      error_p = true;
      msg = 'Vyberte si pozíciu motívu.';
    }
  } else {
    error_p = false;
  }

  if ($("#motive_list").length > 0){
    if (motive > 0 || $('#motive_file').val() != '') {
      error_m = false;
    } else {
      error_m = true;
      msg = 'Prosím vyberte motív.';
    }
  } else {
    error_m = false;
  }

  if (new Number($('#count').val()) < 0) {
    error = true;
    msg = 'Prosím zadajte kladné číslo.';
  } else {
    if (isNaN($('#count').val())) {
    error = true;
    msg = 'Prosím zadajte číslo.';
    }
  }
  
  if ($('#count').val() == 0)
  {
    $('#count').val(1);
  }
  
  if ($('#motive_file').val() != '') {
    if (error_m)
    {
      msg = '';
    }
    error_m = false;
    if (error == false && error_m == false && error_c == false && error_p == false) {      
      if ($('#motive_file').val() == 'error') {
        error_f = true;
        msg = 'Nepodarilo sa nahrať subor.<br>Povolené formáty: JPG, PNG, GIF, PDF.<br>Maximálna veľkosť súboru: 5 MB.';
      } else {
        error_f = false;
      }
    }
  } else {
    error_f = false;
  }
  
  if (error == false && error_m == false && error_c == false && error_f == false && error_p == false) {
    $.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+"/add2cart.php",{ product_id: product_id, posted: $('#pd_form').serializeArray(), motive_file: $('#motive_file').val() },
      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 }); 
          
          $('#h_cart').attr('class','h_cart highlight');

          $.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 });
        }
    });
    
    $('.pd_color div').each(function() {
      $(this).attr('class','color');
    });
      
    $('#motive_list img').each(function() {
      $(this).attr('class','motives');
    });
    
    $('input:checked', '#pd_form').each(function() {
      $(this).removeAttr('checked');
    });
    
  } else {
    $.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 }); 
  }
  
  return false;
}

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 }); 
    }
  }
}

function set_color(id){
  $('#color'+id).attr('checked', 'checked');
  $.post(SITE_URL+"/ajax/color_image.php", { id: id }, function(data){
    $('#main_image').attr('src',SITE_URL+'/content/imageup/color_medium/'+data);
    $('#main_image_a').attr('href',SITE_URL+'/content/imageup/color_medium2/'+data);
  });
  
  $('.pd_color div').each(function() {
    $(this).attr('class','color');
  });

  $('#c'+id).attr('class','color selected');
}

function set_motive(id){
  $('#motive'+id).attr('checked', 'checked');
  $('#motive_list img').each(function() {
    $(this).attr('class','motives');
  });

  $('#m'+id).attr('class','motives selected');
}

function load_motives(wait_msg, ok_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 motive_category = $('#motive_category option:selected').val();
  
	$.post(SITE_URL+"/ajax/motive_list.php",{ motive_category: motive_category },
		function(data){
      $('#motive_list').empty();
      $('#motive_list').html(data);
      $.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 });
    });
  
  return false;
}

$(document).ready(function() {
  $("#contact").validate({
    rules: {
      name: { required: true },
      tel: { required: true },
      email: { email: true, required: true },
      message: { required: true }
    }
  });
  
  $("#pc_form").validate({
    rules: {
      name: { required: true },
      street: { required: true },
      city: { required: true },
      zip: { required: true },
      tel: { required: true },
      email: { email: true, required: true },
      price: { required: true }
    }
  });
  
  if ($("#ref_items").length > 0){
    $('#ref_items a').lightBox({
      fixedNavigation:true,
      imageLoading: SITE_URL + '/images/loading.gif',
      imageBtnClose: SITE_URL + '/images/close_'+LANG+'.gif',
      imageBtnPrev: SITE_URL + '/images/prev_'+LANG+'.gif',
      imageBtnNext: SITE_URL + '/images/next_'+LANG+'.gif',
      txtImage: '',
      txtOf: '/'
    });
  }
  
  if ($("#n_image").length > 0){
    $('#n_image a').lightBox({
      fixedNavigation:true,
      imageLoading: SITE_URL + '/images/loading.gif',
      imageBtnClose: SITE_URL + '/images/close_'+LANG+'.gif',
      imageBtnPrev: SITE_URL + '/images/prev_'+LANG+'.gif',
      imageBtnNext: SITE_URL + '/images/next_'+LANG+'.gif',
      txtImage: '',
      txtOf: '/'
    });
  }
  
  $('#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 ($("a.over[rel]").length > 0){
    var pos = $("#p_list_table").offset();
    $("a.over[rel]").overlay({
      fixed: false,
      top: pos.top
    });
  }
  
  if ($("#top_fade").length > 0){
    $('#top_fade').cycle('fade');
  }
});
//]]>

