
if (typeof(Prototype) === 'undefined') {
	throw "NeuroJSON requires the Prototype JavaScript framework.";
}

var NeuroJSON = new (Class.create({
	_log: function(str) {
		try { console.log('[NeuroJSON] '+str); }
		catch(ex) { /* Gulp */ }
	},
	_queuePoll: function() {
		window.setTimeout(this._poll.bind(this), this._options.pollInterval);
	},
	_poll: function() {
		this._log('Polling..');
		try {
			new Ajax.Request('/?neurojson=1',{
			});
		} catch(ex) {
			this._log(ex);
		}
		this._log('Poll completed');
	},
	initialize: function(options) {
		this._options = Object.extend({
			'pollInterval':	5000 // ms
		},options||{});
		this._log('Initialized..');
		//this._queuePoll();
	},
	addNewPostObserver: function() {},
	addNewCommentObserver: function() {}
}));


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);
		});
	});
}

NeuroJSON.addNewPostObserver(function(post){
});

NeuroJSON.addNewCommentObserver(function(post, comment){
});

// Embed youtube links
site.chainDomLoaded.addFunction(function(){

	var embedRules = [
		{
			'width':	600,
			'height':	361,
			'pattern':	'^http:\/\/www\.youtube\.com\/.*[?&]v=([a-zA-Z0-9-]+)',
			'imgurl':		'http://i1.ytimg.com/vi/#{1}/hqdefault.jpg',
			'markup':	'<object style="width:#{width}px; height:#{height}px;" data="http://www.youtube.com/v/#{1}&hl=en&fs=1&ap=%2526fmt%3D18&autoplay=1">'+
						'<param name="movie" value="http://www.youtube.com/v/#{1}&hl=en&fs=1&ap=%2526fmt%3D18" />'+
						'<param name="allowFullScreen" value="true" />'+
						'<param name="allowscriptaccess" value="always" />'+
						'<param name="wmode" value="transparent" />'+
						'<embed '+
						'src="http://www.youtube.com/v/#{1}&hl=en&fs=1&ap=%2526fmt%3D18&autoplay=1" '+
						'type="application/x-shockwave-flash" '+
						'allowscriptaccess="always" '+
						'allowfullscreen="true" '+
						'width="#{width}" '+
						'height="#{height}" '+
						'></embed>'+
						'</object>'
		}
	];

	return;

	$$('a.embed').each(function(link){
		embedRules.each(function(rule){
			var match = link.href.match(rule.pattern);
			if (match === null) { return; }

			console.log(match);

			var img;
			var markup;
			var container = new Element('div');

			link
				.insert({'after':container})
				.hide();

			container
				.insert(img = new Element('img', {'src': rule.imgurl.interpolate(match)})
					.setStyle({
						'width': rule.width+'px',
						'height': rule.height+'px'
					})
					.observe('click', function(evt){
						Event.stop(evt);
						img.hide();
						markup
							.update(rule.markup.interpolate(Object.extend(match,rule)))
							.show();
					})
				)
				.insert(markup = new Element('div')
					.hide()
				)	
				.wrap(new Element('div')
					.addClassName('embedded')
					.setStyle({
						'width': rule.width+'px',
						'height': rule.height+'px'
					})
				);

		});
	});
});


