function LogGroup( lat, lng ) {
	this.lat = lat;
	this.lng = lng;
	this.logEntries = [];
	this.hasMarker = false;
}

LogGroup.prototype.add = function (logentry ) {
	this.logEntries.push(logentry);
}


LogGroup.prototype.createMarker = function () {
	//alert("We have " + this.logEntries.length + " points at " + this.lat + ", " + this.lng);
	if(this.logEntries.length > 1) {
	this.point = new GPoint(this.lng,this.lat);
	//this.icon = new this.Icon(this);

	var marker = new GMarker(this.point, iconMany);
	
	var markerhtml="<div class=\"multiple-window\"><h3>Number of blogs in this location: " + this.logEntries.length + "</h3><p><ul>";
	for(var n=0; n < this.logEntries.length; n++) {
		markerhtml = markerhtml + this.logEntries[n].getReducedHtml();
	}
	
	markerhtml = markerhtml + "</ul></p></div>";
	
	this.marker = marker;
	this.markerhtml = markerhtml;

	if(this.active !== false){
		GEvent.addListener(marker,'click',
			function(){
				marker.openInfoWindowHtml(markerhtml);
			}
		);
	}
	this.hasMarker = true;
	myMap.map.addOverlay(this.marker);	
	}
}

LogGroup.prototype.hide = function() {
	if(this.hasMarker){
		myMap.map.removeOverlay(this.marker);
		this.visible = false;
	}
}

LogGroup.prototype.show = function() {
	if(this.hasMarker){
		myMap.map.addOverlay(this.marker);
		this.visible = true;
	
	}
}


/*
name: Log()
description: instantiates Log object
params: none
returns: none
*/
function Log(){
	myMap.entries = [];
	myMap.factfiles = [];
	myMap.logGroups = []; // An array of lists
	
	
}





/*
name: Log.prototype.addLogEntry 
description: pushes each LogEntry object into the MyMap.entries array; creates marker for each log entry, defines which InfoWindow to open first
params: mylogentry,totalentries
returns: none
*/
Log.prototype.addLogEntry = function(mylogentry,totalentries, thisindex){
	
	var currLat, currLng;
	currLat = currLatLng[0];
	currLng = currLatLng[1];

	if(mylogentry.type=="log"){
	
		
		
		var found=false;
		var t=0;
		// Check to see if it's the same as any previous entries.
		while(! found && t < myMap.entries.length) {
			
			/*if(mylogentry.id == 2) {
					alert("blog entry 2 with long " + mylogentry.lng + " long of id " + t + " is " + myMap.entries[t].lng);
			}*/	
				
			if(thisindex != t && mylogentry.lat == myMap.entries[t].lat && mylogentry.lng == myMap.entries[t].lng){
				// It's the same as a preceding point.
			
				var match = myMap.entries[t];
				
				if(! match.hasGroup) {
					var index = myMap.logGroups.length;
					var aGroup = new LogGroup(mylogentry.lat, mylogentry.lng);
					myMap.logGroups[ index] = aGroup;
					match.logGroupIndex = index;
					match.hasGroup = true;
					myMap.logGroups[ match.logGroupIndex ].add(myMap.entries[t]); // Put in the first element.
					
				}
				
				myMap.logGroups[ match.logGroupIndex ].add( mylogentry );
				found = true;
				
			}
			
			t++;
		}
		
		myMap.entries.push(mylogentry);
		if(myMap.entries.length == totalentries){
			myMap.lastentry = mylogentry.id;
		}
		
		
		
	}
	
	
	
	else if(mylogentry.type=="factfile"){
		myMap.factfiles.push(mylogentry);
		if(myMap.factfiles.length == totalentries){
			myMap.lastfact = mylogentry.id;
		}
	}

	mylogentry.createMarker();

	if(initialiseToLatest && ! smallmap) {
		if( mylogentry.lat==currLat && mylogentry.lng==currLng ){
			myMap.recenterMap(mylogentry);
		}else if((myMap.entries.length == totalentries)&&(currLatLng === -1)){
			myMap.recenterMap(mylogentry);
		}
	}else if(smallmap && (myMap.entries.length == totalentries)&&(currLatLng === -1)) {
		myMap.goToPoint( myMap.entries[ totalentries-1].lat, myMap.entries[ totalentries-1].lng, 10);
	}

	
}


/*
name: LogEntry 
description: instantiates new LogEntry object
params: none
returns: none
*/
function LogEntry(){
	this.first = false;
	this.last = false;
}

/*
name: LogEntry.prototype.Icon  
description: instantiates new GIcon object and assigns an icon image depending on author and whether it is the most recent entry
params: logentry
returns: none
*/
LogEntry.prototype.Icon = function(logentry){
	this.icon = iconlog;
	if(logentry.last == true) {
		this.icon = iconlast;
	}
	if(logentry.icontype == "info"){
		this.icon = iconfact;
	}
	if(logentry.icontype == "danger") this.icon = icondanger;
	if(logentry.icontype == "journeystart" || logentry.icontype == "journeyend") {
		this.icon = iconjourneypoint;
	}

}


LogEntry.prototype.getReducedHtml = function() {
	var markerhtml = "";
	markerhtml = this.templates["multiplewindow"].supplant(this);
	return markerhtml;
}


/*
name: LogEntry.prototype.formatXHTML  
description: constructs the HTML to inject into each info window. Works in conjunction with LogEntry.prototype.templates in templates.js 
params: none
returns: markerhtml
*/
LogEntry.prototype.formatXHTML = function(){
	var markerhtml = "";
	if(this.type=="log" || this.type=="factfile"){
		this.firstlink = "";
		this.prevlink = "";
		this.nextlink = "";
		this.lastlink = "";
		this.imagelink = "";
		this.videolink = "";
		if(this.first){this.prev="X";}
		if(this.last){this.next="X";}
	
		this.firstlink = this.templates["firstlink"].supplant(this);
		this.prevlink = this.templates["prevlink"].supplant(this);
		this.nextlink = this.templates["nextlink"].supplant(this);
		this.lastlink = this.templates["lastlink"].supplant(this);
		
		
		if(this.first == true ) {
			this.firstlink = this.templates["firstlinkoff"].supplant(this);
			this.prevlink = this.templates["prevlinkoff"].supplant(this);
		}
		
		if(this.last == true ) {
			this.nextlink = this.templates["nextlinkoff"].supplant(this);
			this.lastlink = this.templates["lastlinkoff"].supplant(this);
		}


		if(this.video!=""){
			this.videoimage = this.templates["videoimg_link"].supplant(this);
		}
		else{
			this.videoimage = this.templates["videoimg_nolink"].supplant(this);
		}
		
		switch(this.type){
			case "log":
			if(this.image) {
				markerhtml = this.templates["logwindow"].supplant(this);
			}else{
				markerhtml = this.templates["logwindow_noimg"].supplant(this);
			}
			break;
			case "info":
			markerhtml = this.templates["factwindow"].supplant(this);
			break;			
			case "factfile":
			markerhtml = this.templates["factwindow"].supplant(this);
			break;
			case "danger":
			markerhtml = this.templates["factwindow"].supplant(this);
			break;
			case "journeystart":
			markerhtml = this.templates["factwindow"].supplant(this);
			break;
			case "journeyend":
			markerhtml = this.templates["factwindow"].supplant(this);
			break;			
		}

		markerhtml += this.templates["footer"].supplant(this);
	}

	return markerhtml;
}

/*
name: LogEntry.prototype.createMarker  
description: initiates a new GMarker object for each LogEntry object, adds onclick event to marker
params: none
returns: none
*/
LogEntry.prototype.createMarker = function(){
	this.visible = false;
	
	this.point = new GPoint(this.lng,this.lat);
	this.icon = new this.Icon(this);
	
	var marker = new GMarker(this.point,this.icon);
	var markerhtml = this.formatXHTML(this);
	
	this.marker = marker;
	this.markerhtml = markerhtml;
	

	if(this.active !== false && ! smallmap){
		GEvent.addListener(marker,'click',
			function(){
				marker.openInfoWindowHtml(markerhtml);
			}
		);
	}
	
	var link = this.href;
	
	if(smallmap && this.last) {
		GEvent.addListener(marker,'click',
			function(){
				document.location = link;
			}
		);
	}
	
	this.show();

}

LogEntry.prototype.createJourneyMarker = function(point, icon) {
	
	this.journeyMarker = new GMarker(point, icon);
	var m = this.journeyMarker;
	var comment = this.journeycomment;
	if(this.journeycomment != '') {
		GEvent.addListener(this.journeyMarker, 'click',
			function(){
					m.openInfoWindowHtml(comment);
				}
		);
	}
	return this.journeyMarker;
	
}


Log.prototype.turnOffEntries = function(type) {
	if(type=="log"){
		
		for(var i=0; i< myMap.entries.length; i++) {
			myMap.entries[i].hide();
		}	
		
		for(var i=0; i < myMap.logGroups.length; i++) {
			myMap.logGroups[i].hide();
		}

		
	}
	else if(type=="countryfacts"){
		for(var i=0; i< myMap.factfiles.length; i++) {
			myMap.factfiles[i].hide();
		}
	}
}

Log.prototype.turnOnEntries = function(type) {
	if(type=="log"){
		for(var i=0; i< myMap.entries.length; i++) {
			myMap.entries[i].show();
		}
		
		for(var i=0; i < myMap.logGroups.length; i++) {
			myMap.logGroups[i].show();
		}
		
	}
	else if(type=="countryfacts"){
		for(var i=0; i< myMap.factfiles.length; i++) {
			myMap.factfiles[i].show();
		}
	}

}

/*
name: LogEntry.prototype.show  
description: adds LogEntry marker to map
params: none
returns: none
*/
LogEntry.prototype.show = function(){
	if(!this.visible){
		myMap.map.addOverlay(this.marker);
		this.visible = true;
	}
}

/*
name: LogEntry.prototype.hide  
description: adds LogEntry marker to map
params: none
returns: none
*/
LogEntry.prototype.hide = function(){
	if(this.visible){
		myMap.map.removeOverlay(this.marker);
		this.visible = false;
	}
}


/*
name: LogEntry.prototype.showwindow  
description: opens Info Window relating to this LogEntry object
params: none
returns: none
*/
LogEntry.prototype.showwindow = function(){
	this.marker.openInfoWindowHtml(this.markerhtml);	
}


/*
name: LogEntry.prototype.templates()
description: defines HTML for insertion in a Log Entry map info window 
params: none
returns: none
*/


LogEntry.prototype.templates = {
	multiplewindow : '<li class="{mediatype}"><a href="{href}">{title}</a> - posted by {author} | {date}</li>',
	logwindow : '<div class="info-window"><h3 class="{mediatype}"><a href="{href}">{title}</a></h3><div class="info-window-content"><p>Posted by {author} | {date}</p><div id="kmFromSource">{kmfromsource}km from source</div><div><img src="{image}" alt="{image_alt}"/></div><div class="map-entry-text"><p>{summary}</p></div><div id="map-read-more"><a href="{href}" id="map-read-more">{href_title}</a></div></div>',
	logwindow_noimg : '<div class="info-window"><h3 class="{mediatype}"><a href="{href}">{title}</a></h3><div class="info-window-content"><p>Posted by {author} | {date}</p><div id="kmFromSource">{kmfromsource}km from source</div><div class="map-entry-text"><p>{summary}</p></div><div id="map-read-more"><a href="{href}" id="map-read-more">{href_title}</a></div></div>',
	footer: '<ul id="info-window-nav">{firstlink}{prevlink}{nextlink}{lastlink}</ul></div>',
	prevlink: '<li class="info-window-prev"><a href="#" onclick="myMap.travel(\'{type}\',\'prev\', {id});return false;">Prev</a></li>',
	prevlinkoff: '<li class="inactive">Prev</li>',
	nextlink: '<li class="info-window-next"><a href="#" onclick="myMap.travel(\'{type}\',\'next\', {id});return false;">Next</a></li>',
	nextlinkoff: '<li class="inactive info-window-next-off">Next</li>',
	firstlink: '<li class="info-window-prev"><a href="#" onclick="myMap.travel(\'{type}\',\'first\', {id});return false;">First</a></li>',
	firstlinkoff: '<li class="inactive info-window-prev-off">First</li>',
	lastlink: '<li class="info-window-next"><a href="#" onclick="myMap.travel(\'{type}\',\'last\');return false;">Last</a></li>',
	lastlinkoff: '<li class="inactive info-window-next-off">Last</li>',
	videoimg_link: '<div id="info-window-video"><a href="/mediaselector/check/longwaydown/video/{video}?size=16x9&amp;bgc=000000&amp;nbram=1&amp;bbram=1&amp;nbwm=1&amp;bbwm=1" onclick="window.open(this.href,this.target,\'status=no,scrollbars=yes,resizable=yes,width=409,height=269\');return false;">{image}</a></div>',
	videoimg_nolink: '<div id="info-window-video">{image}</div>',
	factwindow : '<div class="info-window"><h3><a href="{href}">{title}</a></h3><div class="info-window-content"><div class="map-entry-text">{summary}</div><div id="map-read-more"><a href="{href}">Read more</a></div></div></div>'
}
