function initAccordion(number){
	var id = 'div.accordion' + number; 
	
	if($(id).length != 0) {
		
		var options = { header: "h3" , clearStyle: false, autoHeight: false, active: false, collapsible: true };
		
		if($.browser.msie)
			$.extend(options,{ animated : false });
		
		$(id).each(function() {
			if($(this).hasClass('ui-accordion') == false) {
				$(this).accordion(options);
			}
		});
		
	
		$(id + ' a').each(function() {
            $(this).prev().click(function(){
                $(this).next().trigger('click');
                return false;
            });

		    $(this).click(function(){
//		    	var url = $(this).attr('href') + '?type=10' + number;
		    	var url = $(this).attr('href') + 'ajax_content'+number+'.html';
		    	var div = $(this).parent().next();
		    	var item = $(this).parent();
				
		    	var newNumber = (parseInt(number) +1);
	    		var newAccordion = 'div.accordion' +  newNumber;
		    	
	    		var closeAccordion = $(div).parent().parent().find(newAccordion);
				
				if(closeAccordion.length > 0) {
					closeAccordion.accordion('activate',false);
				}
	    		
				if(div.html().length == 0) {
		   	 		jQuery.data(document.body,'CONTENT_DIV',div);
		   	 		
			   	 	$.ajax({
			   			url: url,
			   			async:false,
			   			success: function(html) {
				   	 		//var REG_EX = /<!-- start content -->[\s\S]*?<div id="content">([\s\S]*?)<\/div>[\s\S]*?<!-- end content -->/gi;
					   	 	div = jQuery.data(document.body,'CONTENT_DIV');
				    		div.html(html);
				    		
				    		if($(div.find(newAccordion)).length == 1) {
				    			initAccordion(newNumber);
				    		}
				    		
				    		//Needs to be triggered twice, don't ask y cauz I don't know ;-)
				    		$(id).accordion('activate' , item);$(id).accordion('activate' , item);
			   			}
			   		});
				}
				
				return ($(id).length != 0);
		    });
		});
		
		//hover states
		$('#dialog_link, ul#icons li').hover(
			function() { $(this).addClass('ui-state-hover'); }, 
			function() { $(this).removeClass('ui-state-hover'); }
		);
	}
}

function initNavigation(items){
	items.click(function(){
		CLICKED = true;

		//add active state
		$('ul#topnav li.active').removeClass('active');
		$(this).parent().addClass('active');
		
    	//var url = $(this).attr('href') + '?type=100';
		//var url = $(this).attr('href') + 'ajax.html';
		
		var link;
		if(IS_IE) {
			link = this.pathname;
			link = link.replace('typo3/papinet/','');
            link = link.replace('papinet2/','');
		} else {
			link = $(this).attr('href');
		}
		
		var url = link + 'ajax.html';
    	
    	url = url.replace(/^.*#/, '');

    	var title = $(this).text();
    	$("#page").fadeOut(ANIM_DURATION,function() {
    		$(this)
    	   		.css('display','block')
    	   		.css('visibility','hidden');
    	    		
	  		$("#wrapper")
	  			.addClass("loading")
	    		.fadeIn(ANIM_DURATION);
	  		
	  		document.title = title + ' | papiNet';
	  		location.hash = url;
	  		
	  		$.historyLoad(url);
    	});
		
		$(this).blur();
		return false;
    });
}

function getContent(url) {
	window.clearInterval(ANIM_INTERVAL);
	$.ajax({
		url: url,
		async:false,
		success: function(html) {
			$("#wrapper")
				.removeClass("loading")
			
			$("#page")
				.css('visibility','visible')
				.css('display','none')
				.html(html)
				.fadeIn(ANIM_DURATION);

			initPage();
			CLICKED = false;
		}
	});
}
