//
//  (ñ) SiteLeader, 2005-2009 || http://www.siteleader.ru
//

var img0 = new Array(), img1 = new Array(), btns = new Array();
var PATH = '/db.img/catalog/preview/', i_PATH = '/i/';
var preloaded = 0;

function i_preload(arr, path) {
  if (document.images && arr != null) {
    for (var i = 2; i < arguments.length; i++) {
      arr[i - 2] = new Image();
      arr[i - 2].src = path + arguments[i];
    }
    preloaded = 1;
  }
}

function set_img(obj, arr, index, value) {
  if (!preloaded || !obj || !arr || index < 0 || (index * 2 + 1) >= arr.length) return 0;
  obj.src = value ? arr[index * 2 + 1].src : arr[index * 2].src;
  show_title(index, value);
  return 1;
}

function show_title(index, value) {
  if (value) {
    $('#cat-title-' + index).slideDown(200);
  } else {
    $('#cat-title-' + index).slideUp(100);
  }
}

var cur_mLeft, cur_mTop, cur_wh, cur_count, anim_count = 10, mLeftStart = 4, delta = 2, wait = 8;
var anim_timer, cur_obj;

function anim_assign(obj) {
  if (!obj) return;
  obj.style.marginLeft = cur_mLeft + 'px';
  obj.style.marginTop = cur_mTop + 'px';
  obj.style.width = cur_wh + 'px';
  obj.style.height = cur_wh + 'px';
}

var min_wh = 116;

function anim_reset(obj, value) {
  if (!obj) return;
  if (value) {
    cur_mLeft = mLeftStart;
    cur_mTop = 0;
    cur_wh = min_wh;
  } else {
    cur_mLeft = -(delta * anim_count - mLeftStart);
    cur_mTop = -(delta * anim_count);
    cur_wh = min_wh + 2 * delta * anim_count;
  }
  if (anim_timer) {
    clearInterval(anim_timer);
    anim_timer = null;
  }
  cur_count = 0;
  anim_assign(obj);
}

function set_img_anim(obj, arr, index, value) {
  if (!preloaded || !obj || !arr || index < 0 || (index * 2 + 1) >= arr.length) return 0;
  if (cur_obj && obj.id != cur_obj.id) anim_reset(cur_obj, 1);
  obj.src = value ? arr[index * 2 + 1].src : arr[index * 2].src;
  anim_reset(obj, value);
  anim_timer = setInterval(function() { do_anim_1(obj, value) }, wait);
  cur_obj = obj;
  return 1;
}

function do_anim_1(obj, value) {
  if (!obj) return;
  if (cur_count >= anim_count) {
    clearInterval(anim_timer);
    anim_timer = null;
    cur_count = 0;
    cur_obj = null;
    return;
  }
  if (value) {
    cur_mLeft -= delta;
    cur_mTop -= delta;
    cur_wh += 2 * delta;
  } else {
    cur_mLeft += delta;
    cur_mTop += delta;
    cur_wh -= 2 * delta;
  }
  anim_assign(obj);
  cur_count++;
}

function _transp_adjust_(obj) {
  if (!(obj = $$(obj))) return;
  obj.style.width = document.body.offsetWidth + 'px';
  obj.style.height = document.body.offsetHeight + 'px';
}

