/**
 * @author toby
 */

 function MapList(map,points){
 	this.setListType("latest");
	this.setListTitle("");
	this.setListTitleElement("h3");
	this.setListElement("ol");
	this.setListElementId("recent-posts");
	this.setListHighlightId("recent-posts-current");
	this.setListWrapper("div");
	this.setListWrapperId("recent-posts-wrapper");
	this.map = map;
	this.points = points;
 }
 
 MapList.prototype.loadList = function(){
 	if(document.getElementById(this.getListWrapperId())){
		var wrapper = document.getElementById(this.getListWrapperId());
		while(wrapper.hasChildNodes()){
			wrapper.removeChild(wrapper.lastChild);
		}
	} else {
 		var wrapper = this.getListWrapper();
	}

 	wrapper.setAttribute("id",this.getListWrapperId());
 	
 	
	if(this.getListTitle()!=""){
		this.getListTitleElement().appendChild(this.getListTitle());
		wrapper.appendChild(this.getListTitleElement());
	}
	this.getListElement().setAttribute("id",this.getListElementId());
	wrapper.appendChild(this.getListElement());

	switch(this.getListType()){
		case "thumbsgallery":
			for(id in this.points){
				var listitem = this.createPointsListItem(this.points[id]);
				this.getListElement().appendChild(listitem);
			}
		break;
	}

	this.map.getWrapper().appendChild(wrapper);
}

MapList.prototype.createPointsListItem = function(point){
	this.listitem = this.createListItem(point);
	var point = point;
	var map = this.map;
	var list = this;
	this.listitem.focusPoint = function(){
		var toppost = document.getElementById(list.getListHighlightId());
		if(toppost)toppost.removeAttribute("id");
		this.setAttribute("id",list.getListHighlightId());
		switch(list.getListType()){
			case "thumbsgallery":
				map.map.setZoom(17);
				point.openwindow();
				return false;
			break;
		}
	}
	this.listitem.onmouseover = function(){
		changeCursor(this,"pointer");
	}
	this.listitem.onclick = this.listitem.focusPoint;
	return this.listitem;
}

MapList.prototype.createListItem = function(object){
	this.listitem = document.createElement("li");
	this.listitem.innerHTML = this.templates[this.getListType()].supplant(object);
	return this.listitem;
}

MapList.prototype.templates = {
	"thumbsgallery" : "<img src=\"http://static.flickr.com/{photo_server}/{photo_id}_{photo_secret}_s.jpg\" alt=\"{title}\" />",
	"categories" : "<a href='#'>{category}</a> ({total})"
}

MapList.prototype.setListType = function(listtype){
	this.listtype = listtype;
}

MapList.prototype.getListType = function(){
	return this.listtype;
}

MapList.prototype.setListTitle = function(listtitle){
	this.listtitle = document.createTextNode(listtitle);
}

MapList.prototype.getListTitle = function(){
	return this.listtitle;
}

MapList.prototype.setListTitleElement = function(listtitle_el){
	this.listtitle_el = document.createElement(listtitle_el);
}

MapList.prototype.getListTitleElement = function(){
	return this.listtitle_el;
}

MapList.prototype.setListElement = function(list_el){
	this.list_el = document.createElement(list_el);
}

MapList.prototype.getListElement = function(){
	return this.list_el;
}

MapList.prototype.setListElementId = function(listelementid){
	this.listelementid = listelementid
}

MapList.prototype.getListElementId = function(){
	return this.listelementid;
}

MapList.prototype.setListHighlightId = function(listhighlightid){
	this.listhighlightid = listhighlightid
}

MapList.prototype.getListHighlightId = function(){
	return this.listhighlightid;
}


MapList.prototype.setListWrapper = function(listwrapper){
	this.listwrapper = document.createElement(listwrapper);
}

MapList.prototype.getListWrapper = function(){
	return this.listwrapper;
}

MapList.prototype.setListWrapperId = function(listwrapperid){
	this.listwrapperid = listwrapperid
}

MapList.prototype.getListWrapperId = function(){
	return this.listwrapperid;
}