  ;(function($){

	$.widget("ui.coverflow", {
		init: function() {
			
			var self = this;
			
			this.items = $(this.options.items, this.element).bind("click", function() {
				self.moveTo(this);
				//set the info for the element
				self.setInfo($(this).attr('name'),$(this).attr('info'),$(this).attr('garden_url'),$(this).attr('nto_url'),$(this).attr("largeimage"));
				return false;
			});
			
			this.itemWidth = this.items.outerWidth(true);
			this.itemHeight = this.items.outerHeight(true);
			this.current = this.options.startAt; //item to show at start
			
			this.glideTo(0, this.current);
			this.center();
			this.currentElement = $(this.items).eq(this.current);
			this.setInfo($(this.currentElement).attr('name'),$(this.currentElement).attr('info'),
						 $(this.currentElement).attr('garden_url'),$(this.currentElement).attr('nto_url'),$(this.currentElement).attr('largeimage'));
			
		},
		moveTo: function(item) {
			
			this.previous = this.current;
			this.current = !isNaN(parseInt(item)) ? parseInt(item) : this.items.index(item);
			var self = this, to = Math.abs(self.previous-self.current) <=1 ? self.previous : self.current+(self.previous < self.current ? -1 : 1);
			
			if(this.previous == this.current)
			{
				//alert(this.currentElement);
				//$(this.currentElement).wrap('<a class="iframe" href="' + $(this.currentElement).attr('largeimage') + '"></a>');
				return false;
			}
			//return false; //Don't animate when clicking on the same item
			
			
			
			self.glideTo(to, self.current);
			self.center();
			
			
		},
		clickTo: function(direction) {
		
				switch (direction)
				{
					case 'left':
					    if(this.current >0)
						{
							this.moveTo(this.current -1);
							this.currentElement = $(this.items).eq(this.current);
							this.setInfo($(this.currentElement).attr('name'),$(this.currentElement).attr('info'),
											   $(this.currentElement).attr('garden_url'),$(this.currentElement).attr('nto_url'),
											   $(this.currentElement).attr("largeimage"));
						}
						break;
			 		case 'right':
						if(this.current < (this.items.length -1))
						{
							this.moveTo(this.current + 1);
							this.currentElement = $(this.items).eq(this.current);
							this.setInfo($(this.currentElement).attr('name'),$(this.currentElement).attr('info'),
											   $(this.currentElement).attr('garden_url'),$(this.currentElement).attr('nto_url'),
											   $(this.currentElement).attr("largeimage"));
						}
						break;
				}
			
		},
		center: function(){
			
			var left = (-this.current * this.itemWidth/2) + (this.element.parent()[0].offsetWidth/2) - (this.itemWidth/2) + ((this.current - this.options.startAt) * 100) + (12 * this.current);
			
			this.element.animate({
				left: (left)
			}, {
				duration: 300,
				easing: "easeInQuad"
			});
		},
		setInfo: function(name,info,garden_url,nto_url,largeimage) {
			var parent; 
			var infoText;
			
			if(this.options.infoPane == null)
			{
				parent = this.element.parent().parent().parent();
			}
			else
			{
				parent = this.options.infoPane;
				
			}
			if(this.options.constrainText == true)
			{
				infoText = info.substring(0,this.options.textLimit) + '  ...';
			}
			else
			{
				infoText = info;
			}
			$('.name',parent).html(name);
			$('.info',parent).html(infoText);
			$('.magnify A',parent).attr({'href':largeimage});
			$('.garden_url',parent).attr({'href':garden_url});
			$('.nto_url',parent).attr({'href':nto_url});
		},
		glideTo: function(from,to) {
			
			var self = this, offset = null;
			
			this.items.each(function(i) {
									 
				var side = (i == to && from-to < 0 ) ||  i-to > 0 ? "left" : "right";
				var sideNo = (side == "left" ? to-i : i-to);
				
				//Set the items to show at once
				if(sideNo < -self.options.show)
				{
					$(this).animate({'opacity':'0.0'},300); //set visibility to hidden if not to show.
				}
				else
				{
					$(this).animate({'opacity':'1.0'},300); //reset visibility on items to show.
					
					switch (sideNo)
					{
						case -1:
							$(this).css({'height':(self.itemHeight/4) * 3.2,'top':'14px'});
							break;
						case -2:
							$(this).css({'height':(self.itemHeight/4) * 2.6,'top':'25px'});
							break;
						default:
							$(this).css({'height':'','top':''});
							break;
					}
				}
				
				$(this).css({
					left: ((-i * ((self.itemWidth/4) * 3.5))),
					zIndex: self.items.length + sideNo
				});
			});
		}
	});
	
	$.extend($.ui.coverflow, {
		defaults: {
			items: "> *",
			startAt:0,
			show:2,
			infoPane:null,
			constrainText:false,
			textLimit:0
		}
	});
	
})(jQuery); 