
jQuery(document).ready(function() {
	if(ecologic.check() == true) {
		ecologic.injectResponseLayer();
		ecologic.bindTimestamps();
	}
});
var ecologic = function () {
	
	return {
		check: function() {
			if (window.location.href.indexOf('#_editor_') !== -1) return true;
		},
		get: function(url) {
			$.ajax({
				type: "GET",
				url: url+'?'+new Date().getTime(),
				success: update
			});
		},
		bindTimestamps: bindTimestamps,
		injectResponseLayer: injectResponseLayer,
		fire: fire,
		remove: remove
	};



	function remove(id, slug) {
		$.ajax({
			type: "GET",
			url: '/rpc/remove/'+slug+'/'+id+'/',
			success: ecologic.update
		});

		$('#response').append('removed '+id+'<br/>');
	}


	function fire(id) {
		var time = $('#i'+id).val();
		$.ajax({
			type: "GET",
			url: '/rpc/update/'+time+'/'+id+'/',
			success: ecologic.update
		});

		$('#response').append('cool '+id+'<br/>');
	}

	function injectResponseLayer() {
		$('body').append('<div id=response style="text-align: left; overflow: auto; margin: 0 auto; position: absolute; background-color: white; width: 200px; height: 40px; top: 0px; border: 2px solid red; z-index: 10000 !important; position: fixed;"></div>')
	}

	function bindTimestamps(){
		$('.top_main .middle span.rawtime').each(function(e){
			$(this).css('display', 'block');
			var val = $(this).html();
			var id = $(this).attr('id');
			var slug = $(this).attr('rel');
			
			$(this).html('<form onSubmit="ecologic.fire(' + id + ');return false;" id="f' + id + '">\
							<input name=rawtime id="i' + id + '" value="' + val + '" />\
							<input type=submit value=go>\
							<input type=button onClick=\'javascript:ecologic.remove("' + id + '", "' + slug + '")\' value=del>\
							<input name=id type=hidden value="' + id + '">\
					</form>')
		});
	}

	function update() {
		var iw = $('#response');
		iw.html(arguments[0] ? arguments[0] : arguments[1]);
	}

}();