/*
	Behaviours
*/
Behaviours = {

    'a.showarchive' : function(element) {
        element.onclick = function() {
            Element.cleanWhitespace(element.parentNode.parentNode);
            if (Element.visible('archive_links')) {
				Element.update(this, '+');
                element.title = 'Show archives';
				new Effect.SlideUp('archive_links', {queue: {position: 'end', scope: 'catsmenu', limit:2, duration:0.2} });
            } else {
				Element.update(this, '-');
				element.title = 'Hide Archives';                
				new Effect.SlideDown('archive_links', {queue: {position:'end', scope: 'catsmenu', limit:2} });
            }
        }
    },
    'a.showcats' : function(element) {
        element.onclick = function() {
            Element.cleanWhitespace(element.parentNode.parentNode);
            if (Element.visible('cats_all')) {
		Element.update(this, '+');
                element.title = 'All categories';
		new Effect.SlideUp('cats_all', {queue: {position: 'end', scope: 'catsmenu', limit:2, duration:0.2} });
		new Effect.SlideDown('cats_filtered', {queue: {position:'end', scope: 'catsmenu', limit:2} });
            } else {
		Element.update(this, '-');
		element.title = 'Less categories';                
		new Effect.SlideUp('cats_filtered', {queue: {position: 'end', scope: 'catsmenu', limit:2, duration:0.2} });
		new Effect.SlideDown('cats_all', {queue: {position:'end', scope: 'catsmenu', limit:2} });
            }
        }
    },
    'a.scrolljump' : function(element) {
        element.onclick = function() {
            var href = element.getAttribute('href');
            fx= new Effect.ScrollTo(href.substr(1));
            return false;
        }
    },
    'a.gotocomments' : function(element) {
        element.onclick = function() {
            fx= new Effect.ScrollTo('comments');
            return false;
        }
    },
    'a.contentnext' : function(element) {
        element.onclick = function() {
            lastDiv = Bloxpress.domLast($('posts'));
            postDiv = $(element.parentNode.parentNode.parentNode);
            if (postDiv != lastDiv) {
                fx = new Effect.ScrollTo(Bloxpress.domAfter(postDiv));
            } else {
                fx = new Effect.ScrollTo('footer');
            }
        }
    },
    'a.contentprev' : function(element) {
        element.onclick = function() {
            firstDiv = Bloxpress.domFirst($('posts'));
            postDiv = $(element.parentNode.parentNode.parentNode);
            if (postDiv != firstDiv) {
                fx = new Effect.ScrollTo(Bloxpress.domBefore(postDiv));
            } else {
                fx = new Effect.ScrollTo('header');
            }
        }
    },
    'a.contentswitch' : function(element) {
        element.onclick = function() {
            var postDiv = element.parentNode.parentNode.parentNode;
            var postContent = document.getElementsByClassName(postContentClass, postDiv);
            postContent.each(function(post) {
               fx = new Effect.toggle(post, 'blind', {duration:0.5})
            });
        }
    },
    'a.switchallposts' : function(element) {
        element.onclick = function()
        {
            document.getElementsByClassName(postContentClass).each(
                function(element) {
                    Element.toggle(element);
                }
            );
            if (statefulLinks) {
                if (element.innerHTML == collapseAll) {
                    Element.update(element, expandAll);
                } else {
                    Element.update(element, collapseAll);
                }
            }
        }
    },
    'form.search' : function(element) {

        indicatorId = 'processing_' + Math.round(Math.random() * 10000);
        indicatorEl = Builder.node('div',{id:indicatorId,style:'display:none;padding:0.3em'}, [
        Builder.node('img',{src:spinnerImagePath}), ' searching...' ]);
        element.appendChild(indicatorEl);

        inputfield = element.getElementsByTagName('input');
        inputfield[0].setAttribute('autocomplete', 'off');
        inputfield = inputfield[0].id;

        resultfield = element.getElementsByTagName('div');
        resultfield = resultfield[0].id;

        new Ajax.Autocompleter(inputfield, resultfield, tplpath+'search_ajax.php', {
           method:'get', frequency:0, param: 's', minChars: 3, indicator:indicatorId, updateElement: function(resultItem) {
                window.location = resultItem.getElementsByTagName('span')[0].innerHTML;
           }
        });
    },
    'form.commentform' : function(element) {
		
        element.onsubmit = function() {
	        var nofityElement = Builder.node('div', {id:'comindicator',className:'nofity'}, [Builder.node('img', {src:spinnerImagePath,style:'vertical-align:middle'}),' ' + commentMessage + ' ',]);
            new Ajax.Updater('commentlist', tplpath + 'comments_ajax.php', {
                onLoading:function() {
                    $('commentlist').appendChild(nofityElement);
                },
                onComplete:function(request) { new Effect.Fade('comindicator', {
                    duration: 0.4, afterFinishInternal:function(){
                            Element.remove('comindicator');
                        }
                    });
                },
                parameters:Form.serialize('commentform'),
                asynchronous:true, evalScripts:true,
                insertion:Insertion.Bottom
            });
            return false;
        }
    }
};
Behaviour.register(Behaviours);
