var topMenu;
var topMenuItems;
var topMenuSpacer;
var topMenuAngles = new Array();

function init() {
	topMenu = document.getElementById('top_menu_fon');
	topMenuItems = document.getElementById('top_menu_items');
	topMenuSpacer = document.getElementById('top_menu_spacer');

	topMenuAngles.push(document.getElementById('top_menu_1'));
	topMenuAngles.push(document.getElementById('top_menu_2'));
	topMenuAngles.push(document.getElementById('top_menu_3'));
	topMenuAngles.push(document.getElementById('top_menu_4'));

	menuDecor();
}

function menuDecor() {
	if (topMenuItems && topMenuSpacer) {
		if (70 < topMenuItems.offsetHeight) {
			var angleHeight = 39;
			menuDecorAngles('large');
		} else {
			var angleHeight = 24;
			menuDecorAngles('small');
		}

		var height = topMenuItems.offsetHeight - angleHeight * 2;
		if (height < 0) height = 2;

		topMenuSpacer.style.height = height + 'px';
	}
}

function menuDecorAngles(type) {
	var count = topMenuAngles.length;
	if (4 < count) count = 4;

	for (var i = 0; i < count; i++) {
		topMenuAngles[i].className = 'top_menu_' + ('small' == type ? 'small_' : '') + (i + 1);
	}
}

function getElementPosition(ele) {
	this.x = ele.offsetLeft;
	this.y = ele.offsetTop;
	this.ele = ele;

	while (this.ele.offsetParent != null) {
		this.ele = this.ele.offsetParent;
		this.x += this.ele.offsetLeft;
		this.y += this.ele.offsetTop;
	}

	return this;
}

function scrollToIllu(id) {
	Effect.ScrollTo(
		id,
		{duration: 1, offset: (getTopMenuHeight() + 45) * -1}
	);
}

function getTopMenuHeight() {
	return topMenu.offsetHeight;
}

var map;

function initMap() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById('google_map'));

		map.setCenter(new GLatLng(55.755786, 37.617633), 15);
		map.enableScrollWheelZoom();
		map.setZoom(3);
		map.setMapType(G_SATELLITE_MAP);

		var marker = new GMarker(new GLatLng(55.804365, 37.585079));
		map.addOverlay(marker);

		iWindow = new infoWindow(marker);
		iWindow.open();
		GEvent.bind(marker, 'click', iWindow, iWindow.open);
		GEvent.bind(marker, 'infowindowclose', iWindow, iWindow.close);

		var mapTypeControl = new GMapTypeControl();
		map.addControl(
			mapTypeControl,
			new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(20, 25))
		);

		var mapControl = new GLargeMapControl3D();
		map.addControl(
			mapControl,
			new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(10, 100))
		);
	}
}

function infoWindow(parent) {
	this.parent = parent;
	this.isShown = false;
	this.content =
		'<b>Silver Plate</b><br />' +
		'Бол. Новодмитровская ул., 36/4, стр. 12,<br />' +
		'Дизайн-завод «<a href="http://flacon.su">Флакон</a>», ' +
		'127015, Москва, Россия<br />' +
		'(+7 495) 504-10-78, ' +
		'<a href="mailto:team@silverplate.ru">team@silverplate.ru</a>' +

		'<div id="map_zoomer">' +
		'<a onclick="zoomMap(); return false;">Ближе-ближе-ближе</a>' +
		'</div>';

	this.open = function() {
		if (!this.isShown) {
			this.isShown = true;
			this.parent.openInfoWindowHtml(this.content, {noCloseOnClick: true});
		}
	}

	this.close = function() {
		this.isShown = false;
	}
}

function zoomMap() {
	if (map) {
    	map.setZoom(17);
//
//     	var mapZoomerEle = document.getElementById('map_zoomer');
//     	if (mapZoomerEle) {
//     	    mapZoomerEle.style.display = 'none';
//     	}
	}
}

function changeLogoColor(random) {
	var ele = document.getElementById('logo');
	if (ele) {
		isBlue = (random && Math.random() > 0.5) || (!random && 'logo_blue' != ele.className);
		ele.className = isBlue ? 'logo_blue' : '';

		var bodyEle = document.getElementsByTagName('body');
		if (bodyEle) {
			bodyEle[0].className = isBlue ? 'blue' : '';
		}
	}
}

function centerLogo() {
	var logo = document.getElementById('logo');
	if (logo) {
		var ele = logo.getElementsByTagName('div')[0];
		if (ele) {
			var top = (document.body.clientHeight - ele.offsetHeight) / 2;
			if (top < 0) top = 0;
			ele.style.marginTop = top + 'px';
		}
	}
}

function initGallery() {
	var items = $('img.galleryItem');

	for (var i = 0; i < items.length; i++) {
		items[i].onclick = function() {
			$.scrollTo(this, 'slow', {offset: -95, axis: 'x'});
		}
	}
}
