/* GLOBAL SECTION */

var ajaxDivCloseButton = '<div align="right" style="height:20px;background-color:#FFA050;"><a href="Закрыть" class="sr" onClick="javascript:document.getElementById(\'ajaxDiv\').style.display=\'none\';return false;">Закрыть</a></div>';
var contactsViewed = false;
var postsViewed = false;

function getAJAXObject()
{
  // code for IE7+, Firefox, Chrome, Opera, Safari
  if (window.XMLHttpRequest)
  {
    return new XMLHttpRequest();
  }
  // code for IE6, IE5
  else if (window.ActiveXObject)
  {
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  else
  {
    return null;
  }
}

function getDeadCenterCoords(width, height)
{
  // First, determine how much the visitor has scrolled

  var scrolledX, scrolledY;
  if( self.pageYOffset )
  {
    scrolledX = self.pageXOffset;
    scrolledY = self.pageYOffset;
  }
  else if( document.documentElement && document.documentElement.scrollTop )
  {
    scrolledX = document.documentElement.scrollLeft;
    scrolledY = document.documentElement.scrollTop;
  }
  else if( document.body )
  {
    scrolledX = document.body.scrollLeft;
    scrolledY = document.body.scrollTop;
  }

  // Next, determine the coordinates of the center of browser's window

  var centerX, centerY;
  if( self.innerHeight )
  {
    centerX = self.innerWidth;
    centerY = self.innerHeight;
  }
  else if( document.documentElement && document.documentElement.clientHeight )
  {
    centerX = document.documentElement.clientWidth;
    centerY = document.documentElement.clientHeight;
  }
  else if( document.body )
  {
    centerX = document.body.clientWidth;
    centerY = document.body.clientHeight;
  }

  // Xwidth is the width of the div, Yheight is the height of the
  // div passed as arguments to the function:
  var leftOffset = scrolledX + (centerX - width) / 2;
  var topOffset = scrolledY + (centerY - height) / 2;  
  
  return new Array(leftOffset, topOffset);
  
}

/* SITE TABS SECTION */

var tabs = new Array('index','contacts','photos','videos','audio','posts');

function highlightTab(tab)
{
  if (tab.className == 'tabSelected')
  return;

  tab.style.backgroundColor = '#FFA050';
}

function unhighlightTab(tab)
{
  if (tab.className == 'tabSelected')
  return;

  tab.style.backgroundColor = '#FFC060';
}

function showTab(tabname)
{
  for (var i=0;i<tabs.length;i++)
  {
    var tab = document.getElementById(tabs[i]+'_tab');
    var div = document.getElementById(tabs[i]+'_div');

    if (tabs[i] != tabname && tab.className == 'tabSelected')
    {
      tab.className = 'tab';
      tab.style.backgroundColor = '#FFC060';
      div.style.display = 'none';
    }

    if (tabs[i] == tabname)
    {
      tab.className = 'tabSelected';
      tab.style.backgroundColor = '#FFA050';
      div.style.display = 'block';
    }
  }
  
  if (tabname == 'contacts' && !contactsViewed)
  {
    showAjaxDiv(2,'page');
    contactsViewed = true;
  }
  
  if (tabname == 'posts' && !postsViewed)
  {
    showAjaxDiv(3,'page');
    postsViewed = true;
  }  
}

/* PHOTOS SECTION */

function showPhoto(id, photoWidth, photoHeight) 
{
  // dCC stands for deadCenterCoordinates
  var dCC = getDeadCenterCoords(photoWidth,photoHeight)
  
  var o=document.getElementById('fullImage');
  o.src = "photos/full/"+id+".jpg";
  
  var r=o.style;
  r.position='absolute';
  r.left = dCC[0] + 'px';
  r.top = dCC[1] + 'px';
  r.display = "block";
  
  showAjaxDiv(id,'photo');
}

/* POSTS SECTION */

function showAjaxDiv(id,get)
{
  var xmlhttp = getAJAXObject();

  if (xmlhttp == null)
  {
    alert('Данный браузер не поддерживает технологию AJAX, необходимую для выполнения задачи.\n'+
    'Воспользуйтесь, пожалуйста, более новым браузером, например, Mozilla Firefox или Google Chrome.');
    return;
  }

  xmlhttp.onreadystatechange=function()
  {
    if(xmlhttp.readyState==4)
    {
      if (get=='photo' || get=='page')
        return;
      
      var dCC = getDeadCenterCoords(700,500);
      document.getElementById('ajaxDiv').innerHTML = 
        ajaxDivCloseButton + 
        '<div style="width:700px;height:460px;overflow:auto;">'+xmlhttp.responseText+'</div>' +
        ajaxDivCloseButton;
      document.getElementById('ajaxDiv').style.left = dCC[0];
      document.getElementById('ajaxDiv').style.top = dCC[1];
      document.getElementById('ajaxDiv').style.display = 'block';
        
      return;
    }
  }

  var d = new Date();
  var params = "id="+parseInt(id)+"&get="+encodeURIComponent(get)+"&ajax="+d.getTime();

  xmlhttp.open("POST", location.href, true);

  //Send the proper header textrmation along with the request
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.setRequestHeader("Content-length", params.length);
  xmlhttp.setRequestHeader("Connection", "close");

  xmlhttp.send(params);  
}
