//$.fn.image = function(src, f){
//  return this.each(function(){
//	$(this).css('background-image', 'url('+src+')');
//  });
//}
$(document).ready(function(){
	$('#contentRevealer').click(function(e){
		reveal('#backgroundSwitcher', $(this));
		e.preventDefault();
	});
	$('#backgroundSwitcherGallery li:nth-child(5n+5)').addClass('lastThumb');
	$('#contentRevealer').toggle(function() {
		$(this).html("Show Thumbnails");
		}, function () {
		$(this).html("Hide Thumbnails");
   	});
	
	$('#backgroundSwitcherGallery a.active').parent().siblings().fadeTo(500, 0.4);
	
	$('#backgroundSwitcherGallery a').hoverIntent(function() {
			$(this).not('.active').parent().fadeTo(500, 1.0);
		}, function() {
			$(this).not('.active').parent().fadeTo(500, 0.4);
		}
	);	
		
	$('#backgroundSwitcherGallery a').click(function (event) {
		$(this).addClass('active').parent().fadeTo(500, 1.0);;
		$(this).parent().siblings().children('a').removeClass('active');
		$(this).parent().siblings().fadeTo(500, 0.4);
		$(this).append('<div id="loader"></div>').fadeIn(500);
		var largeImg = $(this).attr('href');
		var currentImg = $(this).attr('title');
		$('#fullscreenBackground').queue(function(){
			//$(this).fadeOut(1000, function(){
				$(this).children('img').remove();							   
			//});
			$(this).dequeue();
		});
		$('#fullscreenBackground').queue(function(){
			var img = new Image();
			
			$(img).load(function () {
				$(this).appendTo('#fullscreenBackground');
				$('#loader').fadeOut(500, function(){
					$(this).remove();
				});
			}).error(function () {}).attr('src', largeImg);
			$(this).dequeue();
		});
		//$('#fullscreenBackground').queue(function(){
			//$(this).fadeIn(1000);
			//$(this).dequeue();
		//});
		$('span.currentImage').html(currentImg);
		
		event.preventDefault();
	});	
	
	$('#downloadBackground').click(function(event) {
		var currentImgLink = $('#backgroundSwitcherGallery a.active').attr('href');
		//var shortTitle = $('#backgroundSwitcherGallery a.active').attr('title');
		//$(this).attr({href: currentImgLink});	
		$(this).attr({href: 'download.php?currentImgLink=' + currentImgLink + ''});
	});
	
});
function reveal(target, targetSource){
	$(target).slideToggle('fast');
	if ($(targetSource).hasClass('minimised')) {
		$(targetSource).removeClass('minimised').addClass('maximised');
	} else if($(targetSource).hasClass('maximised')) {
		$(targetSource).removeClass('maximised').addClass('minimised');	
	}
}