blockEvent = function (event)
{
	event = event ? event : window.event;
	if(event.stopPropagation) event.stopPropagation();
	else event.cancelBubble = true;
	if(event.preventDefault) event.preventDefault();
	else event.returnValue = false;
}

get = function (id)
{
	return document.getElementById(id) || false;
}

move = function (e)
{
	var target = e ? e.target : window.event.srcElement;
	if (!target.id) 
	{
		target = target.parentNode;
	}
	window.animation.move = Array();
	if (target.id == 'store_main')
	{
		window.animation.move.direction = -1;
		window.animation.move.className = 'good_store';
	}
	if (target.id == 'office_main')
	{
		window.animation.move.direction = 1;
		window.animation.move.className = 'good_office';
	}	
	blockEvent(e);
	
	document.body.className = '';
	
	window.animation.logo.style.visibility = window.animation.mainMenu.style.visibility = 'hidden';
	window.animation.move.startPos = window.animation.move.curPos = parseInt(window.animation.mover.style.left);
	window.animation.move.newPos = window.animation.move.direction > 0 ? 0 : -100;
	
	window.animation.move.duration = 25;
	window.animation.move.momentTime = 0;
	window.animation.move.timeout = 20;
	window.animation.move.shift = 100 * window.animation.move.direction;
	window.animation.move.timer = setTimeout("shift()", 800);
}

shift = function ()
{
	clearTimeout(window.animation.move.timer);
	window.animation.move.momentTime++;
	setPos();
	if (window.animation.move.momentTime <= window.animation.move.duration)
	{
		window.animation.move.time = setTimeout("shift()", window.animation.move.timeout);
	}
	else
	{
		window.animation.mover.style.left = window.animation.move.newPos + '%';
		document.body.className = window.animation.move.className;
		window.animation.logo.style.visibility = window.animation.mainMenu.style.visibility = 'visible';
		window.mover = null;
	}
}

setPos = function () 
{ 
	var t = window.animation.move.momentTime / window.animation.move.duration;
	t--;
	window.animation.mover.style.left = window.animation.move.shift * (t * t * t * t * t + 1) + window.animation.move.startPos + '%';
    return true;
}

opacityup = function ()
{
	window.animation.counter += 5;

	var value = window.animation.counter / 100;
	if (value < 1)
	{
		window.animation.setOpacity(window.animation.target, value);
	}
	else
	{
		clearInterval(window.animation.timer);
		document.body.className = window.animation.className;
		window.animation.setOpacity(window.animation.target, 1);
		window.animation.overlay.style.display = '';
		window.animation.target = window.animation.className = null;
		window.animation.block = false;
	}
}

opacitydown = function ()
{
	window.animation.counter -= 5;

	var value = window.animation.counter / 100;
	
	if (value > 0)
	{
		window.animation.setOpacity(window.animation.target, value);
	}
	else
	{
		clearInterval(window.animation.timer);
		document.body.className = window.animation.className;
		window.animation.setOpacity(window.animation.target, 0);
		window.animation.target.style.display = 'none';
		window.animation.badList.style.display = 'none';
		window.animation.target = window.animation.className = null;
		window.animation.block = false;
	}
}

destroyOffice = function (e)
{
	blockEvent(e);
	if (!window.animation.block)
	{
		window.animation.block = true;
		window.animation.className = 'bad_office';
		document.body.className = '';
		window.animation.badList.style.display = '';
		window.animation.target = window.animation.badOffice;
		
		window.animation.setOpacity(window.animation.badStore, 0);
		window.animation.setOpacity(window.animation.target, 0);
		
		window.animation.target.style.display = '';
		window.animation.counter = 0;
		window.animation.timer = setInterval("opacityup()", 1);
	}
}

returnOffice = function (e)
{
	blockEvent(e);
	if (!window.animation.block)
	{
		window.animation.block = true;
		window.animation.className = 'good_office';
		document.body.className = '';
		window.animation.overlay.style.display = 'none';
		window.animation.target = window.animation.badOffice;
		
		window.animation.setOpacity(window.animation.target, 1);
		
		window.animation.counter = 100;
		window.animation.timer = setInterval("opacitydown()", 1);
	}
}

destroyStore = function (e)
{
	blockEvent(e);
	if (!window.animation.block)
	{
		window.animation.block = true;
		window.animation.className = 'bad_store';
		document.body.className = '';
		window.animation.badList.style.display = '';
		window.animation.target = window.animation.badStore;
		
		window.animation.setOpacity(window.animation.badOffice, 0);
		window.animation.setOpacity(window.animation.target, 0);
		
		window.animation.target.style.display = '';
		window.animation.counter = 0;
		window.animation.timer = setInterval("opacityup()", 1);
	}
}

returnStore = function (e)
{
	blockEvent(e);
	if (!window.animation.block)
	{
		window.animation.block = true;
		window.animation.className = 'good_store';
		document.body.className = '';
		window.animation.overlay.style.display = 'none';
		window.animation.target = window.animation.badStore;
		
		window.animation.setOpacity(window.animation.target, 1);
		window.animation.counter = 100;
		window.animation.timer = setInterval("opacitydown()", 1);
	}
}

setFlashPoints = function ()
{
	var divs = document.body.getElementsByTagName('DIV');
	window.points = Array();
	findPoints(divs)
	var so = new SWFObject("/images/f/point.swf?v=1", "indexswf", "20", "20", "8", "");
	so.addParam("wmode", "transparent");
	for (var i = 0; i < window.points.length; i++)
	{
		//var flashPoint = document.createElement('DIV');
		//flashPoint.className = 'flashpoint';
		//flashPoint.id = 'point' + i;
		//points[i].className += ' nobg';
		//points[i].appendChild(flashPoint);
		//so.write('point' + i);
	}
}


findPoints = function (divs)
{
	for (var i = 0; i < divs.length; i++)
	{
		if (divs[i].className.search('point') > -1)
		{
			window.points.push(divs[i]);
		}
		else
		{
			findPoints(divs[i]);
		}
	}
}

imageLoadFunc = function (i)
{
	return function () 
	{
		if (!window.imagesReady)
		{
			window.imagesReady = Array(false, false, false, false);
		}
		window.imagesReady[i] = true;
	};
}

checkAnimationReady = function ()
{
	clearTimeout(window.readyTimeout);
	if (!window.bgImages)
	{
		window.bgImages = Array();
		window.bgImages[0] = new Image();
		window.bgImages[0].src = '/images/t/office_good.jpg';
		window.bgImages[1] = new Image();
		window.bgImages[1].src = '/images/t/office_bad3.jpg';
		window.bgImages[2] = new Image();
		window.bgImages[2].src = '/images/t/store_bad.jpg';
		window.bgImages[3] = new Image();
		window.bgImages[3].src = '/images/t/store_good.jpg';
		
		for (var i = 0; i < window.bgImages.length; i++)
		{
			if (!window.bgImages[i].height) 
			{
				window.bgImages[i].onload = imageLoadFunc(i);
			}
			else 
			{
				if (!window.imagesReady)
				{
					window.imagesReady = Array(false, false, false, false);
				}
				window.imagesReady[i] = true;
			}
		}
	}
	
	if (window.imagesReady)
	{
		var error = false;
		for (var i = 0; i < window.imagesReady.length; i++)
		{
			if (!window.imagesReady[i]) 
			{
				error = true;
				break;
			}
		}
		if (!error)
		{
			window.animation.storeGB.onclick = destroyStore;
			window.animation.storeBG.onclick = returnStore;
			window.animation.officeGB.onclick = destroyOffice;
			window.animation.officeBG.onclick = returnOffice;
		}
		else
		{
			window.readyTimeout = setTimeout("checkAnimationReady()", 1000);
		}
	}
}

window.onload = function ()
{
	// загоняем все нужные элементы в глобальный массив
	
	window.animation = Array();	
	window.animation.move = Array();

	if (!window.opera && !navigator.product)
	{
		window.animation.setOpacity = function (target, value)
		{
			value *= 100;
			target.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + value + ')';
			return true;
		}
	}
	else
	{
		window.animation.setOpacity = function (target, value)
		{
			target.style.opacity = value;
			return true;
		}
	}
	
	window.animation.overlay = get('overlay');
	window.animation.badStore = get('badstore');
	window.animation.badOffice = get('badoffice');
	window.animation.badList = get('badlist');
	window.animation.mover = get('mover');
	window.animation.logo = get('logo');
	window.animation.mainMenu = get('mainmenu');
	
	window.animation.storeMain = get('store_main');
	window.animation.officeMain = get('office_main');
		
	window.animation.storeGB = get('store_g-b');
	window.animation.storeBG = get('store_b-g');
	
	window.animation.officeGB = get('office_g-b');
	window.animation.officeBG = get('office_b-g');
	
	// вешаем события
	window.animation.storeMain.onclick = window.animation.officeMain.onclick = move;
	
	checkAnimationReady();

	//setFlashPoints();
	var point = get('point1');
	point.anim = new Array();
	point.anim.t = 0;
	point.anim.b = 0;
	point.anim.c = 10;
	point.anim.d = 40;
	animPoint(point);
}

animPoint = function (point)
{
	point.anim.t++;
	var y = Math.easeInOutBounce(point.anim.t, point.anim.b, point.anim.c, point.anim.d);
	
	point.style.marginTop = y + 'px';
	//point.style.marginLeft = Math.sin(point.anim.x / 2 + 3.14 / 2) + 'px';
	
	var func = function (param)
	{
		return function ()
		{
			animPoint(param);
		}
	}
	
	setTimeout(func(point), 120);
}

