/* ГЛОБАЛЬНЫЕ ПЕРЕМЕННЫЕ */
var img_dir = "interface/img";

/* ищем host */
var loc = window.location.href;
var matches = loc.match(/http:\/\/([^\/]*)\//);
var host = matches[1]; // имя хоста

/* вырезание пробелов */
if ('undefined' == typeof String.prototype.trim)
{
  String.prototype.trim = function() {
    return this.replace(/^\s+/, '').replace(/\s+$/, '');
  }
}

function fixPNG(element)
{
  //Если браузер IE версии 5.5-6
  if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent))
  {
    var src;
	
    if (element.tagName=='IMG') //Если текущий элемент картинка (тэг IMG)
    {
      if (/\.png$/.test(element.src)) //Если файл картинки имеет расширение PNG
      {
        src = element.src;
        element.src = "/i/blank.gif"; //заменяем изображение прозрачным gif-ом
      }
    }
    else //иначе, если это не картинка а другой элемент
    {
	  //если у элемента задана фоновая картинка, то присваеваем значение свойства background-шmage переменной src
      src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
      if (src)
      {
        src = src[1]; //берем из значения свойства background-шmage только адрес картинки
        element.runtimeStyle.backgroundImage="none"; //убираем фоновое изображение
      }
    };
    //если, src не пуст, то нужно загрузить изображение с помощью фильтра AlphaImageLoader
    if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='crop')";
  }
}

/* перечисление всех свойств любого объекта */
function properties(obj) 
{
	var s = "";
	for (var i in obj) {s += i + ': <font color="#006600">' + obj[i] + '</font><br />';}
  
	var props_window = window.open('','props_window','top=0, left=20,scrollbars=1, menubar=0, toolbar=0, location=0, directories=0, status=1, resizable=1,width=800,height=600');
	props_window.focus();

	props_window.document.write('<small style="font-family: Tahoma; font-size: 10px;">'+s+'</small> <hr />');
}


/* обработка HTML-мнемоник */
if ('undefined' == typeof String.prototype.mnemonicToChar)
{
  String.prototype.mnemonicToChar = function()
  {
  	var str = this;
	var pat = new Array (/&lt;/,/&gt;/,/&quot;/);
	var rep = new Array ('<','>','"');
	var count = pat.length; 
	for (var i=0; i<count; i++)
	{
		while (str.search(pat[i]) != -1) str = str.replace(pat[i],rep[i]);
	}
    return str;
  }
}

function PopUp(script_name,x,y,width,height)
{
	popup = window.open(script_name,'popup','top=' + y + ', left=' + x + ',scrollbars=1, menubar=0, toolbar=0, location=0, directories=0, status=1, resizable=1,width=' + width + ',height=' + height);
	popup.focus();
}

function BrowserDetect() {
   var ua = navigator.userAgent.toLowerCase(); 
   // browser name
   this.isIE = ( (ua.indexOf('msie') != -1) && (ua.indexOf('opera') == -1) && (ua.indexOf('webtv') == -1));
   // browser version
   this.versionMinor = parseFloat(navigator.appVersion); 
   // correct version number
   if (this.isIE && this.versionMinor >= 4) {
      this.versionMinor = parseFloat(ua.substring( ua.indexOf('msie ') + 5 ));
   }
   this.versionMajor = parseInt(this.versionMinor); 
   // dom support
   this.isDOM1 = (document.getElementById);
   this.isDOM2Event = (document.addEventListener && document.removeEventListener);
   this.isIE5up = (this.isIE && this.versionMajor >= 5);
}
var browser = new BrowserDetect();

/* является ли числом */
function isNumeric(event)
{
	var keyASCII = event.which ? event.which : event.keyCode;
	
	if (isNaN(String.fromCharCode(keyASCII)) && keyASCII != 17 && keyASCII !=8) return false;
	else return true;
}

/* переход по адресу */
function moveTo(url)
{
	window.location.href = url;
}

/* предзагрузка картинок */
function preloadImg(img_src) {
	if (document.images) {
	 rslt = new Image();
	 rslt.src = img_src;
	 return rslt;
	}
}

/* Позиция элемента */
function getPosition(elem)
{
	var offTrial=elem;
	var offL=0;
	var offT=0;

	while(offTrial)
	{
		offL+=offTrial.offsetLeft;
		offT+=offTrial.offsetTop;
		offTrial=offTrial.offsetParent;
	}

	if (navigator.userAgent.indexOf("Mac")!=-1 && typeof document.body.leftMargin!="undefined") 
	{
		offL+=document.body.leftMargin;
		offT+=document.body.topMargin;
	}

	return {left:offL , top:offT}
}
/* Возвращает последнее число */
function lastNum(int)
{    return parseInt(int.toString().substr(-1,1));
}
/* Показывает всплывающую картинку */
function popImg(obj, img_src)
{    // Позиция родителя
    var pos = getPosition(obj);
        
    // Показываем картинку
    var img = document.createElement('img');
    img.src = img_src;
    img.style.position = 'absolute';
    img.style.zIndex = 1111;
    img.style.left = pos.left - 50 + 'px';
    img.style.top = pos.top - 100 + 'px';
    img.style.cursor = 'pointer';
    img.className = 'popimg';
    img.onclick = function () {
        if (typeof this.closeImg != 'undefined') {            this.closeImg.parentNode.removeChild(this.closeImg);
        }
        this.parentNode.removeChild(this);
    }
    // Добавляем крестик
    img.onload = function () {
        showPopImgClose(this);
    }
    document.body.appendChild(img);
    showPopImgClose(img);
    return false;
}
// Крестик закрытия для всплывающей картинки
function showPopImgClose(img)
{
    var pos = getPosition(img);
    
    if (typeof img.closeImg == 'undefined') {
        var close = document.createElement('img');
        close.src = '/i/icons/close.gif';
        close.style.position = 'absolute';
        close.style.zIndex = 1112;
        close.style.cursor = 'pointer';
        document.body.appendChild(close);
        img.closeImg = close;
    } else {
        var close = img.closeImg;
    }
    
    close.style.left = pos.left + img.offsetWidth - 25 + 'px';
    close.style.top = pos.top + 11 + 'px';

    close.onclick = function () {
        img.parentNode.removeChild(img);
        close.parentNode.removeChild(close);
    }
}

