var iZoomLevel = 16;
var iMaxZoomLevel = 17;
var iMaxSliderHeight = 320;
var oMap;

var s;
function load() {
	alfamap.init({
		container:document.getElementById("google_map"),
		init:{
			lon:iLon,
			lat:iLat,
			zoom:iDepartmentID != 0 ? iZoomLevel : 11,
			zoomComtrol:true//,
			//onUpdate:getMarkersByBound,
			//onBoundsChange:getMarkersByBound
		}
	});
	getMarker();
	if (iDepartmentID != 0) {
		alfamap.addOverlay({
			lon:iLon,
			lat:iLat,
			name:sDepartmentName,
			description:''
		});
	}
	return;
}

function getMarker(oMap, icon){
	$.getJSON(
		"/_/ajax/_get_offices_coords.html",
		{ cityID: iCityID, departmentID: iDepartmentID },
	function(data) {
		$.each(data.marker, function() {
			alfamap.addOverlay({
				lon:parseFloat(this.lng),
				lat:parseFloat(this.lat),
				description:this.info
			});
			pointsCache.push(this.lng+','+this.lat);
		});
		setTimeout('getMarkersByBound(1);',10);
	});
}

var pointsCache = [];
function getMarkersByBound (all) {
	var bounds = alfamap.getBounds(),
		left = all ? -180 : bounds.getLeft(),
		right = all ? 180 : bounds.getRight(),
		top = all ? 90 : bounds.getTop(),
		bottom = all ? -90 : bounds.getBottom();

		$.getJSON(
			"/_/ajax/_get_depatments_for_bounds.html",
			{
				left: left,
				right: right,
				top: top,
				bottom: bottom,
				exclude: pointsCache.join('~')
			},
			function(data) {
				$.each(data.marker, function() {
					alfamap.addOverlay({
						lon:parseFloat(this.lng),
						lat:parseFloat(this.lat),
						description:this.info
					});
					pointsCache.push(this.lng+','+this.lat);
				});
			}
		);
	
}

function createMarker(point, icon, info) {
	var marker = new GMarker(point, icon);
		GEvent.addListener(marker, 'click', function() {
			marker.openInfoWindowHtml(info);
		});

	return marker;
}

var bLoaded = false;

function closeGoogleMap(){
	$('#google_map_outer').hide();
	$('#googleLink a').removeClass('selected');
	$('#googleLink').removeClass('selected');
	if ($("#full_map_container").length != 0) {
		$(".content").height("auto");
		if ($.browser.msie) {
			setTimeout("$('#ratingHeader, #googleLink a').hide().show()", 1);
		}
	}
}

function openGoogleMap(){
	if($('#google_map').length > 0){
		$('#google_map_outer').show();

		if(!bLoaded){
			load();

			$(".slider_bg").click(function(e){
				var x = e.pageX - this.offsetLeft;
				var y = e.pageY - this.offsetTop - 320;

				var z=Math.round(y*iMaxZoomLevel/iMaxSliderHeight);
				z = iMaxZoomLevel - z;
				oMap.setZoom(z);
			});
		}

		bLoaded = true;
	}
	if ($("#full_map_container").length != 0) {
		if ($(".content").height() < $("#google_map_outer").height()) {
			var height = $("#google_map_outer").height();
			$(".content").height(height+"px");
		}
	}
}

$(document).ready(function() {
	if($('#google_map').length == 0){
		$('#googleLink').hide();
	}

	$('#googleLink a').click(function() {
		if($(this).hasClass('selected')){
			closeGoogleMap();
		}else{
			$(this).addClass('selected');
			$('#googleLink').addClass('selected');
			openGoogleMap();
		}
		return false;
	});
	$.getJSON(
		"/_/ajax/_get_offices_coords.html",
		{ cityID: iCityID, departmentID: iDepartmentID },
		function(data) {
			$("p#googleLink").show();
		}
	);
	
});

