;(function ($) {
	$.fn.jqGallery = function(options) {
		var settings = $.extend({
			photos: [],
			current: "",
			photoContainer: "#photoContainer",
			photoLoader: "#photoLoader",
			photoHolder: "#photoOnDisplay",
			commentHolder: "#photoComment",
			commentUrl: "/Gallery/photo/comment/photo/",
			photoUrl: "/Gallery/photo/show/medium/",
			maxWidth: 450,
			maxHeight: 450,
			nav: "",
			next: "",
			prev: "",
			opacity: '0.35',
			hideDelay: 500
		}, options);
		
		
		var photo = {
			index: 0,
			photoHolder: null,
			tmpPhoto: null,
			caption: null,
			height: 0,
			width: 0,
			on_caption: false,
			on_image: false,
			textLabel: null,
			
			initialize: function(){				
				photo.tmpPhoto = $(settings.photoHolder);
				photo.getIndex();
				photo.initNav();
				photo.resize();				
			},
			
			
			populateNav: function(){
				$(options.nav).empty();
				prevImage = (photo.index > 0 ? photo.index -1 : settings.photos.length-1);
				currImage = photo.index;
				nextImage = (photo.index  + 1 >= settings.photos.length ? 0 : photo.index  + 1);				
				thumb1 = $("<img />").attr("src", "/Gallery/photo/show/thumbnail/" + settings.photos[prevImage]).addClass("navPhoto").attr("id", settings.photos[prevImage]).bind("click", photo.gotoImg);
				thumb2 = $("<img />").attr("src", "/Gallery/photo/show/thumbnail/" + settings.photos[currImage]).addClass("navPhoto").attr("id", settings.photos[currImage]).addClass("currentThumbnail").bind("click", photo.gotoImg);
				thumb3 = $("<img />").attr("src", "/Gallery/photo/show/thumbnail/" + settings.photos[nextImage]).addClass("navPhoto").attr("id", settings.photos[nextImage]).bind("click", photo.gotoImg);
				$(options.nav).append(thumb1);
				$(options.nav).append(thumb2);
				$(options.nav).append(thumb3);				
			},
			
			initNav: function(){
				photo.photoHolder = $("img", settings.photoContainer);

				$(options.next).bind("click", photo.next);
				$(options.prev).bind("click", photo.previous);
				this.populateNav();
			},
			
			gotoImg: function(){
				cphoto = $(this).attr("id");
				for(var i = 0;i < settings.photos.length;i++){
					if(settings.photos[i] == cphoto){
						photo.index = i;
						break;
					}
				}				
				photo.loadphoto();				
			},
						
			next: function(){				
				if((photo.index + 1) >= settings.photos.length){
					photo.index = 0;
				}else{
					photo.index++;
				}				
				photo.loadphoto();
			},
			
			previous: function(){
				if((photo.index - 1) < 0){
					photo.index = settings.photos.length-1;
				}else{
					photo.index -= 1;
				}
				photo.loadphoto();				
			},
			
			loadphoto: function(){
				photo.populateNav();
//				console.info("L:" + photo.index);				
				if(photo.photoHolder != null)
					photo.photoHolder.remove();
				
				
				$(settings.photoContainer).empty();
				photo.photoHolder = $("<img>");
				photo.photoHolderLink = $("<a>").attr("href", "/Gallery/photo/show/full/" + settings.photos[photo.index]);
				photo.photoHolder.attr("src", settings.photoUrl + settings.photos[photo.index]);
				photo.photoHolder.attr("id", settings.photoHolder.substr(1));
				photo.photoHolder.addClass("photo");
				photo.photoHolderLink.append(photo.photoHolder);
				$(settings.photoContainer).append(photo.photoHolderLink);
				photo.photoHolder.load(function(){
					photo.resize();
				});				
				jQuery('.photoContainer > a').lightBox({fixedNavigation:true});				
			},
			
			createCaption: function(){
				photo.caption = $("<div>").addClass("photoCaption");
				photo.textLabel = $("<div>").addClass("CaptionText").css({float: "left", textalign: "center", width: "75%"});
				var prevImg = $("<img>").attr("src", "/Modules/Gallery/Images/previous.png").click(photo.previous).addClass("GalleryButton");
				var nextImg = $("<img>").attr("src", "/Modules/Gallery/Images/next.png").click(photo.next).addClass("GalleryButton");
				var prevdiv = $("<div>").addClass("CaptionText").css({float: "left", width: "10%"}).append(prevImg);
				var nextdiv = $("<div>").addClass("CaptionText").css({float: "right", width: "10%"}).append(nextImg);
				$.get(settings.commentUrl + settings.photos[photo.index], {}, function(data){
					photo.textLabel.html(data);
				});
				photo.caption.append(prevdiv);
				photo.caption.append(photo.textLabel);				
				photo.caption.append(nextdiv);
				$(settings.photoContainer).append(photo.caption);
				
				
				//var captionPositioning = jQuery.browser.msie ? 'static' : 'relative';
				var captionPositioning = "relative";
				photo.caption.css({
					zIndex: 1,
					position: captionPositioning,
					opacity: settings.opacity
				});				
				
				photo.caption.hover(function(){
					photo.on_caption = true;
				},
				function(){
					photo.on_caption = false;
					photo.triggerHide();					
				});
				
				photo.caption.blur(function(){					
					photo.on_caption = false;
					photo.triggerHide();
				});
				
			},
			
			triggerHide: function(){
				if(photo.on_caption == false && photo.on_image == false){
					window.setTimeout(photo.hideCaption, settings.hideDelay);
				}else{					
				}
				
			},
			
			hideCaption: function(){
				if(photo.on_caption == false && photo.on_image == false){				
					photo.caption.fadeOut();
				}
			},
			
			resize: function(){
				var oh = photo.photoHolder.attr("height");
				var ow = photo.photoHolder.attr("width");
				var nw = 0;
				var nh = 0;
				

				if(ow >settings.maxWidth){
					nw  = settings.maxWidth;
					nh = Math.round(oh / (ow / settings.maxWidth)); 						
				}else{
					nw = ow;
					nh =  oh;
				}
				
				if(nh > settings.maxHeight){					
					tnh = nh;
					nh = settings.maxHeight;					
					nw = Math.round(nw / (tnh /settings.maxHeight));
				}
				
				if(nw != 0 && nh != 0){
					photo.height = nh;
					photo.width = nw;
					photo.photoHolder.attr("height", nh);
					photo.photoHolder.attr("width", nw);
				}else{
					setTimeout(photo.resize, 500);
				}
			},			
			
			getIndex: function(){
				for(var i = 0;i < settings.photos.length;i++){
					if(settings.photos[i] == settings.current){
						photo.index = i;
						break;
					}

				}
			}
		};
		
		return this.each(function() {
			photo.initialize();			
			
		});		
	}	
})(jQuery);
/*
var photoDisplay = Class.create();
photoDisplay.prototype = {
		
	photos: null,
	photo: null,
	index: 0,
	imgTag: null,
	imgSrc: "/Gallery/photo/show/medium/",
	maxSize: 450,
	
	initialize: function(formName, options){	
		document.observe("dom:loaded", this.init.bind(this));
	},
	
	init: function(){
		this.imgTag = $("displayPhoto");
//		Event.observe("displayPhoto", "load", this.resize.bind(this));
//		Event.observe("displayPhoto", "change", this.resize.bind(this));
	},
	
	setMaxSize: function(maxsize){
		this.maxSize = maxsize;
	},
	
	setPhotos: function(photos){
		this.photos = photos;
	},
	
	getIndex: function(){
		for(var i=0; i < this.photos.length;i++){
			if(this.photos[i] == this.photo){
				this.index = i;
				
			}
		}
	},
	
	setCurrent: function(photo){
		this.photo = photo;
		this.getIndex();
	},
	
	commitphoto: function(){
		this.imgTag.src = this.imgSrc + this.photos[this.index];
		this._retrieveComment(this.photos[this.index]);
	},
	
	resize: function(){
		var oh = this.imgTag.height;
		var ow = this.imgTag.width;
		
		if(this.imgTag.width > this.imgTag.height){
			if(this.imgTag.width > this.maxSize){
				this.imgTag.width  = this.maxSize;
				this.imgTag.height = Math.round(oh / (ow / this.maxSize)); 
				
			}
		}else{
			if(this.imgTag.height > this.maxSize){
				this.imgTag.height = this.maxSize;
				this.imgTag.width = Math.round(ow / (oh / this.maxSize));
				alert(Math.round(ow / (oh / this.maxSize)));
				alert(this.imgTag.height + "x" + this.imgTag.width + "|"+ow + "x" + oh);
			}
		}	
	},
	
	next: function(){		
		this.index++;
		if(this.index >= this.photos.length) this.index = 0;
		this.commitphoto();
	},
	
	previous: function(){
		this.index--;
		if(this.index < 0) this.index = this.photos.length -1;
		this.commitphoto();	
	},
	
	first: function(){
		this.index = 0;
		this.commitphoto();	
	},
	
	last: function(){
		this.index = this.photos.length -1;
		this.commitphoto();
	},
	
	_retrieveComment: function(file){
		new Ajax.Updater('photoComment', '/Gallery/photo/comment/photo/' + file, {});		
	}
};
*/


