function $(id) {
  return document.getElementById(id);
}

function _(fname, el) {
  if (el != undefined)
    return document.forms[fname].elements[el];
  else
    return document.forms[fname];
}

function Hide(id) {
  if ($(id)) $(id).style.display='none';
  return false;
}

function Show(id) {
  if ($(id)) $(id).style.display='';
  return false;
}

String.prototype.trim = new Function("return this.replace(/^\\s+|\\s+$/g,'')");
String.prototype.URI = new Function("return encodeURIComponent(this)");

//Add/Remove event crossbrowser
function addEvent(obj,evt,fn) {
	if (obj.addEventListener) obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent) obj.attachEvent('on'+evt,fn);
}

function removeEvent(obj,evt,fn) {
	if (obj.removeEventListener) obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)	obj.detachEvent('on'+evt,fn);
}

//AJAX Load document
function loadDoc(url, uri) {
  //AJAX
  req = false;
  try {
    req=new ActiveXObject('Msxml2.XMLHTTP');
  } catch (e) {
    try {
      req=new ActiveXObject('Microsoft.XMLHTTP'); //Internet Explorer
    } catch (e) {
      if(window.XMLHttpRequest) //Mozilla & Safari
        req=new XMLHttpRequest();
    }
  }

  if (req) {
    req.onreadystatechange = processReq;
    req.open("POST", url, true);
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.send(uri);
    return true;
  }
  return false;
}

function processReq() {
  if (req.readyState == 4 && req.status == 200 && req.responseText!='')
    eval(req.responseText);
}

function go(url, repl) {
  if (repl == true)
    window.location.replace(url);
  else
    window.location.href = url;
}

var prevnum = 0;
function ovov(num) {
  if (prevnum>0 && $('fav'+prevnum) && $('fav'+prevnum).childNodes[0].childNodes[0].src.indexOf('_') < 0) Hide('fav'+prevnum);
  prevnum = num;
  Show('fav'+num);
}

function shover(t) {
  if (document.activeElement == $('SSTR')) t = true;
  $('SSTR').style.borderColor = (t==true) ? '#aaaaaa' : '#e1e1e1';
  $('SBTN').src = (t==true) ? '/img/sbutton_.gif' : '/img/sbutton.gif';
}

function sfocus() {
  $('SSTR').style.color = '#000000';
  if ($('SSTR').value == 'Поиск по магазину') $('SSTR').value = '';
  shover(1);
}

function aFav(num) {
  loadDoc('/favorites.php', 'add='+num);
  if ('fav'+num)
    Hide('fav'+num);
  return false;
}

function blinkFav(step) {
  $('favBlock').childNodes[0].childNodes[0].style.color = (step%2 == 0) ? '#CC0000' : '#000000';

  step++;
  if (step >= 8) return false;
  setTimeout('blinkFav('+step+')', 300)
}

function setFav(c, lastid, active) {
  $('favBlock').innerHTML = (c>0) ? '<h3><a href="/favorites.htm">Избранное</a></h3>Отмечено товаров: <b>'+c+'</b><br>Посмотреть список.' : '<h3>Избранное</h3>Отложите интересные товары на будущее.';

  if ($('fav'+lastid)) {
    $('fav'+lastid).childNodes[0].childNodes[0].src = (active==1) ? '/img/mstar_.png' : '/img/mstar.png';
    if (active==1) Show('fav'+lastid); else Hide('fav'+lastid);
  }

  if ($('favB'+lastid))
    $('favB'+lastid).style.backgroundPosition = (active==1) ? '0px 27px' : '0px 0px';

  blinkFav(0);
}

function previm(obj, x, y) {
  var s = obj.src.replace('/x_', '/m_');
  s = s.replace('.png', '.jpg');

  $('CatMlink').href = s.replace('/m_', '/');

  if ($('CatMimg').src != s) {
    $('CatMimg').src = s;
    $('CatMimg').width = x;
    $('CatMimg').height = y;
  }
}

var prdelta = null;
function ch_options(obj) {
  if (prdelta != null && prdelta[obj.value]) {
    $('MPRSP').innerHTML = prdelta[obj.value];
  }
}



var leftFlag = false;
var rightFlag = false;
var moveNavTimer = null;

function MoveNav(p) {
  var l = parseInt($('nav').style.left);

  if (l == p || (l<p && l>p-10) || (l>p && l<p+10)) {
    $('nav').style.left = p+'px';
    moveNavTimer = null;

    if (leftFlag) goLeft();
    else if (rightFlag) goRight();

  } else {
    $('nav').style.left = (p < l) ? (l-10)+'px' : (l+10)+'px';
    moveNavTimer = setTimeout('MoveNav('+p+')', 20)
  }
}

function goLeft() {
  var l = parseInt($('nav').style.left);
  if (l < 0) {
    leftFlag = true;
    rightFlag = false;
    if (moveNavTimer == null)
      moveNavTimer = setTimeout('MoveNav('+(l+136)+')', 20)
  }
}

function goRight() {
  var l = parseInt($('nav').style.left);
  if (l-544 > parseInt($('nav').style.width)*-1) {
    leftFlag = false;
    rightFlag = true;
    if (moveNavTimer == null)
      moveNavTimer = setTimeout('MoveNav('+(l-136)+')', 20)
  }
}


