$(function() {
	
	var $body = $('body');

    // Open external links in a new browser window:    
    $("a[rel*='external'], a.external").attr('target','_blank');

    // Top:
    $("a.top").click(function() {
        $('html, body').animate({ scrollTop:0 }, 'slow');
        return false;
    });

	$.supersized({
		start_slide				:	0,		//Start slide (0 is random) //Requires multiple background images
		vertical_center         :   1,		//Vertically center background
		horizontal_center       :   1,		//Horizontally center background
		min_width		        :   0,		//Min width allowed (in pixels)
		min_height		        :   0,		//Min height allowed (in pixels)
		fit_portrait         	:   0,		//Portrait images will not exceed browser height
		fit_landscape			:   0,		//Landscape images will not exceed browser width
		slides					:   [ 		//Background image
									  		{ image : '/wp-content/themes/musserbuilder/images/backgrounds/bg1.jpg' },
									  		{ image : '/wp-content/themes/musserbuilder/images/backgrounds/bg2.jpg' },
									  		{ image : '/wp-content/themes/musserbuilder/images/backgrounds/bg3.jpg' },
									  		{ image : '/wp-content/themes/musserbuilder/images/backgrounds/bg4.jpg' }
									]					
	});
	
	$("a.lightbox").fancybox({
		cyclic: true,
		hideOnContentClick: true,
		overlayColor: '#2e160b',
		overlayOpacity: 0.75,
		showCloseButton: false,
		titlePosition: 'over'
	});
	
	if ($body.is('.home')) {

		// Initialize slides:
		$('#tabs').find('li:first-child').hide().appendTo($('#tabs'));
		$('#slides').find('li').hide().eq(0).show();
		
		// Swap on click
		$('#tabs').find('a').click(function() {
			var $this_slide = $($(this).attr('href'));

			clearInterval(rotator);
			
			$(this).closest('li').hide().appendTo($('#tabs')).end().siblings().show();
			
			$('#slides').find('li:visible').fadeOut(100, function() {
				$this_slide.fadeIn(400);
			});
			
			return false;
		});
		
		// Cycle through slides:
		var rotator = setInterval(function() {
			var $this_slide = $('#slides').find('li:visible');
			var $next_slide;

			if ($this_slide.next().length == 0) {
				$next_slide = $this_slide.siblings().eq(0);
			} else {
				$next_slide = $this_slide.next();
			}
			
			$this_slide.fadeOut(100, function() {
				$next_slide.fadeIn(400);
			});
			
			$('#tabs').find('a[href$="' + $next_slide.attr('id') + '"]').closest('li').hide().appendTo($('#tabs')).end().siblings().show();
			
		}, 15000); // 15 seconds
	}

});
