var TM =
{
	cat:null,
	catTrig:null,
	arch:null,
	archTrig: null,
	commTrig:null,
	comFx:null,
	catFx:null,
	archFx:null,
	
	initialize: function()
	{
		var logForm = $$( 'form#searchform input[type="text"]' );
		logForm.store( "defaultValue", logForm.get( "value" ) );
		logForm.addEvent( "focus", this.formfocusHandler.bind( logForm ) );
		logForm.addEvent( "blur", this.formBlurHandler.bind( logForm ) );
		
		
		var btnSubmit = $$( 'form#commentform input[type="submit"]' );
		btnSubmit.addEvent( "click", this.commentformClickHandler.bindWithEvent( this ) );
		
		this.cName = $$( 'form#commentform input#author' );
		this.cMail = $$( 'form#commentform input#email' );
		this.cComment = $$( 'form#commentform textarea#comment' );
		this.cHint = $( "commenthint" );
		this.commTrig = $("commTrig");

		this.cat = $("categories").getElement("ul");
		this.arch = $("archCont");
		this.catTrig = $("categories").getElement("h1");
		this.archTrig = $("archives").getElement("h1");

		this.catTrig.addEvent( "click", this.clickSidebarListener.bind(this) );
		this.archTrig.addEvent( "click", this.clickSidebarListener.bind(this) );
		if( this.commTrig) {
			this.commTrig.addEvent("click", this.commTrigListener.bind(this));
		}

		var c = $("commentcontainer");
		if( c ) {
			c.setStyle( "display","block" );
			this.comFx = new Fx.Slide( c, {duration:350} ).hide();
		}

		this.catFx = new Fx.Slide( this.cat.setStyle("display", "block"), { duration:240 } ).hide();
		this.archFx = new Fx.Slide( this.arch.setStyle("display", "block"), { duration:240 } ).hide();

		var wh = window.getScrollHeight();
		document.id("mainLBar").setStyle( "height", wh );
	},

	commTrigListener: function()
	{
		return this.comFx.toggle();
	},
	
	clickSidebarListener: function( e )
	{
		var id = $(e.target).getParent("div.leftGroup").get("id");
		switch( id )
		{
			case "categories" :

				//this.cat.setStyle( "display", this.cat.getStyle( "display" ) == "block" ? "none" : "block" );
				this.catFx.toggle();
				this.sideSlideDoneHandler.delay( 200, this, ["cat" ] );
				break;

			case "archives" :
				//this.arch.setStyle( "display", this.arch.getStyle( "display" ) == "block" ? "none" : "block"  );
				this.archFx.toggle();
				this.sideSlideDoneHandler.delay( 200, this, ["arch" ] );
				break;
		}
	},

	sideSlideDoneHandler: function( who )
	{
		switch( who )
		{
			case "cat":
				if( this.catTrig.hasClass("active") ) {
					this.catTrig.removeClass( "active" );
				}else{
					this.catTrig.addClass( "active" );
				}
				break;

			case "arch":
				if( this.archTrig.hasClass("active") ) {
				
					this.archTrig.removeClass( "active" );
				}else{
					this.archTrig.addClass( "active" );
				}
				break;
		}
	},
	
	formfocusHandler: function()
	{
		if(  String(this.get( "value" )).trim() == String(this.retrieve( "defaultValue" )).trim() ) {
			this.set( "value", "" );
		}
	},
	
	formBlurHandler: function()
	{
		if( this.get( "value" ) == "" ) {
			this.set( "value", this.retrieve( "defaultValue" ) );
		} 
	},
	
	commentformClickHandler: function( e )
	{
		if( this.cName.get("value") == "" || this.cMail.get("value") == "" || this.cComment.get("value") == "" ) {
			e.stop();
			e.preventDefault();	
			this.cHint.setStyle( "display", "block" );
			this.hideCommentHint.delay( 2000, this );
		}
	},
	
	hideCommentHint: function()
	{
		this.cHint.setStyle( "display", "none" );
	}
}
TM =  new Class( TM );




var TMShareLink = new Class
({
	at: null,
	btn:null,
	men:null,
	int:null,
	active:false,
	checkIntId:null,

	initialize: function()
	{
		this.at =  $("addthiswrapper");
		if( !this.at ) {
			return;
		}
		this.btn = this.at.getElement('.custom_button');
		this.men = this.at.getElement('.hover_menu');

	    $$('#addthiswrapper .custom_button, #addthiswrapper .hover_menu').each( function( el ) {
		    el.addEvent( "mouseenter", this.hoverListener.bind(this) );
		    el.addEvent( "mouseleave", this.leaveListener.bind(this) );
	    }.bind(this));

	},
	hoverListener: function(e)
	{
		var btn = this.btn;
		var men = this.men;
		$clear( this.int );
		btn.addClass('active');
		if( this.active ) {
			return;
		}
		men.set("opacity", 0 ).setStyles( {display:"block",bottom:"34px",right:"0px"}).fade("in");
		this.active = true;
	},
	leaveListener: function(e)
	{
		this.btn.removeClass('active');
		this.int = this.hide.delay( 200, this );
	},
	hide: function()
	{
		var btn = this.btn;
		var men = this.men;
		this.active = false;

		btn.removeClass("active");
		men.fade("out");
	}
});





function applicationInitHandler()
{
	new TM();
	new TMShareLink();
}

window.addEvent( "domready", applicationInitHandler );


