//____________________________________________________________________
function XEvent(EVDBServer){
	
	var self				= this;
	var connected_bool		= false;
	var unsavedChanges_bool	= false;
	var eventLoading_bool	= false;
	var saving_bool			= false;
	var loaded_bool			= false;
	
	var broadcaster			= new Broadcaster(this);
	broadcaster.createEventType('define');
	broadcaster.createEventType('reset');
	broadcaster.createEventType('dataLoad');
	broadcaster.createEventType('startDataLoad');
	broadcaster.createEventType('connect');
	broadcaster.createEventType('disconnect');
	broadcaster.createEventType('beforeDataSave');
	broadcaster.createEventType('save');
	broadcaster.createEventType('pollChange');
	broadcaster.createEventType('changesDelta');
	broadcaster.createEventType('startSave');
	broadcaster.createEventType('successfulSave');

	this.addEventListener('define',handleDefine);
	this.addEventListener('reset',handleReset);
	this.addEventListener('connect',function(){handleConnectionStateChange(true);});
	this.addEventListener('disconnect',function(){handleConnectionStateChange(false);});
	
	var editAuth			= new Authorisation();
	this.venue				= new XVenue(EVDBServer,editAuth,self);
	this.venue.addEventListener('changesDelta',detectUnsavedChanges);
	this.venue.addEventListener('save',doPostSaveAnalysis);
	
	bindProperty('id','Event ID');
	this.id.defineAsNodeAttribute();
	bindProperty('owner','Event owner');
	bindProperty('title','Event name');
	bindProperty('description','Description');
	bindProperty('start_time','Start time');
	bindProperty('stop_time','Finish time');
	bindProperty('price','Price');
	bindProperty('venue_id','Venue ID');
	bindProperty('privacy','Privacy');
	
	this.startTimeProp		= new DateProperty(this.start_time,'Time',editAuth);
	this.stopTimeProp		= new DateProperty(this.stop_time,'End time',editAuth);
	this.startTimeProp.addEventListener('valueChange',function(){
		self.stopTimeProp.defineValue(self.startTimeProp.value);});
	
	
	this.eventsitesURL		= new DerivedProperty(this.id,'Webpage',function(){
		if(self.id.value){
			var eventURL_str	= window.hostDomain_str+'evdb/events/'+self.id.value;
		}else{
			eventURL_str	= 'event hasn\'t been created yet';
		}
		return eventURL_str;
	});
	
	var priceManager		= new CompoundPropertyManager(this.price,this,editAuth);
	this.flickr_tag			= new CompoundedProperty('flickr_tag','Flickr tag',priceManager,editAuth);
	this.map_zoom			= new CompoundedProperty('map_zoom','Map zoom',priceManager,editAuth);
	this.map_zoom.setDefaultValue(1);
	this.bannerImgURL		= new CompoundedProperty('bannerImgURL','Image URL',priceManager,editAuth);
	this.bannerImgURL.setDefaultValue(window.hostDomain_str+'banner/party.gif');
	
	
	this.flickrFeedURL		= new DerivedProperty(this.flickr_tag,'Flickr feed URL',function(){
		return 'http://api.flickr.com/services/feeds/photos_public.gne?&format=rss_200&tags='+self.flickr_tag.value;
	});
	this.flickrLinkURL		= new DerivedProperty(this.flickr_tag,'Photos at Flickr',function(){
		return 'http://api.flickr.com/photos/tags/'+self.flickr_tag.value;
	});
	
	this.privacy.setDefaultValue('3');
	
	this.flickrFeed			= new FlickrFeed(this.flickrFeedURL);
	
	editAuth.requireUserLogin(EVDBServer.user,this.owner);

	this.attendeeSet		= new AttendeeSet(this,EVDBServer);
	
	this.__defineGetter__('unsavedChanges',function(){return unsavedChanges_bool;});
	this.__defineGetter__('loaded',function(){return loaded_bool;});
	
	//----------------------------------------------------------------
	function detectUnsavedChanges(){
	
		var originalState_bool 	= unsavedChanges_bool;
		var changeObj			= new Object;
		changeObj.changes		= false;
		broadcaster.triggerEvent('pollChange',changeObj);
		
		// update state and trigger any necessary update event
		unsavedChanges_bool		= changeObj.changes;
		if(unsavedChanges_bool != originalState_bool){
			broadcaster.triggerEvent('changesDelta');
		}
	};
	//----------------------------------------------------------------
	function bindProperty(name_str,label_str){
		
		var property			= new Property(name_str,label_str,self,editAuth);
		self[name_str]			= property;
		property.addEventListener('valueChange',detectUnsavedChanges);
	};
	//----------------------------------------------------------------
	function handleConnectionStateChange(newState_bool){
		
		connected_bool		= newState_bool;
		connected_bool? editAuth.invalidate() : editAuth.validate();
	};
	//----------------------------------------------------------------
	function loadData(id_str){
		
		eventLoading_bool	= true;
		EVDBServer.event.load(handleEventLoad,id_str);
		broadcaster.triggerEvent('startDataLoad');
		broadcaster.triggerEvent('connect');
	};
	//----------------------------------------------------------------
	function handleEventLoad(data_xdoc){

		eventLoading_bool		= false;
		
		if(!data_xdoc.firstChild || data_xdoc.firstChild.tagName=='error'){
			var error_str		= data_xdoc.firstChild? data_xdoc.firstChild.textContent : 'Event could not be fetched';
			broadcaster.triggerEvent('disconnect');
			alert('Error: \n\n'+error_str);
			window.app.selectFrontPage();
		}else{
			broadcaster.triggerEvent('dataLoad',data_xdoc);
			self.venue.define(self.venue_id.value);
			broadcaster.triggerEvent('disconnect');
			loaded_bool			= true;
			document.title 		= self.title.value;
			self.title.addEventListener('valueChange',function(){document.title = self.title.value;});
		}
	};
	//----------------------------------------------------------------
	function doPostSaveAnalysis(){
		
		if(!(self.venue.saving || saving_bool)){
			broadcaster.triggerEvent('save');
			broadcaster.triggerEvent('successfulSave');
			broadcaster.triggerEvent('disconnect');
			detectUnsavedChanges();	
		}
	};
	//----------------------------------------------------------------
	function handleDefine(){
		
		editAuth.requireUserLogin(EVDBServer.user,self.owner);
	};
	//----------------------------------------------------------------
	function handleReset(){
		
		editAuth.removeUserLoginRequirement(self.owner);
	};
	//----------------------------------------------------------------
	function handleEventSaveLoad(data_xdoc){
		
		saving_bool			= false;
		
		// test for success
		var error_xml		= data_xdoc.firstChild.xPathQuery('//error','firstNode');
		if(error_xml){
			alert('Saving failed plese try again.\n\nError: '+error_xml.textContent);
		}
		
		doPostSaveAnalysis();
	};
	//----------------------------------------------------------------
	function handleSaveNewEventLoad(data_xdoc){
		
		saving_bool			= false;
		
		// test for success
		var error_xml		= data_xdoc.firstChild.xPathQuery('//error','firstNode');
		if(error_xml){
			alert('Saving failed plese try again.\n\nError: '+error_xml.textContent);
		}else{
			var id_str		= data_xdoc.firstChild.xPathQuery('id','string');
			self.id.defineValue(id_str);
			window.app.webpageLink_h.setAttribute('active','true');
			window.app.webpageLink_h.setAttribute('highlighted','true');
		}
		doPostSaveAnalysis();
		if(!error_xml){
			window.app.selectEditPanelDefaultPanel();
			alert('Please make a note of your event\'s webpage');
		}
	};
	//----------------------------------------------------------------
	function saveNewEvent(){
		
		var eventSaveParams_obj		= new Object();
		broadcaster.triggerEvent('beforeDataSave',eventSaveParams_obj);
		
		// send parameters to server
		EVDBServer.event.saveNewEvent(handleSaveNewEventLoad,eventSaveParams_obj);
	};
	//________________________________________________________________
	this.handleVenueCreation = function(venueId_str){
		
		
		if(venueId_str){
			this.venue_id.value		= venueId_str;
			saveNewEvent();
		}else{
			doPostSaveAnalysis();
		}
	};
	//________________________________________________________________
	this.saveChanges = function(){

		if(EVDBServer.user.loggedIn){
			broadcaster.triggerEvent('startSave');
			saving_bool			= true;
	
			if(this.id.value && this.venue.id.value){	
				
				if(!this.privacy.value){
					this.privacy.value		= 3;	
				}
				
				// get event save parameters
				var eventSaveParams_obj		= new Object();
				broadcaster.triggerEvent('beforeDataSave',eventSaveParams_obj);
				
				// send parameters to server
				broadcaster.triggerEvent('connect');
				EVDBServer.event.saveChanges(handleEventSaveLoad,eventSaveParams_obj);
				
				// save venue data
				this.venue.saveChanges();
			}else{
				this.venue.saveNewVenue();
			}
		}else{
			window.app.selectEditPanel();
			window.app.updateLoginDisplay(true);
		}
	};
	//________________________________________________________________
	this.bindSaveStateToAttribute = function(element_h,attribute_str){
		
		this.addEventListener('startSave',function(){element_h.setAttribute(attribute_str,true);});
		this.addEventListener('save',function(){element_h.setAttribute(attribute_str,false);});
	};
	//________________________________________________________________
	this.bindLoadStateToAttribute = function(element_h,attribute_str){
		
		this.addEventListener('startDataLoad',function(){element_h.setAttribute(attribute_str,true);});
		this.addEventListener('dataLoad',function(){element_h.setAttribute(attribute_str,false);});
	};
	//________________________________________________________________
	this.reset = function(){
		
		broadcaster.triggerEvent('reset');
	};
	//________________________________________________________________
	this.define = function(id_str){
		
		broadcaster.triggerEvent('reset');
		this.id.defineValue(id_str);
		broadcaster.triggerEvent('define');
		
		if(id_str){
			loadData(id_str);
		}
	};
};	//________________________________________________________________
//____________________________________________________________________