$$(document).ready(function()
{
	$$('a#prices_usd').click(function()
	{
		prices_switch_to('usd');
		return false;
	});
	
	$$('a#prices_jpy').click(function()
	{
		prices_switch_to('jpy');
		return false;
	});
	
	$$('a#prices_rur').click(function()
	{
		prices_switch_to('rur');
		return false;
	});
	$$('a[id^=toggle_]').click(function()
	{
	    var data = this.id.split('_', 3);
	    toggle(data[1], data[2]);
		return false;
	});
	
});

function toggle(what, value) {
	$$('span[id^=toggle_'+what+']').hide();
	$$('span[id=toggle_'+what+'_'+value+']').show();
	$$('a[id^=toggle_'+what+']').show();
	$$('a[id=toggle_'+what+'_'+value+']').hide();
	
	$$('#togglevalue_'+what).attr('value', value);
}

function convert(field, to) {
	var source = Number($(field).value);
	var from = $('inpCurrency').value;
	if (!isNaN(source) && source > 0) {
		$(field).value = (source * rates[from][to]).toFixed(to == 'JPY' ? 0 : 2);
	}
}

function prices_switch_to(what)
{
	$$('span[id^=prices_]').hide();//hide all spans
	$$('span[id=prices_' + what + ']').show();//show `what`'s span
	
	$$('a[id^=prices_]').show();//show all links
	$$('a[id=prices_' + what + ']').hide();//hide `what`'s link
	
	$$('span[id^=price_]').hide();//hide all variants of prices
	$$('span[id=price_' + what + ']').show();//show the required variant of prices
	
	$$.post('/ajax/switch_category_view_currency/', { currency: what });
	
	convert('inpAucMinPrice', what.toUpperCase());
	convert('inpAucMaxPrice', what.toUpperCase());
	convert('inpBuynowMinPrice', what.toUpperCase());
	convert('inpBuynowMaxPrice', what.toUpperCase());
	$$('#inpCurrency').attr('value', what.toUpperCase());
}

function  bookmark_switch_state(id) {
  var file;
  var action;
  if ($$('#bookmarkimage_'+id).attr('src').indexOf('ajax') > 0) {
    return false;
  }
  if ($$('#bookmarkimage_'+id).attr('src').indexOf('-gray') > 0) {
    file = 'star-gray.png';
    action = 'add';
  } else {
    file = 'star.png';
    action = 'remove';
  }
  $$('#bookmarkimage_'+id).attr('src', $$('#bookmarkimage_'+id).attr('src').replace(file, 'ajax.gif'));
  $$.get('/ajax/' + action + 'Bookmark/' + id, function(data) {
    var file;
    var title;
    if (data.indexOf('addBookmark') > 0) {
      file = 'star-gray.png';
      title = 'Добавить в Избранное';
    } else if(data.indexOf('removeBookmark') > 0) {
      file = 'star.png';
      title = 'Убрать из Избранного';
    } else {
      alert("Ошибка: " + data);
      file = 'star-gray.png';
      title = 'Добавить в Избранное';
    }
    
    $$('#bookmarkimage_'+id).attr('src', $$('#bookmarkimage_'+id).attr('src').replace('ajax.gif', file));
    $$('#bookmarklink_'+id).attr('title', title);
  });
  return false;
}
