(function($) {
	$.fn.chGal = function(options){
		return this.each(function(i){
			// Initialize
			var ch = this;
			ch.animating = false;
			ch.jq = $(this);
			ch.jq.wrap("<div id=\""+ch.jq.attr("id")+"_wrap\" class=\"chGalWrap\"></div>");
			ch.pr = ch.jq.parent();
			ch.items = ch.jq.children();
			ch.op = $.extend({}, chGalDefaults, options);
			ch.dsp = ch.op.left + ch.op.right + 1;
			if(ch.op.size == "auto") {
				ch.sz = 0;
				$.each(ch.items, function(){ // Get height of biggest child
					ch.sz = Math.max($(this).height(), ch.sz);
				});
			} else {
				ch.sz = ch.op.size;
			}
			
			for(var i = 0; i < Math.floor((ch.items.length - 1) / 2); i++) {
				ch.items.eq(ch.items.length - 1 - i).remove().prependTo(ch);
			}
			ch.items.css({display:'block',float:"left",width:ch.sz,height:ch.sz,position:"relative", opacity: ch.op.opacity}); // Float children
			ch.pr.css({width: ch.items.outerWidth() * ch.dsp, height: ch.sz, display: "block", overflow:"hidden"}); // Set parent width
			if(ch.items.length % 2 == 0) {
				ch.os = ((ch.pr.width() / 2) - ((ch.items.outerWidth() * ch.items.length) / 2) + (ch.items.outerWidth() / 2));
			} else {
				ch.os = ((ch.pr.width() / 2) - ((ch.items.outerWidth() * ch.items.length) / 2));
			}
			ch.jq.css({display:"block",position:"relative", width: ch.sz * ch.items.length , marginLeft: ch.os});
			ch.items.eq(0).addClass("chCurrent").css({opacity:ch.op.currOpacity}); //.prev().click(function(){$("#"+ch.jq.attr("id")+"_prev").click();}).next().click(function(){$("#"+ch.jq.attr("id")+"_next").click();}).next().click(function(){$("#"+ch.jq.attr("id")+"_next").click();});
			
			if(ch.op.fitContents) {
				ch.items.each(function(){
					$(this).children().each(function(){
						var w = $(this).width();
						var h = $(this).height();
						if(w > h) {
							$(this).width(ch.sz);
							$(this).height(Math.floor(ch.sz*(h/w)));
						} else {
							$(this).height(ch.sz);
							$(this).width(Math.floor(ch.sz*(w/h)));
						}
						$(this).css({
							marginTop: (ch.sz / 2) - ($(this).height() / 2) - 2 + "px",
							marginLeft: (ch.sz / 2) - ($(this).width() / 2) - 2 + "px"
						});
					});
				});
			}
			
			if(ch.items.length > 1) {
				ch.pr.parent().append(
					"<a id=\""+ch.jq.attr("id")+"_prev\" class=\"chGalPrev\" href=\"#\" onclick=\"return false;\">Previous</a>"+
					"<a id=\""+ch.jq.attr("id")+"_next\" class=\"chGalNext\" href=\"#\" onclick=\"return false;\">Next</a>");
				//ch.chGalPrev = function() {
				$("#"+ch.jq.attr("id")+"_prev").click(function() {
					if(!ch.animating) {
						ch.animating = true;
						var last = ch.jq.children().eq(ch.jq.children().length - 1);
						last.fadeTo(ch.op.boundarySpeed, 0, function() {
							$(this).css({width: 0})
							.remove()
							.prependTo(ch)
							.animate({width:ch.sz}, ch.op.slideSpeed, ch.op.easing, function() {
								$(".chCurrent", ch)
									.removeClass("chCurrent")
									.fadeTo(ch.op.highlightSpeed, ch.op.opacity, function() {
										ch.animating = false;
									/*	$(this).prev().andSelf().unbind('click').click(function(){
											$("#"+ch.jq.attr("id")+"_next").click();
										});
										$(this).prev().prev().unbind('click').click(function(){
											$("#"+ch.jq.attr("id")+"_prev").click();
										}); */
									})
									.prev()
									.addClass("chCurrent")
									.fadeTo(ch.op.highlightSpeed, ch.op.currOpacity);
								if(!$(this).hasClass("chCurrent")) $(this).fadeTo(ch.op.boundarySpeed, ch.op.opacity);
								
							});
						});
					} else {
						setTimeout("$(\"#"+ch.jq.attr("id")+"_prev\").click()", 100);
					}
				});
				//ch.jq.chGalNext = function() {
				$("#"+ch.jq.attr("id")+"_next").click(function() {
					if(!ch.animating) {
						ch.animating = true;
						var first = ch.jq.children().eq(0);
						$(".chCurrent", ch).removeClass("chCurrent").next().addClass("chCurrent");
						first.fadeTo(ch.op.boundarySpeed, 0, function() {
							$(this).animate({width:0}, ch.op.slideSpeed, ch.op.easing, function() {
								$(this).remove()
									.appendTo(ch)
									.css({width: ch.sz})
									.fadeTo(ch.op.boundarySpeed, ch.op.opacity);
								$(".chCurrent", ch)
									.fadeTo(ch.op.highlightSpeed, ch.op.currOpacity, function(){
										ch.animating = false;
										/*$(this).next().andSelf().unbind('click').click(function(){
											$("#"+ch.jq.attr("id")+"_next").click();
										});
										$(this).prev().unbind('click').click(function(){
											$("#"+ch.jq.attr("id")+"_prev").click();
										}); */
									})
									.prev()
									.fadeTo(ch.op.highlightSpeed, ch.op.opacity);
							});
						});
					} else {
						setTimeout("$(\"#"+ch.jq.attr("id")+"_next\").click()", 100);
					}
				});
				//$("#"+ch.jq.attr("id")+"_prev").click(function(){ch.previous()});
				//$("#"+ch.jq.attr("id")+"_next").click(function(){ch.next()});
			}
		});
	};
	chGalDefaults = {
		left: 1,
		right: 1,
		size: "auto",
		slideSpeed: "slow",
		boundarySpeed: 0,
		highlightSpeed: "slow",
		easing: "",
		opacity: 0.3,
		currOpacity: 1.0,
		fitContents: false
	};
})(jQuery);