// Metroseeq namespace
var mesq = mesq || {};



mesq.PlaceListManager = Class.create();
Object.extend(mesq.PlaceListManager.prototype, {
	
	// objects
	container: null,
	listContainer: null,
	placeLists: null,
	
	// data
	data:{},
	
	initialize: function(domContainer) {
		this.container = $(domContainer);
		this.listContainer = this.container.down('.container .content');
		
		this.placeLists = new Array();
		
		// add pre-rendered placeLists
		this.container.select('.placeList').each(function(el) {
			this.createPlaceListFromNode(el);
		}.bind(this));
		
		this.setup();
	},
	
	setup: function() {
		// behaviour
		if(this.container.down('a.add')) Event.observe(this.container.down('a.add'), 'click', this.addPlaceList.bind(this));
	},
	
	/**
	 * Adds from existing DOM-structure
	 */
	createPlaceListFromNode: function(node) {
		var placeList = new mesq.PlaceList(node);
		this.placeLists.push(placeList);
		
		Event.observe(placeList.container, 'PlaceList:remove', this.placeList_onRemove.bind(this));

		return placeList;
	},
	
	/**
	 * Creates on server via ajax,
	 * and inserts into DOM.
	 */
	addPlaceList: function(e, listTitle, callback) {
		// TODO ask for title
		new Ajax.Request(
			'placelist/add',
			{
				parameters: {Title: listTitle},
				onSuccess: this.addPlaceList_onSuccess.bind(this, callback),
				onFailure: window.app.ajaxErrorHandler
			}
		)
		
		if(e) { Event.stop(e); return false; }
	},
	
	addPlaceList_onSuccess: function(callback, response) {
		var data = response.responseText.evalJSON();
		this.listContainer.down('#PlaceListInner').insert(data.html);
		var newItem = this.listContainer.select('.placeList').last();
		
		this.createPlaceListFromNode(newItem);
		
		new Effect.Highlight(newItem);
		
		callback();
		
		this.container.fire('PlaceList:add');
	},
	
	placeList_onRemove: function(e) {
		this.placeLists = this.placeLists.without(e.memo.element);
		e.element = null;
	},
	
	addToPlaceList: function(listItem, placeID) {
		var list = this.getListByID(placeID);
		list.addPlace(listItem.data);
	},
	
	getListByID:function(id) {
		return this.placeLists.find(function(list) {
			return (list.getID() == id);
		});
	},
	
	getLists: function() {
		return this.placeLists;
	}
});



mesq.PlaceList = Class.create();
Object.extend(mesq.PlaceList.prototype, {
	
	// objects
	container: null,
	templateSub:null,
	places:null, // mesq.PlaceListItem
	
	// data
	data:null,
	
	// strings
	strDefaultTitle: 'New SeeQList',
	strReallyRemove: "Do you really want to delete this list?",
	
	initialize: function(domContainer) {
		this.container = $(domContainer);
		
		this.data = {};
		this.places = new Array();
		this.data.ID = Number(this.container.id.replace(/PlaceList/, ''));
		
		new Ajax.InPlaceEditor(
			this.container.down('h4'), 
			'placelist/ajaxedittitle/' + this.data.ID
		);
		
		// add pre-rendered placeLists
		var existingPlaces = this.container.select('.places li');
		if(existingPlaces.length) {
			existingPlaces.each(function(el) {
				this.createPlaceFromNode(el);
			}.bind(this));
		}
		
		window.app.myLightWindow.ss_refreshLinks();
		
		Event.observe(this.container.down('.removePlaceList'), 'click', this.remove.bind(this));
		
		Event.observe(this.container.down('.expandPlaceList'), 'click', this.toggle.bind(this));
		
		this.toggle();
	},
	
	createPlaceFromNode: function(node) {
		var place = new mesq.PlaceListItem(node, {}, this.getID());
		this.places.push(place);
		Event.observe(place.container, 'PlaceListItem:remove', this.placeListItem_onRemove.bind(this));
		
		this.refresh();
	},
	
	findPlaceByID: function(id) {
		return this.places.find(function(el) {
			return (el.data.ID == id);
		})
	},
	
	addPlace:function(data) {
		// don't add duplicates
		var existingPlace = this.findPlaceByID(data.ID);
		if(existingPlace) return false;
		
		new Ajax.Request(
			'placelist/addplace/' + this.getID(),
			{
				parameters: {PlaceID: data.ID},
				onSuccess: this.addPlace_onSuccess.bind(this, data),
				onFailure: window.app.ajaxErrorHandler
			}
		);		
	},
	
	addPlace_onSuccess: function(data, response) {
		var container = this.container.down('ul').appendChild(new Element('li'));
		var place = new mesq.PlaceListItem(container, data, this.getID());
		this.places.push(place);
		Event.observe(place.container, 'PlaceListItem:remove', this.placeListItem_onRemove.bind(this));
		
		this.refresh();
	},
	
	placeListItem_onRemove: function(e) {
		this.places = this.places.without(e.memo.element);
		
		this.refresh();
	},
	
	remove:function(e) {
		if(confirm(this.strReallyRemove)) {
			// only delete from server if we have an id
			if(Number(this.data.ID) > 0) {
				new Ajax.Request(
					'placelist/delete/',
					{
						parameters: {ID:this.data.ID},
						onSuccess: this.remove_onSuccess.bind(this),
						onFailure: window.app.ajaxErrorHandler
					} 
				)
			} else {
				this.removeItem_onSuccess(null);
			}
			
		}
		
		if(e) Event.stop(e);
		return false;
	},
	
	remove_onSuccess: function(response) {
		this.container.fire('PlaceList:remove', {element:this});
		this.container.remove();
		
		this.refresh();
		
		/*new Effect.Fade(this.container, {
			afterFinish: function(e) {
				this.container.fire('PlaceList:remove', {element:this});
				this.container.remove();
			}.bind(this)
		});
		*/
	},
	
	toggle: function(e) {
		this.container.down('.toggle').toggleClassName('expanded');
		this.container.down('.places').toggle();
		this.container.down('.toggle a').update((this.container.down('.places').visible() ? "collapse" : "expand" ));
		
		if(e) e.stop();
	},
	
	expand: function() {
		this.container.down('.toggle').removeClassName('expanded');
		this.container.down('.places').show();
		this.container.down('.toggle a').update("collapse");
	},
	
	collapse: function() {
		this.container.down('.toggle').addClassName('expanded');
		this.container.down('.places').hide();
		this.container.down('.toggle a').update("expand");
	},
	
	refresh: function() {
		this.container.down('.count span').update(this.places.length);
	},
	
	getTitle: function() {
		return this.container.down('.title').innerHTML;
	},
	
	getID: function() {
		return this.data.ID;
	}
});

mesq.PlaceListItem = Class.create();
Object.extend(mesq.PlaceListItem.prototype, {
	
	// objects
	container: null,
	data: null,
	
	// strings
	strReallyRemove: 'Do you really want to delete this place from the list?',
	
	initialize: function(domContainer, data, placeListID) {
		this.container = $(domContainer);
		this.data = data;
		this.placeListID = placeListID;
		
		// only create from data if no nodes are pre-rendered (e.g. loaded from server)
		if(!this.container.down('h5')) {
			this.templateSub = new Template(
				'<h5><a href="#{FullURL}" class="showPlaceListItem" target="_blank">#{title}</a> <small>(<a href="#" class="removePlaceListItem">remove</a>)</small></h5>' +
				'<div class="address">#{streetAddress}</div>'
			);
			
			this.container.insert(this.templateSub.evaluate(data));
		}
		
		if(this.data.ID) {
			this.container.id = 'PlaceListItem' + this.data.ID;			
		} else {
			this.data.ID = Number(this.container.id.replace(/PlaceListItem/, ''));
		}
		
		this.setup();
	},
	
	setup: function() {
		Event.observe(this.container.down('.removePlaceListItem'), 'click', this.remove.bind(this));
		
		// handled as normal website link
		//Event.observe(this.container.down('.showPlaceListItem'), 'click', this.showInMap.bind(this));
	},
	
	showInMap: function(e) {
		new Effect.ScrollTo('Map');
		
		window.app.map.showPlaceByID(this.getID());
		
		e.stop();
	},
	
	remove: function(e) {
		if(confirm(this.strReallyRemove)) {
			new Ajax.Request(
				'placelist/deleteplace/' + this.placeListID,
				{
					parameters: {PlaceID:this.getID()},
					onSuccess: this.remove_onSuccess.bind(this),
					onFailure: window.app.ajaxErrorHandler
				} 
			)
		}
		
		e.stop();
	},
	
	remove_onSuccess: function(response) {
		new Effect.Fade(this.container, {
			afterFinish: function(e) {
				this.container.fire('PlaceListItem:remove', {element:this});
			}.bind(this)
		});
	},
	
	getID: function() {
		return this.data.ID;
	}
});