function showPopup(popup)
{
  if (!popup) popup = 'popup';
  var windowHeight = document.height ? document.height : document.body.clientHeight ? document.body.clientHeight : 0;
  var coords = getScrollXY();
  
  document.getElementById('gray_page').style.height = windowHeight + 'px';  
  document.getElementById('gray_page').style.display = 'block';

  document.getElementById(popup).style.top = (coords[1] + getClientHeight() / 2) + 'px';
  document.getElementById(popup).style.display = 'block';
  document.getElementById(popup).style.marginTop = '-' + (document.getElementById(popup).offsetHeight / 2) + 'px';
}

function showSubscribePopup(email)
{
  $.get('/ajax/subscribe.php', {'sf_EMAIL': email}, function(respond){
    $('#popup_subscribe .popup_inner_right').html(respond);
    showPopup('popup_subscribe');
  });
}

function closePopup(popup)
{
  if (!popup) popup = 'popup';
  document.getElementById('gray_page').style.display = 'none';
  document.getElementById(popup).style.display = 'none';  
}

function popupIsClose(popup)
{
  if (!popup) popup = 'popup';
  return document.getElementById(popup).style.display == 'none';    
}

function getScrollXY() 
{
       var scrOfX = 0, scrOfY = 0;
       if( typeof( window.pageYOffset ) == 'number' ) {
               //Netscape compliant
               scrOfY = window.pageYOffset;
               scrOfX = window.pageXOffset;
       } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
               //DOM compliant
               scrOfY = document.body.scrollTop;
               scrOfX = document.body.scrollLeft;
       } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
               //IE6 Strict
               scrOfY = document.documentElement.scrollTop;
               scrOfX = document.documentElement.scrollLeft;
       }
       return [ scrOfX, scrOfY ];
}

function getClientHeight()
{
    if (window.innerHeight) return window.innerHeight;
    if (document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
    if (document.body) return document.body.clientHeight;
}

function subscribeSubmit(form, url)
{
  el = $('#' + form)[0];
  
  if (!url) url = el.action;
 
  $.ajax({
    type: el.method,
    url: url,
    data: $('#' + form).serialize(),
    success: function(respond) {
      $('#popup_subscribe .popup_inner_right').html(respond);
      showPopup('popup_subscribe');
    }
  });
}


function moveleftThumbsList(relative) 
{
  if (firstThumbItem == 1) return;
  firstThumbItem--;

  // скрываем/показываем стрелочки
  if (firstThumbItem > countThumbs - 6) $('#rightArrow').hide();
  else $('#rightArrow').show();  
  if (firstThumbItem == 1) $('#leftArrow').hide();
  else $('#leftArrow').show();
  
  $('#thumbs_list').animate({
    'left': '+=' + relative + 'px'
  });
}

function moverightThumbsList(relative) 
{
  if (firstThumbItem > countThumbs - 6) return;
  firstThumbItem++;
  
  // скрываем/показываем стрелочки
  if (firstThumbItem > countThumbs - 6) $('#rightArrow').hide();
  else $('#rightArrow').show();
  if (firstThumbItem == 1) $('#leftArrow').hide();
  else $('#leftArrow').show();

  $('#thumbs_list').animate({
    'left': '-=' + relative + 'px'
  });  
}

function moveToThumb(num, relative)
{
  if (countThumbs < 7) {
    $('#rightArrow').hide();
    $('#leftArrow').hide();
    return;
  }
  var first = 0;
  
  // выбираем, какой элемент будет показываться первым
  for (var i = 1; i <= countThumbs; i++) {
    if (i >= num - 2 && first == 0) first = i;
  }
  
  // определяем, после первого элемента будет еще 5
  if (countThumbs - first < 6) first -= 5 - (countThumbs - first);
    
  $('#thumbs_list')[0].style.left = '-' + (first - 1) * relative + 'px';
  firstThumbItem = first; 
  
  // скрываем/показываем стрелочки
  if (firstThumbItem > countThumbs - 6) $('#rightArrow').hide();
  else $('#rightArrow').show();
  if (firstThumbItem == 1) $('#leftArrow').hide();
  else $('#leftArrow').show();    
}
