gbl.methods.push('Events');

var SetDate = new Class({
	
	// Format 2008-06-27 15:04:16
	
	Implements: Options,
	
	options : {

		targets : null,
		setter : null
		
	},
	
	initialize : function(options){
		
		this.setOptions(options);
		
		this.date = this.options.setter.get('value')
		this.meridiem
		this.update('minute','00');
		
		this.add_events();
		//this.update('hour','00');
		
		
	},

	add_events : function(){
		
		for (var i=0; i < this.options.targets.length; i++){
		
			var el = this.options.targets[i];
			var obj = this;
						
			if (el.get('title') == 'meridiem'){
			
				this.meridiem = el
			
			};
			
			el.addEvent('change',function(){
						
				obj.update(this.get('title'),this.get('value'));
			
			});
			
			el.fireEvent('change');
		};
	},
	
	update : function(type,value){
	
		switch (type){
		
			case 'year' :

				this.date = this.date.replace(/\d{4}[-]/,value+'-')			
			
			break;
		
			case 'month' :
			
				this.date = this.date.replace(/[-]\d{1,2}[-]/,'-'+value+'-');
				
			
			break;
		
			case 'day' :
			
				this.date = this.date.replace(/[-]\d{1,2}[\s]/,'-'+value+' ');
			
			break;
			
			case 'hour' :				
			
				if (this.meridiem) var meridiem = this.meridiem.get('value');
			
				var hour = value;
			
				if (meridiem == 'PM' && hour < 12){
					
					hour = parseInt(hour) + 12
									
				} else if (meridiem == 'AM' && hour >= 12){
					
					hour = parseInt(hour) - 12
				
				};
			
				this.date = this.date.replace(/[\s]\d{1,2}[:]/,' '+hour+':');
				
			break;
			
			case 'minute' :
			
				this.date = this.date.replace(/[:]\d{1,2}[:]/,':'+value+':');
		
			break;
		
			case 'meridiem' : 
			
				var hour = this.date.match(/[\s](\d{1,2})[:]/);
					
				hour = hour[1]
				
				if (value == 'PM' && hour <= 12){
					
					hour = parseInt(hour) + 12
									
				} else if (value == 'AM' && hour >= 12){
					
					hour = parseInt(hour) - 12
				
				};
				
				this.date = this.date.replace(/[\s]\d{1,2}[:]/,' '+hour+':');
			
			break;		
		};
		
		this.options.setter.set('value',this.date)
	
	}

});

var ShowEvent = new Class({
	 Implements: Options,
	
	options : {

		target : null,
		els : null
		
	},
	
	initialize : function(options){
		
		this.setOptions(options);
			
			
		this.add_events();
	//	this.add_containers();
		
	},
	
	add_events : function(){
		
		var t = this
		
		this.options.els.each(function(el,i){
		
			t.add_event(el);
							   
		});
				
	},
	
	add_event: function(el){
		
		var t = this
		
		el.addEvent('click',function(e){
		
			e.stop();
		
			t.get_event(this.get('rel'));
		
		})
		
	},
	
	add_containers : function(){
		var t = this;
		
		if (!this.els) this.els = {}
		
		this.els.cover = new Element('div',{ 'id' : 'cover' }).inject(this.options.target,'bottom');
		this.els.event_container = new Element('div',{ 'id' : 'event', 'class' : 'load_event' }).inject(this.els.cover,'after');
		
		/*this.els.cover.addEvent('click',function(){
		
			t.destroy();
		
		});*/
		
	
	},
	
	get_event : function(id){
		
		var t = this;
		
		var data = {
			'token' : id			
		}
		
		function request(){
		
			t.add_containers();
			
		}
						
		function success(response,xml){
		
			t.els.event_container.removeClass('load_event');
		
			t.els.event_container.set('html',response);
		
			t.els.event_container.getElement('a[name=close_event]').addEvent('click',function(e){
			
				e.stop();
				
				t.destroy();
			
			})
			
			
		}
		function failure(){
			
			alert('There was an error loading the event. Please try again');
			
			t.destroy();
		}

		var grab_event = new Request(
		{	url : '/utility/calendar/get_event',
			data : data,
			method : 'post',			
			onRequest : request,
			onSuccess : success,
			onFailure : failure
		}).send();
		
		
		
	},
	
	destroy : function(){
						
		$each(this.els,function(el){
		
			el.dispose();
		
		});
		
		delete this.els
		
	}
})

var ToggleEventList = new Class({
	 Implements: Options,
	
	options : {
		
		els : null
	
	},
	
	initialize : function(options){
		
		this.setOptions(options);
		
		var cookie = Cookie.read("active_events");
				
		this.active_events = cookie == null ? [] : cookie.split(',');	
	
		this.subscribe_to = [];
		
		this.add_events();
	},
	
	add_events : function(){
		
		var t = this
		
		this.options.els.each(function(el,i){
			
			el.store('pos',i.toString())
			t.subscribe_to.push(el.retrieve('pos'));
			t.check_open(el);		
			t.add_event(el);
			
		});	
		
	},
	
	add_event: function(el){
		
		var t = this
		
		el.addEvent('click',function(e){
		
			e.stop();
			t.toggle_events(this,this.get('name'));
		
		})
		
	},
	
	save_open_types : function(){
		
		var t = this
		
		this.active_events.each(function(arr,i){
		
			if (arr.length == 0)  t.active_events.splice(i, 1);
		
		});
		
		this.active_events.sort();
		
		Cookie.write('active_events', this.active_events.toString());
		
	},
	
	check_open : function(el){
		
		var pos = el.retrieve('pos');

		var is = this.subscribe_to.indexOf(pos)
		
		this.update_subscriptions();
		
		if (this.active_events.indexOf(pos) == -1 ) { return; }
		
		//el.addClass('disabled');
		this.toggle_events(el,el.get('name'));
		
		
	},
	
	update_subscriptions : function(){
		
		this.subscribe_to.sort();
		
		var url = this.options.els.length == this.subscribe_to.length ? '' : '?audience='+this.subscribe_to.toString()
		
		$('ical_subscribe').set('href','/calendar/ical'+url);
		$('rss_subscribe').set('href','/calendar/rss'+url);
	},
	
	toggle_events : function(ref,classname){
	
		var els = $('event_calendar').getElements('a[class~='+classname+']')
		
		for (var i = 0; els.length > i; i++){
		
			var el = els[i]
			
			if (el.hasClass('hide')){
							
				el.removeClass('hide');
			
			} else {
			
				el.addClass('hide');
			
			}
		}
		
		var ix = this.active_events.indexOf(ref.retrieve('pos'))
		var is = this.subscribe_to.indexOf(ref.retrieve('pos'))
		
		if (ref.hasClass('disabled'))
		{	ref.removeClass('disabled');
			
			console.log(is)
			if (ix > -1){ this.active_events.splice(ix,1) }
			if (is == -1){ this.subscribe_to.push(ref.retrieve('pos')) }
			
		} else {
			ref.addClass('disabled');
			
			if (ix == -1){ this.active_events.push(ref.retrieve('pos')) }
			if (is > -1){ this.subscribe_to.splice(is,1) }
		}		
		
		this.save_open_types();
		this.update_subscriptions();
		console.log(this.subscribe_to)
	}
})

var DuplicateEvent = new Class({
	 Implements: Options,
	
	options : {
		
		target : null,
		button : null
		
	},
	
	initialize : function(options){
		
		this.setOptions(options);
			
		this.add_event();
		
	},
	
	add_event : function(){
		
		var t = this
		
		this.options.button.addEvent('click',function(){
													  
			t.options.target.getElement('input[name=on_token]').dispose();
			t.options.target.getElement('input[name=on_next]').dispose();
			t.options.target.getElement('input[name=on_current]').dispose();
			
			t.options.target.submit();
			
		});
		
				
	}
})