$(document).ready(function(){
	setMapHeight();
	$("#mapObjects").show();
	
	$(".switcher a").click(function(){
		obj = $(this).parents(".objBox");
		
		if (obj.hasClass("boxHidden")) {
			rollPanel($(".objBox").not(obj).not(".objBox:has(.close)").not(".boxHidden"));
			unrollPanel(obj);
		}
		else {
			rollPanel(obj);
		}
		return false;
	});
	
	$(".objBoxHeader:has(.switcher)").css("cursor", "pointer");
	
	$(".objBoxHeader").click(function(){
		$(this).find(".switcher a").trigger("click");
	});
	$("#baseWholeSelect").add("#baseWholeShow").click(function(){
		return false;
	});
	
	$(".close a").click(function(){
		closePointInfo(this);
		return false;
	});
	
	$("#mapReset").click(function(){
		closeBoxes();
		clearActiveMarker();
		
		restartHoldMapEvent = GEvent.addListener(map, 'moveend', function(){
			startMapCenterV = null;
			holdMapPosition();
		});
		
		map.returnToSavedPosition();
		GEvent.removeListener(restartHoldMapEvent);
		gdir.clear();
	});
	$("#mapZoomButton").click(function(){
		dragzoom.initiateZoom();
	})
	$("#mapFullScreen").click(function(){
		showFsInfo();
		return false;
	})
	
	// mapZoom
	$("#mapZoom .zoomIn").click(function(){
		map.zoomIn();
		return false;
	});
	$("#mapZoom .zoomOut").click(function(){
		map.zoomOut();
		return false;
	});
	
	$("#printMap").click(function(){
		preparePrint();
		window.print();
		return false;
	});
	$("#help").click(function(){
		showHelp();
		return false;
	});
});

$(window).resize(function(){
	setMapHeight();
});

function unrollPanel(obj) {
	obj.find(".objBoxContent").slideDown("fast", function(){
		obj.removeClass("boxHidden");
		obj.find(".switcher a").text("schowaj");
		holdMapPosition();
	});
}

function rollPanel(obj) {
	obj.find(".objBoxContent").slideUp("fast", function(){
		obj.addClass("boxHidden");
		obj.find(".switcher a").text("rozwiń");
		holdMapPosition();
	});
}

function setMapHeight() {
	mapHeight = $(window).height() - $("#topOuter").height();
	
	$("#map").add("#mapPrint").add("#mapOuter").add("#bgLayer").height(mapHeight);
}

function showHelp() {
	$("#helpLayer").height($("#map").height());
	
	if($("#helpLayer").hasClass('hidden')) {
		closeBoxes();
		$(".topLayer").addClass('hidden');
		$("#helpLayer").removeClass('hidden');
		$("#mapDisable").remove();
		$("#mapOuter").append('<div id="mapDisable"></div>');
		$("#bgLayer").show();
	}
	else {
		$("#helpLayer").addClass('hidden');
		$("#mapDisable").remove();
		$("#bgLayer").hide();
	}
}

function showFsInfo() {
	$("#helpLayer").height($("#map").height());
	
	if($("#fsLayer").hasClass('hidden')) {
		closeBoxes();
		$(".topLayer").addClass('hidden');
		$("#fsLayer").removeClass('hidden');
		$("#mapDisable").remove();
		$("#mapOuter").append('<div id="mapDisable"></div>');
		$("#bgLayer").show();
	}
	else {
		$("#fsLayer").addClass('hidden');
		$("#mapDisable").remove();
		$("#bgLayer").hide();
	}
	
	$("#fsLayerClose").click(function(){
		$("#fsLayer").addClass('hidden');
		$("#mapDisable").remove();
		$("#bgLayer").hide();
	});
}

function closeBoxes() {
	closePointInfo('all');
	boxes = $(".objBox").not(".boxHidden");
	boxes.find(".objBoxContent").css("display", "none");
	boxes.find(".switcher a").text("rozwiń");
	boxes.addClass("boxHidden");
}

function preparePrint() {
	$("#mapPrint").empty().append($("#map").clone().attr("id", "mapCopy"));
	
	leftMargin = -(($("#map").width() - 980) / 2);
	$("#mapCopy").css("margin-left", leftMargin + "px");
}
