//--------------------------------------------------
// toggle show-hide element
//--------------------------------------------------

function toggle(id)
{
  var item = document.getElementById(id);
  if(!item) return;
  item.style.display = (item.style.display != 'none' ? 'none' : 'block')
  return 0;
}

//--------------------------------------------------
// pick count of items and send to cart
//--------------------------------------------------

function send_cart(id)
{
  var count = document.getElementById('count_' + id).value;
  location.href = 'index.php?page=cart&act=add&id=' + id + '&count=' + count;
}

//--------------------------------------------------
// use search hint
//--------------------------------------------------

function fill_search()
{
  $('#search-cap').val($('#search-hint').text());
}

//--------------------------------------------------
// toggle mode in productlist
//--------------------------------------------------

function change_mode(loc)
{
  location.href = loc;
}

//--------------------------------------------------
// stars
//--------------------------------------------------

function stars_restore()
{
  var rating = $('#rating').val();
  for(var idx = 1; idx <= 5; idx ++)
  {
    $('#star' + idx).attr('src', 'templates/images/' + (idx <= rating ? '' : 'in') + 'activestar.png');
  }
}

function stars_hover(rating)
{
  for(var idx = 1; idx <= 5; idx ++)
  {
    $('#star' + idx).attr('src', 'templates/images/' + (idx <= rating ? '' : 'in') + 'activestar.png');
  }
}

function stars_set(rating)
{
  $('#rating').val(rating);
}

//--------------------------------------------------
// menu hassle
//--------------------------------------------------

var selected_popup;
var hidepopup = false;

function menu_over(id)
{
  $('.popup_div').css('display', 'none');
  $('#popup_div_' + id ).css('display', 'block');
  selected_popup = id;
}

function menu_out(id)
{
  selected_popup = false;
  setTimeout('menu_realout(' + id + ')', 200);
}

function menu_realout(id)
{
  if(selected_popup != id)
    $('#popup_div_' + id).css('display', 'none');
}

function popup_over(id)
{
  selected_popup = id;
}

//--------------------------------------------------
// scroll similar gifts
//--------------------------------------------------

var curr_scroll = 0;
var max_scroll = -1;

function scroll_right()
{
  if(curr_scroll == max_scroll) return;
 
  curr_scroll+=4;
  if(curr_scroll > max_scroll) curr_scroll = max_scroll;
  var div = $('#scroll_div');
  
  var new_left = div.position().left - 656;
  // 4 pics per page, 160px each. limit is negative, 4 magic pixels included
  var limit = (4 - max_scroll) * 164;
  if(new_left < limit) new_left = limit;
  
  div.animate(
    {left: new_left},
    500
  );
}

function scroll_left()
{
  if(curr_scroll == 0) return;
  
  curr_scroll-=4;
  if(curr_scroll < 0) curr_scroll = 0;
  var div = $('#scroll_div');
  
  var new_left = div.position().left + 656;
  if(new_left > 0) new_left = 0;
  
  div.animate(
    {left: new_left},
    500
  );
}

//--------------------------------------------------
// toggle subs by cat picking
//--------------------------------------------------

var filter_timer = false;

function toggle_subs(id)
{
  var cat = $('#cat_' + id).attr('checked');
  if(!cat)
  {
    $('.sub_' + id).css('display', 'none');
    $('.sub_' + id + ' input').attr('checked', false);
  }
  else
  {
    $('.sub_' + id).css('display', 'block');
    $('.sub_' + id + ' input').attr('checked', true);
  }
  
  filter_apply();
}

function filter_apply()
{
  clearTimeout(filter_timer);
  
  filter_timer = setTimeout("document.filter.submit();", 1500);
}

//--------------------------------------------------
// discount distribution
//--------------------------------------------------

// `discount_const` and `points` declared at receipt_1.tpl

// maximal discount per one product
var max_one = 12;

function discount_inc(id, num)
{
  if(points > 0)
  {
    var curr = $('#points_' + id + '_' + num);
    if(parseInt(curr.attr('value')) + discount_const < max_one)
    {
      // redistribute points
      points--;
      curr.attr('value', parseInt(curr.attr('value'))+1);
      $('#points').html(points);
      
      discount_refresh(id, num);
    }
    else
      alert('На один товар нельзя установить больше ' + max_one + '% скидок.');
  }
  else
    alert('Вы уже распределили все проценты скидок.');
}

function discount_dec(id, num)
{
  var curr = $('#points_' + id + '_' + num);
  if(curr.attr('value') > 0)
  {
    points++;
    curr.attr('value', parseInt(curr.attr('value'))-1);
    $('#points').html(points);
    
    discount_refresh(id, num);
  }
}

function discount_refresh(id, num)
{
  var total = $('#cart_total');
  var curr = $('#points_' + id + '_' + num);
  var item_total;
  var product_total = 0;
  
  // recalc per-item total
  item_total = Math.ceil(parseFloat($('#price_' + id).attr('value') * (100 - curr.attr('value') - discount_const) / 100 ));
  $('#result_' + id + '_' + num).html(item_total)

  // prepare per-cart total
  var pretotal = parseInt($('#cart_total').text()) - parseInt($('#total_' + id).text());
  
  // calc per-product total
  var max = parseInt($('#count_' + id).attr('value'));
  var total = 0;
  for(idx = 1; idx <= max; idx++)
    total += parseInt($('#result_' + id + '_' + idx).text());
    
  $('#total_' + id).text(total);
  $('#cart_total').text(pretotal + total);

}

//--------------------------------------------------
// frontpage animation
//--------------------------------------------------

var anim_page = 1;
var anim_timer = false;

function select_anim(id)
{
  if(anim_page != id)
  {
    if(anim_timer) clearTimeout(anim_timer);
    
    // hide current page & show next
    $('#animation' + anim_page).fadeOut(150,
      function()
      {
        $('#animation' + id).fadeIn(150);
      }
    );
    
    // change class
    
    $('#anim' + anim_page).attr('class', 'anim-button');
    $('#anim' + id).attr('class', 'anim-button-active');
    
    anim_page = id;
    
    anim_timer = setTimeout('select_anim(' + (id == 5 ? 1 : id+1) + ')', 10000);
  }
}

//--------------------------------------------------
// ready to go!
//--------------------------------------------------

$(document).ready(
  function()
  {
    
    anim_timer = setTimeout('select_anim(2)', 8000);
  }
);
