
function show_sub(el)
{
  el2 = document.getElementById(el.id + 'menu');
  if (el2)
  {
    if (/topitem/.test(el.className)) el.className = (el2.style.display == 'none') ? 'topitem hover' : 'topitem'; else el.className = (el2.style.display == 'none') ? 'hover' : '';
    el2.style.display = (el2.style.display == 'none') ? '' : 'none';
  }
}
/*function show_smenu(el)
{
  var el2 = document.getElementById(el.id + 'menu');
  if (el2)
  {
    var pos = findPos(el);
    el2.style.left = pos[0] + 'px';
    el2.style.top = pos[1] + 'px';
    el2.style.display = '';
  }
}
function hide_smenu(el)
{
  el = document.getElementById(el.id + 'menu');
  if (el) el.style.display = 'none';
}*/

var show_j = 0;
function show_smenu(i, el)
{
 // if (show_j && (show_j != i)) hide_smenu(show_j);
  if (!document.getElementById('cp' + i)) return;
//  if (el)
//  {
//    var pos = findPos(el);
//    document.getElementById('cp' + i).style.left = pos[0] + 'px';
//    document.getElementById('cp' + i).style.top = (pos[1] + 42) + 'px';
//  }
  document.getElementById('cp' + i).style.display = '';
//  if (window.tm2) window.clearTimeout(window.tm2);
//  window.tm2 = window.setTimeout('hide_smenu('+i+')', 800);
  show_j = i;
}
function hide_smenu(i)
{
  if (!document.getElementById('cp' + i)) return;
  if (document.getElementById('cp' + i)) document.getElementById('cp' + i).style.display = 'none';
}


function checkForm(frm, arr)
{
  if (!frm || !frm.elements)
  {
    alert('Форма не определена');
    return false;
  }
  el = null;
  err = '';
  for(i in arr)
  {
    if (frm.elements[i])
    switch(frm.elements[i].type)
    {
      case 'text':
      case 'textarea':
      case 'password':
        if (frm.elements[i].value == '')
        {
          if (!el) el = frm.elements[i];
          err += arr[i] + "\n";
        }
        else if ((i == 'email') && (!/^[^@]+@[^@]+$/.test(frm.elements[i].value)))
        {
          if (!el) el = frm.elements[i];
          err += arr[i] + ' | Формат: email@somethere.org' + "\n";
        }
        else if ((/phone/.test(i)) && (!/^(\d(\s|-)*){9}$/.test(frm.elements[i].value)))
        {
          if (!el) el = frm.elements[i];
          err += arr[i] + ' | Формат: 9 цифр' + "\n";
        }
            break;
      case 'select-one':
        if (frm.elements[i].selectedIndex == 0)
        {
          if (!el) el = frm.elements[i];
          err += arr[i] + "\n";
        }
            break;
    }
  }
  if (err == '') return true;
  else
  {
    el.focus();
    alert("Пожалуйста заполните обязательные поля:\n-----------------------------------\n" + err + "-----------------------------------");
    return false;
  }
}
var ppgallery_timeout = 0;

function findPos(obj)
{
        var curleft = curtop = 0;

        if (obj.offsetParent) {
                curleft = obj.offsetLeft
                curtop = obj.offsetTop
                while (obj = obj.offsetParent) {
                        curleft += obj.offsetLeft
                        curtop += obj.offsetTop
                }
        }
        return Array(curleft, curtop);
}
var pgallery_type = 0;
function pgallery_img_over(id, img_w, img_h, type)
{
        if (this.pgallery_id) pgallery_img_out();
  if (type) pgallery_type = type; else pgallery_type = 0;
        var el = document.getElementById("pgallery-img" + id);
        var el_img = document.getElementById("pgallery-img" + id + "-image");
        var el_h = document.getElementById("pgallery-img" + id + "-hidden");
        var el_h_img = document.getElementById("pgallery-img" + id + "-hidden-image");

//        el_h.style.display = "block";

        this.pgallery_id = id;
        this.pgallery_el = el_img;
        this.pgallery_el_layer = el;
        this.pgallery_el_h = el_h_img;
        this.pgallery_el_h_layer = el_h;
        ppgallery_timeout = 0;

        var pos = findPos(el);

        if (img_h > img_w) pgallery_show_routine(el_img.offsetWidth, img_w, pos[0], pos[1], 1, img_h/img_w, 0);
        else pgallery_show_routine(el_img.offsetHeight, img_h, pos[0], pos[1], 1, img_w/img_h, 1);
}
function pgallery_img_click(id, img_w, img_h)
{
        if (this.pgallery_id)
  {
    if (this.pgallery_id == id)
    {
      pgallery_img_out();
      return;
    }
    pgallery_img_out();
  }

        var el = document.getElementById("pgallery-img" + id);
        var el_img = document.getElementById("pgallery-img" + id + "-image");
        var el_h = document.getElementById("pgallery-img" + id + "-hidden");
        var el_h_img = document.getElementById("pgallery-img" + id + "-hidden-image");

//        el_h.style.display = "block";

        this.pgallery_id = id;
        this.pgallery_el = el_img;
        this.pgallery_el_layer = el;
        this.pgallery_el_h = el_h_img;
        this.pgallery_el_h_layer = el_h;
        ppgallery_timeout = 0;

        var pos = findPos(el_img);


        if (img_h > img_w) pgallery_show_routine(el_img.offsetWidth, img_w, pos[0], pos[1], 1, img_h/img_w, 0);
        else pgallery_show_routine(el_img.offsetHeight, img_h, pos[0], pos[1], 1, img_w/img_h, 1);
}

function pgallery_show_routine(cDim, dDim, i_x, i_y, isFirst, k, direction)
{
        if (!this.pgallery_id) pgallery_img_out();
        if (!this.pgallery_el) return;
        if (cDim < dDim) {
                var newHeight = 0, newWidth = 0;
                if (!direction) {
                        newWidth = cDim + 10;

                        if (newWidth > dDim) newWidth = dDim;
                        newHeight = newWidth * k;
                } else {
                        newHeight = cDim + 10;

                        if (newHeight > dDim) newHeight = dDim;
                        newWidth = newHeight * k;
                }

                if (isFirst) {
                        this.pgallery_el_h_layer.style.display = "block";
                }

//                var pLeft = i_x - (this.pgallery_el_h.offsetWidth - this.pgallery_el.offsetWidth) / 2;
//                var pTop = i_y - (this.pgallery_el_h.offsetHeight - this.pgallery_el.offsetHeight) / 2;
                var pLeft = i_x - (newWidth - this.pgallery_el.offsetWidth) / 2;
                //var pTop = i_y - (newHeight - this.pgallery_el.offsetHeight) / 2; //
    if (pgallery_type)
    {
      pTop = i_y + 3; //
      pLeft = i_x - newWidth - 8;
    }
    else if (/^g/.test(this.pgallery_id) )
    {
      var pTop = i_y + 75; //
    }
    else
    {
      var pTop = i_y - 10; //
    }

//                alert(pTop);

                this.pgallery_el_h.width = newWidth;

                this.pgallery_el_h_layer.style.left = pLeft + "px";
                this.pgallery_el_h_layer.style.top = pTop + "px";

                ppgallery_timeout = setTimeout("pgallery_show_routine("+(cDim + 10)+","+dDim+","+i_x+","+i_y+",0,"+k+","+direction+")", 1);

                return;
        }
        ppgallery_timeout = 0;
}

function pgallery_img_out()
{
        if (!this.pgallery_id) return;
        if (ppgallery_timeout) clearTimeout(ppgallery_timeout);;

        var el_h = document.getElementById("pgallery-img" + this.pgallery_id + "-hidden");
        this.pgallery_id = 0;
        ppgallery_timeout = 0;

        el_h.style.display = "none";
}
var galPos = 0;
function gallery_move(par)
{
  galPos += par;
  if (galPos < 0)
  {
    galPos = 0;
    while (document.getElementById('pgallery-imgg' + (galPos + 4) + '-image')) galPos++;
  }
  else if (!document.getElementById('pgallery-imgg' + (galPos + 3) + '-image')) galPos = 0;
  for(var i=1; i<20; i++)
  {
          if (!document.getElementById('pgallery-imgg' + i + '-image')) break;
    if (!(i > galPos && i < galPos + 4)) document.getElementById('pgallery-imgg' + i + '-image').style.display = 'none';
  }
  for(var i=1; i<20; i++)
  {
          if (!document.getElementById('pgallery-imgg' + i + '-image')) break;
    if (i > galPos && i < galPos + 4) document.getElementById('pgallery-imgg' + i + '-image').style.display = '';
  }

}
var show_i = 0;
function show_drop(i, el)
{
//  if (show_i && (show_i != i)) hide_drop(show_i);
//  document.getElementById('menu' + i).className = 'over';
//  if (!document.getElementById('dropmenu' + i)) return;
//  if (el)
//  {
//    var pos = findPos(el);
//    document.getElementById('dropmenu' + i).style.left = pos[0] + 'px';
//  }
//  document.getElementById('dropmenu' + i).style.display = '';
//  if (window.tm) window.clearTimeout(window.tm);
//  window.tm = window.setTimeout('hide_drop('+i+')', 800);
//  show_i = i;
}

function h1ide_drop(i)
{
//  document.getElementById('menu' + i).className = '';
//  if (!document.getElementById('dropmenu' + i)) return;
//
//  if (document.getElementById('dropmenu' + i)) document.getElementById('dropmenu' + i).style.display = 'none';
}
window.loadIntoId = null;
function loadInto(url, id)
{
  window.loadIntoId = id;
  new Ajax.Request( url, { method: 'get', parameters: {},
                                onComplete: function(transport) {
                                        var response = new String(transport.responseText);
                                        
                                        document.getElementById(window.loadIntoId).innerHTML = response;
                                        alert(document.getElementById(window.loadIntoId).innerHTML);
                                }.bind(this)
                        });
}
var gallery_pi = 1;
var gallery_pi2 = 0;
function gallery_load2(pi, pi2, cnt)
{
  if (gallery_pi && gallery_pi == pi) return;
  if (gallery_pi)
  {
    document.getElementById('pgallery-imgg' + gallery_pi + '-a').className = '';
    if (document.getElementById('pgallery-imgg' + (gallery_pi+cnt) + '-a')) document.getElementById('pgallery-imgg' + (gallery_pi+cnt) + '-a').className = '';
  }
  gallery_pi = pi;
  document.getElementById('pgallery-imgg' + gallery_pi + '-a').className = 'act';
  if (document.getElementById('pgallery-imgg' + (gallery_pi+cnt) + '-a')) document.getElementById('pgallery-imgg' + (gallery_pi+cnt) + '-a').className = 'act';
  document.getElementById('gallery_div' + gallery_pi2).style.display = 'none';
  gallery_pi2 = pi2;
  document.getElementById('gallery_div' + gallery_pi2).style.display = '';
}
function gallery_load(pi, cnt, fn, w, h)
{
  if (gallery_pi && gallery_pi == pi) return;
  if (gallery_pi)
  {
    document.getElementById('pgallery-imgg' + gallery_pi + '-a').className = '';
    if (document.getElementById('pgallery-imgg' + (gallery_pi+cnt) + '-a')) document.getElementById('pgallery-imgg' + (gallery_pi+cnt) + '-a').className = '';
  }
  gallery_pi = pi;
  document.getElementById('pgallery-imgg' + gallery_pi + '-a').className = 'act';
  if (document.getElementById('pgallery-imgg' + (gallery_pi+cnt) + '-a')) document.getElementById('pgallery-imgg' + (gallery_pi+cnt) + '-a').className = 'act';
  document.getElementById('gallery_img').style.visibility = 'hidden';
  document.getElementById('gallery_img').src = fn;
  document.getElementById('gallery_img').width = w;
  document.getElementById('gallery_img').height = h;
  document.getElementById('gallery_div').style.width = ((w%2==1)?(w-1):w) + 'px';
  document.getElementById('gallery_div').style.height = ((h%2==1)?(h-1):h) + 'px';
}
var cart_pos = 0;
var cur_cart_pos = 0;
function move_scroll()
{
  var scrOfY = 0;
  if( window.pageXOffset || window.pageYOffset ) {
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    scrOfY = document.documentElement.scrollTop;
  }
  if (scrOfY > 280)
  {
    cart_pos = (scrOfY - 250);
    //document.getElementById('cart').style.marginTop = (scrOfY - 250) + 'px';
  }
  else
  {
    cart_pos = 0;
    //document.getElementById('cart').style.marginTop = '0px';
  }
  return true;
}
function move_scroll_()
{
  move_scroll();
  if (!document.getElementById('cart') || (cart_pos == cur_cart_pos))
  {
    return false;
  }
  var sp = parseInt(Math.abs(cart_pos - cur_cart_pos) / 5);
  if (sp < 5) sp = 5;
  if (cart_pos > cur_cart_pos)
  {
    cur_cart_pos = cur_cart_pos + sp;
    if (cur_cart_pos > cart_pos) cur_cart_pos = cart_pos;
  }
  if (cart_pos < cur_cart_pos)
  {
    cur_cart_pos = cur_cart_pos - sp;
    if (cur_cart_pos < cart_pos) cur_cart_pos = cart_pos;
  }
  document.getElementById('cart').style.marginTop = cur_cart_pos + 'px';
  //window.setTimeout('move_scroll_', 50);
  return true;
}
window.setInterval('move_scroll_()', 20);
