
/* included from includes/tagger.js *********************************************/
var tagger = new Class({
	
	'Implements': [Options, Events],
	
	'options': {
		'getXhr': {
			'url':		'./',
			'method':	'get',
			'chain':	'cancel'
		},
		'setXhr': {
			'url':	'./'
		},
		'applyTo': 		'a.add-tags',
		'autoCompleteClass': 'autocomplete',
		'ajaxSet': true,
		'showHide': true
	},
	
	'initialize': function(wrapper, form, field, willBeAdded, tagSuggestions, options){
		
		this.setOptions(options);
		this.wrapper = wrapper;
		this.form = form;
		this.field = field;
		this.willBeAdded = willBeAdded;
		this.tagSuggestions = tagSuggestions;
		this.toAdd = [];
		
		this.xhr = {};
		this.xhr.get = new Request.JSON(this.options.getXhr);
		
		this.xhr.get.addEvent('complete', function(response){
			this.tagSuggestions.empty();
			if(response)
				response.each(function(tag){
					this.addToSuggestions(tag);
				}, this);
		}.bind(this));
		
		this.xhr.set = new Request(this.options.setXhr);	
		
		if(this.options.showHide)
			this.wrapper.setStyle('display', 'none');
		this.willBeAdded.setStyle('display', 'none');
				
		if(this.options.applyTo);
			this.addTaggerTriggers();
			
		this.addFormEvents();
		
	},
	
	addTaggerTriggers: function(){
		$$(this.options.applyTo).addEvent('click', function(event){
			var event = new Event(event).stop();
			this.currentPosition = event.target.getPosition();
			this.currentPost = event.target.get('id').replace(/[^\d]+/, '');
			this.showTagger();
		}.bind(this));
	},
	
	showTagger: function(){
		
		if (this.options.showHide) {
			this.wrapper.setStyles({
				'display': '',
				'position': 'absolute',
				'left': this.currentPosition.x,
				'top': this.currentPosition.y
			});
			
			this.field.focus();
		}
	},
	
	hideTagger: function(){
		
		if (this.options.showHide) {
			this.wrapper.setStyle('display', 'none');
			this.willBeAdded.setStyle('display', 'none');
			this.willBeAdded.getElements('a').destroy();
			this.toAdd = [];
			this.tagSuggestions.empty();
			this.field.set('value', '');
		}
		
	},
	
	addFormEvents: function(){
		
		this.form.addEvent('submit', function(event){
			new Event(event).stop();
			if (this.options.ajaxSet) {
				this.xhr.set.options.data = {
					'post': this.currentPost,
					'tags-to-add': this.getTagsToAdd()
				}
				this.xhr.set.addEvent('success', function(response){
					$('tags-' + this.currentPost).set('html', response).highlight();
					this.hideTagger();
				}
.bind(this));
				this.xhr.set.send();
			}
			else{
				this.field.removeEvents();
				this.field.set('value', this.getTagsToAdd());
				this.field.setStyle('display', 'none');
				this.form.submit();
			}
		}.bind(this));
		
		this.field.set('autocomplete', 'off');
		
		this.field.addEvent('keypress', function(event){
			if(event.key == 'tab'){
				var autocomplete = this.tagSuggestions.getElement('.autocomplete');
				if ($chk(autocomplete)) {
					new Event(event).stop();
					this.addToWillBeAdded(autocomplete);
					this.field.set('value', '');
					this.field.fireEvent('keyup');
				}
			}
			if(event.key == 'esc'){
				this.hideTagger();
			}
		}.bind(this));
		
		this.field.addEvent('keyup', function(event){
			
			if(this.field.get('value') == '')
				this.currentValue = null;
			
			if($chk(this.currentValue)){
				if (this.field.get('value').indexOf(',') > 0) {
					var value = this.field.get('value').split(',')[0];
					this.addToWillBeAdded(this.makeAnchor(value));
					this.field.set('value', '');
					this.field.fireEvent('keyup');
					return;
				}
				this.filterSimilarTags();
				return;
			}
			
			if (this.xhr.get.running) 
				this.xhr.get.cancel();
			
			this.currentValue = this.field.get('value')[0];
			
			if ($chk(this.currentValue)) 
				this.xhr.get.send('tag=' + this.currentValue);
			else
				this.tagSuggestions.empty();
				
		}.bind(this));
		
	},
	
	makeAnchor: function(tag){
		
		var e = new Element('a', {
			'href': './',
			'html': tag,
			'class': ['red','pink','green', 'blue', 'orange', 'yellow', 'teal'].getRandom()
		});
		
		return e;
	},
	
	addToSuggestions: function(tag){
		
		if(this.toAdd.contains(tag)){
			this.toAdd.erase(tag);
			return false;
		}

		var e = this.makeAnchor(tag);

		e.addEvent('click', function(event){
			new Event(event).stop();
			this.addToWillBeAdded(e);
		}.bind(this));
		
		this.tagSuggestions.grab(e);
		
	},
	
	addToWillBeAdded: function(e){
		
		if(this.toAdd.contains(e.get('text')))
			return false;
		this.toAdd.push(e.get('text'));
		this.willBeAdded.setStyle('display', '');
		this.willBeAdded.grab(e);
		e.removeEvent('click');
		e.addEvent('click', function(event){
			new Event(event).stop();
			this.addToSuggestions(e.get('text'));
			e.destroy();
			if(!this.willBeAdded.getElement('a'))
				this.willBeAdded.setStyle('display', 'none');
		}.bind(this));
		
	},
	
	getTagsToAdd: function(){
		
		this.willBeAdded.getElements('a').each(function(e){
			var tag = e.get('text')
			if(!this.toAdd.contains(tag))
				this.toAdd.push(tag);
		}, this);
		
		var inField = this.field.get('value');
		if(!this.toAdd.contains(inField))
			this.toAdd.push(inField);
		
		return this.toAdd.join(',');
		
	},
	
	filterSimilarTags: function(){
		
		this.tagSuggestions.getElements('a').each(function(e){
			if (e.get('text').indexOf(this.field.get('value')) == 0) {
				e.setStyle('display', '');
				e.removeClass('hidden');
			}
			else {
				e.setStyle('display', 'none');
				e.addClass('hidden');
			}
			
			e.removeClass('autocomplete');
			
		}, this);
		
		var autocomplete = this.tagSuggestions.getElement('a:not(a.hidden)');
		if(autocomplete)
			autocomplete.addClass('autocomplete');
		
	}
	
});
/* end of includes/tagger.js ****************************************************/

/* included from includes/base.js ***********************************************/
var party = {

    'letsGetThisStarted': function(){
	if($$('.add-tags'))
	    party.tagger = new tagger(
		$('add-tags'),
		$('add-tags-form'),
		$('tags-to-add'),
		$('tags-will-add'),
		$('tag-suggestions')
	    );
	if ($('add-new-image')) {
	    // party.tagger = new tagger($('add-new-image'), $('image-upload'), $('image_tags'), $('tags-will-add'), $('tag-suggestions'), {
	    // 	'ajaxSet': false,
	    // 	'showHide': false
	    // });

	    // $('image-upload').addEvent('submit', function(){

	    // 	$(document.body).setStyle('background', 'url('+$('bunchie').get('src')+') #000');

	    // });

	}
	party.lights();
//	party.recentComments();
    },

    'recentComments': function(){
	var lis = $$('#recent-comments li');
	if(lis){
	    lis.addEvent('click', function(){
		window.location = this.getElement('a').get('href');
	    });
	}

    },

    'lights': function(){
	party.mp = new multiBox('mb', {
	    'overlay': new overlay()
	});
    },

}
window.addEvent('domready', party.letsGetThisStarted);

/* end of includes/base.js ******************************************************/

/* had some help from trickyInc: http://code.google.com/p/trickyinc/ ************/

/* Generated in 0.009458065032959 seconds. **************************************/
