$(document).ready(function() {
	if ($("div#LiveEditingToolbar").length <= 0) {
		$('body').attr('class','jsOn');
		$('#pagesponsor').sponsors();
		$('#leden').accordion($.LedenAjax);
		$('#linken').accordion();
		$('#kalender').accordion();
		$('#gastenboek a').guestbook();
		$.MapMediaFormats();
		$('.audio').mediaplayer();
	}
	});

$(window).load(function() {
	$('#gallery img').reflection();
		});
	

(function($) {
	//extending jQuery
	$.fn.extend({
		guestbook : function() {
			return this.each(function() {
				$.CreateLoadingPanel();
				$(this).click(function() {
					$.ToggleLoadingPanel();
					$.get($(this).attr('href'),function(data) {
						$('#gastenboek').html($(data).find('#gastenboek').html());
						$('html,body').animate({scrollTop:0}, 'slow');
						$('#gastenboek a').guestbook();
						$.ToggleLoadingPanel();
						});
					return false;
					});
				});
			},
			
		mediaplayer : function() {
			return this.each(function() {
				$(this).click(function() {
					$(this).media({autoplay:true,height:45,width:225,caption:false});
					return false;
					});
				});
			},
			
		reflection : function() {
			return this.each(function() {
				if($.browser.msie && $.browser.version<7) return;
				Reflection.add(this,{height: 0.5, opacity :0.5});
				});
			},
			
		accordion : function(ajaxcallback) {
			return this.each(function() {
				var accordion = $(this);			
				accordion.attr('opensection',function() { return accordion.find('div.sectiontitle:first').attr('id');});
				if(accordion.find('div.sectiontitle a').length==0) {
					accordion.find('div.sectioncontent').hide();
					accordion.find('div.sectioncontent:first').show();
					accordion.find('div.sectiontitle').each(function() {$(this).html('<a href="#">' + $(this).text() + '</a>')});
					}
				accordion.find('div.sectiontitle a').click(function() {
					var link = $(this);
					var section = link.parent()
					var opensection = '#' + accordion.attr('opensection');
					var newsection =  section.attr('id');
					if(opensection ==  '#' + newsection) return false;
					if(section.next().length == 0) {
						if(ajaxcallback) ajaxcallback(link,section);
						$(opensection).next().slideToggle('slow');
						}
					else {
						$(opensection).next().hide('slow');
						section.next().show('slow');
					}
					accordion.attr('opensection',newsection);
					return false;
					});
				});
			},
			
		sponsors : function() {			
			return this.each(function() {
				var sponsor = $(this);
				var url = '/sponsors/SponsorsAjax.aspx?url=' + document.location.pathname;
				//'/sponsors.aspx?altTemplate=SponsorsAjax&url=' + document.location.pathname
				$.get(url,function(data) {	
					if($(data).length > 1) {
						var current = sponsor.find('h2').eq(0).attr('id');						
						var index = 0;
						var titles = $(data).find('h2');
						titles.each(function() {							
							if($(this).attr('id') == current) index = titles.index(this);
							});						
						setTimeout(function() {$.SponsorScroll(sponsor,$(data),index+1);},10000);
						}
					});
				});
			}
	});
	
	//custom methods
	$.extend({
		ToggleLoadingPanel : function() {
			$('#loadingPanel').toggle();
			},
			
		CreateLoadingPanel : function() {			
			$('#container:not(#loadingPanel)').append('<div id="loadingPanel" style="display:none;"><div id="progressBackgroundFilter"></div><div id="processMessage"><img src="/images/loadingAnimation.gif" wdith="208" height="13" /></div></div>');
			},
			
		MapMediaFormats : function() {
			if($.fn.media) {
				$.fn.media.mapFormat('mp3','winmedia');
				}
			},
					
		LedenAjax : function(link,section) {
			var url = link.attr('href').replace('.aspx','/MuzikantenAjax.aspx');		
			$.get(url,function(data) {
				section.after(data);	
				section.next().find('a').click(function(){
					var t = this.title || this.name || null;
					var a = this.href || this.alt;
					var g = this.rel || false;
					tb_show(t,a,g);
					this.blur();
					return false;
					});								
				});
			},
			
		SponsorScroll : function(elem,data,current) {
			if(current > data.length-1)  {
				current=0;
				}
			elem.fadeOut('slow',function() {elem.html(data.eq(current).html());});
			
			elem.fadeIn('slow');
			setTimeout(function() {$.SponsorScroll(elem,data,current+1);},10000);
			}
		
	});
}(jQuery));	