(function(){

  var d = document, e = d.documentElement, s = d.createElement('style');

  if ( (e.style.MozTransform === '') || (e.style.OTransform === '') ){
    s.textContent = 'body{visibility:hidden}';
    var r = document.getElementsByTagName('script')[0];
    r.parentNode.insertBefore(s, r);
    function f(){ s.parentNode && s.parentNode.removeChild(s); }
    addEventListener('load',f,false);
    setTimeout(f,3000); 
  }
})();

var playlistIndex=0;
var $player;
var gallery;
var loaded = false;

function play(index){
	
	if (index == playlist.length)
		index = 0;

	if (index < 0)
		index = playlist.length - 1;

	$player.jPlayer('setMedia',{
		mp3:playlist[index].mp3,
		oga:playlist[index].oga
	}).jPlayer('play');
	
	$('a[title="' + playlistIndex + '"]').removeClass('active');	
	
	playlistIndex = index;
	
	$('a[title="' + playlistIndex + '"]').addClass('active');
}

function isIE8() {
	return ( $.browser.msie && parseInt($.browser.version) == 8 )	
}

function switchCSS(node){

	var pageClass = node.attr('class');

	if(isIE8() && loaded)
		$('#content').css({'position':'relative', 'left':'-1px'});

	
	if(pageClass=='blog') {
		switchBlog2();
	}
	else if (pageClass!='blog') {
		switchOther2();
	}

}


function switchBlog2() {
	if(isIE8())	
		$('html').css('background', '#f2f0e0 url(http://www.skylarks.de/assets/templates/main/images/skylarks_blog.jpg) no-repeat top center fixed');
	else
		$('html').css('background', '#f2f0e0 url(http://www.skylarks.de/assets/templates/main/images/blog_1065_1.jpg) no-repeat top center fixed');
	
	$('#main').css('border-bottom','0px');
	$('#main').css('border-right','0px');
	$('.main-left').css({'border-bottom':'2px black solid'});
	$('.main-right').css('border-left','0px');
	
	$('#content').css({'border-left':'2px black solid','border-right':'2px black solid'});

	$('#links').css({'border-left':'2px black solid','border-right':'2px black solid','border-bottom':'2px black solid', 'background':'#fff'});
	
	$('#nav').css({'border-left':'2px black solid','border-right':'2px black solid'});
	
	
	$('#content').css({'background':'rgba(255,255,255,0.9)'});	
	
	$('.main-right').css('background','rgba(255,255,255,0)');
		
	$('#background').css({'-moz-box-shadow':'0 0 0px #695550', '-webkit-box-shadow':'0 0 0px #695550', 'box-shadow':'0 0 0px #695550'});
	
	if ( isIE8() )
	{
		$('html').css('height', $('#content').height() * 1.5);
		$('#content').css({'background':'#fff'});		
		$('.main-right').css('background','');
	}
	
	var main_left =	$('.main-left');
	main_left.width(167+2);
	main_left.height(258-2);
}

function switchOther2() {
	$('html').css('background', '#f2f0e0');
	$('#content').css({'background':'rgba(255,255,255,0)'});
	
	if($.browser.mozilla)
		$('#links').css({'border-bottom':'0px', 'background':'#fff'});
	
	
	$('#main').css({'border-bottom':'2px black solid','border-right':'2px black solid'});
	$('.main-left').css('border-bottom','0px');
	$('.main-right').css({'border-left':'2px black solid'});
	
	$('#content').css({'border-left':'0px','border-right':'0px'});
	
	$('#links').css({'border-left':'0px','border-right':'0px','border-bottom':'0px','border-top':'0px'});
	
	$('#nav').css({'border-left':'0px','border-right':'0px','border-bottom':'0px','border-top':'0px'});
	
	$('.main-right').css('background','rgba(255,255,255,0.9)');
	
	var shadow = '1px 1px 9px #cacaac';
	
	$('#background').css({'-moz-box-shadow':shadow, '-webkit-box-shadow':shadow, 'box-shadow':shadow}); 
	

	if ( isIE8() )
	 {
		$('html').css('height', '100%');
		$('#content').css({'background':''});
		$('.main-right').css('background','#fff');
	 }							
	
	var main_left =	$('.main-left');
	main_left.width(167);	
	main_left.height(258);
}


(function(){
	$.fn.bindLinks=function(){

		return this.each(function(){
			$('a',this).bind('click',function(){
		
					var a=this;

					$.ajax({
						url:this.href,
						dataType:'html',
						success:function(data){		
													
							$('div#background').css('opacity',0);
										
							$('div.main-right').css('opacity',0).html($('div.main-right > *',data)).animate({opacity:1}, 8);
							
							var node = $('div#page',data);
							var pageClass = node.attr('class');
							$("#background").removeClass();
							$("#background").addClass(pageClass);
						
							// Change Background etc.
					
							switchCSS(node);
							

							$('div#background').animate({opacity:1}, 800);


							
							$('#nav').bindLinks();
							$('li.ajax').bindLinks();
							
							if($('a[title="Home"]').hasClass('active') && $('#sub-nav li.active').length==0)
								$('#sub-nav li.first').addClass('active');

							if($('a[href^="saenger"]').hasClass('active') && $('#sub-nav li.active').length==0)
								$('#sub-nav li.first').addClass('active');
							
							if(pageClass=='impressum')
								$('a[title="impressum"]').addClass('active');
							
							if(pageClass=='kontakt')
								$('a[title="kontakt"]').addClass('active');


							if(pageClass=='anhoeren') {

								$('a[title="' + playlistIndex + '"]').addClass('active');
								
								$('a.play').bind('click',function(evt){	
									
																	
									play(Number(this.title)); 
									
									evt.preventDefault();
									return false;
								});					
													
							}
							
							if(pageClass=='anschauen') {
			
							    var opts = {"delay":2500,"numThumbs":10,"preloadAhead":10,"enableTopPager":false,"enableBottomPager":false,"maxPagesToShow":7,"thumbsContainerSel":"#gal-gaff-thumbs","imageContainerSel":"#gal-gaff-slideshow","captionContainerSel":"#gal-gaff-caption","controlsContainerSel":"#gal-gaff-controls","loadingContainerSel":"#gal-gaff-loading","renderSSControls":false,"renderNavControls":true,"playLinkText":"Play Slideshow","pauseLinkText":"Pause Slideshow","prevLinkText":"&lsaquo; Previous Photo","nextLinkText":"Next Photo &rsaquo;","nextPageLinkText":"Next &rsaquo;","prevPageLinkText":"&lsaquo; Prev","enableHistory":false,"autoStart":false,"syncTransitions":true,"defaultTransitionDuration":500,"navigationWidth":"585px","onMouseOutOpacity":"0.67","album":"Anschauen","tag":"","plugin":"galleriffic","pluginPath":"","thumbTpl":"galItemThumb","containerTpl":"","itemCls":"gal-item","toPlaceholder":"","thumbWidth":"100","thumbHeight":"100","thumbZoomCrop":true,"thumbFar":"C","thumbQuality":90,"thumbProperties":"","linkToImage":"","imageGetParam":"galItem","imageWidth":"587px","imageHeight":"307px","imageZoomCrop":false,"imageFar":"","imageQuality":90,"imageProperties":"","sort":"rank","dir":"ASC","limit":0,"start":0,"showInactive":false,"checkForRequestAlbumVar":true,"albumRequestVar":"galAlbum","checkForRequestTagVar":true,"tagRequestVar":"galTag","useCss":true,"gallerifficThumbTpl":"galleryThumbChunkTpl","gallerifficContainerTpl":"galleryChunkTpl","gallerifficThumbWidth":"115","gallerifficThumbHeight":"76"};
							    var slidesChanged = 0;
							    
							    $('#gal-gaff-slideshow').css('opacity',0);
							    $('#photo-index').hide();
							    
							    $('div.galleriffic div.navigation').css({ 'width' : opts.navigationWidth, 'float' : 'left' });
							    $('div.galleriffic div.gal_main').css('display', 'block');
							    
							    var onMouseOutOpacity = opts.onMouseOutOpacity;
							    $(opts.thumbsContainerSel+' ul.thumbs li').opacityrollover({
							        mouseOutOpacity:   onMouseOutOpacity
							        ,mouseOverOpacity:  1.0
							        ,fadeSpeed:         'fast'
							        ,exemptionSelector: '.selected'
							    });
							
							    opts.onSlideChange = function(prevIndex, nextIndex) {
							        this.find('ul.thumbs').children()
							            .eq(prevIndex).fadeTo('slow', onMouseOutOpacity).end()
							            .eq(nextIndex).fadeTo('slow', 1.0);
	
   								    // Update the photo index display
									    $('#photo-index').html((nextIndex+1) +' / '+ this.data.length);
									    slidesChanged++;
								      
									    if(slidesChanged == 2) {
										    $('#gal-gaff-slideshow').animate({opacity:1}, 800);
									       /*$('#photo-index').animate({opacity:1}, 800);		*/		
									       $('#photo-index').show();
									    }   
							    };
							    opts.onPageTransitionOut = function(callback) { this.fadeTo(0, 0.0, callback); };
							    opts.onPageTransitionIn = function() { this.fadeTo('slow', 1.0); };
							    gallery = $(opts.thumbsContainerSel).galleriffic(opts);
							    							
							    $('a.pageLinkprev').click(function(e) {
								    gallery.previousPage();
								    e.preventDefault();
							    });
							    
							    $('a.pageLinknext').click(function(e) {
								    gallery.nextPage();
								    e.preventDefault();
							    });
							}
						}				
					})

				return false;
			});
		});
	};
})();

$(document).ready(function(){

	
	$player = $("#jquery_jplayer_1").jPlayer({
		ready: function () {
			play(Math.floor(Math.random() * playlist.length));
		},
		ended: function (event) {
			play(playlistIndex + 1);
		},
		volume:0.50,

		cssSelectorAncestor:'.jp-interface',
		cssSelector:{
			play:'.jp-play',
			pause:'.jp-pause'

		},
		swfPath: 'http://avenu.de/modx/assets/templates/main/js/',
		errorAlerts:false,
		warningAlerts:false
	});

	$('.jp-previous').bind('click',function(evt){
		play(playlistIndex - 1);
		evt.preventDefault();
	});
	
	$('.jp-next').bind('click',function(evt){
		play(playlistIndex + 1);
		evt.preventDefault();
	});

	$('a.play').bind('click',function(evt){	
		play(Number(this.title));
		evt.preventDefault();
	});
	
	
	$('#nav').bindLinks();
	$('li.ajax').bindLinks();
	

	if($('a[title="Home"]').hasClass('active') && $('#sub-nav li.active').length==0)
		$('#sub-nav li.first').addClass('active');

	if($('a[href^="saenger"]').hasClass('active') && $('#sub-nav li.active').length==0)
		$('#sub-nav li.first').addClass('active');

	switchCSS($('div#page'));
	loaded = true;
});
