//window.addEvent("domready", function() {
//	if($('log')){
//		$('log').addEvent('click', function(){
//			if ($('log').hasClass('active')){
//				$('log').removeClass('active');
//				$('loginform').setStyle('display', 'none');
//			} else {
//				$('log').addClass('active');
//				$('loginform').setStyle('display', 'block')
//			}
//		});								
//	}
//	if($('username')){
//		$('username').addEvent('click', function(){
//			if ($('user').hasClass('active')){
//						$('user').removeClass('active');
//						$('mod-clientmenu').setStyle('display', 'none')
//			} else {
//						$('user').addClass('active');
//						$('mod-clientmenu').setStyle('display', 'block')
//			}
//		});
//	}
//}


var SimpleCarousel = new Class({

  Extends: Fx.Scroll,
	
	options: {
		// Fx.Scroll options
		duration: 500,
		link: 'cancel',
		transition: 'cubic:out',
		mode: 'horizontal',
		wheelStops: false,
		// static carousel options
		loop: true,
		auto: false,
		interval: 0,
		visible: 1,  // number of visible items
		size: false, // overrides item dimensions
		childSelector: ':first-child > div',
		contentSelector: ':first-child'
	},
	
	initialize: function(element, options){
		this.parent(element, options);
		
		this.elements = this.element.getElements(this.options.childSelector);
		this.count = this.elements.length;
		this.content = this.element.getElement(this.options.contentSelector);
		this.current = 0;
		this.index = 0;
		
		// set width
		this.size = this.options.size ? this.options.size : this.elements[0].getSize().x;
		this.content.setStyle('width', this.elements.length*this.size);

		// auto move
		this.scrollTimer = null;
		if (this.options.auto){
			this.autoScroll('start');
		}
		
		// self events
		this.addEvents({
		  'complete': function(){
				this.fireEvent('scrollComplete', this.index);
			}
		});
		this.fireEvent('ready');
		
		return this;
		
	},
	
	getCount: function(){
		return this.elements.length;
	},
	
	getCurrent: function(){
		return this.index;
	},
	
	toNext: function(steps){
		if (steps == undefined) steps = 1;
		for (var i = 0; i<steps; i++){
			if (this.current+this.options.visible == this.count){
				if (this.options.loop){
					var scroll = this.element.getScroll();
					var element = this.content.getFirst();
					element.dispose();
					this.set(scroll.x-this.size, scroll.y)
					element.inject(this.content, 'bottom');
					this.index = ++this.index%this.count;
				} else {
					this.current = 0;
					this.index = 0;
				}
			} else {
				this.current++;
				this.index = ++this.index%this.count;
			}
		}
		this.toElement(this.elements[this.index]);
		
		return this;
	},
	
	toPrevious: function(steps){
		if (steps == undefined) steps = 1;
		for (var i = 0; i<steps; i++){
			if (this.current == 0){
				if (this.options.loop){
					var scroll = this.element.getScroll();
					var element = this.content.getLast();
					element.dispose();
					this.set(scroll.x+this.size, scroll.y)
					element.inject(this.content, 'top');
					this.index--;
					if (this.index < 0) this.index = this.count-1;
				} else {
					this.current = this.count-1;
					this.index = this.count-1;
				}
			} else {
				this.current--;
				this.index--;
				if (this.index < 0) this.index = this.count-1;
			}
		}
		this.toElement(this.elements[this.index]);
		
		return this;
	},
	
	toItem: function(index){
	  if (this.options.auto) this.autoScroll('stop');
		
		if (index == 'next') index = (this.index+1)%this.count;
		if (index == 'prev') index = this.index-1;
		if (index < 0) index = this.count-1;
	

		var stepToPrevious = (index < this.index ? this.index - index : this.index + this.count - index);
		var stepToNext = (index > this.index ? index - this.index : index + this.count - this.index);

    if (stepToPrevious < stepToNext){
			this.toPrevious(stepToPrevious);
		} else {
			this.toNext(stepToNext);
		}
		
		if (this.options.auto) this.autoScroll('start');
		return this;
	},
	
	autoScroll: function(command){
		switch (command){
			case 'start':
			  this.scrollTimer = this.toNext.periodical(this.options.interval, this);
			break;
			case 'stop':
			  $clear(this.scrollTimer);
			break;
		}
		
		return this;
	}


});


var Site = {
	
	init: function(){

	// Homepage carousel
			var topBooks = document.id('top-books');
			if (topBooks){
				var carousel = new SimpleCarousel(topBooks.getElement('div.carousel'), {
					size: 326,
					visible: 3,
					duration: 500,
					interval: 7000,
					loop: true,
					auto: true,
					childSelector: 'div.good-classic-outer',
					contentSelector: 'div.carousel-content'
				});
				
				var prevBtn = topBooks.getElement('a.prev');
				var nextBtn = topBooks.getElement('a.next');
				
				if (prevBtn){
					prevBtn.addEvent('click', function(event){
						event.preventDefault();
						carousel.toItem('prev');
					});
				}
				if (nextBtn){
					nextBtn.addEvent('click', function(event){
						event.preventDefault();
						carousel.toItem('next');
					});
				}
			}
	}
}
		


window.addEvent('domready', Site.init.bind(Site));
