$(function(){
var HTTP_PATH = "http://www.stoprapenow.org/get-cross/";
// stoprapenow map object
window.SRN.Map = {
	countries:null,
	markers: [],
	totalm: 0,
	countm: 0,
	init: function(countries) {
		this.countries = countries;
		this.totalm = countries.length;
		this.countm = 0;
		
		try {
			this.loadMap();
		} catch (err) {
			//console.dir(err);
			//alert("Oops, there is an error in the application.\nDon't worry! Our support has been notified.\n\n Please try again later");
		}
	},
	loadMap:function(){
		var tilelayers = [new GTileLayer(new GCopyrightCollection("stoprapenow.org"),1,1)];
		tilelayers[0].getCopyright = function(point,zoom) {
			return {prefix:"", copyrightTexts:["stoprapenow.org"]};
		};
		tilelayers[0].getTileUrl = function (point, zoom) {
				return HTTP_PATH + "images/map/map_r" + (point.y+1) + "_c" + (point.x+1) + ".png";
		};
		tilelayers[0].isPng = function() {return true;}; 

		var custommap = new GMapType(tilelayers, G_NORMAL_MAP.getProjection(), "Mercator", {errorMessage: "No Data Available", alt:"The usual projection"});
		var map = this.GMap = new GMap2(document.getElementById("map"), {mapTypes:[custommap]});
		
		GEvent.addListener(map, 'load', function(){
			//console.log("Map loaded!");
			var geocoder = null;
			var icon = new GIcon();
			// Set icon details
			icon.image = HTTP_PATH + "images/map/marker.png";
			icon.iconSize = new GSize(17, 17);
			icon.iconAnchor = new GPoint(10, 10);
			icon.infoWindowAnchor = new GPoint(5, 0);
			SRN.Map.loadPoint(map, new GClientGeocoder(), icon, 0);
		});
		map.setCenter(new GLatLng(15.961329081596647, 7.03125), 2, custommap);
		
	},
	loadPoint: function(map, geocoder, icon, index){
		var country = SRN.Map.countries[index];
		geocoder.getLatLng(country.Country, function(point){
		if (point) {
			SRN.Map.countm++;
			var marker = new GMarker(point, {icon: icon});
			/*
			GEvent.addListener(marker, 'click', function(){ 
	            marker.openExtInfoWindow(
	              map,
	              "srn_window",
	              '<div class="text"><strong>Wait...</strong></div>', 
	              {
					beakOffset: 3,
					width:200,
					height:200
				  }
	            );
				SRN.Map.loadCycle(country.Country);
	          });
			*/
			GEvent.addListener(marker, 'mouseover', function(){
				map.closeExtInfoWindow(); 
				/*
				marker.openExtInfoWindow(
						map,
						"srn_window",
						'<div class="text"><strong>' + country.Country + '</strong><br /><span>' + 
						 country.total + ' photo' + (parseInt(country.total, 10) > 1 ? 's' : '')+"</span></div>", 
						{
							beakOffset: 3,
							width:150
					  	}
				);
				*/
				marker.openExtInfoWindow(
	              map,
	              "srn_window",
	              '<div class="text"><strong>Wait...<br/><br/><span style="color:#6C6C6C">'+ country.Country +'<br/>' + country.total + ' photo' + (parseInt(country.total, 10) > 1 ? 's' : '') + '</span></strong></div>', 
	              {
					beakOffset: 3,
					width:200,
					height:200
				  }
	            );
				SRN.Map.loadCycle(country.Country);
			});
			SRN.Map.markers.push(marker);
		} 
		var c = index + 1;
		$("div#loading span").text("Loading item " + c + " of " + SRN.Map.countries.length)
		if (c < SRN.Map.countries.length) {
		// if (c < 10) {
			SRN.Map.loadPoint(map, geocoder, icon,c);
		} else {
			for (i = 0; i < SRN.Map.markers.length; i++) {
				map.addOverlay(SRN.Map.markers[i]);
			}
			$("div#loading").css("display", "none");
			try {
				delete SRN.Map.markers;
			} catch (err) {}
			return false;
		}

		});
	},
	loadCycle:function(country){
		$("#srn_window_contents").load("newhome/window.php", {"country": country}, SRN.Map.initCycle);
	},
	initCycle:function(){
		// change the current photo index
		var changePagination = function(curr, next, opts){
			$("#pag").text((opts.currSlide+1) + " of " + opts.slideCount + (opts.slideCount > 1 ? " photos" : " photo"));
		}
		
		// creates the jquer cycle on the gallery
		$("#thumb_slide").cycle({
			fx:'scrollHorz',
			speed:'fast',
			timeout:0,
			next:'#thumb_right',
			prev:'#thumb_left',
			after:changePagination
		}).css("width", "170px").css("height", "170px").css("margin", "10px");
		
		// resize the window
		SRN.Map.GMap.getExtInfoWindow().resize();
		
		var shadowGallery = [];
		
		$("a.thumb_link").each(function(index, elem){
			
			shadowGallery.push({
				gallery:"GetCross",
				content: $(this).attr("href"),
				player:'img',
				options:{
					description:$(this).attr("name")
				}
			})
			
			$(this).click(function(){
				Shadowbox.open(shadowGallery, index)
				return false;
			})
		})
		
		Shadowbox.init({
			skipSetup: true,
			adapter: 'jquery',
			players:['iframe','img']
		});
		
		
	}
}
});