// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 6;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Revision of Flash required
var requiredRevision = 0;
// the version of javascript supported
var jsVersion = 1.0;
// -----------------------------------------------------------------------------


var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
jsVersion = 1.1;
// JavaScript helper required to detect Flash Player PlugIn version information
function JSGetSwfVer(i){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
      		var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			descArray = flashDescription.split(" ");
			tempArrayMajor = descArray[2].split(".");
			versionMajor = tempArrayMajor[0];
			versionMinor = tempArrayMajor[1];
			if ( descArray[3] != "" ) {
				tempArrayMinor = descArray[3].split("r");
			} else {
				tempArrayMinor = descArray[4].split("r");
			}
      		versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
            flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
      	} else {
			flashVer = -1;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	// Can't detect in all other cases
	else {
		
		flashVer = -1;
	}
	return flashVer;
} 
// If called with no parameters this function returns a floating point value 
// which should be the version of the Flash Player or 0.0 
// ex: Flash Player 7r14 returns 7.14
// If called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) 
{
 	reqVer = parseFloat(reqMajorVer + "." + reqRevision);
   	// loop backwards through the versions until we find the newest version	
	for (i=25;i>0;i--) {	
		if (isIE && isWin && !isOpera) {
			versionStr = VBGetSwfVer(i);
		} else {
			versionStr = JSGetSwfVer(i);		
		}
		if (versionStr == -1 ) { 
			return false;
		} else if (versionStr != 0) {
			if(isIE && isWin && !isOpera) {
				tempArray         = versionStr.split(" ");
				tempString        = tempArray[1];
				versionArray      = tempString .split(",");				
			} else {
				versionArray      = versionStr.split(".");
			}
			versionMajor      = versionArray[0];
			versionMinor      = versionArray[1];
			versionRevision   = versionArray[2];
			
			versionString     = versionMajor + "." + versionRevision;   // 7.0r24 == 7.24
			versionNum        = parseFloat(versionString);
        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
			if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
				return true;
			} else {
				return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );	
			}
		}
	}	
	return (reqVer ? false : 0.0);
}

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 getBodyScrollTop()
{
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft()
{
	return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function getClientCenterX()
{
	return parseInt(getClientWidth() / 2) + getBodyScrollLeft();
}

function getClientCenterY()
{
	return parseInt(getClientHeight() / 2) + getBodyScrollTop();
}

function init() 
{
	if (arguments.callee.done) 
	{
		return;
	}
		
	arguments.callee.done = true;
		
	if (_timer) 
	{
		clearInterval(_timer);
		_timer = null;
	}

	if (!DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision))
	{
		return;
	}
		
	var logo = document.getElementById("logo");
	logo.style.display = "none";

	var message = document.getElementById("intro");

	message.innerHTML = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"1000\" height=\"590\" id=\"index\" align=\"middle\"><param name=\"allowScriptAccess\" value=\"sameDomain\" /><param name=\"movie\" value=\"/intro.swf\" /><param name=\"quality\" value=\"high\" /><param name=\"bgcolor\" value=\"#014330\" /><embed src=\"/intro.swf\" quality=\"high\" bgcolor=\"#014330\" width=\"1000\" height=\"590\" name=\"index\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" /></object>";

	message.style.display = 'block';

	var width  = getClientWidth();
	var height = getClientHeight();

	var clwi = 1000;
	var clhe = 590;

	message.style.width    =  width  - 1;
	message.style.height   =  height - 1;

	var index = document.getElementById("index");
	
	index.style.position = 'absolute';
	var left = (width  - clwi) / 2;
	var top  = (height - clhe) / 2;
	index.style.Left = left;
	index.style.posLeft = left;
	index.style.Top = top;

	index.style.cssText += "left: " + left + "px; top: " + top + "px;";
};

function hide()
{
	var intro = document.getElementById("intro");
	intro.style.display = 'none';

	var logo = document.getElementById("logo");
	logo.style.display = "block";
}

function showProperties(obj, objName) 
{
	var result = "Свойства " + objName + ":" + "\n";
  
	for (var i in obj)
	{
		result += i + " = " + obj[i] + "\n";
	}
  
	return result;
}

function corrpics()
{
	for (i = 1; i <= count; i++)
	{
		var name = "p" + String(i);
		var pic  = document.getElementById(name);

		corrpic(name, pic.clientWidth, pic.clientHeight);
	}
}

function corrpic(name, width, height)
{
	if (width == 0 || height == 0)
	{
		return;
	}

	var pics = document.getElementById("pics");
	var pic  = document.getElementById(name);

	if (pics.clientWidth / pics.clientHeight > width / height)
	{
		pic.style.width = "100%";
	}
	else
	{
		pic.style.height = "100%";
	}
}

var timer  = true;
var curpos = 1;

function setElementOp(sElemId, nOpacity)
{
	setElementOpacity(document.getElementById(sElemId), nOpacity);
}

function setElementOpacity(elem, nOpacity)
{
	nOpacity /= 100.0;
	var opacityProp = getOpacityProperty();
	
	if (!elem || !opacityProp)
	{
		return;
	}
  
	if (opacityProp == "filter")
	{
		nOpacity *= 100;
	
		var oAlpha = elem.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha;
		if (oAlpha)
		{
			oAlpha.opacity = nOpacity;
		}
		else
		{
			elem.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + nOpacity + ")";
		}
	}
	else
	{
		elem.style[opacityProp] = nOpacity;
	}
}

function getOpacityProperty()
{
	if (typeof document.body.style.opacity == 'string')
	{
		return 'opacity';
	}
	else if (typeof document.body.style.MozOpacity == 'string')
	{
		return 'MozOpacity';
	}
	else if (typeof document.body.style.KhtmlOpacity == 'string')
	{
		return 'KhtmlOpacity';
	}
	else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) 
	{
		return 'filter';
	}

	return false;
}

function scroll(pic, type)
{
	var p    = document.getElementById("p" + String(pic));
	var pics = document.getElementById("pics");
	var all  = false;


	if (type == 1)
	{
		var raz = p.clientWidth - pics.clientWidth;
		var left = parseInt(p.style.left);

		if (isNaN(left))
		{
			left = 0;
		}

		if (-left < 10 * Math.floor(raz / 10))
		{
			p.style.left = String(left - 10) + "px";
		}
		else
		{
			all = true;
		}
	}
	else
	{
		var raz = p.clientHeight - pics.clientHeight;
		var top = parseInt(p.style.top);
		if (isNaN(top))
		{
			top = 0;
		}
		if (-top < 10 * Math.floor(raz / 10))
		{
			p.style.top = String(top - 10) + "px";
		}
		else
		{
			all = true;
		}
	}

	if (!all)
	{
		setTimeout("javascript:scroll(" + pic + ", " + type + ")", 50);
	}
	else
	{
		otkaz = false;
		setTimeout("nexttime()", 1000);
	}
}

function swapop(one, two, val)
{
	var o = document.getElementById("p" + String(one));
	var t = document.getElementById("p" + String(two));

	setElementOpacity(o, val);
	setElementOpacity(t, 100 - val);
	
	if (val > 0)
	{
		setTimeout("javascript:swapop("+one+", "+two+", "+(val - 5)+")", 50);
	}
	else
	{
		t.style.display = "block"
		t.style.zIndex  = 100;
		o.style.display = "none";
		o.style.zIndex  = -100;
		o.style.left    =  0;
		o.style.top     =  0;
		setElementOpacity(o, 100);
		setElementOpacity(t, 100);

		var pics = document.getElementById("pics");

		if (t.clientWidth / pics.clientWidth > 1.2)
		{
			setTimeout("javascript:scroll("+two+", 1)", 1000);
		}
		else if (t.clientHeight / pics.clientHeight > 1.2)
		{
			setTimeout("javascript:scroll("+two+", 2)", 1000);
		}
		else if (timer)
		{
			otkaz = false;
			setTimeout("nexttime()", 3000);
		}
	}
}

otkaz = false;

function n()
{
	if (otkaz || count <= 1)
	{
		return;
	}
	otkaz = true;
	for (i = 1; i <= count; i++)
	{
		k  = document.getElementById("p" + String(i));
		k.style.display = "none";
		k.style.zIndex  = -10;
	}

	var tek = curpos;
	curpos  = (tek == count ? 1 : tek + 1);

	t  = document.getElementById("p" + String(tek));
	t.style.zIndex  = 1;
	t.style.display = "block";

	var s = document.getElementById("p" + String(curpos));

	setElementOpacity(s, 0);
			
	s.style.display = "block";
	s.style.zIndex  = 100;

	swapop(tek, curpos, 95);
}

function nexttime()
{
	if (count > 1)
	{
		n();
	}
	else if (count == 1)
	{
		var t = document.getElementById("p1");
		t.style.left = 0;
		t.style.top  = 0;

		var pics = document.getElementById("pics");

		if (t.clientWidth / pics.clientWidth > 1.2)
		{
			setTimeout("javascript:scroll(1, 1)", 50);
		}
		else if (t.clientHeight / pics.clientHeight > 1.2)
		{
			setTimeout("javascript:scroll(1, 2)", 50);
		}
	}
}

setTimeout("nexttime()", 5000);