var loadPage = function (element, updateid, column) {
    $$(element+' a').each(function(el){
        el.onclick = function() {
	    var url = el.href;
	    // Check if element already exists
            var container = $(updateid);
            var children = container.getChildren('div#' + el.get('href').toLowerCase());
	    if(!children.length) {
	        new Request.HTML({
	        	url: el.href + '?request=' + column,
		        method: 'get',
                        evalScript: false,
                        onRequest: function() { $('preloader').setStyle('display','block'); },
		        onSuccess: function(tree, elements, html, js) {
                            container.set("html", container.get("html") + html);
                            var newElement = container.getChildren('div#' + el.get('href').toLowerCase());
                            newElement.addClass('inactive');
                            $$('.mod_customnav li.active').removeClass('active');
                            el.getParent('li').addClass('active');
                            return showPage(newElement);
                        },
		        onComplete: function() { $('preloader').setStyle('display','none'); }
	        }).send();
            } else {
	        $$('.mod_customnav li.active').removeClass('active');
	        el.getParent('li').addClass('active');
	        return showPage(children);
            }
            return false;
        }
    });
};

var showPage = function(element) {
    var activePage = $('main-wrapper').getChildren('.mod_article:not(.inactive)');
    if(element[0].get('id') == activePage[0].get('id')) {
        return false;
    }
    // Tweens
    var activeFX = new Fx.Tween(activePage[0], {duration: 500, unit: 'px'});
    var newFX = new Fx.Tween(element[0], {duration: 500, unit: 'px'});
    activeFX.start('left', 0, -552);
    activeFX.addEvent('complete', function() {
        activePage[0].addClass('inactive');
    });
    newFX.start('left', 552, 0);    
    newFX.addEvent('complete', function() {
        element[0].removeClass('inactive');
    });
    return false;
};
