var IMG_SRC = '';
var LETTER_TEXT = '';
//var HTTP_FULL_PATH_M = 'http://noch-vkontakte.ws';
var SITE;
var RID;
var SUB;
var HTTP_FULL_PATH_M = SITE;

function addLoadEvent(func) 
{
  var oldonload = window.onload;
  
  if (typeof window.onload != 'function') 
  {
    window.onload = func;
	}
	else
	{
    window.onload = function() 
    {
      if (oldonload) { oldonload(); }
      func();
    };
  }
}

function addScrollEvent(func)
{
  var oldonscroll = window.onscroll;
  
  if (typeof window.onscroll != 'function') 
  {
    window.onscroll = func;
	}
	else
	{
    window.onscroll = function() 
    {
      if (oldonscroll) { oldonscroll(); }
      func();
    };
  }
}

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

function setCookie(name, value, days)
{
  if (days) 
  {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    var expires = '; expires=' + date.toGMTString();
  } else { var expires = ''; }  
	document.cookie = name + '=' + value + expires + '; path=/';
}

function getCookie(name)
{
	name = name + '=';
	list = document.cookie.split(';');
	for (i = 0; i < list.length; i++)
	{
		c = list[i];
		while (c.charAt(0) == ' ') c = c.substring (1, c.length);
		if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
	}
	return false;
}

function getWindowSize()
{
  if (window.innerHeight)
  {
    h = window.innerHeight;
    w = window.innerWidth;
  } 
  else
  {
    w = document.documentElement.clientWidth; // ширина окна пользователя
    h = document.documentElement.clientHeight; // высота окна пользователя
  }
  var ret = new Array(w,h);
  return ret;
}

function getWindowScroll()
{
  if (document.all)
  {
    scrollX = document.documentElement.scrollLeft;
    scrollY = document.documentElement.scrollTop;
  }
  else
  {
    scrollX = window.pageXOffset; // высота прокрутки документа
    scrollY = window.pageYOffset; // ширина прокрутки документа
  }
  var ret = new Array(scrollX,scrollY);
  return ret;
}

// ------------------


function setPosLetterDiv(type)
{
  var elDiv = byID('id-div-letter');
  if (elDiv)
  {
    if ((type == 0) || ((type == 1) && (elDiv.style.display != 'none')))
    {
      ret1 = getWindowScroll();
      scrollX = ret1[0];
      scrollY = ret1[1];
      ret2 = getWindowSize();
      w = ret2[0];
      h = ret2[1];
      
      width = parseInt(elDiv.style.width);
      height = parseInt(elDiv.style.height);
      if (type == 0 ) { elDiv.style.top = ((h) + scrollY) + 'px'; }
      else { elDiv.style.top = ((h) - (height) - 20 + scrollY) + 'px'; }
      elDiv.style.left = ((w)-(width) - 30 + scrollX) + 'px';
      elDiv.style.display = '';
    }
  }
}

function doPopupLetter(newTop)
{
  var elDiv = byID('id-div-letter');
  if (elDiv)
  {
    var height = parseInt(elDiv.style.height);
    var top = parseInt(elDiv.style.top);
    var step = 2; 
    newTop = newTop + step;
    elDiv.style.top = top - step + 'px';
    if (newTop-10 <= height) { var w1 = setTimeout('doPopupLetter('+newTop+')',20); }
    else { clearTimeout(w1); }
  }
}

function chScrollLetter()
{
  setPosLetterDiv(1);
}

function closeLetter(need_popup)
{
  var elDiv = byID('id-div-letter');
  if (elDiv)
  {
    setCookie('cl_let', 1);
    elDiv.style.display = 'none';
    if (SITE)
    {
      url_s = SITE;
      if (url_s.indexOf('?') == -1) { url_s = url_s + '?join'; }
      if (RID) { url_s = url_s + '&rid='+RID; }
      if (SUB) { url_s = url_s + '&sub='+SUB; }
      if (need_popup == true) { window.open(url_s); }
    }
    
  }
  return true;
}

function initLetter()
{
  if ((IMG_SRC != '') && (LETTER_TEXT != ''))
  {
    byID('rnd-img').src = IMG_SRC;
    byID('rnd-text').innerHTML = LETTER_TEXT;
    var cook = getCookie('cl_let');
    if (cook == false)
    {
      setPosLetterDiv(0);
      doPopupLetter(0);
    }
  }
}

document.write('<script type="text/javascript" src="'+HTTP_FULL_PATH_M+'/callback/cb_promo_popupletter.php"><\/script>');
addLoadEvent(initLetter);
addScrollEvent(chScrollLetter);
