Prototype.Browser = {
	IE:     !!(window.attachEvent && !window.opera),
	Opera:  !!window.opera,
	WebKit: document.childNodes && !document.all && !navigator.taintEnabled,
	Gecko:  (document.getBoxObjectFor != null),
	MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
};

Object.extend(Prototype.Browser, {
     WebKit419: Prototype.Browser.WebKit && (Prototype.BrowserFeatures.XPath),
     WebKit420: Prototype.Browser.WebKit && (!Prototype.BrowserFeatures.XPath),
     IE6:      Prototype.Browser.IE && (typeof window.XMLHttpRequest == "undefined"),
     IE7:      Prototype.Browser.IE && (typeof window.XMLHttpRequest == "object")
});


var SlideContent = Class.create({
	container: null,
	trigger: null,
	subtrigger: null,
	wrapper: null,
	slider: null,
	height: null,
	state: null,
	debug: null,
	duration: 0.8,
	
	initialize: function(container, subtrigger) {
		if(window.location.href.match(/debug/)) {
			this.debug = false;
		}
		this.container = container;
		this.subtrigger = subtrigger;
		this.height = 0;
		this.state = 'closed';
		
		this.prepareDOM();
	},
	
	prepareDOM: function() {
		var header = this.container.select('.fce-foldout-header').first();
		var content = this.container.select('.fce-foldout-content').first();
		var innerContent = content.childElements().first();

		innerContent.childElements().each(function(item) {
			this.height += item.getHeight()+parseInt(item.getStyle('marginTop'))+parseInt(item.getStyle('marginBottom'));
			
		}.bind(this));

		//this.height += parseInt(content.getStyle('paddingTop'));
		//this.height += parseInt(content.getStyle('paddingBottom'));
		this.height = innerContent.getHeight();

		if(this.state == 'closed') {
			content.setStyle({ height: '0px', display: 'none' });
			innerContent.setStyle({ margin: '-'+this.height+'px 0 0 0' });
			this.hideBlocker();
		}
		content.setStyle({ overflow: 'hidden' });

		this.trigger = header;
		this.wrapper = content;
		this.slider = innerContent;
		this.addHandling(this.trigger, this.click);
		if(this.subtrigger) {
			this.addHandling(this.subtrigger, this.open);
		}
	},
	
	getHeightWithDelay: function() {
		new PeriodicalExecuter(function(pe) {
			this.showBlocker();
			this.height = this.slider.getHeight();
			this.hideBlocker();
			pe.stop();
		}.bind(this), 0.4);
	},
	
	addHandling: function(element, func) {
		element.setStyle({ cursor: 'pointer' });
		element.observe('click', func.bindAsEventListener(this));
	},
	
	click: function(event) {
		if(this.state == 'moving') {
			return;
		}
		else if(this.state == 'open') {
			this.close();
		}
		else if(this.state == 'closed') {
			this.open();
		}
	},
	
	open: function() {		
		this.state = 'open';
		this.trigger.down('a').addClassName('show');
		this.showBlocker();
		
		this.wrapper.setStyle({ display: 'block' });
		if(Prototype.Browser.IE && (parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7)) {
			this.wrapper.setStyle({ height: this.height });
			this.slider.setStyle({ margin: '0px 0 0 0' });
		} else {
			this.wrapper.morph('height: '+this.height+'px', {  duration: this.duration });
			this.slider.morph('margin: 0px 0 0 0', {  duration: this.duration });
		}
	},
	
	close: function() {
		this.state = 'closed';
		this.trigger.down('a').removeClassName('show');
		
		if(Prototype.Browser.IE && (parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7)) {
			this.wrapper.setStyle({ height: 0, display: 'none' });
			this.hideBlocker();
			this.slider.setStyle({ margin: '-'+this.height+'px 0 0 0' });
		} else {
			this.wrapper.morph('height: 0px', { duration: this.duration, afterFinish: function() { this.hideBlocker(); }.bind(this) });
			this.slider.morph('margin: -'+this.height+'px 0 0 0', { duration: this.duration });
		}
	},
	
	showBlocker: function() {
		if(Prototype.Browser.IE && (parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 8)) { return }
		this.container.select('select', 'object', 'embed').each( function(element) { element.show(); } );
	},
	
	hideBlocker: function() {
		if(Prototype.Browser.IE && (parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 8)) { return }
		this.container.select('select', 'object', 'embed').each( function(element) { element.hide(); } );
	}
});

Event.observe(window, "load", function (event) {
	try {
		var container = $$('.tx-fbcontentnavigation-pi1').first();
		if (container) {
			var subtriggers = container.down('ul').childElements();
			$$('.main-content .fce-foldout-container-slide').each(function(item, index) {
				new SlideContent(item, subtriggers[index]);
			});
		}
	}
	catch(excp) {
	}
});

