jQuery(document).ready(function($) { 

	/* MISC FIXES */
	//$('.category-featured-artists .entry-content img').parent('p').hide();

	/* BACKGROUND */
	$(window).load(function(){

		scaleBg();
		
		var winW = $(window).width();
		var winH = $(window).height();		
		
		$(window).resize(function(){
			var winH2 = $(window).height();
			var winW2 = $(window).width();
			if( winH2 > winH || winW2 > winW ) {
				scaleBg();
			}
		});	
	});

	/* --------------- GALLERY STUFF ------------------ */
	
	// FRONT PAGE CAROUSEL
	if( $('body').hasClass('home') ) {
		$('#front-image img').unwrap('p');

		function carousel()
		{
			var pause = 3500;
			var fade = 1500;
			var last = $('#front-image img:visible').eq(-1);
			if( last.prevAll().length ) last.delay(pause).fadeOut(fade, function(){
				carousel();
			});
			else $('#front-image img').delay(pause).fadeIn(fade, function(){
				carousel();
			});
		}
		$(window).load(function(){
			carousel();
		});
	}	
	
	/* MOVE IMAGES FROM BODY TO GALLERY */
	if( $('body').hasClass('single') && $('#breadcrumbs:contains("Featured Artists")') ) {
		if( $('.gallery').length < 1 ) {
			$('#gallery').append('<div class="gallery"/>');
			$('.post p img').unwrap();
			$('.post img').appendTo('.gallery');		
		}
		else {
			$('.gallery').appendTo('#gallery');
		}
	}
		
	/* MAIN SLIDING GALLERY */
	sliderGallery();


	/* CATEGORY LIST GALLERY */
	if( $('body').hasClass('category-gallery') || $('body').hasClass('single') && $('#breadcrumbs:contains("Gallery")') ) {
		$('.gallery img').each(function(){
			$(this).clone().appendTo('.below .thumbs');
		});
		$('.below .thumbs img').show().wrap('<div class="thumb"/>').click(function(){			
			var img = $(this).parent('.thumb').prevAll().length;
			$('.gallery .index a').eq(img).trigger('click');
		});
	}


	function sliderGallery() 
	{
		$('.gallery').before('<div class="gallery2"><div class="viewer"></div><div class="nav"><div class="index"></div><div class="caption"></div></div></div>');
		$('.gallery img').appendTo('.gallery2 .viewer');
		$('.gallery').remove();
		$('.gallery2').addClass('gallery').removeClass('gallery2');
		$('.gallery img').not(':first').hide();
		var firstTitle = $('.gallery img:visible').attr('title');
		$('.gallery .caption').html(firstTitle);

		$('.gallery img').each(function(){
			var n = $(this).prevAll().length + 1;
			$(this).addClass('img_n'+n);
			$('.gallery .index').append('<a href="#img_n'+n+'">'+n+'</a> ');
		});
		
		$('.gallery .index a:first').css({ 'border-bottom': '1px #9f9999 solid' });
		$('.gallery .index a').click(function(){
			var href = $(this).attr('href');
			href = href.substr(1, href.length);
			$('.gallery img').hide();
			$('.'+href).show();
			var newTitle = $('.'+href).attr('title');
			$('.gallery .caption').html(newTitle);
			$('.gallery .index a').css({ 'border-bottom': 0 });
			$(this).css({ 'border-bottom': '1px #9f9999 solid' });
			return false;
		});
		
		$('.gallery img').click(function(){
			var tot = $('.gallery img').length;
			var n = $(this).prevAll().length + 1;
			if(n == tot) n = 0;
			$('.gallery img').hide();
			var img = $('.gallery img').eq(n);
			img.show();
			var newTitle = img.attr('title');
			$('.gallery .caption').html(newTitle);
			$('.gallery .index a').css({ 'border-bottom': 0 });
			$('.gallery .index a').eq(n).css({ 'border-bottom': '1px #9f9999 solid' });
		});
	}
	
	function scaleBg()
	{
		var img = $('#bg img');
		var imgW = img.width();
		var imgH = img.height();
		var winW = $(window).width();
		var winH = $(window).height();

		var imgRatio = imgH / imgW;
		var winRatio = winH / winW;
		
//		img.log('window: '+winW+'x'+winH+' ('+winRatio+')');
//		img.log('image: '+imgW+'x'+imgH+' ('+imgRatio+')');
		
		if(winRatio > imgRatio) {
			// scale by height
			var h = winH;
			var w = winH / imgRatio;
//			img.log('new bg by H: '+w+'x'+h+' ('+(h/w)+')');
			img.height(h).width(w);
//			alert('1')
		}
		else {
			// scale by width
			var w = winW;
			var h = winW * imgRatio;
//			img.log('new bg by W: '+w+'x'+h+' ('+(h/w)+')');
			img.height(h).width(w);
//			alert('2')
		}	
	}

});

jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};
