
	/**
	 * class T3NewsGallery
	 *
	 * depends on:
	 * prototype >= 1.6.0
	 * scriptaculous >= 1.8.1
	 * Xover >= 0.0.1
	 * 
	 * @since 03.10.2008 12:00
	 * @version 0.2.0
	 * @author Markus Huber, Herbert Roth
	 *
	 * ChangeLog:
	 * v0.1.1 - 30.06.2008
	 *          + bugfix for each site to have unique summary elements
	 *            in cookie
	 * v0.1.2 - 30.07.2008
	 *          + refactor this script and now uses frameworks:
	 *            prototype, scriptaculous, Xover
	 * v0.1.3 - 02.10.2008
	 *          - removed cookie handling. not necessary anymore.
	 *          + mark seleted element
	 *          + current selected element always in the middle
	 *          + better handling after loading or reloading
	 * v0.2.0 - 03.10.2008
	 *          + prevent much clicking and so items move over range
	 *
	 *
	 * ToDo:
	 * - make more instances in one site possible
	 * - ajax loading for details
	 * - make a own typo3 extension
	 *
	 *
	 *
	 */
	var T3NewsGallery = Class.create();
	
	T3NewsGallery.prototype = {
	
		/**
		 * method initialize
		 *
		 *
		 *
		 */
		initialize: function()
    	{
			var defaults = {
				container: 'T3NewsGallery',
				containerInner: 'T3NewsGalleryInner',
				containerOuter: 'T3NewsGalleryOuter',
				containerLeft: 'T3NewsGalleryLeft',
				containerRight: 'T3NewsGalleryRight',
				sumElements: 0,
				currentElement: 0,
				scrollWidth: 65,
				T3ElementId: 0,
				currentItemUid: 0,
				offsetLeftOriginator: 0,
				offsetLeftOuter: 0,
				currentSequence: 0,
				maxLeft: 0,
				maxRight: 0,
				leftObserver: null,
				rightObserver: null
			}

			var options = Object.extend(defaults, {});

			if(!this.container) this.container = options.container;
			if(!this.containerInner) this.containerInner = options.containerInner;
			if(!this.containerOuter) this.containerOuter = options.containerOuter;
			if(!this.containerLeft) this.containerLeft = options.containerLeft;
			if(!this.containerRight) this.containerRight = options.containerRight;
			if(!this.sumElements) this.sumElements = options.sumElements;
			if(!this.currentElement) this.currentElement = options.currentElement;
			if(!this.scrollWidth) this.scrollWidth = options.scrollWidth;
			if(!this.T3ElementId) this.T3ElementId = pageId;
			if(!this.currentItemUid) this.currentItemUid = options.currentItemUid;
			if(!this.offsetLeftOriginator) this.offsetLeftOriginator = options.offsetLeftOriginator;
			if(!this.offsetLeftOuter) this.offsetLeftOuter = options.offsetLeftOuter;
			if(!this.currentSequence) this.currentSequence = options.currentSequence;
			if(!this.maxLeft) this.maxLeft = options.maxLeft;
			if(!this.maxRight) this.maxRight = options.maxRight;
			if(!this.leftObserver) this.leftObserver = options.leftObserver;
			if(!this.rightObserver) this.rightObserver = options.rightObserver;
	        
	        /**
	         * first we find out how much items we have
	         *
	         *
	         *
	         */
	         
	        childs = $(this.containerInner).childElements();
			this.sumElements = childs.length;
			
			/**
			 * find current Item
			 *
			 *
			 *
			 */
			var http = new Xover_Http;
			this.currentItemUid = http.get('tx_ttnews[tt_news]');
			
			if(this.currentItemUid > 0)
			{	
				var currentSelectedElement = $('T3NewsGalleryItem_' + this.currentItemUid);
			}
			else
			{
				var currentSelectedElement = childs[0];
			}
			
			this.T3ElementId = currentSelectedElement.id;
			
			/**
			 * then we find originators offset left
			 *
			 *
			 *
			 */
	        this.offsetLeftOriginator = this.getOriginatorOffsetLeft();
	        
	        /**
	         * set items container width
	         *
	         *
	         *
	         */
	        this.scrollWidth = $(this.T3ElementId).getWidth();
	        containerInnerWidth = (this.sumElements * this.scrollWidth);
	        $(this.containerInner).setStyle({ width: containerInnerWidth + 'px' });
			
			

			/**
	         * then we have to find out current item sequence
	         *
	         *
	         *
	         */
	        for(i = 0; i < this.sumElements; i++)
	        {
	        	if(childs[i].id == this.T3ElementId)
	        	{
	        		this.currentSequence = (i + 1);
	        		break;
	        	}
	        }
	        
	        /**
	         * pos current item
	         *
	         *
	         *
	         */
	        this.setStartPosition();
		},
		
		/**
		 * method start
		 *
		 *
		 *
		 */
		start: function()
		{
			this.leftObserver = this.moveRight.bindAsEventListener(this);
			this.rightObserver = this.moveLeft.bindAsEventListener(this);
			
			Event.observe(
				$(this.containerLeft), 
				'click', 
				this.leftObserver
			);
			
			Event.observe(
				$(this.containerRight), 
				'click', 
				this.rightObserver
			);
		},
		
		/**
		 * method stopObserving
		 *
		 *
		 *
		 */
		stopObserving: function()
		{
			Event.stopObserving(
				$(this.containerLeft), 
				'click', 
				this.leftObserver
			);
			
			Event.stopObserving(
				$(this.containerRight), 
				'click', 
				this.rightObserver
			);
		},
		
		/**
		 * method moveLeft
		 *
		 *
		 *
		 */
		moveLeft: function(continues)
		{
			if(this.currentSequence >= this.sumElements)
			{
				return;
			}
			
			leftOffset = parseInt($(this.containerInner).style.left);
			
			if(leftOffset >= 0)
			{
				diff = (leftOffset + this.offsetLeftOuter);
			}
			else
			{
				diff = (leftOffset - this.offsetLeftOuter);
			}
			
			if(diff > this.maxLeft)
			{
				this.currentSequence++;
				
				new Effect.Move(
					this.containerInner,
					{
						x: '-' + this.scrollWidth, 
						y: 0, 
						mode: 'relative',
						queue: 'end',
						duration: 0.5
					}
				);
			}
		},
		
		/**
		 * method moveRight
		 *
		 *
		 *
		 */
		moveRight: function(continues)
		{
			if(this.currentSequence <= 1)
			{
				return;
			}
			
			leftOffset = parseInt($(this.containerInner).style.left);
			
			if(leftOffset >= 0)
			{
				diff = (leftOffset + this.offsetLeftOuter);
			}
			else
			{
				diff = (leftOffset - this.offsetLeftOuter);
			}
			
			if(leftOffset < this.maxRight)
			{
				this.currentSequence--;
				
				new Effect.Move(
					this.containerInner,
					{
						x: this.scrollWidth, 
						y: 0, 
						mode: 'relative',
						queue: 'end',
						duration: 0.5
					}
				);
			}
		},
		
		/**
		 * method setStartPosition
		 *
		 *
		 *
		 *
		 */
		setStartPosition: function()
		{

			if(this.currentItemUid > 0)
			{
				alert("hier");
				itemWidth = $(this.T3ElementId).getWidth();
				addWidth = ((this.currentSequence - 1) * itemWidth);
			
				if(this.currentSequence < 2)
				{
					addWidth = this.offsetLeftOuter;
				}
				else
				{
					addWidth = (this.offsetLeftOuter - addWidth);
				}

				new Effect.Move(
					this.containerInner,
					{
						x: addWidth, 
						y: 0, 
						mode: 'relative',
						queue: 'end',
						duration: 0.4,
						delay: 1.0
					}
				);

			}

			this.markCurrentItem();

		},
		
		/**
		 * method getOriginatorOffsetLeft
		 *
		 *
		 *
		 */
		getOriginatorOffsetLeft: function()
		{
			containerInnerWidth = (this.sumElements * $(this.T3ElementId).getWidth());
			originatorWidth = $(this.containerOuter).getWidth();
			itemWidth = $(this.T3ElementId).getWidth();
			originatorCum = Element.cumulativeOffset($(this.containerOuter));
			originatorLeft = originatorCum[0];
			
			itemHalf = (itemWidth / 2);
			originatorHalf = (originatorWidth / 2);
			addWidth = (originatorHalf - itemHalf);
			oLo      = (originatorLeft + addWidth);
			this.offsetLeftOuter = addWidth;
			
			if(!this.currentItemUid > 0)
			{
				new Effect.Move(
					this.containerInner,
					{
						x: addWidth, 
						y: 0, 
						mode: 'relative',
						queue: 'end',
						duration: 0.5
					}
				);
			}
			
	        this.maxLeft = (addWidth - containerInnerWidth);
	        this.maxRight = addWidth;
			return oLo;
		},
		
		/**
		 * method markCurrentItem
		 *
		 *
		 *
		 */
		markCurrentItem: function()
		{
			var currentSelectedElement = $(this.T3ElementId);
			new Effect.Opacity(
				this.T3ElementId,
				{
					from: 1.0,
					to: 0.2,
					duration: 3.0
				}
			);
		}
    }
