/**
 * Funkce pro Google Maps API
 * 
 * @author Jakub Maják
 * @version 1.02
 */

/**
 * deklarování obecných proměnných
 */
var map;
var iconStyle;
var mgr;
var layerArray=new Array();

/**
 * deklarování lokalizace s defaultními možnostmi
 */
var defaultsLocalization = {
	more:				"More...",
	photo:				"Photo",
	videos:				"Videos"
}; 
var localization = $.extend(defaultsLocalization, jsLocalization);

/**
 * Deklarování tlačítka další do mapy
 */
function MoreControl() {}
MoreControl.prototype = new GControl();

/**
 * nastavení vzhledu a vlastností
 * @param {Object} map
 */
MoreControl.prototype.initialize = function(map) {
	var containerMore = document.createElement("div");
	containerMore.style.border = "2px solid black";
	containerMore.style.fontSize = "12px";
	containerMore.style.fontFamily = "Arial, sans-serif";
	containerMore.style.width="80px";
	containerMore.style.backgroundColor = "#ffffff";
	containerMore.style.textAlign = "center";
	containerMore.innerHTML = localization.more;
	
	map.getContainer().appendChild(containerMore);
	
	GEvent.addDomListener(containerMore, "mouseover", function() {
		map.addControl(layerControl);
	});
	
	return containerMore;
}

/**
 * navrácení buttonu s pozicí na mapě
 */
MoreControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(210, 7));
}

/**
 * vrácení optionů
 * @param {Object} opts
 */
function LayerControl(opts) {
	this.opts = opts;
}
/**
 * deklarace nového kontrolu - zobrazení po najetí na tlačítko "další"
 */
LayerControl.prototype = new GControl();

/**
 * Nastavení vzhledu a vlastností kontroloru
 * @param {Object} map
 */
LayerControl.prototype.initialize = function(map) {
	var containerLayer = document.createElement("div");
	containerLayer.style.border = "2px solid black";
	containerLayer.style.fontSize = "12px";
	containerLayer.style.fontFamily = "Arial, sans-serif";
	containerLayer.style.width="80px";
	containerLayer.style.backgroundColor = "#ffffff";
	containerLayer.innerHTML = '<center><b>'+localization.more+'<\/b><\/center>';
	for (var i=0; i<this.opts.length; i++) {
		if (mapLayer[i].Visible) {
			var c = 'checked';
		} else {
			var c = '';
		}
		containerLayer.innerHTML += '<input type="checkbox" id="'+this.opts[i][1]+'" onclick="toggleLayer('+i+')" ' +c+ ' /> <label for="'+this.opts[i][1]+'">'+this.opts[i][0]+'</label><br />';
	}
	map.getContainer().appendChild(containerLayer);
	setTimeout("map.removeControl(layerControl)",3000);
	
	return containerLayer;
}

/**
 * navrácení kontroloru s pozicí na mapě
 */
LayerControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(210, 7));
}

/**
 * funkce pro překlikávání vrstev na mapě
 * @param {Integer} i
 */
function toggleLayer(i) {
	if (mapLayer[i].Visible) {
		mapLayer[i].hide();
	} else {
		if(mapLayer[i].Added) {
			mapLayer[i].show();
		} else {
			map.addOverlay(mapLayer[i]);
			mapLayer[i].Added = true;
		}
	}
	mapLayer[i].Visible = !mapLayer[i].Visible;
}

/**
 * definování tlačítek v kontroleru
 */
var layerControl = new LayerControl([Array(localization.photo,"photos"), Array(localization.videos,"videos")]);

/**
 * deklarování vrstev a jejich vlastností
 */
var mapLayer = new Array();
mapLayer[0] = new GLayer("com.panoramio.all");
mapLayer[0].Visible = false;
mapLayer[0].Added = false;
mapLayer[1] = new GLayer("com.youtube.all");
mapLayer[1].Visible = false;
mapLayer[1].Added = false;

/**
 * Inicializace Mapy
 * 
 * @param {Array} arrayMapView - pole s classami pro zobrazení map
 */
function initializeMap(arrayMapView) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("g-map"));
		setMapCenterAndZoom (Array(35.221997,-101.831297), 4);
		/* map.setCenter(new GLatLng(35.221997,-101.831297), 4);*/
		map.setUIToDefault();
		map.setMapType(G_PHYSICAL_MAP);
		map.addControl(new MoreControl());
		map.disableScrollWheelZoom();
	}
	/**
	 * nasatevení stylu ikon
	 */
	iconStyle = new Array();
	/* index 0 */
	var placesIcon = new GIcon(G_DEFAULT_ICON); /* nastavena jako defaultni */
	placesIcon.image = "/img/u/icons/ico-places.png";
	placesIcon.iconSize = new GSize(33,38);
	placesIcon.shadow = "/img/u/icons/ico-places-shadow.png";
	placesIcon.shadowSize = new GSize(40,30);
	placesIcon.imageMap = [18,0,22,7,22,8,31,10,32,12,32,14,26,20,27,31,22,31,16,28,7,32,5,30,5,27,6,20,0,14,0,11,6,9,10,8,15,0];
	iconStyle.push(placesIcon);
	/* index 1 */
	var r66Icon = new GIcon(G_DEFAULT_ICON);
	r66Icon.image = "/img/u/icons/ico-r66.png";
	r66Icon.iconSize = new GSize(31,36);
	r66Icon.shadow = "/img/u/icons/ico-r-shadow.png";
	r66Icon.shadowSize = new GSize(39,16);
	r66Icon.imageMap = [24,0,30,5,30,7,29,8,29,14,30,15,30,23,25,28,23,28,22,29,16,29,16,30,14,30,14,29,8,29,6,28,4,28,4,27,1,24,1,22,0,15,1,15,1,15,2,12,2,8,1,8,0,8,0,5,5,0,7,0,8,0,8,1,13,1,13,0,16,0,16,0,16,0,19,1];
	iconStyle.push(r66Icon);
	/* index 2 */
	var r395Icon = new GIcon(G_DEFAULT_ICON);
	r395Icon.image = "/img/u/icons/ico-r395.png";
	r395Icon.iconSize = new GSize(30,36);
	r395Icon.shadow = "/img/u/icons/ico-r-shadow.png";
	r395Icon.shadowSize = new GSize(41,30);
	r395Icon.imageMap = [24,0,29,5,29,5,29,6,29,8,28,9,28,14,29,15,29,16,29,17,29,16,29,22,29,23,28,24,26,26,24,27,24,28,22,28,22,29,15,29,15,30,13,30,13,28,6,29,4,27,2,26,1,24,0,22,0,16,1,14,1,12,2,9,0,8,0,6,0,4,4,0,6,0,7,0,9,1,12,1,12,0,16,0,18,1,21,1,21,0];
	iconStyle.push(r395Icon);
	/* index 3 */
	var cadillacIcon = new GIcon(G_DEFAULT_ICON);
	cadillacIcon.image = "/img/u/icons/ico-cadillac.png";
	cadillacIcon.iconSize = new GSize(39,27);
	cadillacIcon.shadow = "/img/u/icons/ico-cadillac-shadow.png";
	cadillacIcon.shadowSize = new GSize(43,30);
	cadillacIcon.imageMap = [38,1,38,0,38,5,34,8,34,21,33,22,32,23,30,24,30,24,23,24,22,25,22,26,17,26,17,25,11,24,15,24,7,24,4,20,4,8,1,4,0,3,0,0,0,0];
	iconStyle.push(cadillacIcon);
	/* index 4 */
	var midpointIcon = new GIcon(G_DEFAULT_ICON);
	midpointIcon.image = "/img/u/icons/ico-midpoint.png";
	midpointIcon.iconSize = new GSize(49,41);
	midpointIcon.iconAnchor = new GPoint(26,41);
	midpointIcon.shadow = "/img/u/icons/ico-midpoint-shadow.png";
	midpointIcon.shadowSize = new GSize(85,41);
	midpointIcon.imageMap = [48,0,0,0,0,24,8,24,8,40,9,40,10,38,10,24,39,24,39,40,39,40,40,40,40,24,48,24,48,24];
	iconStyle.push(midpointIcon);
	/* var orangeIcon = new GIcon(G_DEFAULT_ICON);
	orangeIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/orange/blank.png";
	iconStyle.push(orangeIcon);*/
	
	/**
	 * deklarování MarkerManageru a zobrazení bodů v nadefinovaných polích
	 */
	mgr = new MarkerManager(map);
	var aaaa = arrayMapView;
	if ($.inArray("us-main",arrayMapView)!=-1) {
		showPointOnMaps(pointToMap["usMain"], 0, '', 0);
	}
	if ($.inArray("r66", arrayMapView)!=-1) {
		showPointOnMaps(pointToMap["r66"], 6, '', 1);
		showPointOnMaps(pointToMap["r66Main"], 0, 5, 1);
	}
	if ($.inArray("r66-alternatives", arrayMapView)!=-1) {
		showPointOnMaps(pointToMap["r66Alternatives"], 6, '', 1);
	}
	if ($.inArray("r66-alternatives2", arrayMapView)!=-1) {
		showPointOnMaps(pointToMap["r66Alternatives"], 6, '', 1);
	}
	if ($.inArray("r395", arrayMapView)!=-1) {
		showPointOnMaps(pointToMap["r395"], 6, '', 2);
		showPointOnMaps(pointToMap["r395Main"], 0, 5, 2);
	}
	if ($.inArray("cadillac",arrayMapView)!=-1) {
		showPointOnMaps(pointToMap["cadillac"], 0, '', 3);
	}
	/**
	 * deklarovani zazoomovani na jednotliv staty
	 */
	if ($.inArray("illinois",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(40.287907,-88.077393), 6);
	}
	if ($.inArray("missouri",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(38.376115,-92.504883), 6);
	}
	if ($.inArray("kansas",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(38.440395,-98.294678), 6);
	}
	if ($.inArray("oklahoma",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(35.56798,-96.877441), 6);
	}
	if ($.inArray("texas",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(34.912362,-101.447754), 6);
	}
	if ($.inArray("new-mexico",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(34.125448,-106.018066), 6);
	}
	if ($.inArray("arizona",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(34.234512,-111.708984), 6);
	}
	if ($.inArray("california",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(35.137879,-118.388672), 6);
	}
	if ($.inArray("nevada",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(39.402244,-116.71875), 6);
	}
	if ($.inArray("oregon",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(43.802819,-120.563965), 6);
	}
	if ($.inArray("washington",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(47.737353,-119.569702), 6);
	}
	if ($.inArray("hd-museum",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(43.0314,-87.9162), 7);
	}
	if ($.inArray("monument-valley",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(37.010556,-110.242778), 7);
	}
	if ($.inArray("grand-canyon",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(36.247257,-112.333777), 7);
	}
	if ($.inArray("death-valley",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(36.220134,-116.881684), 7);
	}
	if ($.inArray("las-vegas",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(36.114646,-115.172816), 7);
	}
	if ($.inArray("sequoia",arrayMapView)!=-1) {
		setMapCenterAndZoom (Array(36.581944,-118.751111), 7);
	}
	mgr.refresh();
}

/**
 * Zobrazení bodů na mapě
 * @param {Array} pointsArray - pole s body
 * @param {String} minZoom - zobrazení od zoomu
 * @param {String} maxZoom - zobrazení do zoomu
 * @param {Integer} iconStyleIndex - styl ikony
 */
function showPointOnMaps(pointsArray, minZoom, maxZoom, iconStyleIndex){
	for (x = 0; x < pointsArray.length; x++) {
		if (!pointsArray[x][3] && !iconStyleIndex){
			markerOptions = { icon: iconStyle[0] };
		}else{
			if (pointsArray[x][3]) {
				markerOptions = {icon: iconStyle[pointsArray[x][3]]};
			}else{
				markerOptions = {icon: iconStyle[iconStyleIndex]};
			}
		}
		if (pointsArray[x][4]) {
			minZoomView = pointsArray[x][4];
			maxZoomView = pointsArray[x][5];
		}else{
			minZoomView = minZoom;
			maxZoomView = maxZoom;
		}
		/* map.addOverlay(addMarker(pointsArray[x][0], pointsArray[x][1], pointsArray[x][2], markerOptions));*/
		if (maxZoomView) {
			mgr.addMarker(createMarker(pointsArray[x][0], pointsArray[x][1], pointsArray[x][2], markerOptions), minZoomView, maxZoomView);
		}else{
			mgr.addMarker(createMarker(pointsArray[x][0], pointsArray[x][1], pointsArray[x][2], markerOptions), minZoomView);
		}
	}
}

/**
 * Vytvoření bodu na mapě
 * @param {Integer} y - gps hodnota y
 * @param {Integer} x - gps hodnota x
 * @param {String} htmlText - text pro zobrazení v bublině po kliknutí na značku (s html tagy)
 * @param {Object} markerOptions - optiony pro bod na mapě (styl ikony,...)
 */
function createMarker(y, x, htmlText, markerOptions){
	var point = new GLatLng(y, x);
	var marker = new GMarker(point, markerOptions);
	marker.textHtml = htmlText;
	GEvent.addListener(marker, "click", function(){
		marker.openInfoWindowHtml(marker.textHtml);
	});
	return marker;
}

/**
 * Zacentrovani a zoomovani mapy
 * @param {Array} gpsPosition - pole s gps pozici pro zacentrovani mapy
 * @param {Integer} zoom - zoom mapy
 */
function setMapCenterAndZoom (gpsPosition, zoom){
	map.setCenter(new GLatLng(gpsPosition[0],gpsPosition[1]), zoom);
}

$(function(){
	if ($("#g-map").length > 0){
		var classForMap = $("#g-map").attr("class").split(" ");
		initializeMap(classForMap);
	}
})

