﻿/// <reference name="MicrosoftAjax.js"/>

// Верстка
function MakeUp(objs, tplLeft, tplTop, tplWidth, tplHeight, bndObjects) {
	var b = new Array();
	if (bndObjects)
		for (var i = 0; i < bndObjects.length; i++)
			b[i] = Sys.UI.DomElement.getBounds(bndObjects[i]);
	for (var i = 0; i < objs.length; i++) {
		var obj = objs[i];
		if (tplWidth != null) obj.style.width = eval(tplWidth) + "px";
		if (tplHeight != null) obj.style.height = eval(tplHeight) + "px";
		if (tplLeft != null) obj.style.left = eval(tplLeft) + "px";
		if (tplTop != null) obj.style.top = eval(tplTop) + "px";
	}
}
//function MakeUp(obj, tplLeft, tplTop, tplWidth, tplHeight, bndObjects) {
//	var b = new Array();
//	if (bndObjects)
//		for (var i = 0; i < bndObjects.length; i++)
//			b[i] = Sys.UI.DomElement.getBounds(bndObjects[i]);
//	if (tplWidth != null) obj.style.width = eval(tplWidth) + "px";
//	if (tplHeight != null) obj.style.height = eval(tplHeight) + "px";
//	if (tplLeft != null) obj.style.left = eval(tplLeft) + "px";
//	if (tplTop != null) obj.style.top = eval(tplTop) + "px";
//}

function _show(name) {
	$get(name).style.display = "block";
}
function _hide(name) {
	$get(name).style.display = "none";
}

var _isPrint = false;

function resizeWindow() {
	var _border = 10;
	var _splitter = 7;

	// Предварительная верстка
	MakeUp([$get('Footer')],
		null, "b[0].height - b[1].height",
		null, null,
		[document.body, $get('Footer')]
	);
	$get('Footer').style.display = "block";

	// Основная верстка
	MakeUp([$get('Body')],
		null, null,
		null,
		"b[0].height - b[1].height - b[2].height",
		[document.body, $get('Header'), $get('Footer'), $get('PagesLeft')]
	);
	if (!_isPrint) {
		MakeUp([$get('CenterPane')],
			"b[2].width + 0 * " + _border + " + " + _splitter,
			null,
			"b[0].width - b[2].width - 0 * " + _border + " - " + _splitter,
			"b[1].height - b[3].height - 1 * " + _border,
			[document.body, $get('Body'), $get('LeftPane'), $get('HeaderMenu')]
		);
		MakeUp([$get('LeftPane')],
			null, null,
			null,
			"b[0].height",
			[$get('CenterPane')]
		);
	
		MakeUp([$get('PagesLeft_Content')],
			null, null,
			null,
			"b[0].height - 2 * " + _border + " - b[1].height",
			[$get('LeftPane'), $get('PagesLeft_Header')]
		);
		MakeUp([$get('LayerList'), $get('Search'), $get('InfoList'), $get('Route')],
			null, null,
			"b[0].width - 2 * " + _border,
			"b[0].height - 2 * " + _border,
			[$get('PagesLeft_Content')]
		);
		MakeUp([$get('PagesRight')],
			"b[0].x + b[0].width + 1*" + _splitter, //null,
			"b[0].y",
			"b[1].width - 1*" + _border, null,
			[$get('PagesLeft'), $get('CenterPane')]
		);
		$get('PagesRight').style.display = "block";
	} else {
		MakeUp([$get('CenterPane')],
			"b[0].x", null,
			"600", "400",
			[$get('Header_Print')]
		);
	}
	
	// Окончательная вертстка
	MakeUp([$get('Footer')],
		null, "b[0].height - b[1].height",
		null, null,
		[document.body, $get('Footer')]
	);
	MakeUp([$get('Footer_Print')],
		null,
		"b[0].y - b[1].height",
		null, null,
		[$get('Footer'), $get('Footer_Print')]
	);
//	MakeUp([$get('Footer_Print')],
//		null,
//		"b[0].height - b[1].height - b[2].height",
//		null, null,
//		[document.body, $get('Footer_Print'), $get('Footer')]
//	);
}

function beforePrint() {
	if (_isPrint) return;
	_isPrint = true;
	
	_hide("Header");
	_hide("HeaderMenu");
	_hide("LeftPane");
	//_hide("ToolBar");
	_show("Header_Print");
	_show("Footer_Print");

	resizeWindow();

	var map = $get("Map").control;
	map.get_toolBar().hide();
	map._doResize(map, null);
}

function afterPrint() {
	if (!_isPrint) return;
	_isPrint = false;

	_show("Header");
	_show("HeaderMenu");
	_show("LeftPane");
	//_show("ToolBar");
	_hide("Header_Print");
	_hide("Footer_Print");
	
	resizeWindow();

	var map = $get("Map").control;
	map.get_toolBar().show();
	map._doResize(map, null);
}

function clickPagesLeft(e) {
	selectPagesLeft(this);
}

function selectPagesLeft(page) {
	if (page.className == "page active") return;
	
	// Показать страницу если она скрыта
	page.style.display = "inline";

	var pages = page.parentNode;
	for (var i = 0; i < pages.childNodes.length; i++) {
		if (pages.childNodes[i].id) {
			pages.childNodes[i].className = "page";
			$get(pages.childNodes[i].id + "_Content").style.display = "none";
		}
	}
	page.className = "page active";
	var obj = $get(page.id + "_Content");
	obj.style.display = "block";

	for (var i = 0; i < obj.childNodes.length; i++) {
		var c = obj.childNodes[i].control;
		if ((c) && (c._doResize)) {
			c._doResize(c);
		}
	}

	var header = $get('PagesLeft_Header');
	var d = page.getElementsByTagName("DIV");
	if (d.length > 0)
		d = d[0].innerHTML
	else
		d = "";
	header.innerHTML = d;
	
	/*switch (page.id) {
		case "PageLayers": header.innerHTML = "Информационные слои"; break;
		case "PageSearch": header.innerHTML = "Поиск объектов"; break;
		case "PageInfoList": header.innerHTML = "Информация об объектах"; break;
	}*/

//	obj._doResize();
//	if (Sys.Browser.agent == Sys.Browser.InternetExplorer) {
//		alert(obj.childNodes[0].outerHTML);/*obj.childNodes[0].click(); alert(0);*/
//	} else {
//		obj.childNodes[1].control.click();
//	}
}

//function addPage(id, name, description, visible) {
//	var tab = document.createElement('DIV');
//	tab.id = id;
//	tab.innerHTML = name;
//	tab.description = description;
//	if (!visible) tab.style.display = "none";
//	$get('PagesLeft').appendChild(tab);

//	$addHandler(tab, "click", clickPagesLeft);
//}

function addPages() {
	if ($get('PageLayers')) $addHandler($get('PageLayers'), "click", clickPagesLeft);
	if ($get('PageSearch')) $addHandler($get('PageSearch'), "click", clickPagesLeft);
	if ($get('PageInfoList')) $addHandler($get('PageInfoList'), "click", clickPagesLeft);
	if ($get('Route')) $addHandler($get('PageRoute'), "click", clickPagesLeft);

	//addPage("PageRoute", "Маршрут", "Поиск маршрута", true);
}

function Shadow(parent, w, h, x, y, dx, dy, k) {
	/*if ((Sys.Browser.agent == Sys.Browser.InternetExplorer) && (Sys.Browser.version >= 8)) {
		x += 2;
		y += 2;
	}*/

	for (var i = 1; i <= k; i++) {
		var obj = document.createElement("div");
		//obj.style.border = "solid 1px white";
		obj.style.backgroundColor = "black";
		obj.style.position = "absolute";
		obj.style.zIndex = 250;
		obj.style.fontSize = "0pt";
		obj.style.padding = 0;
		obj.style.margin = 0;
		if (dx != 0)
			obj.style.width = (w + i*dx) + "px";
		else
			obj.style.width = w + "px";
		if (dy != 0)
			obj.style.height = (h + i*dy) + "px";
		else
			obj.style.height = h + "px";
		obj.style.left = x + "px";
		obj.style.top = y + "px";
		//Sys.UI.DomElement.setLocation(obj, x, y);
		if (Sys.Browser.agent == Sys.Browser.InternetExplorer) {
			obj.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity=" + ((k - i + 1) * 2) + ")"; 
		} else {
			obj.style.opacity = (k - i + 1) / 50;
		}
		parent.appendChild(obj);	
	}
}

function Shadows() {
	var map = $get("Map");
	//var toolBar = $get("ToolBar")
	var header = $get("Header")

	var bMap = Sys.UI.DomElement.getBounds(map);
	//var bTB = Sys.UI.DomElement.getBounds(toolBar);

	Shadow(map, 0, 3000, 0, 0, 1, 0, 5);
	Shadow(map, 3000, 0, 0, 0, 0, 1, 5);

//	Shadow(header, 0, bMap.height /*- bTB.height*/, bMap.x, bMap.y /*+ bTB.height*/, 1, 0, 5);
//	Shadow(header, bMap.width /*- bTB.width*/, 0, bMap.x /*+ bTB.width*/, bMap.y, 0, 1, 5);

	/*Shadow(header, bTB.width, 0, bMap.x, bMap.y + bTB.height, 0, 1, 5);
	Shadow(header, 0, bTB.height, bMap.x + bTB.width, bMap.y, 1, 0, 5);
	Shadow(header, 0, 0, bMap.x + bTB.width, bMap.y + bTB.height, 1, 1, 5);*/
}

function appLoad(sender, args) {
    //$addHandler(document.body, "contextmenu", function() { return false; });

	_show('Body');
	_show('HeaderMenu');

	resizeWindow();
	
	Shadows();
	addPages();
	
	selectPagesLeft($get('PageLayers'));
//alert(Sys.Browser.agent);	
//	$addHandler($get('PageLayers'), "click", clickPagesLeft);
//	$addHandler($get('PageSearch'), "click", clickPagesLeft);
//	$addHandler($get('PageInfoList'), "click", clickPagesLeft);

	$addHandler(window, "resize", resizeWindow);
}

Sys.Application.add_init(appLoad);
//var b = document.getElementsByTagName("body");
//$addHandler(b, "resize", resizeWindow);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
