var common = {
	init:function(){
		
	}
}

var produkt = {
	init:function(){
		$(".pm_sub").click(produkt.showSub);
	},
	
	showSub:function(){
		var id = $(this).attr("rel").split("_")[2];
		//alert($("." + id).length);
		if ($("." + id).length >= 1) {
			$("#" + $(this).attr("rel")).slideToggle();
			
			return false;
		}
	}
}

var karta = {
	init: function(){
		//karta.search();
		$(".addrLink").click( karta.clicky );
	},
	
	initList:function(){
		$(".showMap").click( karta.showMapLight );
	},
	
	showMapLight:function(){
		
		$("body").append("<div id='lightbox_bg'></div>");
		$("#lightbox_bg").click(function(){
			$("#lightbox_bg").remove();
			$("#mapmap2").remove();
		});
		
		
		$("#lightbox_bg").css("width", $("body").innerWidth());
		$("#lightbox_bg").css("height", $("body").innerHeight());
		
		$("body").append("<div id='mapmap2'><div id='mapmap_map'></div> <a href='#' id='closeMap'>Stäng karta</a></div>");
		$("#mapmap2").css("left",  ($("body").innerWidth()/2 - 200) + "px");
		
		$("#closeMap").click(function(){
			$("#lightbox_bg").remove();
			$("#mapmap2").remove();
			return false;
		});
		
		karta.search($(this).attr("rel"), 2);
		
		return false;
	},
	
	clicky:function(){
		$(".map_selected").removeClass("map_selected");
		$(this).parent().addClass("map_selected");
		//alert($(this).attr("rel"));
		karta.search($(this).attr("rel"), 1);
		
		return false;
	},
	
	search: function(address, divtag){
		
		if (divtag == 1) {
			var map = new GMap(document.getElementById("mapmap"));
		}else{
			var map = new GMap(document.getElementById("mapmap_map"));
		}
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(20, 0), 2);
		
		// ====== Create a Client Geocoder ======
		var geo = new GClientGeocoder();
		
		// ====== Array for decoding the failure codes ======
		var reasons = [];
		reasons[G_GEO_SUCCESS] = "Success";
		reasons[G_GEO_MISSING_ADDRESS] = "Missing Address: The address was either missing or had no value.";
		reasons[G_GEO_UNKNOWN_ADDRESS] = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
		reasons[G_GEO_UNAVAILABLE_ADDRESS] = "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
		reasons[G_GEO_BAD_KEY] = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
		reasons[G_GEO_TOO_MANY_QUERIES] = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
		reasons[G_GEO_SERVER_ERROR] = "Server error: The geocoding request could not be successfully processed.";
		
		// ====== Geocoding ======
		//function showAddress(){
			// var search = document.getElementById("search").value;
			var search = address;
			// ====== Perform the Geocoding ======        
			geo.getLocations(search, function(result){
				// If that was successful
				if (result.Status.code == G_GEO_SUCCESS) {
					// How many resuts were found
					//alert("Found " + result.Placemark.length + " results");
					// Loop through the results, placing markers
					for (var i = 0; i < result.Placemark.length; i++) {
						var p = result.Placemark[i].Point.coordinates;
						var marker = new GMarker(new GLatLng(p[1], p[0]));
						//document.getElementById("message").innerHTML += "<br>" + (i + 1) + ": " + result.Placemark[i].address + marker.getPoint();
						map.addOverlay(marker);
					}
					// centre the map on the first result
					var p = result.Placemark[0].Point.coordinates;
					// ===== Look for the bounding box of the first result =====
					var N = result.Placemark[0].ExtendedData.LatLonBox.north;
					var S = result.Placemark[0].ExtendedData.LatLonBox.south;
					var E = result.Placemark[0].ExtendedData.LatLonBox.east;
					var W = result.Placemark[0].ExtendedData.LatLonBox.west;
					var bounds = new GLatLngBounds(new GLatLng(S, W), new GLatLng(N, E));
					// Choose a zoom level that fits
					var zoom = map.getBoundsZoomLevel(bounds);
					
					map.setCenter(bounds.getCenter(), zoom);
					
					//var points = [new GLatLng(N, W), new GLatLng(N, E), new GLatLng(S, E), new GLatLng(S, W), new GLatLng(N, W)];
					//map.addOverlay(new GPolyline(points));
					
				}
				// ====== Decode the error status ======
				else {
					var reason = "Code " + result.Status.code;
					if (reasons[result.Status.code]) {
						reason = reasons[result.Status.code]
					}
					alert('Could not find "' + search + '" ' + reason);
				}
			});
		//}
		
		
	// display a warning if the browser was not compatible
	
	
	}
}

window.onload = common.init;