function poll_submit_callback(response) {
	if(response.error) return !!alert(response.error);

	$("#poll_" + response.poll_id).replaceWith(response.html);
}

function poll_show_results(id) {
	$.postJSON("/ajax/poll/", {poll_id: id}, function(response) {
		if(response.error) return !!alert(response.error);
		$("#poll_" + response.poll_id).replaceWith(response.html);
	});
	return false;
}

function poll_show_form(id) {
	$.postJSON("/ajax/poll/", {poll_id: id, show_form: 1}, function(response) {
		if(response.error) return !!alert(response.error);
		$("#poll_" + response.poll_id).replaceWith(response.html);
		$("#poll_" + response.poll_id + " form").ajaxForm({dataType: 'json', success: poll_submit_callback});
	});


	return false;
}

function poll_init() {
	$(".FF_post .poll form").ajaxForm({dataType: 'json', success: poll_submit_callback});
}

var cal = {

	init:	function ( t, year, month, reload ) {
			cal.o = t
			return !! cal.load( year, month, reload )
		},

	load:	function ( year, month, reload ) {
			$.get(	location.href,
				{ ajax: 'calendar', year: year, month: month },
				function ( response ) {
					if ( !reload ) $( document.body ).prepend( '<div id="Calendar">' + response + '</div>' )
					else $( '#Calendar' ).html( response )
					cal.show()
				}
			)
		},

	show:	function () {
			id( 'Calendar' ).style.left = $( cal.o ).offset().left + 'px'
			id( 'Calendar' ).style.top  = $( cal.o ).offset().top + 20 + 'px'
			$( '#Calendar' ).show( 180 )
			hider.add( cal.hide )
		},

	hide:	function () {
			$( '#Calendar' ).hide( 180 )
			hider.remove()
		}

}

FF_Blog.ShowCalendar = cal.init

FF_Blog.CalSubmit = function(  ) {
	return !! cal.load( id( 'Cal_year' ).value, id( 'Cal_month' ).value, 1 )
}

$(poll_init);

$(function(){

	$( '#content div.FF_post a[rel=do]' ).click( tools.init )

})

var tools = {

	init:	function ( e ) {
		tools.cur = this.parentNode.getElementsByTagName('div')[0]
		tools.o = tools.cur.cloneNode( true )
		return ( tools.o.className == 'tools' ) ? !! tools.show( e ) : !! tools.hide()
	},

	show:	function ( e ) {
		var e = e || event
		var tmp = document.createElement( 'div' )
		tools.o.className = 'in'
		tmp.appendChild( tools.o )
		tools.o = tmp
		$( tools.o ).addClass( 'cmtTools tools' ).prepend( '<div class="t"></div>' ).append( '<div class="b"></div>' )
		tools.o.style.left = e.clientX + document.documentElement.scrollLeft - 20 + 'px'
		tools.o.style.top  = e.clientY + document.documentElement.scrollTop  + 20 + 'px'
		tools.o.onclick = function ( e ) {
			var e = e || event
			var t = e.target || e.srcElement
			if ( t.tagName == 'A' ) {
				if ( t.rel == 'ajax' ) {
					t.innerHTML = '...'
					return !! $.postJSON(
						t.href.substring( 0, t.href.indexOf('?') ),
						t.href.substring( t.href.indexOf('?') +1, t.href.length),
						function ( response ) {
							t.href = response.href
							t.innerHTML = response.text
							tools.cur.innerHTML = tools.o.getElementsByTagName( 'div' )[1].innerHTML
							tools.hide()
						}
					)
				}
				else tools.hide()
			}
		}
		document.body.appendChild( tools.o )
		$( tools.o ).fadeIn( 180 )
		hider.add( tools.hide )
	},

	hide:	function () {
		$( tools.o ).fadeOut( 150 , function () { $( tools.o ).remove(); tools.o = null })
		hider.remove()
	}

}

var tagCloud = {

	init:	function ( t ) {
			tagCloud.o = t
			return ( !id( 'tag_cloud' ) )
				? !! tagCloud.load()
				: ( id( 'tag_cloud' ).className == 'hidden' )
					? !! tagCloud.show()
					: !! tagCloud.hide()
		},

	load:	function () {

			$.get(	location.href,
				{ ajax: 'tagcloud' },
				function ( response ) {
					$( document.body ).prepend( '<div id="tag_cloud" class="hidden">' + response + '</div>' )
					tagCloud.show()
				}
			)
		},

	show:	function () {
			id( 'tag_cloud' ).style.left = $( tagCloud.o ).offset().left + 'px'
			id( 'tag_cloud' ).style.top  = $( tagCloud.o ).offset().top + 20 + 'px'
			$( '#tag_cloud' ).show( 180 ).removeClass( 'hidden' )
			hider.add( tagCloud.hide )
		},

	hide:	function () {
			$( '#tag_cloud' ).hide( 180 ).addClass( 'hidden' )
			hider.remove()
		}

}

FF_Blog.TagCloud = tagCloud.init