jQuery.fn.gPhoto = function(options) {
	//First set the object to a var so we cna use it in the scope of the functions
	var _this_ = this;
	//Build all the html elements 
	var frameHeight = ( 'undefined' != typeof(options) && 'undefined' != typeof(options.frameHeight) ? options.frameHeight : '650px' );
	var picHeight = ( 'undefined' != typeof(options) && 'undefined' != typeof(options.picHeight) ? options.picHeight : '460px' );
	var thumbHeight = ( 'undefined' != typeof(options) && 'undefined' != typeof(options.thumbHeight) ? options.thumbHeight : false );
	var picFrame = $(document.createElement('div')).attr('align', 'center').addClass('frame-gPhoto').insertAfter(_this_).css('height', frameHeight);
	var picDiv = $(document.createElement('div')).addClass('picDiv-gPhoto').appendTo(picFrame);
	$('<div style="vertical-align: top"></div>').addClass('thumbBar-gPhoto').append(_this_).appendTo(picFrame);
	var hiddenDiv = $(document.createElement('div')).addClass('hiddenDiv-gPhoto').appendTo(picFrame);
	var currPic = $(document.createElement('div')).addClass('currPic-gPhoto').appendTo(picDiv).css('height', picHeight).
			html('<table style="height:100%;color:white;"><tr><td><img src="/javascript/loader.gif" alt="Loading..." /></td></tr></table>');
	_this_.addClass('list-gPhoto').children('li').click(function () {
		_this_.addPic($(this), false);
	});
	var i = 0;
	_this_.find('li').each(function () {
		var _li_ = this;
		if ( false !== thumbHeight ) {
			$(_li_).find('img').attr('height', thumbHeight);
		}
		var bigImgSrc = $(_li_).find('img').attr('longdesc');
		var bigImg = $(document.createElement('img')).attr('src', bigImgSrc).appendTo(hiddenDiv);
		if ( i == 0 ) {
			bigImg.load(function () {
				_this_.addPic($(_li_), true);
			});
		}
		i++;
	});
	_this_.addPic = function (li, start) {
		if ( 'undefined' == typeof(currPic.find('img').get(0)) ) {
			_addPic(li, start);
		} else {
			var halfUp = currPic.find('img').height() / 2 + 'px';
			currPic.find('img').animate({marginTop: halfUp, height: '0px'}, "slow", null, function () {
				_this_._addPic(li, start);
			});
		}
	}
	_this_._addPic = function (li, start) {
		currPic.html('');
		var pic = li.find('img').get(0);
		_this_.find('img').css('opacity', .25);
		$(pic).css('opacity', 1.0);
		var fullPicPath = $(pic).attr('longdesc');
		var caption = $(pic).attr('title');
		var img = $(document.createElement('img')).attr('src', fullPicPath).addClass('viewPic-gPhoto');
		img.appendTo(currPic);
		var captionFrame = $(document.createElement('div')).appendTo(currPic).addClass('captionFrame-gPhoto');
		var captionDiv = $(document.createElement('div')).addClass('caption-gPhoto').html(caption).css('opacity', .55).appendTo(captionFrame);
		var height = $('.viewPic-gPhoto').height();
		var width = $('.viewPic-gPhoto').width();
		img.attr({
			height: '1px',
			width: '1px'
		}).css({
			'opacity': .2,
			marginTop: height/2 + 'px'
		});
		var heightDiff = parseInt(picHeight.replace('px', ''));
		if ( $.browser.msie ) {
			picDiv.css('height', parseInt(heightDiff + 70) + 'px');
			$('.thumbBar-gPhoto').css('marginTop', '-35px');
		} else {
			picDiv.css({'height': parseInt(heightDiff + 35) + 'px'});
		}
		captionDiv.css('width', width + 'px');
		img.animate({height: height, width: width, marginTop: 0}, 500, null, function () {
			img.animate({opacity: 1.0}, 500);	
		});
	}
};
