var toolbarActions = new Class({
    Implements: [Chain, Events, Options],

    options:{
        toolbar:'.toolbar',
        anchor_selectors: 'a',
        activity_module_selector: '.activity',
        activity_status_selector: '.response',
        tag_form: '.tag_form',
        ajax_load_msg: 'Submitting...',
        response_type: 'html',
        dw_url: null,
        onSubmitComplete: Class.empty
    },

    initialize: function(element, options){
        //setup the variables and options
		if($chk(options)){
			this.setOptions(this.options, options)
        };

        // Check for the element and options before continuing.
        if(!$chk(element) && !$chk(element.getElement(this.options.toolbar))){
            return false;
        }

        // Flag for only sending one 'click' at a time.
        this.event_active = false;

        // Setup tag submission
        this.tag_form = element.getElement(this.options.tag_form);
        if($chk(this.tag_form)){
            this.tag_data = this.tag_form.retrieve('data', {});
            this.tag_data.chain = new Chain(), this.tag_data.effect = new Fx.Tween(this.tag_form);
            this.input_field = this.tag_form.getElement('[name=tag_body]');
            //*
            this.tag_form.addEvent('submit', function(event){
                (new Event(event)).stop();
                if(this.input_field.get('value') != ''){
                    //this.ajaxSubmit(this.tag_form.get('action'), this.tag_form);
                    this.ajaxSubmit(this.tag_form.getAttributeNode('action').nodeValue, this.tag_form);
                }
            }.bind(this));
            //*/

            //* Prepare the toolbar links to correspond with their activities
            this.toolbar = element.getElement(this.options.toolbar);
            this.anchors = this.toolbar.getElements(this.options.anchor_selectors)
            this.anchors.each(function(item, i){
                this.anchors[i] = $H({
                    'anchor': item,
                    'class': item.get('class'),
                    'href': item.get('href'),
                    'rel': item.get('rel')
                });
                item.addEvent('click', function(event){
                    (new Event(event)).stop();

                    if($chk(this.event_active)){
                        return;
                    }

                    switch(this.anchors[i].get('class')){
                        case 'tag_show':
                        case 'tag_person':
                        case 'tag_blog':
                        case 'tag_story':
                            if(!$chk(this.tag_form)){
                                break;
                            }
                            if(this.activity_module.hasClass('in')){
                                this.hideTagForm();
                                this.hideActivity();
                            }else{
                                this.showTagForm();
                                this.showActivity();
                            }
                            break;
                        case 'add_favorites':
                            this.ajaxSubmit(this.anchors[i].href);
                            break;
                    }
                }.bind(this));
            }.bind(this));
            //*/
        }

        this.activity_module = element.getElement(this.options.activity_module_selector);
        if(this.activity_module){
            this.activity_data = this.activity_module.retrieve('data', {});
            this.activity_data.effect = new Fx.Slide(this.activity_module, {'link':'cancel'});
        }

        this.response_module = element.getElement(this.options.activity_status_selector);
        if(this.response_module){
            this.response_data = this.response_module.retrieve('data', {});
            this.response_data.effect = new Fx.Tween(this.response_module, {'link':'cancel'});
        }

        //* Makes sure the .js_on class is applied before sliding in the now visible activity module.
        this.moduleSetup(this.toolbar);
        //*/
    },

    showTagForm: function(){
        this.tag_data.chain.clearChain();
        this.tag_data.chain.chain(
            function(){
                this.tag_form.addClass('on');
                this.tag_data.chain.callChain();
            }.bind(this),
            function(){this.tag_data.effect.start('opacity',1);}.bind(this)
        );
        this.tag_data.chain.callChain();
    },

    hideTagForm: function(){
        this.tag_data.chain.clearChain();
        this.tag_data.chain.chain(
            function(){
                this.tag_data.effect.start('opacity',0).chain(function(){
                    this.tag_form.removeClass('on');
                }.bind(this));
            }.bind(this)
        );
        this.tag_data.chain.callChain();
    },

    // Adding Favorites via ajax using data from link href.
    ajaxSubmit: function(url, form){
        dbug.log(url);
        if($chk(this.event_active)){
            return;
        }

        this.event_active = true;

        if(!this.activity_module.hasClass('in')){
            this.showActivity();
        }
        if(this.options.response_type == 'json'){
            new Request.JSON({
                'url':url,
                'data':form,
                'onComplete': function(data){
                    //dbug.log(data);
                    this.showResponse(data);
                    //this.response_module.setStyle('display','block').tween('opacity', 1);
                }.bind(this)
            }).send();
        }else{
            new Request({
                'url':url,
                'data':form,
                'onComplete': function(data){
                    //dbug.log(data);
                    this.showResponse(data);
                    //this.response_module.setStyle('display','block').tween('opacity', 1);
                }.bind(this)
            }).send();
        }
    },

    moduleSetup: function(element){
        if(!element){
        	return false;
        }
        this.chain(
            function(){element.addClass('js_on'); this.callChain();}.bind(this),
            function(){
            	if(this.activity_module){
            		this.activity_module.slide('hide');
            	}
            }.bind(this)
        );
        this.callChain();
    },

    hideActivity: function(){
        this.event_active = true;
        this.activity_data.effect.slideOut().chain(function(){
            this.activity_module.removeClass('in');
            this.event_active = false;
        }.bind(this));
    },

    showActivity: function(){
        this.event_active = true;
        this.activity_data.effect.slideIn().chain(function(){
            this.activity_module.addClass('in');
            this.event_active = false;
        }.bind(this));
    },

    showResponse: function(data){
        var text = '';

        if(this.options.response_type == 'json' && $type(data) == 'object'){
            var tags_str = data['my_tags']['tag_str'];
            text = $chk(tags_str) ? 'Tagged with ' + tags_str : 'Saved with no tags.';
        }else{
            text = data;
        }

        this.clearChain();
        this.chain(
            function(){
                if(this.tag_form.hasClass('on')){
                    this.hideTagForm();
                }
                this.callChain();
            }.bind(this),
            function(){
                this.response_module.set('text', text).setStyle('display','block');
                this.callChain();
            }.bind(this),
            function(){
                if(!this.activity_module.hasClass('in')){
                    this.showActivity();
                }
                this.response_data.effect.start('opacity', 0, .9999);
                this.callChain.delay(4000,this);
            }.bind(this),
            function(){
                this.response_data.effect.start('opacity', .9999, 0).chain(function(){
                    this.hideActivity();
                    this.callChain.delay(500, this);
                }.bind(this));
            }.bind(this),
            function(){
                this.response_module.set('text','').setStyle('display','none');
                this.event_active = false;
            }.bind(this)
        );
        this.callChain();
    }
});