window.addEvent('domready',function() {
	
	// = RATING RADIO GROUPS = =============================
	// Add bg colour change for rating-group radios
	var rating_groups = $$('form span.rating-group');
	$each(rating_groups, function(group) {
		var radios = group.getElements('input[type=radio]');
		var labels = group.getElements('label');
		$each(radios, function(radio,i) {
			if(radio.get('checked')) {
				labels[i].addClass('set');	
			}
			// hover state for ie6
			if(Browser.Engine.trident4) {
				labels[i].addEvent('mouseenter',function() {
					this.addClass('hover');
				});
				labels[i].addEvent('mouseleave',function() {
					this.removeClass('hover');
				});
			}
			
			if(Browser.Engine.trident) {
				// IE (any)
				radio.addEvent('click', function() {
					if(radio.get('checked')) {
						$each(labels,function(l){ l.removeClass('set'); });
						labels[i].addClass('set');
					}
				});
			}else{
				// Any other browser
				radio.addEvent('change', function() {
					$each(labels,function(l){ l.removeClass('set'); });
					labels[i].addClass('set');
				});
			}
		});
	});
	
	/* = DELETE CHECKBOXES in tables = */
	var delete_boxes = $$('td.delete input[type=checkbox]');
	$each(delete_boxes, function(checkbox){
		var tr = checkbox.getParent();
		while(tr.get('tag')!='tr') {
			tr = tr.getParent();
		}
		
		if(checkbox.get('checked')) {
			tr.addClass('delete');
		}
				
		if(Browser.Engine.trident) {
			// IE (any)
			checkbox.addEvent('click', function(event) {
				if(checkbox.get('checked')) {
					tr.addClass('delete');
				}else{
					tr.removeClass('delete');
				}
			});
		}else{
			// Any other browser
			checkbox.addEvent('change', function(event) {
				if(checkbox.get('checked')) {
					tr.addClass('delete');
				}else{
					tr.removeClass('delete');
				}
			});
		}
	});
	
	// = Word Countdown Textareas = =========================
	var word_limit_tags = $$('li p.word-limit');
	$each(word_limit_tags, function(p) {
		new Word_countdown(p);
	});
});

// = Word Countdown = =======================================
// Add countdown based on specified limit
// requires the following markup inside of li.item wrapper
// the textarea is wrapped in
// <p class="word-limit">[whatever str]<strong>#</strong>[whatever str]</p>
// NB: The limit number is taken from the strong tag which must contain a number 

var Word_countdown = new Class({
	initialize: function(p) {
		this.p = p;
		this.textarea = this.p.getParent().getElement('textarea');
		this.limit = p.getElement('strong');
		if(!$chk(this.limit) && !$chk(this.textarea)) return;
		
		this.prevent_typing = false;
		
		this.limit = this.limit.get('text').toInt();
		this.remaining = this.limit;
		this.el_remaining = new Element('span').addClass('remaining').inject(this.p);
		this.count_words();
		this.update_remaining();
		
		this.textarea.store('bg',this.textarea.getStyle('background-color'));
		this.textarea.addEvent('keyup', function(event) {
			this.count_words();
			this.update_remaining();
		}.bind(this));
		
		this.textarea.addEvent('keydown', function(event) {
			event = new Event(event);
			// Prevent any typing except for usefull kes to remove extra words:
			//  backspace
			//  ctr+a (for selecting all)
			//  arrows (shift+[arrow] will also return true)
			//  ctr+c / ctr+x
			var allowed = (event.key=='backspace') || (event.key=='a' && event.control) || (event.key=='left') || (event.key=='right') || (event.key=='up') || (event.key=='down') || (event.key=='c' && event.control) || (event.key=='x' && event.control);
			if(this.prevent_typing && !allowed) {
				event.stop();
			}
		}.bind(this));
	},
	
	update_remaining: function() {
		//this.el_remaining.set('text', this.remaining+'/'+this.limit+' remaining');
		this.el_remaining.set('html','<strong>'+this.word_count+'</strong> &bull; '+this.remaining+'/'+this.limit+' remaining');
		// NB: 	have to also check the if last character is a 
		//		space, tab or new line otherwise it will prevent you typing
		//		before you finnished typing the last word because it will
		//		think the word is complete after more than 2 chars
	
		if(
		   (this.word_count > this.limit)
		   || (this.remaining==0 && this.value.test(/[ \t\n]$/))
		) {
			if(!this.prevent_typing) {
				this.textarea.highlight('#BFE1FF', this.textarea.retrieve('bg'));
				this.el_remaining.addClass('maxed-reached');
				if(this.word_count > this.limit) {
					this.el_remaining.addClass('maxed-over');
				}
			}
			this.prevent_typing = true;
		}else{
			this.el_remaining.removeClass('maxed-reached');
			this.el_remaining.removeClass('maxed-over');
			this.prevent_typing = false;
		}
	},
	
	count_words: function() {
		this.value = this.textarea.get('value');
		this.words = this.value.clean().split(' ');
		this.word_count = 0;
		$each(this.words, function(word){
			if(this.valid_word(word)) {
				this.word_count++;	
			}
		},this);
		this.remaining = this.limit - this.word_count;
		
		if(this.remaining<0) this.remaining = 0;
	},
	
	valid_word: function(word) {
		if(word.length==1) {
			// If theres only 1 character
			// valid if: 0-9, a, i, &
			return word.toLowerCase().test(/[0-9ai\&]/);
		}else{
			
			return word.test(/\w/);
		}
		return true;
	}
});