//Взять ширину окна браузера
function getClientWidth() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

//Взять высоту окна браузера
function getClientHeight() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

//Устанавливаем размеры картинок
function getImagesWidth(w) {
  return Math.round(((getClientWidth()-340)*0.7*w)/835);
}

//Показать маленькую карту в координатах (x,y)
function showSmallMap(x, y) {
  document.getElementById("iMapSmall").style.left=x+'px';
  document.getElementById("iMapSmall").style.top=y+'px';
  document.getElementById("iMapSmall").style.visibility="visible";
}

//Скрыть маленькую карту
function hiddenSmallMap() {
  document.getElementById("iMapSmall").style.visibility="hidden";
}

//Определить координаты курсора
function mousePosition(event) {
  var x = y = 0;
  if (document.attachEvent != null) { // IE или Opera
    x = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    y = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
  }
  else
    if (!document.attachEvent && document.addEventListener) { // Gecko
      x = event.clientX + window.scrollX;
      y = event.clientY + window.scrollY;
    }
  return {x:x, y:y};
}

//Определить координату мыши относительно начала экрана
function mousePositionDisplay(event) {
  var x = y = 0;
  if (document.attachEvent != null) { // IE или Opera
    x = window.event.clientX;
    y = window.event.clientY;
  }
  else
    if (!document.attachEvent && document.addEventListener) { // Gecko
      x = event.clientX;
      y = event.clientY;
    }
  return {x:x, y:y};
}

//Пытаемся вывести картинку так, чтобы она полностью влезла в экран
function checkDisplay(e) {
  var smallMapHeight = 353;
  var smallMapWidth = 400;
  var ix = iy = 0;
  iy = mousePosition(e).y - 177;
  //Проверим выводить картинку вправо или влево
  if (mousePositionDisplay(e).x + smallMapWidth + 7 < getClientWidth()) {
    ix = mousePosition(e).x + 7;
  }
  else
    if (mousePositionDisplay(e).x - smallMapWidth - 7 > 0) {
      ix = mousePosition(e).x - smallMapWidth - 7;
    }
    else ix = mousePosition(e).x + 7;
  showSmallMap(ix, iy);
}

//Получить таблицу
function getTable() {
  mapPath = "/templates/template7/img/Map/"; 
  document.write('<table border=0 cellpadding=0 cellspacing=0><tr><td colspan="3">');
  document.write('<img src="' + mapPath + 'top.gif" width='+getImagesWidth(835)+' />');
  document.write('</td></tr><tr><td>');
  document.write('<img src="' + mapPath + 'left.gif" width='+getImagesWidth(246)+' />'); 
  document.write('</td><td>');
  document.write('<img src="' + mapPath + 'center.gif" width='+getImagesWidth(38) +                 
                 ' onMouseMove="checkDisplay(event);" onMouseOut="hiddenSmallMap();"' + 
                 ' alt="Наш склад на схеме города Ярославля" />');        
  document.write('</td><td>');         
  document.write('<img src="' + mapPath + 'right.gif" width='+getImagesWidth(553)+' />');
  document.write('</td></tr><tr><td colspan="3">'); 
  document.write('<img src="' + mapPath + 'bottom.gif" width='+getImagesWidth(835)+' />')         
  document.write(' </td></tr></table>');
}

//Показать увеличенную картинку
function OpenImage(imagePath, width, height, title, caption) {
    var iHeight = document.body.clientHeight, iWidth = self.innerWidth;
	var left = (self.opera ? iWidth : screen.availWidth)/2 - width/2;
    var top =  (self.opera ? iHeight : screen.availHeight)/2 - height/2;
    var win = window.open('','Preview','width='+width+',height='+height+
	',left='+left+',top='+top+',screenX=0,screenY=0,resizable=0,scrollbar=0,status=0');
    var wDoc = win.document;
	var content = '<html><head><title>' +title+ '</title><style>body{overflow: hidden;margin:0;}img{border:0;}</style>' +
	              '<link rel="stylesheet" type="text/css" href="/templates/template7/style.css" /></head>' +
				  '<body><div onClick="javascript:self.close()"><div class="Caption">'+caption+'</div>' +
	              '<img alt="' + caption + '" id="image" src="' + imagePath + '" /></body></html>';
    wDoc.write(content); 
	win.document.close();
    win.focus();
}

//Показать увеличенную карту
function OpenLargeMap(width, height) {
    var iHeight = document.body.clientHeight, iWidth = self.innerWidth;
	var left = (self.opera ? iWidth : screen.availWidth)/2 - width/2;
    var top =  (self.opera ? iHeight : screen.availHeight)/2 - height/2;
    var win = window.open('','Preview','width='+width+',height='+height+
	',left='+left+',top='+top+',screenX=0,screenY=0,resizable=0,scrollbar=0,status=0');
    var wDoc = win.document;
	var content = '<html><head><title>Наш склад на схеме города Ярославля</title><style>body{overflow: hidden;margin:0;}img{border:0;}</style></head>' +
				  '<body><div onClick="javascript:self.close()">' +
	              '<img alt="Наш склад на схеме города Ярославля" id="image" src="/templates/template7/img/Map/map2.gif" /></body></html>';
    wDoc.write(content); 
	win.document.close();
    win.focus();
}
