$(document).ready(function()
{
 $('.prod-info-gallery a.prod-info-gallery-prevju').click(function(event)
 {
  event.preventDefault();
  
  var $element = $(this);
  $element.parent().children('a.current').removeClass('current');
  $element.addClass('current');
  $element.parent().find('div.prod-info-gallery-img img').attr('src', $element.attr('href'));
 });
 
 $('#popup a.prod-info-gallery-prevju').click(function(event)
 {
  event.preventDefault();
  
  var $element = $(this);
  $element.parent().children('a.current').removeClass('current');
  $element.addClass('current');
  $element.parent().parent().find('div.popup-bg-block-img img').attr('src', $element.attr('href'));
 });
 
 $('#popup .cs-next').click(function()
 {
  $current = $('#popup .prod-info-gallery-prevju.current');
  $element = $current.next('.prod-info-gallery-prevju');
  
  if (!$element.length) 
   $element = $current.prevAll('.prod-info-gallery-prevju').last();
  
  if ($element.length) 
  {
   $current.removeClass('current');
   $element.addClass('current');
   $('#popup div.popup-bg-block-img img').attr('src', $element.attr('href'));
   $(".popup-bg-block-save").attr('href', '/catalog/save/id/' + $element.attr('href').replace(/[^\d]/g, ''));
  }
 });
 
 $('#popup .cs-prev').click(function()
 {
  $current = $('#popup .prod-info-gallery-prevju.current');
  $element = $current.prev('.prod-info-gallery-prevju');
  
  if (!$element.length) 
   $element = $current.nextAll('.prod-info-gallery-prevju').last();
  
  if ($element.length) 
  {
   $current.removeClass('current');
   $element.addClass('current');
   $('#popup div.popup-bg-block-img img').attr('src', $element.attr('href'));
   $(".popup-bg-block-save").attr('href', '/catalog/save/id/' + $element.attr('href').replace(/[^\d]/g, ''));
  }
 });
 
 $('.product-popup-trigger').click(function()
 {
  $current = $('.prod-info .prod-info-gallery-prevju.current');
  $current_index = $current.prevAll('.prod-info-gallery-prevju').length;
  
  $popup_current = $('#popup .prod-info-gallery-prevju').eq($current_index);
  $popup_current.click();
  
  $('#popup').show();
 });
 
 $('#form-login').submit(function()
 {
  login();
  return false;
 });
});

function login()
{
 $('#form-login input[type=text], #form-login input[type=password]').removeClass('input-invalid');
	$('#form-login .input-error-container').remove();
	
 $.ajax(
 {
  url: '/user/login',
  dataType: 'json',
  type: 'POST',
  
  data: 
  {
   email: $('#form-login input[name=email]').val(),
   password: $('#form-login input[name=password]').val(),
   remember: $('#form-login input[name=remember]').val()
  },
  
  success: function(response)
  {
   if (response.success) 
    window.location.href = '/catalog/category/id/all';
   else 
   {
    $('#form-login input[name=' + response.field + ']').addClass('input-invalid');
    
    var html = '';
    html += '<div class="input-error-container">';
    html += ' <div class="input-error">';
    html += '  <div class="input-error-c">' + (response.message || 'Неизвестная ошибка') + '</div>';
    html += ' </div>';
    html += '</div>';
				
				$('#form-login input[name=' + response.field + ']').parent().append(html);
   }
  }
 });
	
 return false;
}

function process_errors(response)
{
 var message = response.message || 'Невозможно выполнить действие';
 
 $.pnotify(
 {
  pnotify_title: 'Ошибка',
  pnotify_text: message
 });
}

function cart_add(id)
{
 $.ajax(
 {
  url: '/user/cart-add/',
  type: 'POST',
  dataType: 'JSON',
  
  data: 
  {
   id: id
  },
  
  success: function(response)
  {
   if (response.success) 
   {
    $.pnotify(
    {
     pnotify_title: 'Корзина',
     pnotify_text: 'Товар добавлен в корзину'
    });
   }
   else 
    process_errors(response);
  }
 });
}

function comparison_add(id)
{
 $.ajax(
 {
  url: '/user/comparison-add/',
  type: 'POST',
  dataType: 'JSON',
  
  data: 
  {
   id: id
  },
  
  success: function(response)
  {
   if (response.success) 
   {
    var html = '';
    html += '<p>Товар добавлен к сравнению</p>';
    html += '<span>Выбрано товаров в сравнения : <b>' + response.count + '</b></span>';
    html += '<a href="/user/comparison">Сравнить</a>';
    
    $('#product-fav-popup .prod-info-descr-popup-c-center-container').html(html);
    $('#product-fav-popup').fadeIn();
   }
   else 
    process_errors(response);
  }
 });
}

function comparison_save()
{
 $.ajax(
 {
  url: '/user/comparison-save/',
  type: 'POST',
  dataType: 'JSON',
  
  success: function(response)
  {
   if (response.success) 
   {
    $.pnotify(
    {
     pnotify_title: 'Сравнение',
     pnotify_text: "Сравнение сохранено"
    });
    window.location.href = window.location.href;
   }
   else 
    process_errors(response);
  }
 });
}

function comparison_clear()
{
 $.ajax(
 {
  url: '/user/comparison-clear/',
  type: 'POST',
  dataType: 'JSON',
  
  success: function(response)
  {
   if (response.success) 
   {
    window.location.href = '/user/comparison/';
   }
   else 
    process_errors(response);
  }
 });
}

function comparison_remove(id)
{
 $.ajax(
 {
  url: '/user/comparison-remove/',
  type: 'POST',
  dataType: 'JSON',
  
  data: 
  {
   id: id
  },
  
  success: function(response)
  {
   if (response.success) 
   {
    $.pnotify(
    {
     pnotify_title: 'Сравнение',
     pnotify_text: "Сравнение удалено"
    });
    window.location.href = '/user/comparisons/';
   }
   else 
    process_errors(response);
  }
 });
}

function comparison_remove_item(id)
{
 $.ajax(
 {
  url: '/user/comparison-remove-item/',
  type: 'POST',
  dataType: 'JSON',
  
  data: 
  {
   id: id
  },
  
  success: function(response)
  {
   if (response.success) 
   {
    $.pnotify(
    {
     pnotify_title: 'Сравнение',
     pnotify_text: "Товар исключен"
    });
    window.location.href = '/user/comparison/';
   }
   else 
    process_errors(response);
  }
 });
}

function favourites_add(id)
{
 $.ajax(
 {
  url: '/user/favourites-add/',
  type: 'POST',
  dataType: 'JSON',
  
  data: 
  {
   id: id
  },
  
  success: function(response)
  {
   if (response.success) 
   {
    var html = '';
    html += '<p>Товар добавлен в избранное</p>';
    html += '<a href="/user/favourites">Перейти в избранное</a>';
    
    $('#product-fav-popup .prod-info-descr-popup-c-center-container').html(html);
    $('#product-fav-popup').fadeIn();
   }
   else 
    process_errors(response);
  }
 });
}

function favourites_remove(id)
{
 $.ajax(
 {
  url: '/user/favourites-remove/',
  type: 'POST',
  dataType: 'JSON',
  
  data: 
  {
   id: id
  },
  
  success: function(response)
  {
   if (response.success) 
   {
    $.pnotify(
    {
     pnotify_title: 'Избранное',
     pnotify_text: "Товар удален из избранного"
    });
    window.location.href = window.location.href;
   }
   else 
    process_errors(response);
  }
 });
}

// social auth

function social_auth_popup(by)
{
 var popup = window.open('/social-auth/index/by/' + by, 'Авторизация', 'width=800,height=400,status=no,toolbar=no,menubar=no,location=no, resizable=no');
 try 
 {
  popup.moveTo((screen.width - 800) / 2, 200); 
 }
 catch(e)
 {
  
 }
 popup.focus();
}

