/**
 * @author nd
 */

var ExtScroller = new Class({

		options: {
			rootline: {
				height: 21 //px
			},

			vscrollbar: {
				width: 12, //px
				button: {
					height: 18, //px
					width: 12 //px
				}
			}
		},

		contentWrapper: '',

		initialize: function(options){
			this.setOptions(options);
			this.contentWrapper = $('content_wrapper');
			this.inserted = 0;
			
			// set the content_wrapper div to overflow hidden, because overflow auto is only for the no JS version
			this.contentWrapper.setStyle('overflow','hidden');

			// get the size of the content_wrapper div
			var contentwrappersize = this.contentWrapper.getSize();
			
			// Content Div 
			this.contentDIV = $('content');
			var contentDIVsize = this.contentDIV.getSize();
			
			// check if the vertical scrollbar is needed
			if (contentDIVsize.scrollSize.y > ( contentwrappersize.size.y + this.options.rootline.height ) ){
				// init Vertical Scrollbar
				this.initVerticalScrollbar();
			}

		},

		update: function(){
			var contentwrappersize = this.contentWrapper.getSize();
			var contentDIVsize = this.contentDIV.getSize();
		
			// check if the vertical scrollbar is needed
			if (contentDIVsize.scrollSize.y > ( contentwrappersize.size.y + this.options.rootline.height ) ){
				// init Vertical Scrollbar
				this.initVerticalScrollbar(contentwrappersize);
			} else {
				// remove the vScrollbar divs from the DOM if exist
				this.removeVerticalScrollbar();

				// set the width and height for the content div to available size
				$('content').setStyles({
					'width': contentwrappersize.size.x,
					'float': 'none',
					'height': contentwrappersize.size.y
				});
			}

		},

		initVerticalScrollbar: function(size){
						
			// insert the vScrollbar divs to the DOM
			this.insertVerticalScrollbarDivs();

			// fix  IE6 issue
			// 26px: 2*this.options.vscrollbar.button.height
			var ieCorrection = window.ie6 ? ( $('floor').getSize().scrollSize.y + 26 ): 0;
			var contentwrappersize = size ? size : this.contentWrapper.getSize();
			var vScrollbarHeight = contentwrappersize.size.y - ( this.options.rootline.height + ieCorrection );


			// set the styling for the scrollbar_wrapper div
			$('scrollbar_wrapper').setStyles({'height': vScrollbarHeight});

			// set the styling for the content div
			// reduce the width to have place for the left floated vScrollbar
			$(par.contentId).setStyles({
				'width': contentwrappersize.size.x -  this.options.vscrollbar.width,
				'float': 'left',
				'overflow': 'hidden',
				'height': vScrollbarHeight
			});

			// set the height off the Scrollbar
			$E('div.vscrollarea div.scrollBarContainer').setStyle('height', vScrollbarHeight - 2 * this.options.vscrollbar.button.height );

			if(!this.inserted){	
				// creat the vertical Mooscroller Object
				this.theMooScroller = new MooScroller(
										$E('div#scrollbar_wrapper div#content'), 
										$E('div#scrollbar_wrapper .vscrollarea .scrollKnob'), {
											scrollLinks: {
												forward: $E('div#scrollbar_wrapper .vscrollarea div.scrollForward'),
												back: $E('div#scrollbar_wrapper .vscrollarea div.scrollBack')
											}
				});
				this.inserted = 1;
			} else {
				this.theMooScroller.update();
			}
		},

		/*
		 * insertVerticalScrollbarDivs
		 *
		 * This function insert the scrollbar object into the DOM
		 *
		 * 	<div class="vscrollarea">
		 *		<div class="scrollBack"></div>
		 *		<div class="scrollBarContainer">
		 *			<div class="scrollKnob">
		 *				<div class="scrollKnobTop"></div>
		 *				<div class="scrollKnobBottom"></div>
		 *			</div>
		 *		</div>
		 *		<div class="scrollForward"></div>
		 *	</div>
		 */
		insertVerticalScrollbarDivs: function(){
			if(this.inserted){
				$$('.vscrollarea').setStyle('display','block');		
			} else {
				
				if ($$('.vscrollarea').length <= 0) {
					var vscrollarea = new Element('div', {
						'class': 'vscrollarea'
					});
	
					var scrollBack = new Element('div', {
						'class': 'scrollBack'
					}).injectInside(vscrollarea);
	
					var scrollbarcontainer = new Element('div', {
						'class': 'scrollBarContainer'
					});
					var scrollknob = new Element('div', {
						'class': 'scrollKnob'
					});
	
					var scrollKnobTop = new Element('div', {
						'class': 'scrollKnobTop'
					}).injectInside(scrollknob);
					var scrollKnobBottom = new Element('div', {
						'class': 'scrollKnobBottom'
					}).injectInside(scrollknob);
	
					scrollknob.injectInside(scrollbarcontainer);
					scrollbarcontainer.injectInside(vscrollarea);
	
					var scrollForward = new Element('div', {
						'class': 'scrollForward'
					}).injectInside(vscrollarea);
	
					vscrollarea.injectInside('scrollbar_wrapper');
				}		
			}	
		},

		removeVerticalScrollbar: function(){
			$$('.vscrollarea').setStyle('display','none');		
			/*
			var vscrollarea = $E('vscrollarea');
			
			// remove form dom if exist

			if (vscrollarea){
				vscrollarea.remove();
			}
			*/
		}

});
ExtScroller.implement(new Events());
ExtScroller.implement(new Options());