$.fn.fadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

function listPropertyNames(obj) {
	var names ="";
	for(var i in obj) names += i + " = " + obj[i] + "\n";
	console.log(names);
}

function galleryBegin(obj) {
	//$('#imgHolder li.active').removeClass('active');
	//$('#imgHolder li img:hidden').show();
	for (var i in obj) {
		//$(obj[1]).css('border','10px solid red').addClass('active');
	}
}

function galleryEnd(obj) {
		var path = $(obj[1]).find('img').attr('src');
		$('button.prev').css('background-image','url(' + $(obj[0]).find('img').attr('src') + ')');
		$('#mainImg').attr('src', path.replace('-150x150.jpg','.jpg')).fadeIn();
		$('button.next').css('background-image','url(' + $(obj[2]).find('img').attr('src') + ')');
}

$.fn.gallery = function() {
	$(this).find('img').each(function() {
		var img = $(this).attr('src');
		img = img.replace('-150x150.jpg','.jpg');
		$.preLoadImages(img);
	});
	
	//$(this).find('li:eq(2)').addClass('active');
	$(this).jCarouselLite({
		btnNext: '.next',
		btnPrev: '.prev',
		visible: 3,
		circular: true,
		speed: 100,
		beforeStart: function(a) {
			galleryBegin(a);
		},
		afterEnd: function(a) {
			galleryEnd(a);
		}
	});
	
	
	$('.next').click();
/*	
	$(this).find('ul').galleria({
		history   : false, // activates the history object for bookmarking, back-button etc.
		clickNext : false, // helper for making the image clickable
		insert    : '#galleryImg', // the containing selector for our main image
		onImage   : function(image,thumb) { // let's add some image effects for demonstration purposes

			// fade in the image & caption
			if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
				image.css('display','none').fadeIn(1000);
			}

			// fetch the thumbnail container
			var _li = thumb.parents('li');

			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(500,0.3);

			// fade in active thumbnail
			thumb.fadeTo('fast',1).addClass('selected');

			// add a title for the clickable image
			image.attr('title','Next image >>');
		}
	});
	
	$('.next').click(function() {
  		$.galleria.next();
		return false;
	});

	$('.prev').click(function() {
  		$.galleria.prev();
		return false;
	});

	$(this).find('img').live('click', function() {
		$(this).add('img.active').toggleClass('active');
	});
*/
	
	$('button').live('click',function() {
		//$('#mainImg').fadeOut();
	});

	return this;
};

$(document).ready(function() {
	
	$('<div />').attr('id','homeslide').insertAfter('.home #content h1');
	
	$('.home #content img').appendTo('#homeslide');
	
	$('#homeslide').cycle();
	
	//$('.home #content p:first').attr('id','homeslide').cycle();
	
	$('.home #content p:empty').remove();

	$('.home #content p:not(:first)').addClass('rightColumn');
	
	$('.homeBucket img').fadeTo('slow',0.6);
	
	$('.homeBuckets .homeBucket').hover(
		function() {
			$(this).find('img').fadeTo('slow',1.0);
		}, function() {
			$(this).find('img').fadeTo('slow',0.6);
		}	
	);

	$('.footer li:last').addClass('last');
	
	$('input[title]').example(function() {
		return $(this).attr('title');
	});	
	
	$('#imgHolder').gallery();
	
});
