// JavaScript Document
var Noobie = {
	Topics: new Class({
		ls: null,
		interval: null,
		more: null,
		less: null,
    	initialize: function(){
			if (this.more = $E('li#topics_more')) {
				this.ls = this.more.getParent().getElements('li.hidden');
				this.more.addEvent('mouseover', this.handler_mouseover.bindWithEvent(this));
				this.more.addEvent('mouseout', this.handler_mouseout.bindWithEvent(this));
				this.more.addEvent('click', this.handler_moreclick.bindWithEvent(this));
			}
			if (this.less = $E('li#topics_less')) {
				this.less.addEvent('click', this.handler_click.bindWithEvent(this));
			}
	    },
		expand: function () {
			this.more.setStyle('display', 'none');
			this.less.setStyle('display', 'block');
			this.ls.each(function (item) {
				item.setStyle('display', 'block');
			});
		},
		collapse: function () {
			clearInterval(this.interval);
			this.more.setStyle('display', 'block');
			this.less.setStyle('display', 'none');
			this.ls.each(function (item) {
				item.setStyle('display', 'none');
			});			
		},
		handler_mouseover: function (e) {			
			this.interval = this.expand.delay(500, this);
		},
		handler_mouseout: function (e) {
			clearInterval(this.interval);
		},
		handler_moreclick: function (e) {
			clearInterval(this.interval);
			this.expand();
			e.stop();
			e.preventDefault();
		},
		handler_click: function (e) {			
			this.less.getElement('a').blur();
			this.collapse();
			e.stop();
			e.preventDefault();
		}
	}),
	
	InputFocus: new Class({
		text: null,
		input: null,
		initialize: function (elm) {
			this.input = $(elm);
			
			if (this.input == null || this.input == undefined) return false;
			
			this.text = this.input.value;
			this.input.addEvent('focus', this.handler_focus.bindWithEvent(this));
			this.input.addEvent('blur', this.handler_blur.bindWithEvent(this));
		},
		handler_focus: function (e) {
			if (this.input.value == this.text) this.input.value = "";
		},
		handler_blur: function (e) {
			if (this.input.value == "") this.input.value = this.text;
		}
	}),
	
	ExternalLinks: new Class({
		initialize: function () {
			$$('a').each( function (item) {
				if (item.getProperty('rel') == "external") item.setProperty('target', '_blank');
			});
		}
	}),
	
	FilterForm: new Class({
		initialize: function (el) {
			if ($(el)) {
				$(el).addEvent('submit', function (e) {
					this.setProperty('action', this.getProperty('action') + (this.getElement('select').getValue()));
				});
			}
		}
	}),
		
	Pages: new Class({
		initialize: function () {
			this.global();
			$ES('body').getProperty('class').each( function (item) {
				if (this[item]) this[item]();
			}.bind(this));

		},
		
		global: function () {
			new Noobie.ExternalLinks();
			new Noobie.Topics();
			new Noobie.InputFocus('searchfield');
			new Noobie.InputFocus('subscribefield');
            
            Cufon.replace('h1');
		},
		
		glossary: function () {
			new Noobie.FilterForm('form_filter');
		},
		
		faqs: function () {
			new Noobie.FilterForm('form_filter');	
		}
	})
}

window.addEvent('domready', function(){
	new Noobie.Pages();
});

