/**
 * main setup
 */

var spinnerImagePath = themepath + jsiFolder + loadIconImage;


// Init Class
mousSystem = {
    start: function() {
	    //Bloxpress.initialize();
    },
    run: function() {
        runFast = new FastInit(function() { mousSystem.start() });
    }
}

// gogogo!
mousSystem.run();

/*
	Behaviours
*/
Behaviours = {

    'a.archief' : function(element) {
        element.onclick = function() {
            if (Element.visible('archive')) {
            	$('archief').removeClassName('active');
            	new Effect.BlindUp('archive', {queue: {position: 'end', scope: 'catsmenu', limit:2, duration:0.2} });
            } else {
	            $('archief').addClassName('active');
				new Effect.BlindDown('archive', {queue: {position:'end', scope: 'catsmenu', limit:2} });
            }
        }
    },
    'a.categorie' : function(element) {
        element.onclick = function() {
            if (Element.visible('category')) {
	            $('categorie').removeClassName('active');
            	new Effect.BlindUp('category', {queue: {position: 'end', scope: 'catsmenu', limit:2, duration:0.2} });
            } else {
	            $('categorie').addClassName('active');
				new Effect.BlindDown('category', {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}), ' bezig...' ]);
        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);