var Beanstalk = {
	
	init: function(){

		if (typeof(setupZoom) != 'undefined') {
			setupZoom();
		}

		Beanstalk.releaseNotes();
		Beanstalk.apiScroll();
		Beanstalk.apiOutput();
		
		var tooltips = new Tips($$('abbr[title]'), {
			className: 'tooltip',
			fixed: true,
			text: function(tip){
				return tip.get('title');
			}
		});
		
		$$('.code_container.response').addEvent('click', function(){
			this.addClass('expand');
		});
	},
	
	releaseNotes: function(){
		if (!exists($('release-notes'))) return;
		
		$('release-notes').getElements('article:last-child, article ul:last-child, article ul li:last-child').addClass('last');
	},
	
	apiScroll: function(){
		if (!exists($('api'))) return;

		var position = $('sidebar').getStyle('top').toInt();

		if (window.getSize().y > ($('sidebar').getSize().y + position)) {
			window.addEvent('scroll', function(e) {
				var scrollPosition = document.getScroll().y;

				if (scrollPosition < position) {
					$('sidebar').setStyle('top', position - scrollPosition + 'px');
				} else {
					$('sidebar').setStyle('top', 0);
				}
			});
		}

	},
	
	apiOutput: function(){
		if (!exists($('api'))) return;

		if ($$('#output-switcher a.selected').get('text') == 'JSON') {
			$$('.output-json').showMe();
			$$('.output-xml').hideMe();
		} else {
			$$('.output-json').hideMe();
			$$('.output-xml').showMe();
		}

		$$('#output-switcher a').addEvent('click', function(e) {
			e.stop();

			if (!this.hasClass('selected')) {
				$$('#output-switcher a').removeClass('selected');
				this.addClass('selected');
				Beanstalk.apiOutput();
			}
		});

	}
}

window.addEvent('domready', Beanstalk.init);


/* Extensions */

Element.implement({
	
	showMe: function(){
		if (this.isHidden()) this.removeClass('hidden');
		
		return this;
	},
	
	hideMe: function(){
		if (!this.isHidden()) this.addClass('hidden');
		
		return this;
	},
	
	toggleMe: function(){
		return this.toggleClass('hidden');
	},
	
	isHidden: function(){
		if (!this.hasClass('hidden')) {
			return false;
		} else {
			return true;
		}
	},
	
	appendHTML: function(html){
		if ($type(html) != 'string') return false;
		
		var tmp = new Element('div');
		tmp.set('html', html);
		this.adopt(tmp.childNodes);
		
		return this;
	}
	
});

Window.implement({
	
	$E: function(selector){
		return $$(selector)[0];
	},
	
	exists: function(selector){
		if ($$(selector).length > 0 || $(selector) != undefined)
			return true;
		else
			return false
	}
	
});
