
var PostObserver = Class.create({
	initialize: function(element) {
		this.element = element;
		element.observe('mouseenter',this.onMouseEnter.bindAsEventListener(this));
		element.observe('mouseleave',this.onMouseLeave.bindAsEventListener(this));

		this.sidebarElement = new Element('div');
		this.sidebarElement.addClassName('postsidebar');
		this.element.insert({'top':this.sidebarElement});

		this.innerElement = new Element('div');
		this.innerElement.addClassName('postsidebar_inner');
		this.sidebarElement.insert({'top':this.innerElement});

		this.popupEffect1 = undefined;
		this.popupEffect2 = undefined;

		this.elementWidth = this.element.getWidth();

		site.log("PostObserver");
	},
	onMouseEnter: function(e) {
		site.log('Enter');
		this.sidebarElement.setStyle({
			'position':		'absolute',
			'left':			this.elementWidth+'px'
		});
		this.innerElement.setStyle({
			'height':		this.element.getHeight()+'px'
		});

		this.innerElement.update('hello world');

		if (!Object.isUndefined(this.popupEffect1)) { this.popupEffect1.cancel(); }
		if (!Object.isUndefined(this.popupEffect2)) { this.popupEffect2.cancel(); }
		this.popupEffect1 = new Effect.Move(this.innerElement,{
			mode: 'absolute',
			duration: 1,
			x: 0,
			y: 0
		});
		this.popupEffect2 = new Effect.Appear(this.innerElement,{
			duration: 1
		});


	},
	onMouseLeave: function(e) {
		if (!Object.isUndefined(this.popupEffect1)) { this.popupEffect1.cancel(); }
		if (!Object.isUndefined(this.popupEffect2)) { this.popupEffect2.cancel(); }
		this.popupEffect1 = new Effect.Move(this.innerElement,{
			mode:		'absolute',
			x:			-300,
			y:			0,
			duration:	1
		});
		this.popupEffect2 = new Effect.Fade(this.innerElement,{
			duration:	1
		});

	}
});


var Site = Class.create({

	initialize: function(options) {
		this.chainDomLoaded = new CodeCompany.FunctionChain('domLoaded');
		this.chainWindowLoaded = new CodeCompany.FunctionChain('windowLoaded');

		document.observe('dom:loaded', this.chainDomLoaded.executeChain.bind(this.chainDomLoaded));
		Event.observe(window, 'load', this.chainWindowLoaded.executeChain.bind(this.chainWindowLoaded));

		this.log('Initialized');
	},

	log: function(str) {
		if (Object.isUndefined(console)) { return; }
		console.log('[Site] '+str);
	}

});

var site = new Site();
site.chainDomLoaded.addFunction(function(){
	new DynCommentsObserver('.dyncomments_container');
});

if (window.location.search.match(/[?&]test/)!==null) {
	site.chainDomLoaded.addFunction(function(){
		$$('.postcontainer').each(function(element){
			new PostObserver(element);
		});
	});
}


