// JavaScript Document

/* параметры кружков */
document.circles = [
	{name: 'domen',		x: 124, y: 276, radius:  80, 
			img1: 'imgs/domen1.gif',
			img2: 'imgs/domen2.gif',
			link: 'http://domain.by', newwindow: true},
	{name: 'hosting',	x: 168, y: 397, radius:  50,
			img1: 'imgs/hosting1.gif',
			img2: 'imgs/hosting2.gif',
			link: 'hosting1.html', newwindow: false},
	{name: 'shop',		x: 306, y: 388, radius:  87, 
			img1: 'imgs/shop1.gif',
			img2: 'imgs/shop2.gif',
			link: 'http://www.shop.by', newwindow: true},
	{name: 'mail',		x: 445, y: 224, radius:  49, 
			img1: 'imgs/mail1.gif',
			img2: 'imgs/mail2.gif',
			link: 'http://www.mail.by', newwindow: true},
	{name: 'easypay',	x: 461, y: 455, radius:  84,
			img1: 'imgs/easypay1.gif',
			img2: 'imgs/easypay2.gif',
			link: 'http://www.easypay.by', newwindow: true},
	{name: 'open',		x: 610, y: 299, radius: 135,
			img1: 'imgs/open1.gif',
			img2: 'imgs/open2.gif',
			link: 'http://www.open.by', newwindow: true},
	{name: 'internet',	x: 728, y: 435, radius:  49,
			img1: 'imgs/internet1.gif',
			img2: 'imgs/internet2.gif',
			link: 'inet1.html', newwindow: false},
	{name: 'rabota',	x: 826, y: 324, radius:  86, 
			img1: 'imgs/rabota1.gif',
			img2: 'imgs/rabota2.gif',
			link: 'http://www.rabota.by', newwindow: true},
	{name: 'afisha',	x: 916, y: 209, radius:  63,
			img1: 'imgs/afisha1.gif',
			img2: 'imgs/afisha2.gif',
			link: 'http://afisha.open.by', newwindow: true},
	{name: 'contact',	x: 741, y: 596, radius:  49,
			img1: 'imgs/contact1.gif',
			img2: 'imgs/contact2.gif',
			link: 'contacts.html', newwindow: false},
];

/* текущий кружочек */
document.cc = null;

/* длинна главной области */
document.mr_width = 1000;
document.sw_width = 902;

/* смещение */
document.region_offset = {x: 0, y: 0};

/* координаты мыши */
function GetMouseCoordinat(e)
{
  var pos = {x: 0, y: 0};

  if (!e) e = window.event;

  if (e.pageX || e.pageY)
  {
    pos.x = e.pageX;
    pos.y = e.pageY;
  }
  else if (e.clientX && e.clientY)
  {
  	if (document.documentElement)
    	pos.x = e.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft;
	else if (document.body)
	   	pos.x = e.clientX + document.body.scrollLeft - document.documentElement.clientLeft;

	if (document.documentElement)
    	pos.y = e.clientY + document.documentElement.scrollTop - document.documentElement.clientTop;
	else if (document.body)
	   	pos.y = e.clientY + document.body.scrollTop - document.documentElement.clientTop;
		
  }

  return pos;
}

/* возвращает текущий размер окна браузера */
function GetClientSize() {

	var size = {width: null, height: null};
	
	if (self.innerWidth)
		size.width = self.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth)
		size.width = document.documentElement.clientWidth;
	else if (document.body)
		size.width = document.body.clientWidth;
		
	if (self.innerHeight)
		size.height = self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		size.height = document.documentElement.clientHeight;
	else if (document.body)
		size.height = document.body.clientHeight;
		
	return size;
	
} 

/* создаёт кружочки */
function CreateCircleElements() {
	
	var mn = document.getElementById('main');
	

	for (i in document.circles) {
		var circle = document.circles[i];
		
		var a = document.createElement('a');
		a.setAttribute('href', circle.link);
		if (circle.newwindow)
			a.setAttribute('target','_blank');

		a.className = 'circle';
		a.style.width 	= (2*circle.radius) + 'px';
		a.style.height 	= (2*circle.radius) + 'px';
		
		a.onfocus = new Function('this.blur()');
		a.onclick = new Function('return OnCircleClick(this);');
		
		/* прелоад */
		var img1 = new Image(); img1.src = circle.img1;
		//img1.style.border = '1px solid green';
		var img2 = new Image(); img2.src = circle.img2;
		//img2.style.border = '1px solid red';
		
		
		a.appendChild(img1);
			
		document.body.appendChild(a);
		
		circle.a = a;
		circle.img1 = img1;
		circle.img2 = img2;
	}
}

/* перемещает кружочки при изм. размеров окна */
function MoveCircleElement() {
	for (i in document.circles) {
		var circle = document.circles[i];
		circle.a.style.left   = circle.x - circle.radius +
										document.region_offset.x + "px";
		circle.a.style.top    = circle.y - circle.radius +
										document.region_offset.y + "px";
	}
}


/* проверка в какому кругу принадлежит точка  */
function CheckPosition(c) {
	for (i in document.circles) {
		var circle = document.circles[i];
		if (  (c.x - circle.x - document.region_offset.x)*
			  (c.x - circle.x - document.region_offset.x) +
			  (c.y - circle.y - document.region_offset.y)*
			  (c.y - circle.y - document.region_offset.y) <=
			circle.radius * circle.radius )
			return circle;
	}
	return null;
}

/* вход мышки в куржок */
function OnCircleIn(cd) {
	if (cd.img1.parentNode == cd.a)
		cd.a.removeChild(cd.img1);
	cd.a.appendChild(cd.img2);
	cd.a.style.zIndex = 2;
	document.cc = cd;
}

/* выход мышки в куржок */
function OnCircleOut(cd) {
	if (cd.img2.parentNode == cd.a)
		cd.a.removeChild(cd.img2);
	cd.a.appendChild(cd.img1);
	cd.a.style.zIndex = 0;
	document.cc = null;
}

function OnCircleClick(a) {
	
	if (!document.cc)
		return false;
	
	return true;
}

/* событие на движение мышки */
function OnBodyMouseOver(e) {
	var c = GetMouseCoordinat(e);
	var cc = CheckPosition(c);
	
	if ( cc != document.cc) {
		if (document.cc) OnCircleOut(document.cc);
		document.cc = cc;
		if (document.cc) OnCircleIn(document.cc);
	}
}

/* событие на изменение размеров */
function OnBodyResize() {
	var size = GetClientSize();
	
	var mr = document.getElementById("mr");
	if (mr) {
		document.region_offset.x = Math.round((size.width - document.mr_width)/2);
		if (document.region_offset.x < 0) 
			document.region_offset.x = 0;
		document.region_offset.y = 0; 

		mr.style.left = document.region_offset.x + "px";
		mr.style.top  = document.region_offset.y + "px";
		MoveCircleElement();
	}
	
	for (var i = 1; i <= 4; i++) {
		var w = document.getElementById('stepwin' + i);
		if (w) {
			w.style.width = document.sw_width + 'px';
			w.style.left =  Math.round(document.region_offset.x + (document.mr_width - document.sw_width)/2) + 'px';
		}
	}
}

/* всплывающее окно с текстом */
function ShowStep(step) {
	HideStep();
	OnBodyResize();
	
	for (var i = 1; i <= 4; i++) {
		var w = document.getElementById('stepwin' + i);
		if (w) {
			if (i == step)
				w.style.display = '';
			else
				w.style.display = 'none';
		}
	}
}

function HideStep() {
	for (var i = 1; i <= 4; i++) {
		var w = document.getElementById('stepwin' + i);
		if (w) {
		w.style.display = 'none';
		}
	}
}


/* обработчики событий */
document.onmousemove = OnBodyMouseOver;
window.onresize	 = OnBodyResize;

