sliderImages = []
sliderOptions = []
sliderOptionsDeafults = {
	mode: 'standard', //peopleRegistry, thumbnails
	listID: null, 
	galleryListID: null
}
sliderConfig = {}
sliderConfigDeafults = {
	speed: 6000, 
	animatonDuration: 1250,
	delayNextSlider: 3000
}
calendarConfig = {}
calendarConfigDefaults = {
	animationDuration: 750,
	dayNames: ['S', 'M', 'T', 'O', 'T', 'F', 'L'],
	monthNames: ['Januar','Februar','Mars','April','May','Juni', 'Juli','August','September','Oktober','November','Desember']
}
mediaOptions = []
mediaOptionsDefaults = {
	url: null, 
	descr: null, 
	opptak: null, 
	type: null, 
	width: null, 
	height: null
}
mediaConfig = {}
mediaConfigDefaults = {
	type: 'embed',
	width: '800', 
	height: '100'
}

$(document).ready(function(){
	
	trace = function(s) {
		try { console.log(s) } catch (e) { alert(s) }
	}
	
	trimMe = function(a, b){
		if(b==1) return a.replace(/^\s+/,'').replace(/\s+$/,'')
		else return encodeURI(a.replace(/^\s+/,'').replace(/\s+$/,'').replace(/\s/g, '-').toLowerCase())
	}
	
	hashURL = function(hash){
		if(hash!=null){
			window.location.href = window.location.href.replace(/#.*$/, '')+'#'+hash
			return true
		}
		else{
			hash = window.location.href.match(/\#(.*)$/)
			return hash==null ? null : hash[1]
		}
	}
	
	if($('#calendar').length){
		for(var k in calendarConfigDefaults){
			if(calendarConfig[k] == undefined) calendarConfig[k] = calendarConfigDefaults[k]
		}
		function checkConcert(date){
			current = '#day-'+$.datepicker.formatDate( 'dd-mm-yy', date)
			if($(current).length){
				return Array(true,'ui-datepicker-highlighted')
			}
			else return Array(true,'')
		}
	
		function dateSelected(dateText){
			chosen = '#day-'+dateText
			if($(chosen).length==0) chosen='#no-concert-msg'
			if($('#calendar .concert-list .day.chosen').length){
				$('#calendar .concert-list .day.chosen').stop(true,false).removeClass('chosen').fadeOut(calendarConfig.animationDuration/2, function(){
					$('#calendar .concert-list .day.chosen').fadeTo(calendarConfig.animationDuration/2, 1)
				})
				$(chosen).addClass('chosen')
			}
			else $(chosen).show().addClass('chosen', calendarConfig.animationDuration/2)
		}
		$('#datepicker').datepicker({
			inline: true,
			firstDay: 1,
			selectOtherMonths: true,
			showOtherMonths: true,
			dateFormat: 'dd-mm-yy',
			dayNamesMin: calendarConfig.dayNames,
			monthNames: calendarConfig.monthNames,
			onSelect: function(dateText, inst) {dateSelected(dateText)},
			beforeShowDay: function(date) {return checkConcert(date)}
		})
	
		$('#calendar').removeClass('no-js').addClass('js')
		$('#calendar .concert-list .day').hide()
		dateSelected($.datepicker.formatDate( 'dd-mm-yy', $('#datepicker').datepicker('getDate')))
	
	}
	
	if($('.sliderwrapper').length){
		for(var k in sliderConfigDeafults){
			if(sliderConfig[k] == undefined) sliderConfig[k] = sliderConfigDeafults[k]
		}
	
		makeImagesList = function(theS){
			sliderImages[theS] = []
			S = $('#'+theS)
			list = $('#'+sliderOptions[theS].listID)
			if(sliderOptions[theS].mode='peopleRegistry'){
				list.find('li').each(function(){
					ind = list.find('li').index(this)
					$(this).attr('id', 'list'+ind)
					sliderImages[theS][ind] = {
						image: $(this).find('a.image').attr('href'),
						link: $(this).find('a.link').attr('href'),
						thumb: $(this).find('a.image img') ? $(this).find('a.image img').attr('src') : null,
						name: $(this).find('.name') ? $(this).find('.name').html() : null,
						captionStyle: $(this).find('.name') && $(this).find('.name').attr('style') ? $(this).find('.name').attr('style') : null,
						email: $(this).find('a.name') ? $(this).find('a.name').attr('href') : null,
						info: $(this).find('.info') ? $(this).find('.info').html() : null
					}
				})
			}
			else{
				list.find('li').each(function(){
					ind = list.find('li').index(this)
					$(this).attr('id', 'list'+ind)
					sliderImages[theS][ind] = {
						image: $(this).find('a.image').attr('href'),
						thumb: $(this).find('a.image img').length ? $(this).find('a.image img').attr('src') : null,
						title: $(this).find('.title').length ? $(this).find('title').html() : null,
						author: $(this).find('.author').length ? $(this).find('author').html() : null
					}
				})
			}
		}
		
		function numSlide(theS, num){
			S = $('#'+theS)
			last = parseInt(sliderImages[S.attr('id')].length-1)
			if(num>last) return 0
			else if(num<0) return last
			else return parseInt(num)
		}
		
		photoLoaded = function(t){
			$(t).find('img').addClass('loaded')
		}
		
		function addSlide(theS, num, cls){
			S = $('#'+theS)
			num = numSlide(theS, num)
			if(S.find('#img'+num).length==0){
				S.prepend(
					'<div class="main-image'+(cls!=undefined ? ' '+cls : '')+'" id="img'+num+'">'
						+(sliderImages[theS][num].link ? '<a href="'+sliderImages[theS][num].link+'">' : '')
						+'<img alt="'+sliderImages[theS][num].title+'" onload="photoLoaded(\'#img'+num+'\');"/>'
						+(sliderImages[theS][num].link ? '</a>' : '')
						+(sliderOptions[theS].mode=='standard' || sliderOptions[theS].mode=='thumbnails' ? ''
							+'<div class="photo-descr">'
								+(sliderOptions[theS].mode=='thumbnails' ? '<span class="counter">'+(num+1)+'/'+sliderImages[theS].length+'</span>' : '')
								+'<span class="title">'+sliderImages[theS][num].title+'</span>'
								+'<span class="author">'+sliderImages[theS][num].author+'</span>'
							+'</div>' : ''
						)
						+(sliderOptions[theS].mode=='peopleRegistry' && sliderImages[theS][num].captionStyle ? ''
							+'<div class="photo-caption" style="'+sliderImages[theS][num].captionStyle+'">'
								+(sliderImages[theS][num].email ? '<a href="'+sliderImages[theS][num].email+'">' : '')
								+(sliderImages[theS][num].name ? '<span class="name">'+sliderImages[theS][num].name+'</span>' : '')
								+(sliderImages[theS][num].email ? '</a>' : '')
								+(sliderImages[theS][num].info ? '<div class="info">'+sliderImages[theS][num].info+'</div>' : '')
							+'</div>' : ''
						)
					+'</div>'
				)
				S.find('#img'+num+' img').load(function(e){
					$(this).addClass('loaded')
				}).attr('src', sliderImages[theS][num].image)
			}
		}
		
		checkQueue = function(theS){
			//trace('checkQueue: '+theS+', animated='+sliderOptions[theS].animated)
			S = $('#'+theS)
			if(sliderOptions[theS].animated==0 && sliderOptions[theS].queue && sliderOptions[theS].queue.length>0){
			//if(S.children('.main-image:animated').length==0 && sliderOptions[theS].queue && sliderOptions[theS].queue.length>0){
				changeSlide(theS, sliderOptions[theS].queue.shift())
			}
		}
		
		changeSlide = function(theS, dir, why){
			//trace('changeSlide: '+theS)
			S = $('#'+theS)
			if(sliderOptions[theS].animated>0 && why!='timer'){
			//if(S.children('.main-image:animated').length && why!='timer'){
				if(sliderOptions[theS].queue && sliderOptions[theS].mode!='peopleRegistry') sliderOptions[theS].queue.push(dir)
				else sliderOptions[theS].queue = [dir]
				return false
			}
			else if(sliderOptions[theS].animated>0) return false
			theWidth = S.width()
			current = parseInt(S.children('.current').attr('id').substring(3))
			last = sliderImages[theS].length-1
			direction = '-'
			if(dir=='next') nextSlide = numSlide(theS, current+1)
			else if(dir=='prev'){
				nextSlide = numSlide(theS, current-1)
				if(current==nextSlide) return false
				$('#img'+nextSlide).css('left', -theWidth)
				direction = '+'
			}
			else if(parseInt(dir)<current){
				nextSlide = numSlide(theS, dir)
				if(current==nextSlide) return false
				$('#img'+nextSlide).css('left', -theWidth)
				direction = '+'
			}
			else nextSlide = numSlide(theS, dir)
			if(current==nextSlide) return false
			//trace(dir)
			addSlide(theS, nextSlide)
			if(sliderOptions[theS].mode=='peopleRegistry' && $('#img'+nextSlide).find('img.loaded').length==0 ){
				sliderOptions[theS].queue = []
				clearInterval(sliderOptions[theS].preload)
				sliderOptions[theS].preload = setTimeout("changeSlide('"+theS+"', '"+dir+"', 'preloader')", 50)
				return false
			}
			else clearInterval(sliderOptions[theS].preload)
			
			sliderOptions[theS].animated +=2
			//trace('changeSlide animate from: '+S.children('.current').attr('id')+'@'+S.children('.current').css('left')+', '+S.children('#img'+nextSlide).attr('id')+'@'+S.children('#img'+nextSlide).css('left'))
			S.children('.current, #img'+nextSlide).animate({left: direction+'='+theWidth}, sliderConfig.animatonDuration, 'swing', function(){
				theS = $(this).parents('.sliderwrapper').attr('id')
				if($(this).hasClass('current')){
					//trace('callback: '+theS+' called by: '+$(this).attr('id'))
					sliderOptions[theS].animated -=1
					$(this).removeClass('current').css('left', '')
					checkQueue(theS)
					//setTimeout("checkQueue('"+theS+"')", 1)
				}
				else{
					//trace('callback: '+theS+' called by: '+$(this).attr('id'))
					sliderOptions[theS].animated -=1
					$(this).addClass('current').css('left', '')
					checkQueue(theS)
					//setTimeout("checkQueue('"+theS+"')", 1)
				}
			})
			
			if(sliderOptions[theS].mode=='standard'){
				S.find('#slider-pager > .current').removeClass('current')
				S.find('#slider-pager > a:nth('+nextSlide+')').addClass('current')
			}
			else if(sliderOptions[theS].mode=='thumbnails'){
				$('#thumb-carousel .current').removeClass('current')
				$('#thumb-carousel a:nth('+nextSlide+')').addClass('current')
				$('#img'+nextSlide+' .photo-descr .counter').text((nextSlide+1)+'/'+(last+1))
			}
			
			if (sliderOptions[theS].listID != null){
				$('#'+sliderOptions[theS].listID+' .current').removeClass('current')
				$('#'+sliderOptions[theS].listID+' li#list'+nextSlide).addClass('current')
			}
			
			addSlide(theS, nextSlide+1)
			addSlide(theS, nextSlide-1)
		}
		
		restartSlider = function(theS){
			$('#'+theS).children('.main-image').stop(true, false).remove()
			$('#thumb-carousel').add('#slider-navi').add('#carousel-navi').remove()
			clearInterval(sliderOptions[theS].timer)
			sliderOptions[theS].queue = []
			sliderImages[theS] = sliderImages[$(this).parent().attr('id')]
			startSlider(theS, true)
		}
		
		startCarousel = function(){
			$('#thumb-carousel').addClass('paginated').wrapInner('<div id="thumb-carousel-wrapper"></div>').after('<div id="carousel-navi"><a href="#" class="prev"></a><a href="#" class="next"></a></div>')
			$('#carousel-navi a.prev').hide()
			$('#carousel-navi a').click(function(e){
				e.preventDefault()
				if($('#thumb-carousel-wrapper:animated').length) return false;
				if($(this).hasClass('next')) sign = '-'
				else sign = '+'
				$('#thumb-carousel-wrapper').animate({marginLeft: sign+'='+$('#thumb-carousel').width()}, sliderConfig.animatonDuration, 'swing', function(){
						$('#carousel-navi a').show()
						if($('#thumb-carousel-wrapper').offset().left+$('#thumb-carousel-wrapper').width()<$('#thumb-carousel').offset().left+$('#thumb-carousel').width())
							$('#carousel-navi a.next').hide()
						else if($('#thumb-carousel-wrapper').offset().left>=$('#thumb-carousel').offset().left)
							$('#carousel-navi a.prev').hide()
				})
			})
		}
		
		sliderTimer = []
		startSlider = function(theS, restarted){
			if(restarted==null) restarted==false
			S = $('#'+theS)
			if(sliderOptions[theS] == undefined) sliderOptions[theS] = {}
			for(var k in sliderOptionsDeafults){
				if(sliderOptions[theS][k] == undefined) sliderOptions[theS][k] = sliderOptionsDeafults[k]
			}
			sliderOptions[theS].animated = 0
			if(hashURL()!=null && sliderImages[hashURL().substring(8)] !=undefined ){
				newS = hashURL().substring(8)
				sliderImages[theS] = sliderImages[newS]
				$('#'+sliderOptions[theS].galleryListID+' #'+newS).addClass('current')
				if($('#page-title .default').length==0) $('#page-title').append('<span class="default hidden">'+$('#page-title h1').text()+'</span>')
				$('#page-title h1').text($('#page-title .default').text()+' - '+trimMe($('#'+sliderOptions[theS].galleryListID+' #'+newS).text(), 1))
			}
			else if(sliderOptions[theS].galleryListID != null && sliderImages[theS] == undefined){
				sliderImages[theS] = sliderImages[$('#'+sliderOptions[theS].galleryListID+' .gallery:nth(0)').attr('id')]
				$('#'+sliderOptions[theS].galleryListID+' .gallery:nth(0)').addClass('current')
				if($('#page-title .default').length==0) $('#page-title').append('<span class="default hidden">'+$('#page-title h1').text()+'</span>')
				$('#page-title h1').text($('#page-title .default').text()+' - '+trimMe($('#'+sliderOptions[theS].galleryListID+' .gallery:nth(0)').text(), 1))
			}
			if(sliderOptions[theS].listID != null){
				makeImagesList(theS)
				$('#'+sliderOptions[theS].listID+' li#list0').addClass('current')
				$('#'+sliderOptions[theS].listID+' li').click(function(e){
					e.preventDefault()
					S = $(this).parents('.sliderlist').siblings('.sliderwrapper')
					theS = S.attr('id')
					ind = $(this).attr('id').substring(4)
					clearInterval(sliderOptions[theS].timer)
					sliderOptions[theS].timer = setInterval("changeSlide('"+theS+"', 'next', 'timer')", sliderConfig.speed)
					changeSlide(theS, ind)
				})
				if(sliderOptions[theS].mode=='peopleRegistry'){
					$('body').append($('<img id="peopleRegistryThumb" class="hidden" src="'+sliderImages[theS][0].thumb+'" style="position: absolute; z-index: 100; display: none;" />'))
					$('#'+sliderOptions[theS].listID+' li').hover(
						function(){
							theS = $(this).parents('.sliderlist').siblings('.sliderwrapper').attr('id')
							ind = $(this).attr('id').substring(4)
							if(sliderImages[theS][ind].thumb){
								$('#peopleRegistryThumb').attr('src', sliderImages[theS][ind].thumb).show()
							}
						},
						function(){
							$('#peopleRegistryThumb').hide()
						}
					)
					$('#'+sliderOptions[theS].listID+' li').mousemove(function(e){
						if($('#peopleRegistryThumb').is(':visible')) $('#peopleRegistryThumb').css({'left': e.pageX+20,'top': e.pageY+20})
					})
				}
			}
			ind = $('.sliderwrapper').index(S)
			last = sliderImages[theS].length-1
			if(sliderOptions[theS].mode=='peopleRegistry'){
				S.append($('<div id="slider-navi"><a href="#" class="prev"></a><a href="#" class="next"></a></div>') )
				S.find('#slider-navi .prev').click(function(e){e.preventDefault(); changeSlide($(this).parents('.sliderwrapper').attr('id'), 'prev')})
				S.find('#slider-navi .next').click(function(e){e.preventDefault(); changeSlide($(this).parents('.sliderwrapper').attr('id'), 'next')})
			}
			else if(sliderOptions[theS].mode=='standard'){
				S.append($('<div id="slider-navi"><a href="#" class="prev"></a><a href="#" class="next"></a></div>') )
				S.find('#slider-navi .prev').click(function(e){e.preventDefault(); changeSlide($(this).parents('.sliderwrapper').attr('id'), 'prev')})
				S.find('#slider-navi .next').click(function(e){e.preventDefault(); changeSlide($(this).parents('.sliderwrapper').attr('id'), 'next')})
				S.append($('<div id="slider-pager"></div>') )
				for(var i=0; i<sliderImages[theS].length; i++){
					S.find('#slider-pager').append($('<a href="#"></a>'))
				}
				S.find('#slider-pager a:first-child').addClass('current')
				S.find('#slider-pager a').click(function(e){e.preventDefault(); changeSlide($(this).parents('.sliderwrapper').attr('id'), $('#slider-pager a').index(this))})
			}
			else if(sliderOptions[theS].mode=='thumbnails'){
				if(!restarted){
					$('#'+sliderOptions[theS].galleryListID+' .gallery a').click(function(e){
						e.preventDefault()
						theS = $(this).parents('.gallerylistwrapper').siblings('.sliderwrapper').attr('id')
						$(this).parents('.gallerylistwrapper').find('.current').removeClass('current')
						$(this).parent().addClass('current')
						hashURL('gallery-'+$(this).parent().attr('id'))
						restartSlider(theS)
					})
				}
				S.append($('<a href="#" id="fullscreen"></a>'))
				S.after($('<div id="slider-navi"><a href="#" class="prev"></a><a href="#" class="next"></a></div>') )
				$('#slider-navi .prev').click(function(e){e.preventDefault(); changeSlide($(this).parent().siblings('.sliderwrapper').attr('id'), 'prev')})
				$('#slider-navi .next').click(function(e){e.preventDefault(); changeSlide($(this).parent().siblings('.sliderwrapper').attr('id'), 'next')})
				S.after($('<div id="thumb-carousel"></div>') )
				for(var i=0; i<sliderImages[theS].length; i++){
					$('#thumb-carousel').append($('<a href="#"><img src="'+sliderImages[theS][i].thumb+'" /></a>'))
				}
				$('#thumb-carousel a:first-child').addClass('current')
				$('#thumb-carousel a').click(function(e){
					e.preventDefault(); 
					theS = $(this).parents('#thumb-carousel').siblings('.sliderwrapper').attr('id')
					clearInterval(sliderOptions[theS].timer)
					sliderOptions[theS].timer = setInterval("changeSlide('"+theS+"', 'next', 'timer')", sliderConfig.speed)
					changeSlide(theS, $('#thumb-carousel a').index(this))
				})
				$('#thumb-carousel a').hover(
					function(){
						theS = $(this).parents('#thumb-carousel').siblings('.sliderwrapper').attr('id')
						clearInterval(sliderOptions[theS].timer)
					}, 
					function(){
						theS = $(this).parents('#thumb-carousel').siblings('.sliderwrapper').attr('id')
						clearInterval(sliderOptions[theS].timer)
						sliderOptions[theS].timer = setInterval("changeSlide('"+theS+"', 'next', 'timer')", sliderConfig.speed)
					}
				)
				if(sliderImages[theS].length>12) startCarousel()
			}
			if(S.children('.current').length>0) S.children('.current').attr('id', 'img0')
			else addSlide(theS, 0, 'current')
			addSlide(theS, 1)
			addSlide(theS, last)
		
			if(ind==0) sliderOptions[theS].timer = setInterval("changeSlide('"+theS+"', 'next', 'timer')", sliderConfig.speed)
			else sliderOptions[theS].timerInitial = setTimeout("changeSlide('"+theS+"', 'next', 'timer'); sliderOptions['"+theS+"'].timer = setInterval(\"changeSlide('"+theS+"', 'next', 'timer')\", sliderConfig.speed)", sliderConfig.delayNextSlider*ind)
		
			S.hover(
				function(){
					clearTimeout(sliderOptions[$(this).attr('id')].timerInitial)
					clearInterval(sliderOptions[$(this).attr('id')].timer)
				}, 
				function(){
					clearInterval(sliderOptions[$(this).attr('id')].timer)
					sliderOptions[$(this).attr('id')].timer = setInterval("changeSlide('"+$(this).attr('id')+"', 'next', 'timer')", sliderConfig.speed)
				})
		}
		
		$('.sliderwrapper').each(function(){
			startSlider($(this).attr('id'))
		})
		
	}

	if($('.accordion').length){
		
		initFilters = function(acc){
			if($('#filter-news').length && $(acc).is('.filtered')){
				changeFilter = function(f, e){
					A = $('.accordion.filtered')
					Sh = A.find('.'+$(f).attr('id'))
					//A.accordion('activate', false)
					$(f).siblings().removeClass('current')
					$(f).addClass('current')
					A.children().removeClass('ui-accordion-header-odd').removeClass('ui-accordion-content-odd').not(Sh).filter('h3').hide()
					Sh.filter('h3').show()
					A.find('.ui-accordion-header:visible:odd').addClass('ui-accordion-header-odd').next().addClass('ui-accordion-content-odd')
					$('.accordion.filtered').accordion( "option", "animated", false)
					//if(Sh.length>0) $('.accordion.filtered').accordion('activate', A.find('h3').index(Sh.filter('h3')[0]))
					//else $('.accordion.filtered').accordion("option", "collapsible", true).accordion('activate', false).accordion("option", "collapsible", false)
					$('.accordion.filtered').accordion( "activate", e!=null ? e : false)
					$('.accordion.filtered').accordion( "option", "animated", 'slide')
				}
				$('.accordion.filtered h3').each(function(){
					H = $(this)
					C = H.next().find('.categories li')
					H.addClass('cat-vis-alle')
					H.next().addClass('cat-vis-alle')
					C.each(function(){
						if($(this).attr('class')!=undefined) cl = 'cat-'+$(this).attr('class')
						else cl = 'cat-'+trimMe($(this).text())
						H.addClass(cl)
						//H.next().addClass(cl)
					})
				})
				F = $('#filter-news')
				F.append('<a href="#" id="cat-vis-alle" class="current">Vis alle</a>')
				for(var i in newsCategories){
					ncn = (i.length>2 ? i : trimMe(newsCategories[i]))
					F.append('<a href="#" id="cat-'+ncn+'">'+newsCategories[i]+'</a>')
				}
				F.find('a').click(function(e){
					e.preventDefault()
					changeFilter(this)
				})
				if(accHash()[0]!=false && accHash()[0].length>0){
					changeFilter($('#filter-news #cat-'+accHash()[0]), accHash()[2].length>0 ? '#'+accHash()[2] : false)
				}
			}
		}
	
		accHash = function(acc, elem){
			if($(acc).hasClass('no-url-change')) return false
			else if(acc!=null){
				accNr = $('.accordion').index(acc)
				filt = $(acc).is('.filtered') ? $('#filter-news a.current').attr('id').substring(4) : ''
				elemName = elem!=undefined && $(elem).length ? $(elem).attr('id') : ''
				//elemName = elem!=undefined && $(elem).length ? $(elem).find('a[href]:nth(0)').clone().find('span').remove().end().text() : ''
				//elemNr = elemName!= '' ? $(acc).find('h3').index(elem) : ''
				hashURL(filt+'/'+elemName)
				return true
			}
			else{
				if(hashURL()!=null && hashURL().length>0){
					h = hashURL().split('/')
					return [h[0], 0, h[1]]
				}
				else return [false, false, false]
			}
			
		}
		
		$('.accordion').accordion({
			autoHeight: false,
			collapsible: true, 
			active: false,
			change: function(event, ui){
						accHash(this, ui.newHeader)
			},
			create: function(){
						if($(this).hasClass('start-odd')){
							$(this).find('.ui-accordion-header:even').addClass('ui-accordion-header-odd')
							$(this).find('.ui-accordion-content:even').addClass('ui-accordion-content-odd')
						}
						else{
							$(this).find('.ui-accordion-header:odd').addClass('ui-accordion-header-odd')
							$(this).find('.ui-accordion-content:odd').addClass('ui-accordion-content-odd')
						}
						$(this).find('.ui-accordion-header').each(function(){
							if($(this).attr('id')==undefined){
								newID = trimMe($(this).find('a[href]:nth(0)').clone().find('span').remove().end().text())
								$(this).attr('id', newID)
							}
						})
						if($(this).attr('id')==undefined) $(this).attr('id', 'news-'+$(this).find('h3:nth(0)').attr('id'))
						if(accHash()[1]==$('.accordion').index(this) && accHash()[2].length>0){
							$('#'+accHash()[2]+' img').load(function(){
								$('.accordion').accordion('resize')
							})
							$(this).accordion( "activate", '#'+accHash()[2])
							initFilters('#'+$(this).attr('id'))
							$(window).scrollTop($('#'+accHash()[2]).offset().top)
						}
						else{
							$(this).accordion( "activate", false)
							initFilters('#'+$(this).attr('id'))
						}
			}
		})
		$(window).load(function(){
			$('.accordion').accordion('resize')
		})
		$('.accordion .btn-kjop').click(function(){window.location.href = $(this).attr('href')})
	}
	
	if($('#gallery-list .gallery').length){
		$('#gallery-list').prepend('<ul class="bars fifth start-odd"></ul>')
		for(var i=0; i<5; i++) $('#gallery-list ul.bars').append('<li></li>')
		for(var k in $('#gallery-list .gallery')){
			$('#gallery-list .gallery:nth('+k+')').appendTo('#gallery-list ul.bars li:nth('+((k)%5)+')')
		}
	}
	
	$('ul.bars').each(function(){
		if($(this).hasClass('start-odd')) $(this).find('li:even').addClass('odd')
		else $(this).find('li:odd').addClass('odd')
	})
	$('.people-registry .group').each(function(){
		if(($(this).parent().find('.group').index(this)+1) % 5 == 0) $(this).after('<div class="float-clear"></div>')
		else if($(this).is(':last-child')) $(this).after('<div class="float-clear"></div>')
	})
	
	if($('#lyd-layout .gallery').length){
		embedSound = function(obj){
			id = $(obj).attr('id')
			url = mediaOptions[id].url != null ? mediaOptions[id].url : $(obj).find('a').attr('href')
			descr = mediaOptions[id].descr != null ? mediaOptions[id].descr : $(obj).find('.descr').text()
			opptak = mediaOptions[id].opptak != null ? mediaOptions[id].opptak : $(obj).find('.opptak').text()
			if(mediaOptions[id].width != null) width = mediaOptions[id].width
			else if($(obj).find('.size').length) width = parseInt($(obj).find('.size').css('width'))
			else width = mediaConfig.width
			if(mediaOptions[id].height != null) height = mediaOptions[id].height
			else if($(obj).find('.size').length) height = parseInt($(obj).find('.size').css('height'))
			else height = mediaConfig.height
			if(mediaOptions[id].type=='iframe' || (mediaOptions[id].type==null && mediaConfig.type=='iframe') || $(obj).hasClass('iframe')){
				$('#lyd-slider').empty().append(''
					+'<iframe src="'+url+'" width="'+width+'" height="'+height+'" frameborder="0"></iframe>'
					+'<div class="audio-descr">'
						+'<span class="descr">'+descr+'</span> '
						+'<span class="opptak">'+opptak+'</span>' 
					+'</div>' 
				)
			}
			else{
				$('#lyd-slider').empty().append(''
					+'<object height="'+height+'" width="'+width+'">'
						+'<param name="movie" value="'+url+'"></param> '
						+'<param name="allowscriptaccess" value="always"></param> '
						+'<embed allowscriptaccess="always" height="'+height+'" src="'+url+'" type="application/x-shockwave-flash" width="'+width+'"></embed> '
					+'</object>  '
					+'<div class="audio-descr">'
						+'<span class="descr">'+descr+'</span> '
						+'<span class="opptak">'+opptak+'</span>' 
					+'</div>' 
				)
			}
			$('#lyd-navi a').height($('#lyd-slider').height()+parseInt($('#lyd-slider').css('paddingTop'))+parseInt($('#lyd-slider').css('paddingBottom')))
			$('#lyd-slider .audio-descr').width(width - 2*parseInt($('#lyd-slider .audio-descr').css('paddingLeft')))
		}
		changeSound = function(id){
			if(mediaOptions[id] == undefined) mediaOptions[id] = {}
			for(var k in mediaOptionsDefaults){
				if(mediaOptions[id][k] == undefined) mediaOptions[id][k] = mediaOptionsDefaults[k]
			}
			id = $('#'+id)
			$(id).parents('.gallerylistwrapper').find('.current').removeClass('current')
			$(id).addClass('current')
			hashURL('lyd-'+$(id).attr('id'))
			if($('#page-title .default').length==0) $('#page-title').append('<span class="default hidden">'+$('#page-title h1').text()+'</span>')
			$('#page-title h1').text($('#page-title .default').text()+' - '+trimMe($(id).find('a').text(), 1))
			embedSound(id)
		}
		$('#lyd-layout .gallery a').click(function(e){
			e.preventDefault()
			changeSound($(this).parent().attr('id'))
		})
		$('#lyd-slider').after($('<div id="lyd-navi"><a href="#" class="prev"></a><a href="#" class="next"></a></div>') )
		$('#lyd-navi a').click(function(e){
			e.preventDefault(); 
			dir = $(this).hasClass('next') ? 'next' : 'prev'
			L = $(this).parent().siblings('.gallerylistwrapper').find('.gallery')
			G = L.filter('.current')
			if(dir=='next' && L.index(G)==L.length-1) changeSound($(L[0]).attr('id'))
			else if(dir=='prev' && L.index(G)==0) changeSound($(L[L.length-1]).attr('id'))
			else if(dir=='next') changeSound($(L[L.index(G)+1]).attr('id'))
			else changeSound($(L[L.index(G)-1]).attr('id'))
		})
		for(var k in mediaConfigDefaults){
			if(mediaConfig[k] == undefined) mediaConfig[k] = mediaConfigDefaults[k]
		}
		if(hashURL()!=null && $('#'+hashURL().substring(4)).length>0 ){
			changeSound(window.location.href.match(/\#(.*)$/)[1].substring(4))
		}
		else{
			changeSound($('#lyd-layout .gallery:nth(0)').attr('id'))
		}
	}

})


