var locs = new Array();
var bounds = null;
var order =100;
var map = null;
var session = null;
var pageID = null;
var identifier = null;
//var mIcon = null;


$(document).ready(function(){

	$('form.artwalk').submit(function(me){
		//me.preventDefault();
		
		var error = '';
		if ($('input#name').val() == '') {
			error += 'Please enter your name.\n';		
		}
		var email = $('input#email').val();
		if (email == '' || email.indexOf('@') == -1) {
			error += 'Please enter a valid email address.\n';
		}
		if ($('input#postcode').val() == '') {
			error += 'Please enter your postcode.';
		}
		if (error == '') {
			return true;
		} else {
			alert(error);
		}
		
		return false;
	});
		
	
	
	
	 if (GBrowserIsCompatible()) {
		 $('div.e_name').each(function(){
			var t = $($(this));
			var latlong = t.find('p.geo').html().split(',');
			var lat = trim(latlong[0]);
			var lng = trim(latlong[1]);
			var en = t.find('a:eq(0)').html();
			var link = t.find('a:eq(1)').attr('href')
			//mIcon = new GIcon(G_DEFAULT_ICON, "/layout/images/markers/marker0.png");
			
			var point = new GLatLng(lat,lng);
			var	html = "<div style='width:200px;line-height:1.5em;margin:0px 0px 5px 0px;'><h4 style='margin-left:0px;font-size:1.2em;'><a href='"+link+"'>"+en+"</a></h4></div>";
			
			var marker = createMarker(point,en,html);
			var no = locs.length;
			var clickH = GEvent.addListener(marker, "mouseover", function() {
				venueClick(no);
				/*if (map!=null) {
					marker.openInfoWindowHtml(html);
				}*/
			});
			//alert(clickH);
			/*GEvent.addListener(marker, "mouseout", function() {
				if (map!=null) {
					map.closeInfoWindow();
				}
			});*/

			locs[locs.length] = new locObj(lng,lat,html,en,marker,t);
			
			//locs.push([t.find('span').html(),t.find('a').attr('href'),t.find('p').html().split(',')]);
		 
		 });
			 map = new GMap2(document.getElementById("artmap"));
			 map.addControl(new GSmallMapControl());
			 map.addControl(new GMapTypeControl());
			 map.setCenter(new GLatLng(0,0),0);
		 
		 
		 if($('div.mw').length > 0) {
			 $('div#waypoints').hide();
			 
			 bounds = new GLatLngBounds();
					//alert(maplocs.length);
			 for(i=0;i<locs.length;i++) {
						
				var temp = locs[i];
				drawMarker(temp);
			}
						
			parseWaypoints();		
			// ===== determine the zoom level from the bounds =====
			map.setZoom(map.getBoundsZoomLevel(bounds));
	
			 // ===== determine the centre from the bounds ======
			 var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
			 var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
			 map.setCenter(new GLatLng(clat,clng));	 
		} else {
			map.setZoom(14);
			map.setCenter(new GLatLng(51.523964788896365,-0.05690574645996094));
		}
		
		
		
		
	 }
	 
	 
	 
	 
	 
	 
	 
});



function venueClick(no) {
	if (map!=null) {
		locs[no].marker.openInfoWindowHtml(locs[no].html);
	}
}


function locObj(lng,lat,html,en,marker,domobj) {
	this.lng = lng;
	this.lat = lat;
	this.html = html;
	this.en = en;
	this.marker = marker;
	this.domobj = domobj;
	return this;
}


function drawMarker(temp) {
	if (temp.lat != '' && temp.lng != '' && temp.lat !=0 && temp.lng != 0) {
		var point = new GLatLng(temp.lat,temp.lng);
		var marker = temp.marker;
		map.addOverlay(marker);
		bounds.extend(point);
	}
}

function createMarker(point,name,html) {
        var marker = new GMarker(point, {title:name, zIndexProcess:importanceOrder});
//        var marker = new GMarker(point,mIcon);
//		marker.setImage("/layout/images/markers/marker0.png");
        order--;
        marker.importance = order;
        return marker;
}

function importanceOrder (marker,b) {
   return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
}


var rmks = new Object();
var addStatus = false;
var noMore = true;
var rpt = 0;
var polyline = null;
var pf = 'p_';
var pfd = 'd_';
var mno = 1;
var saveS = true;

function parseWaypoints() {

// here we need to see if the waypoint is at the same position as a venue... if yes hide the point
// and do not add the same actions
// we need to find a way to add the delete waypoint action to the venue popup
	

	var lastpoint = null;
	 if (GBrowserIsCompatible()) {
		 $('div.mw').each(function(){
			var t = $($(this));
			var html = t.html();
			t.html(mno+'. '+html);
			var latlong = t.find('p').html().split(',');
			var lat = trim(latlong[0]);
			var lng = trim(latlong[1]);
			//alert(typeof(lat)+','+typeof(locs[0].lat));
			
			var venueno = null;
			for (var i in locs) {
				if (locs[i].lat == lat && locs[i].lng == lng) {
					//alert('found: '+i);
					venueno = i;
					break;
				}
			}
			
			
			
			
			var text = t.find('span').html();
			var ptid = t.find('span').attr('class');
			ptid = ptid.substr(ptid.indexOf('_')+1,ptid.length);
			ptid = parseInt(ptid);
			lastpoint = ptid;
			//mIcon = new GIcon(G_DEFAULT_ICON, "/layout/images/markers/marker0.png");
			
			var point = new GLatLng(lat,lng);
			var temp = new GMarker(point);
			//var	html = "<div style='width:200px;line-height:1.5em;margin:0px 0px 5px 0px;'><h4 style='margin-left:0px;font-size:1.2em;'><a href='"+link+"'>"+en+"</a></h4></div>";
			rmks[pf+ptid] = temp;
			rmks[pfd+ptid] = text;
			map.addOverlay(rmks[pf+ptid]);
		 	rmks[pf+ptid].setImage('/layout/images/markers/marker'+mno+'.png');
		 	
		 	
		 	if (venueno != null) {
				rmks[pf+ptid].hide();	
				//var html = locs[venueno].domobj.html();
				//locs[venueno].domobj.html(mno+'. '+html);
				//modifyVenueHtml(ptid,venueno);						 	
		 	} else {
				//var pushpin = rmks[pf+ptid];
				//createRouteMarkerEvents(pushpin,ptid);
				
				
				var	html = "<div style='width:200px;line-height:1.5em;margin:0px 0px 5px 0px;'><p>"+text+"</p></div>";
				var marker = rmks[pf+ptid];
				GEvent.addListener(marker, "mouseover", function() {
					marker.openInfoWindowHtml(html);
				});
				/*GEvent.addListener(marker, "mouseout", function() {
						map.closeInfoWindow();
				});*/
				
				
				bounds.extend(point);
		 	}
		 	
		 	
		 	mno++;
		 	
		 	
	 	});
	}
	if (lastpoint != null) {
		rpt = lastpoint+1;
		drawPoly();	
	}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


function drawPoly() {
	//$('#mapaction').html('drawig poly');
	if (rpt > 1) {
		if (polyline != null) {
			map.removeOverlay(polyline);
		}
		var pl = new Array();
		for (var i in rmks) {
		//for(var i=0;i<rmarkers.length;i++) {
			//$('#mapaction').html('drawig poly '+rmks[i].getPoint());
			//alert(i.substr(0,1));
			if (i.substr(0,1) == 'p') {
				pl.push((rmks[i].getPoint()));
			}
		}
		polyline = new GPolyline(pl, "#FF0000", 3,.5);
		map.addOverlay(polyline);
	}
}


