var cameraSizes = {minSize:{width:400, height:300},
				   mediumSize:{width:600, height:400},
				   bigSize:{width:800, height:600}};
var cameraOrientation = 0;
var cameraSize = cameraSizes.mediumSize;
if (cameraOrientation == 1) cameraSize = {width:cameraSizes.mediumSize.height, height:cameraSizes.mediumSize.width};
var cameraRectangles = new Array();
var cameraRectangle = null;

var cameraInfos = null;

function drawCameraRect(center)
{
	if (cameraOrientation == 1)
	{
		width = cameraSize.height;
		height = cameraSize.width;
	}
	else
	{
		width = cameraSize.width;
		height = cameraSize.height;
	}
	var size = map.getSize();
	var points = new Array();
	var zoom = map.getZoom();
	points.push(map.fromContainerPixelToLatLng(new GPoint((size.width / 2 - width / 2), (size.height / 2 - height / 2)), zoom));
	points.push(map.fromContainerPixelToLatLng(new GPoint((size.width / 2 + width / 2), (size.height / 2 - height / 2)), zoom));
	points.push(map.fromContainerPixelToLatLng(new GPoint((size.width / 2 + width / 2), (size.height / 2 + height / 2)), zoom));
	points.push(map.fromContainerPixelToLatLng(new GPoint((size.width / 2 - width / 2), (size.height / 2 + height / 2)), zoom));
	points.push(map.fromContainerPixelToLatLng(new GPoint((size.width / 2 - width / 2), (size.height / 2 - height / 2)), zoom));
	
	if (cameraRectangle) removeCameraRectangle();
	cameraRectangle = new GPolygon(points, "#000000", 1, 1, "#FFFFFF", 0.4);
	map.addOverlay(cameraRectangle);
}

function removeCameraRectangle()
{
	map.removeOverlay(cameraRectangle);
}

function removeCameraRectangles()
{
	for (i in cameraRectangles)
	{
		map.removeOverlay(cameraRectangles[i]);
	}
}

function Camera()
{
	if (!camera)
	{
		map.closeInfoWindow();
		document.getElementById('cameraControl').src = "content/module/connemap/images/print_sel.png";
		camera = true;
		document.getElementById("background-cameratoolbox").style.display = "";
		document.getElementById("foreground-cameratoolbox").style.display = "";
		drawCameraRect(map.getCenter());
	}
}

function setCameraSize(size)
{
	switch(size)
	{
		case 1:
			cameraSize = cameraSizes.minSize;
			document.getElementById('cameraControl_small').src = "content/module/connemap/images/small_sel.png";
			document.getElementById('cameraControl_medium').src = "content/module/connemap/images/medium.png";
			document.getElementById('cameraControl_big').src = "content/module/connemap/images/big.png";
			break;
		case 2:
			cameraSize = cameraSizes.mediumSize;
			document.getElementById('cameraControl_small').src = "content/module/connemap/images/small.png";
			document.getElementById('cameraControl_medium').src = "content/module/connemap/images/medium_sel.png";
			document.getElementById('cameraControl_big').src = "content/module/connemap/images/big.png";
			break;
		case 3:
			cameraSize = cameraSizes.bigSize;
			document.getElementById('cameraControl_small').src = "content/module/connemap/images/small.png";
			document.getElementById('cameraControl_medium').src = "content/module/connemap/images/medium.png";
			document.getElementById('cameraControl_big').src = "content/module/connemap/images/big_sel.png";
			break;
	}
	drawCameraRect(map.getCenter());
}

function setCameraOrientation(orientation)
{
	switch (orientation)
	{
		case 0:
			document.getElementById('cameraControl_horizontal').src = "content/module/connemap/images/horizontal_sel.png";
			document.getElementById('cameraControl_vertical').src = "content/module/connemap/images/vertical.png";
			break;
		case 1:
			document.getElementById('cameraControl_horizontal').src = "content/module/connemap/images/horizontal.png";
			document.getElementById('cameraControl_vertical').src = "content/module/connemap/images/vertical_sel.png";
			break;
	}
	cameraOrientation = orientation;
	drawCameraRect(map.getCenter());
}

function printPicture()
{
	savePicture(false);
	openPrintWindow();
	closePrintDialog();
}

function savePicture(newPicture)
{
	var bounds = cameraRectangle.getBounds();
	var c = bounds.getCenter().lat()+","+bounds.getCenter().lng();
	var txt_title = "";
	var title = document.getElementById("camera_title");
	if (title) txt_title = title.value;

	var txt_description = "";
	var description = document.getElementById("camera_description");
	if (description) var txt_description = description.value;

	width = 50;
	height = 50;
	switch (cameraOrientation)
	{
		case 0:
			width = cameraSize.width;
			height = cameraSize.height;
			break;
		case 1:
			width = cameraSize.height;
			height = cameraSize.width;
			break;
	}
	if (!cameraInfos) cameraInfos = new Array();
	cameraInfos.push({center:bounds.getCenter(), zoom:map.getZoom(), title:txt_title, description:txt_description, width:width, height:height});

	cameraRectangle.setFillStyle({color:'#00FF00', opacity:0.1}); 
	cameraRectangle.redraw();
	cameraRectangles.push(cameraRectangle);
	cameraRectangle = null;
	if (newPicture) drawCameraRect(map.getCenter());
}

function openPrintWindow()
{
	var query = "content/module/connemap/include/showprint.php?im="+cameraInfos.length;
	window.open(query, "_blank", "width=900px, height=600px, scrollbars=yes");
}

function closePrintDialog()
{
	document.getElementById("background-cameratoolbox").style.display = "none";
	document.getElementById("foreground-cameratoolbox").style.display = "none";
	if (cameraRectangle) removeCameraRectangle();
	removeCameraRectangles();
	document.getElementById('cameraControl').src="content/module/connemap/images/print.png";
	document.getElementById("camera_title").value ="";
	document.getElementById("camera_description").value = "";
	
	camera = false;
}