/**
*Function for adding AJAX to menu-elements
*/
var go_ajaxMenu = function(element, body_id, updateid, column){
    var to_update = $(updateid);
    var the_body = body_id;
    $$(element + ' a').each(function(el){
        addAjaxToMenuElement(el, the_body, to_update, column, element);
        return false;
    });
	}
/**
*Function for adding AJAX to elements in the main-sections
*/
var go_ajaxMain = function(element, body_id, updateid, column){
    var to_update = $(updateid);
    var the_body = body_id;
    $$(element + ' a').each(function(el){
        addAjaxToMainElement(el, the_body, to_update, column, element);
        return false;
    });
}

var addAjaxToMenuElement = function(el, the_body, to_update, column, element){
    el.addEvent('click', function(){
        var isMarked = el.hasClass('markit');
        if (!isMarked) { //check if not the current page
            var url = el.href;
            to_update.fade(0.0); //fade out content
            $$(element + ' a.markit').each(function(e2){ //delete marker
                e2.removeClass('markit');
            });
            new Request.HTML({
                url: url,
                method: 'post',
                evalScripts: false,           
                onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript){
                    to_update.fade(1.0); //fade in updated content
                    el.addClass('markit'); //mark the current page					
                },
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
                    var response_main = responseElements.filter('#main').get('html'); //grab the html-part
                    to_update.set('html', response_main);//inject the new html-code
                    var filtered_js = responseJavaScript.replace("window.addEvent('domready', init_ajaxMenu);", ''); //remove js-event
                    filtered_js = removeCommentsForIE(filtered_js); //remove the comments
                    $exec(filtered_js); //execute the javascript
                }
            }).send();
        }
        return false;
    });
}

var addAjaxToMainElement = function(el, the_body, to_update, column, element){
    el.addEvent('click', function(){
            var url = el.href;
            to_update.fade(0.0);
            $$('.navMain' + ' a').each(function(e2){
                e2.removeClass('markit');
				if (e2.href==url)
				{
					e2.addClass('markit');
				}
            });
            new Request.HTML({
                url: url,
                method: 'post',
                evalScripts: false,           
                onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript){
                    to_update.fade(1.0);           
                },
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
                    var response_main = responseElements.filter('#main').get('html');
                    to_update.set('html', response_main);
                    var filtered_js = responseJavaScript.replace("window.addEvent('domready',init_ajaxMenu);", '');
                    filtered_js = removeCommentsForIE(filtered_js);
                    $exec(filtered_js);
                }
            }).send();        
        return false;
    });
}
/*
*Remove all comments in the JS-Part of the response so that IE can process it
*/
var removeCommentsForIE = function(filtered_response)
{
                    /*really ugly workaround until the regular expressions are fixed*/
                    var comment_open = '<!\-\-\/\/\-\-><!\[CDATA\[\/\/><!\-\-';
                    var comment_close = '\/\/\-\-><!\]\]>';

                    for(i=0;i<=2;i++)
                    {
                    filtered_response = filtered_response.replace(comment_open, ''); 
                    filtered_response = filtered_response.replace(comment_close, ''); 
                    }
                    filtered_response = filtered_response.replace('<!--','');
                    filtered_response = filtered_response.replace('//--><![CDATA[//><!-- ','');
                    /*work-around end*/  
                    return filtered_response;                  
}

