jQuery.fn.extend({
	haccordion: function(params){
		var jQ = jQuery;
		if(window.location.hash) {
			  var goto_url = window.location.hash;
			  var openmepls = 5;
			} else {
				var openmepls = 0;
			  // Fragment doesn't exist
			}
		var params = jQ.extend({
			speed: 500,
			headerclass: "slide_handle",
			contentclass: "slide_content",
			contentwidth: 666,
			openone:openmepls,
			closeBig:0
			//neverclose:6
		},params);
		return this.each(function(index){
			jQ("."+params.headerclass,this).each(function(index){
				if(index == params.openone){ //open one slide from the start
					var p = jQ(this).parent()[0];
					p.opened = this;
					jQ(this).next("div."+params.contentclass).css({
						height: "auto"
					});
					jQ(this).next("div."+params.contentclass).animate({
						width: params.contentwidth + "px"
					}, params.speed).children('.slide_inner_header').css({'position': 'fixed'});
					
							
				} else { //close all others
					if(index == 0){
						jQ(this).next("div."+params.contentclass).css({
							width: "70px",
							height: "1px"
						});	
					} else {
						jQ(this).next("div."+params.contentclass).css({
							width: "1px",
							height: "1px"
						});	
					}
				}
				if(index == params.neverclose){ //exept the one not to close, to prevent a scrollbar, set height to windowheight
					jQ(this).next("div."+params.contentclass).css({
						'width' : params.contentwidth + "px",
						'height' : $(window).height(),
						'overflow-y' : 'hidden'
					}).children('.innerContent').css({ 'position' : 'fixed'});	
					jQ(this).next("div."+params.contentclass).children('.header_spacer').find('.slide_inner_header').css({'position': 'fixed'});
				}
				
				
			}).click(function(){
				var p = jQ(this).parent()[0];
				
				
				if (p.opened != "undefined"){
					if(jQ(p.opened).hasClass('first_handle') && p.opened != this){ //the first slide is thicker when closed
						jQ(p.opened).next("div."+params.contentclass).animate({
							width: "70px"
						},params.speed, function() {
						    $(this).css({
								height: "1px"
							});
						}).children('.header_spacer').find('.slide_inner_header').css({'position': 'relative'});
						//jQ(p.opened).next('.slide_inner_header').css({'position': 'relative'});
					} else if(jQ(p.opened).hasClass('edition_slide') && p.opened != this){ //this one doesnt close at all, to prevent scrollbar, set height wo window height
						jQ(p.opened).next("div."+params.contentclass).css({
							width: params.contentwidth + "px",
							height: $(window).height(),
							'overflow-y': 'hidden'
						}).children('.innerContent').css({ 'position' : 'fixed'}).children('.header_spacer').find('.slide_inner_header').css({'position': 'fixed'});	
						//jQ(p.opened).next('.slide_inner_header').css({'position': 'relative'});
						
					} else if(p.opened != this){
						jQ(p.opened).next("div."+params.contentclass).animate({
							width: "1px"
						},params.speed, function() {
						    $(this).css({
								height: "1px"
							});
						}).children('.header_spacer').find('.slide_inner_header').css({'position': 'relative'});
						$('html').animate({scrollTop:0},500);
						//jQ(p.opened).next('.slide_inner_header').css({'position': 'relative'});
					}
				}
				p.opened = this;
				if(jQ(p.opened).hasClass('edition_slide')){
					jQ(p.opened).next("div."+params.contentclass).css({
						width: params.contentwidth + "px",
						height: "auto",
						'overflow-y': 'auto'
					}).children('.innerContent').css({ 'position' : 'relative'});	
				}
				jQ(this).next("div."+params.contentclass).css({
					height: "auto"
				});
				jQ(this).next("div."+params.contentclass).animate({
					width: params.contentwidth + "px"
				}, params.speed, function() {
				    $(this).children('.header_spacer').find('.slide_inner_header').css({'position': 'fixed'});
				});
				//jQ(this).next('.slide_inner_header').css({'position': 'fixed'});
			});
		});
	}
});

jQuery(document).ready(function() {
		setCss();
	   $("#slide_container").haccordion();
	   
	   var fixed_height = $(window).height();
	   var margin_img = (fixed_height/2) - 162;
	   var linie_image_margin = (fixed_height/2)-225;
	   var bottom_up = fixed_height -30;
	   
	   $('#impressum_button').css({'margin-top' : bottom_up});
	   $('.verlaufstrenner').css({'height' : fixed_height});	   
	   $('.fixed_container').css({'height' : fixed_height});
	   $('.fixed_container IMG').css({'margin-top' : margin_img});
	   $('.west_image').css({'margin-top' : linie_image_margin });
	   $('.edition_content .scroll_content').css({'margin-top' : linie_image_margin });
	   
	   $(window).resize(function() {
		   var fixed_height = $(window).height();
		   var linie_image_margin = (fixed_height/2)-225;
		   var margin_img = (fixed_height/2) - 162;
		   var bottom_up = fixed_height -30;
		   
		   $('#impressum_button').css({'margin-top' : bottom_up});
		   $('.fixed_container').css({'height' : fixed_height});
		   $('.verlaufstrenner').css({'height' : fixed_height});	   
		   $('.fixed_container IMG').css({'margin-top' : margin_img});
		   $('.west_image').css({'margin-top' : linie_image_margin });
		   $('.edition_content .scroll_content').css({'margin-top' : linie_image_margin });
		   if($('.edition_slide').next("div.slide_content").css('overflow-y') == 'hidden') {
			   //we have to see if the tab is open, else adjust height that no scrollbar appears
			   $('.edition_slide').next("div.slide_content").css({'height' : fixed_height});
		   }
		 });
	   
	   $('.slide_handle').each(function(index){
		   $(this).click(function(){
			   changeLogo(index);
		   });
			
		});
	   
	   //impressum slider
	   $('#impressum_button').click(function(){
		   toggle_impressum();
	   });
	   /*
	   $('body').click( function(){
		   var $content = $('#impressum_content');
			
			if($content.hasClass('hide_this')){
				
			} else {
				$content.removeClass('hide_this').animate({
					width: "1px"
				},500, function() {
					$(this).addClass('hide_this');
				});				
			}
	   });
	   */
	   //jump directly to a blog entry
	   if(window.location.hash) {
		   	var elem = $(window.location.hash);
		    
		   	$(document).scrollTop(elem.scrollTop());
		    
	   }
});	  

function toggle_impressum(){
	var $content = $('#impressum_content');
	
	if($content.hasClass('hide_this')){
		$content.removeClass('hide_this').animate({
			width: "250px"
		},500, function() {
			 $('body').one('click', function(e) {
				 $content.removeClass('hide_this').animate({
						width: "1px"
					},500, function() {
						$(this).addClass('hide_this');
					});	
				});
		});		
	} 
	/*
	else {
		$content.removeClass('hide_this').animate({
			width: "1px"
		},500, function() {
			$(this).addClass('hide_this');
		});	
		
	}
	*/
	
}

function changeLogo(index){
	//console.log("jaja eingegangen "+index);
	
	switch (" "+index) {
	  case " 1":
		  $('#logo').css({'background-position' : '-102px 0'});
		  
	    break;
	  case " 2":
		  $('#logo').css({'background-position' : '-204px 0'});
	    break;
	  case " 3":
		  $('#logo').css({'background-position' : '-306px 0'});
	    break;
	  case " 4":
		  $('#logo').css({'background-position' : '-408px 0'});
	    break;
	  default:
		  $('#logo').css({'background-position': '0 0'});
	    break;
	}
}

//set the css with js, for non-script view
function setCss(){
	$('body').css({ 'overflow-x' : 'hidden'});
	$('#logo').css({ 'position' : 'fixed'});
	$('.trenner_container .verlaufstrenner').css({'position' : 'fixed'});
	$('#slide_container .slide_handle .fixed_container').css({'position' : 'fixed'});
	$('#impressum_content').addClass('hide_this').css({'width' : '1'});
	
	
}

