(function($){
	function num(el, props) {
		var r = 0, el = $(el);
		
		$.each(props.split(/\s+/) || [], function(i,v){
			r += parseInt($(el).css(v)) || 0;
		});
		
		return r;
	};
	
	$.fn.zoomCarousel = function(options) {
		var isMethodCall = (typeof options == "string") || false;
		var args = arguments;
		if (!isMethodCall)
			var options = $.extend({}, options, {});
		
		$(this).each(function(){
			var tab = $(this).data("zoomCarousel");
			if (isMethodCall && tab) {
				if (tab[options] && typeof tab[options] == "function"){
					tab[options].apply(tab, $.makeArray(args).slice(1));
				}
				return;
			} else {
				tab = new $.zoomCarousel(this, options);
				$(this).data("zoomCarousel", tab);
			}
		});
		return this;
	};
	
	$.zoomCarousel = function(elem, options) {
		this.elem = elem;
		this.options = $.extend({}, options, $.zoomCarousel.defaults);
		this.init();
	};
	
	$.extend($.zoomCarousel, {
		defaults: {
			normalWidth: 92,
			normalHeight: 58,
			centerWidth: 311,
			centerHeight: 196,
			visibleSlides: 3
			
		},
		prototype: {
			init: function() {
				var slides = $("ul:eq(0)>li", this.elem);
				var slidesContainer = $("ul:eq(0)", this.elem);
				var slidesHolder = $("<div />").appendTo(this.elem).append(slidesContainer);
				var opt = this.options;
				var slidesHpad = (parseInt($(slides[0]).css("paddingLeft"),10) || 0) + (parseInt($(slides[0]).css("paddingRight"),10) || 0);
				var slidesVpad = (parseInt($(slides[0]).css("paddingTop"),10) || 0) + (parseInt($(slides[0]).css("paddingBottom"),10) || 0);
				var w;
				
				for (var i = slides.length-1; i >= slides.length-(opt.visibleSlides-1); i--) {
					$(slides[i]).clone().removeClass("current").prependTo(slidesContainer);
				}
				
				for (var i = 0; i <= (opt.visibleSlides-2); i++) {
					$(slides[i]).clone().removeClass("current").appendTo(slidesContainer);
				}
				
				slides = $("ul:eq(0)>li", this.elem);
				
				slidesContainer.css({
					width: (opt.normalWidth * (slides.length-1)) + opt.centerWidth + (slidesHpad * slides.length),
					height: opt.centerHeight + slidesVpad,
					position: "absolute",
					left: 0,
					top: 0
				});
				
				w = (opt.normalWidth * (opt.visibleSlides-1)) + opt.centerWidth + (slidesHpad * opt.visibleSlides);
				slidesHolder.css({
					width: w + (slidesContainer.outerWidth(true) - slidesContainer.width()),
					height: slidesContainer.outerHeight(true),
					position:"relative",
					left:0,
					top:0,
					overflow:"hidden",
					margin:"0 auto"
				});
				
				this.slidesTitle = $("<div class='photo_title'><table><tr><td class='photo_title_wrapper'><div class='clear'></div></td></tr></table></div>").appendTo(this.elem);
				this.slidesTitleWrapper = $(".photo_title_wrapper", this.slidesTitle);
				
				//console.debug(w,w + (slidesContainer.outerWidth(true) - slidesContainer.width()));
				//console.debug(slidesContainer.outerWidth(true), slidesHolder.innerWidth());
				this.flipPointRight = slidesHolder.innerWidth() - slidesContainer.outerWidth(true);
				this.flipPointRightRestart = (opt.normalWidth + slidesHpad) * -1;
				//this.flipPointRight = slidesHolder.innerWidth() - slidesContainer.outerWidth(true);
				
				this.flipPointLeft = 0;
				this.flipPointLeftRestart = (slidesContainer.outerWidth(true) - (((opt.normalWidth + slidesHpad) * 3) + (opt.centerWidth + slidesHpad))) * -1;
				
				opt.normalCenterX = (opt.centerWidth - opt.normalWidth) / 2;
				opt.normalCenterY = (opt.centerHeight - opt.normalHeight) / 2;
				
				slides.css("position", "relative");
				$('a',slides).css("position", "relative");
				
				if (slides.filter(".current").length < 1) {
					$(slides[Math.floor((opt.visibleSlides-1)/2) + 1]).addClass("current");
				}
				
				this.slides = slides;
				this.slidesContainer = slidesContainer;
				this.slidesHolder = slidesHolder;
				this.slidesHpad = slidesHpad;
				this.slidesVpad = slidesVpad;
				
				
				this.imageHspace = (function(){
					var img = $("img:eq(0)", slides[0]);
					var ret = num(img, "borderLeftWidth borderRightWidth paddingLeft paddingRight marginLeft marginRight");
					while ((img = img.parent()) && !img.is("li")) {
						ret += num(img, "borderLeftWidth borderRightWidth paddingLeft paddingRight marginLeft marginRight");
					}
					return ret;
				})();
				
				this.imageVspace = (function(){
					var img = $("img:eq(0)", slides[0]);
					var ret = num(img, "borderTopWidth borderBottomWidth paddingTop paddingBottom marginTop marginBottom");
					while ((img = img.parent()) && !img.is("li")) {
						ret += num(img, "borderTopWidth borderBottomWidth paddingTop paddingBottom marginTop marginBottom");
					}
					return ret;
				})();
				
				
				this.currentIdx = slides.index(slides.filter(".current:eq(0)"));
				this.moveTo(this.currentIdx, false);
				
				slidesHolder = slidesContainer = slides = null;
				
				this.initButtons();
			},
			
			initButtons: function() {
				var self = this;
				
				this.btnNext = $("<a href='javascript:void(0);' title='' class='btnNext'>Next</a>").appendTo(this.elem);
				this.btnPrev = $("<a href='javascript:void(0);' title='' class='btnPrev'>Prev</a>").appendTo(this.elem);
				
				this.btnSlideNext = $("<a href='javascript:void(0);' title='' class='photo_title_next'>Next</a>").appendTo(this.slidesTitle);
				this.btnSlidePrev = $("<a href='javascript:void(0);' title='' class='photo_title_prev'>Prev</a>").appendTo(this.slidesTitle);
				
				$(this.btnNext).add(this.btnSlideNext).unbind("click").bind("click", function(event) {
					self.moveNext.call(self);
					event.preventDefault();
					return false;
				});
				
				$(this.btnPrev).add(this.btnSlidePrev).unbind("click").bind("click", function(event) {
					self.movePrev.call(self);
					event.preventDefault();
					return false;
				});
				
				$("a:eq(0)", self.slides).unbind("click").bind("click", function(event){
					var idx = self.slides.index( $(this).closest("li") );
					self.moveTo.call(self, idx, true);
					event.preventDefault();
				});
			},
			
			moveTo: function(idx, anim, animDuration) {
				var self = this;
				var opt = this.options;
				var curLeft = (this.currentIdx-1) * (opt.normalWidth + this.slidesHpad) * -1;
				var toLeft = (idx-1) * (opt.normalWidth + this.slidesHpad) * -1;
				
				var toHide = [], toShow = [];
				var dir = curLeft - toLeft;
				
				animDuration = animDuration || 800;
				
				for (var i =0; i < this.slides.length; i++) {
					var slide = $(this.slides[i]);
					$("img", slide).stop();
					$('a', slide).stop();
					if (i == idx) {
						toShow.push(slide);
					} else {
						toHide.push(slide);
					}
				}
				
				for (var i = 0; i < toHide.length; i++) {
					var slide = toHide[i];
					if (anim) {
						$("img", slide).animate({
							width: opt.normalWidth - this.imageHspace,
							height: opt.normalHeight - this.imageVspace
						}, {queue:false, duration:animDuration});
						
						$('a', slide).animate({
							left:0,
							top: opt.normalCenterY
						}, {queue:false, duration:animDuration});
					} else {
						$("img", slide).css({
							width: opt.normalWidth - this.imageHspace,
							height: opt.normalHeight - this.imageVspace
						});
						
						$('a', slide).css({
							left:0,
							top: opt.normalCenterY
						});
					}
					this.hideFrame.call(this, $('a', slide), anim);
					slide.removeClass("current");
				}
				
				for (var i = 0; i < toShow.length; i++) {
					var slide = toShow[i];
					if (anim) {
						$("img", slide).animate({
							width: opt.centerWidth - this.imageHspace,
							height: opt.centerHeight - this.imageVspace
						}, {queue:false, duration: animDuration});
						
						$('a', slide).animate({
							left:0,
							top: 0
						}, {queue: false, duration: animDuration});
					} else {
						$("img", slide).css({
							width: opt.centerWidth - this.imageHspace,
							height: opt.centerHeight - this.imageVspace
						});
						
						$('a', slide).css({
							left:0,
							top: 0
						});
					}
					this.showFrame.call(this, $('a', slide), anim);
					slide.addClass("current");
					break;
				}
				
				if (anim) {
					this.slidesContainer.animate({left:toLeft}, {queue:false, duration:animDuration, step:function(val, prop){
						var n, slides, oldEnd, oldNow, d, r, opt = self.options;
						if ( dir > 0 ) {
							
							//console.debug(arguments);
							//-584
							if (prop.now <= self.flipPointRight) {
								slides = self.slides;
								n = slides.length-1;
								self.slidesContainer.stop().css({
									left: self.flipPointRightRestart
								});
								
								oldEnd = prop.end;
								oldNow = prop.now;
								
								prop.end = prop.now = self.flipPointRightRestart;
								
								for (var i =0; i < opt.visibleSlides; i++) {
									var cssImg = {
										width: $("img", slides[n-i]).css("width"),
										height: $("img", slides[n-i]).css("height")
									}
									var cssA = {
										left: $("a", slides[n-i]).css("left"),
										top: $("a", slides[n-i]).css("top")
									}
									$("img", slides[opt.visibleSlides-i]).css(cssImg);
									$("a", slides[opt.visibleSlides-i]).css(cssA);
								}
								
								opt = self.options;
								for (var i = 0; i <= n; i++) {
									$("img", slides[i]).stop();
									$("a", slides[i]).stop();
									
									if ( (i >= 1 && i <= opt.visibleSlides) ) {
										//
									} else {
										$("img", slides[i]).css({
											width: opt.normalWidth - self.imageHspace,
											height: opt.normalHeight - self.imageVspace
										});
										
										$('a', slides[i]).css({
											left:0,
											top: opt.normalCenterY
										});
									}
								}
								
								slides.removeClass("current");
								$(slides[opt.visibleSlides-1]).addClass("current");
								self.currentIdx = opt.visibleSlides-1;
								if (oldEnd != oldNow) {
									d = Math.abs(oldEnd - oldNow);
									d = Math.ceil(d / (opt.normalWidth + self.slidesHpad));
									
									//self.currentIdx += d;
									//idx = self.currentIdx;
									
									idx = self.currentIdx + d;
									
									
									//(idx-1) * (opt.normalWidth + this.slidesHpad) * -1
									//console.debug('a');
									self.moveTo.call(self, idx, true, prop.options.duration * (1-prop.state));
									//console.debug(prop.options, prop.state, prop.options.duration * prop.pos);
									//prop.end = (idx-1) * (opt.normalWidth + self.slidesHpad) * -1;
									//console.debug(prop.end, prop.now);
								}
							}
							
						} else {
							if (prop.now >= self.flipPointLeft) {
								slides = self.slides;
								n = slides.length-1;
								self.slidesContainer.stop().css({
									left: self.flipPointLeftRestart
								});
								
								oldEnd = prop.end;
								oldNow = prop.now;
								
								prop.end = prop.now = self.flipPointLeftRestart;
								
								for (var i =0; i < 3; i++) {
									var cssImg = {
										width: $("img", slides[i]).css("width"),
										height: $("img", slides[i]).css("height")
									}
									var cssA = {
										left: $("a", slides[i]).css("left"),
										top: $("a", slides[i]).css("top")
									}
									$("img", slides[n-3+i]).css(cssImg);
									$("a", slides[n-3+i]).css(cssA);
								}
								
								opt = self.options;
								for (var i = 0; i <= n; i++) {
									$("img", slides[i]).stop();
									$("a", slides[i]).stop();
									if ( (i >= n-3 && i <= n-3+2) ) {
										//
									} else {
										$("img", slides[i]).css({
											width: opt.normalWidth - self.imageHspace,
											height: opt.normalHeight - self.imageVspace
										});
										
										$('a', slides[i]).css({
											left:0,
											top: opt.normalCenterY
										});
									}
								}
								
								slides.removeClass("current");
								$(slides[n-2]).addClass("current");
								self.currentIdx = n-2;
								if (oldEnd != oldNow) {
									d = Math.abs(oldEnd - oldNow);
									d = Math.ceil(d / (opt.normalWidth + self.slidesHpad));
									
									//self.currentIdx += d;
									//idx = self.currentIdx;
									
									idx = self.currentIdx - d;
									
									
									//(idx-1) * (opt.normalWidth + this.slidesHpad) * -1
									//console.debug('a');
									self.moveTo.call(self, idx, true, prop.options.duration * (1-prop.state));
									//console.debug(prop.options, prop.state, prop.options.duration * prop.pos);
									//prop.end = (idx-1) * (opt.normalWidth + self.slidesHpad) * -1;
									//console.debug(prop.end, prop.now);
								}
							}
						}
					}});
				} else {
					this.slidesContainer.css({
						left: toLeft
					});
				}
				
				this.currentIdx = idx;
			},
			
			moveNext: function() {
				if (this.currentIdx < (this.slides.length-1)) {
					this.moveTo(this.currentIdx+1, true);
				}
			},
			
			movePrev: function() {
				if (this.currentIdx > 0) {
					this.moveTo(this.currentIdx-1, true);
				}
			},
			
			hideFrame: function(link, anim) {
				var title = $(link[0]).attr("title") || "";
				
				this.slidesTitleWrapper.text(title);
				//this.hideContent.call(this, this.slidesTitle.stop(), anim);
			},
			
			showFrame: function(link, anim) {
				var title = $(link[0]).attr("title") || "";
				
				this.slidesTitleWrapper.text(title);
				//this.showContent.call(this, this.slidesTitle.stop(), anim);
			},
			
			IEFixContent: function(content) {
				if ($.browser.msie) {
					if (content.data("IEOpacityFix") == undefined) {
						$("div, img", content).not(".mainImgOverlay").each(function(){
							if ($(this).css("position") == "absolute" || $(this).css("position") == "relative") {
								$(this).css({
									filter: "inherit"
								});
							}
						});
						content.data("IEOpacityFix", true);
					}
				}
			},
			
			showContent: function(content, anim) {
				var self = this, display, opacity;
				
				self.IEFixContent.call(self, content);
				
				if (anim) {
					display = content.css("display");
					opacity = content.css("opacity");
					
					if (display == "none") {
						content.css({
							display:"block",
							opacity: (opacity == "" || parseFloat(opacity,10) == 1) ? 0 : opacity
						});
					}
					content.animate({opacity:1}, {queue:false, duration:800, complete:function(){
						$(this).css({
							display:"block",
							opacity:""
						});
					}});
				} else {
					content.css({
						display:"block",
						opacity:""
					});
				}
			},
			
			hideContent: function(content, anim) {
				var self = this, display;
				
				self.IEFixContent.call(self, content);
				
				if (anim) {
					display = content.css("display");
					if (display !== "none") {
						content.animate({opacity:0}, {queue:false, duration:800, complete:function(){
							$(this).css({
								display:"none",
								opacity:""
							});
						}});
					}
				} else {
					content.css({
						display:"none",
						opacity:""
					});
				}
			}
		}
	});
})(jQuery);

;(function($){
	$(function(){
		$(".recent_photos").zoomCarousel({
			normalWidth: 92,
			normalHeight: 58,
			centerWidth: 311,
			centerHeight: 196,
			visibleSlides: 3
		});
	});
})(jQuery);
