// 
//  ((rpSavannah.js))
//  «rabidpraxis savannah photoblog»
//  
//  Updated by Kevin Webster on 12.17.09
//  -- rabidpraxis.com --
// 

var map; // define global map var for ie

(function() {
	var w = window.rp = {};
	var activeImgNum;
	var rp_cache = [];
	var allMarkers = [];
	var markerHash = [];
	var markerLen = 0;
	var oldMarker;
	var activeMarker;
	var pan=true;
	
	// ------ public functs --------------------------------------------------------
	w.createMarker = createMarker = function(id, age, latlong, type) {
		var img;
		if (type=="inactive") {
			img = getLogo(age);
		} else {
			img = getLogo(3);
		}

		if(latlong instanceof Array) latlong = new GLatLng(latlong[0], latlong[1])
		var marker = new GMarker(latlong, img);

		if(type=="inactive") { 
			marker.mpos = (allMarkers.push(marker) - 1); // Store position of marker in global array
			markerHash[id.toString()] = marker; // create hash lookup table for loading markers by id
			marker.id = id;
			attachListener(id, marker, latlong);
		}
		// cache old marker for switch
		if (type=="active") {
			activeMarker = marker;
			oldMarker = createMarker(id, age, latlong, 'inactive')
			activeMarker.child = oldMarker;
		}
		
		markerLen++;
		
		return marker;
	};
	
	w.getImageData = getImageData = function(id) {
		id = id.toString();
		if (id in rp_cache) {
			loadImage(rp_cache[id]);
		} else {
			$.ajax({
			  url: 'includes/rpImageService.php',
			  type: 'POST',
			  dataType: 'json',
			  data: {'data': id, 'operation': 'getImageData'},
				success: function(data, status) {
					rp_cache[id] = data;
					loadImage(data);
			 	}
			})
		}
		
	};	
	
	w.prevImg = function() {
		prevMarker = allMarkers[activeMarker.child.mpos-1];
		swapMarker(prevMarker);
		if (pan) {zoomHere(prevMarker.getLatLng())}
	};
	
	w.nextImg = function() {
		nextMarker = allMarkers[activeMarker.child.mpos+1];
		swapMarker(nextMarker);
		if (pan) {zoomHere(nextMarker.getLatLng())}
	};
	
	w.centerMap = function() {
		//get center point for active image
		zoomHere(activeMarker.getLatLng());
	};
	
	w.switchPan = function() {
		if(pan) {
			pan = false;				
			document.getElementById("nav_pan").className = 'inactive';	
		} else {
			pan = true;
			document.getElementById("nav_pan").className = 'fixed';	
		}
	};
	
	w.mapMoved = function(latmove) {
		console.log(latmove);
		console.log(activeMarker.getLatLng());
		if(latmove == activeMarker.getLatLng()){
		document.getElementById("nav_map").className = 'inactive';			
		} else {
		document.getElementById("nav_map").className = 'fixed';
		}	
	};
	
	w.focusImg = function(id) {
		marker = markerHash[id.toString()];
		swapMarker(marker);
		if (pan) {zoomHere(marker.getLatLng())}
	};
	
	// ------ private functs -------------------------------------------------------
	function checkImgStat() {
		// Determine if left & right button functions are still relevant
		if(activeMarker.child.mpos == markerLen-2) {
			document.getElementById("nav_right").className = 'inactive';
		} else {
			document.getElementById("nav_right").className = 'fixed';			
		}
		if (activeMarker.child.mpos == 0) {
			document.getElementById("nav_left").className = 'inactive';
		} else {
			document.getElementById("nav_left").className = 'fixed';
		}
	}
	function zoomHere(point) {
		window.setTimeout(function() {
		  map.panTo(point);
		}, 400);
	}
	function getLogo(type) {
		switch (type)
		{
			case 0: // New
				var logo = new GIcon();
				logo.image = "templates/rp/siteImages/marker_new.png";
				logo.iconSize = new GSize(12, 12);
				logo.iconAnchor = new GPoint(12, 12);
				logo.infoWindowAnchor = new GPoint(1, 1);
				return logo
				break;
			case 1: // Mid
				var logo = new GIcon();
				logo.image = "templates/rp/siteImages/marker_mid.png";
				logo.iconSize = new GSize(12, 12);
				logo.iconAnchor = new GPoint(12, 12);
				logo.infoWindowAnchor = new GPoint(1, 1);
				return logo
				break;
			case 2: // Old
				var logo = new GIcon();
				logo.image = "templates/rp/siteImages/marker_old.png";
				logo.iconSize = new GSize(12, 12);
				logo.iconAnchor = new GPoint(12, 12);
				logo.infoWindowAnchor = new GPoint(1, 1);
				return logo
				break;
			case 3: // Active
				var logo = new GIcon();
				logo.image = "templates/rp/siteImages/marker_active.png";
				logo.iconSize = new GSize(40, 40);
				logo.iconAnchor = new GPoint(26, 26);
				logo.infoWindowAnchor = new GPoint(1, 1);
				return logo
				break;
		}
	}
	function swapMarker(marker) {
		map.addOverlay(oldMarker);
		oldMarker = marker;
		map.removeOverlay(marker);
		activeMarker.setLatLng(marker.getLatLng());

		// Set reference to replaced marker
		activeMarker.child = oldMarker;

		getImageData(marker.id);
		checkImgStat();
	}
	function loadImage(data){
		$itxt = $('#infotext');
		$('#i_date', $itxt).text(data.capturedate);
		$('#i_title', $itxt).text(data.title);
		$('#i_time', $itxt).text(data.capturetime);
		$('#i_location', $itxt).text(data.imgloc);
		$('#i_desc', $itxt).text(data.imgdesc);
		$('#i_aperture', $itxt).text(data.aperture);
		$('#i_exposure', $itxt).text(data.exposure);
		$('#i_focal', $itxt).text(data.focal);
		$('#image').html('<img src="images/'+data.imgname+'" />');
	};
	function attachListener(id, marker, latlong){
		GEvent.addListener(marker, "click", function() {
			swapMarker(marker);
		});
	};
})()

$(document).ready(function() {
	// ------ map & listeners ------------------------------------------------------
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallZoomControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(32.07930196929541, -81.09223365783691), 14, G_SATELLITE_MAP)

	GEvent.addListener(map, "moveend", function() {
	  	var center = map.getCenter();
	  	var latLngStr = '(' + center.lat() + ', ' + center.lng() + ')';
		rp.mapMoved(latLngStr);
	});
	
	// ------ Load Markers ---------------------------------------------------------
	for (var i=0; i < mapData.length; i++) {
		if (i>0) {
			map.addOverlay(rp.createMarker(mapData[i][0], mapData[i][1], mapData[i][2], 'inactive'));
		} else {
			rp.getImageData(mapData[i][0]);
			map.addOverlay(rp.createMarker(mapData[i][0], mapData[i][1], mapData[i][2], 'active'));
		}
	}

	// ------ Setup click handlers -------------------------------------------------
	$('#nav_left').click(function() {
		if (!$(this).is('.inactive')) {rp.prevImg()}; 
		return false;
	});
	$('#nav_right').click(function() {
		if (!$(this).is('.inactive')) {rp.nextImg()}; 
		return false;
	});
	$('#nav_map').click(function() {
		rp.centerMap(); return false;
	});
	$('#nav_pan').click(function() {
		rp.switchPan(); return false;
	});
	$('.randimg').click(function() {
		$this = $(this);
		rp.focusImg($this.children().attr('id'))
		return false;
	});
	
});

