
/* Thanks to noobSlide, operating under the MIT License, for inspiration and guidance */

var scrollGallery = new Class({

	Implements: Options,
	options: {
		mode: 'horizontal',
		modes: { horizontal: ['left','width'], vertical: ['top','height'] }, 
		screen: null,
		reel: null,
		frames: new Array(),
		framecount: 0,
		frame_selector: null
	},

	initialize: function(options){
		this.setOptions(options) ;
		this.screen_coords = this.options.screen.getCoordinates() ;
		this.screensize = this.options.mode == 'horizontal' ? this.screen_coords.width : this.screen_coords.height ;
		this.reelsize = 0 ;
		if ($chk(this.options.frame_selector)) {
			this.options.frames = this.options.reel.getChildren(this.options.frame_selector) ;
		}
		this.options.framecount = this.options.frames.length ;
		this.options.frames.each(function(it, ind){
			var tmp_it = $(it) ;
			this.reelsize += tmp_it.getSize().x + parseInt(tmp_it.getStyle('marginLeft')) + parseInt(tmp_it.getStyle('marginRight')) ;	
		}, this) ;
		this.options.reel.setStyle('width', this.reelsize) ;
	},

	previous: function(){},
	bound_previous: function(){},

	next: function(){},
	bound_next: function(){},

	walk: function(){},

	halt: function(){}
}) ;

var autoMouseScrollGallery = new Class({

	Extends: scrollGallery,
	options: {
		fxOptions: { duration: 10, link: 'ignore' },
		restarea: 600,
		prevtime: null,
		nexttime: null,
		movestate: 0,
		maxspeed: 16
	},

	scrollspeed: 0,

	initialize: function(options){
		this.parent(options) ;
		this.setOptions(options) ;
		this._s_walk = this.walk.bind(this) ;
		this._s_halt = this.halt.bind(this) ;
		this.slideFX = new Fx.Scroll(this.options.screen, this.options.fxOptions) ;
		this.options.screen.addEvent('mouseover', this._s_walk) ;
		this.options.screen.addEvent('mousemove', this._s_walk) ;
		this.options.screen.addEvent('mouseout', this._s_halt) ;
	},

	previous: function() {
		this.options.movestate = 1 ;
		if (this.options.screen.getScroll().x > 0) {
			this.slideFX.start(this.options.screen.getScroll().x - this.scrollspeed, 0) ;
			this.options.prevtime = this.previous.delay(10, this) ;
		} else {
			this.slideFX.toLeft() ;
		}
	},

	next: function() {
		this.options.movestate = 2 ;
		if (this.options.screen.getScroll().x < this.reelsize) {
			this.slideFX.start(this.options.screen.getScroll().x + this.scrollspeed, 0) ;
			this.options.nexttime = this.next.delay(10, this) ;
		} else { 
			this.slideFX.toRight() ;
		}
	},

	walk: function(e) {
		var mainobjoffset = this.getoffset(this.options.screen),
			dsocx = window.getScroll().x,
			dsocy = window.getScroll().y,
			curposy = e.client.x ;
		curposy -= mainobjoffset - dsocx ;
		var leftbound = (this.screensize - this.options.restarea) / 2 ;
		var rightbound = (this.screensize + this.options.restarea) / 2 ;

		if (curposy > rightbound) {
			this.scrollspeed = (curposy - rightbound) / ((this.screensize - this.options.restarea) / 2) * this.options.maxspeed ;
			if (this.options.movestate != 2) this.next() ;
		} else if (curposy < leftbound) {
			this.scrollspeed = (leftbound - curposy) / ((this.screensize - this.options.restarea) / 2) * this.options.maxspeed ;
			if (this.options.movestate != 1) this.previous() ;
		} else {
			this.slideFX.cancel() ;
			this.halt() ;
			this.scrollspeed = 0 ;
		}
	},

        halt: function(e) {
		$clear(this.options.prevtime) ;
		$clear(this.options.nexttime) ;
		this.options.movestate = 0 ;
        },

	
        getoffset: function() {
                var totaloffset = (this.options.mode == 'horizontal') ? this.screen_coords.left : this.screen_coords.top ;
                var _parent = this.options.screen.offsetParent ;
                while (_parent != null) {
                        // var _tmpcoords = _parent.getPosition() ;
                        // totaloffset = (this.options.mode == 'horizontal') ? totaloffset + _tmpcoords.x : totaloffset + _tmpcoords.y ;
			totaloffset = (this.options.mode == 'horizontal') ? totaloffset + _parent.offsetLeft : totaloffset + _parent.offsetTop;
                        _parent = _parent.offsetParent ;
                }
                return totaloffset ;
        }
}) ;

var navClickScrollGallery = new Class({

	Extends: scrollGallery,
	options: {
		fxOptions: { duration: 500, link: 'ignore' },
		startitem: 0,
		addButtons: null,
		btnCntConfig: null,
		frames_per_screen: 1,
		framecount_screen: 0
	},

	currentIndex: null,
	// previousIndex: null,
	// nextIndex: null,
	lastIndex: null,

	initialize: function(options){
		this.parent(options) ;
		this.setOptions(options) ;

		this.options.framecount_screen = this.options.frames_per_screen > 1 ? Math.ceil(this.options.framecount/this.options.frames_per_screen) : this.options.framecount ;

		this.options.button_event = this.options.button_event || 'click' ;
		this.options.handle_event = this.options.handle_event || 'click' ;
		this.options.handles = this.options.handles || null ;
		if ($chk(this.options.handles)) {
			this.addHandleButtons(this.options.handles) ;
		}       

		this.bound_previous = this.previous.bind(this),
		this.bound_next = this.next.bind(this),
		this.buttons = {
			previous: [],
			next: [] 
		} ;
		this.enableActionButtons() ;
		if ($chk(this.options.btnCntConfig)) {
			this.btnCntConfig = this.options.btnCntConfig ;
			this.countElements = [] ;
			this.addCountElements(this.options.startitem) ;
		}
		this.slideFX = new Fx.Tween(this.options.reel, $extend(this.options.fxOptions, { property: this.options.modes[this.options.mode][0] })) ;
		this.walk(this.options.startitem) ;
	},

	addHandleButtons: function(handles) {
		for (var i=0; i<handles.length; i++) {
			handles[i].addEvent(this.options.handle_event, this.walk.bind(this, [i])) ;
		}
	},

	addActionButtons: function(action, buttons) {
		for (var i=0; i<buttons.length; i++) {
			switch(action) {
				case 'previous': 
					buttons[i].addEvent(this.options.button_event, this.bound_previous) ; 
					break ;
				case 'next': 
					buttons[i].addEvent(this.options.button_event, this.bound_next) ; 
					break ;
			}
			this.buttons[action].push(buttons[i]) ;
		}
	},

	removeActionButtons: function(action, buttons) {
		for (var i=0; i<buttons.length; i++) {
			switch(action) {
				case 'previous': 
					buttons[i].removeEvent(this.options.button_event, this.bound_previous) ; 
					break ;
				case 'next': 
					buttons[i].removeEvent(this.options.button_event, this.bound_next) ; 
					break ;
			}
			this.buttons[action].push(buttons[i]) ;
		}
	},

	disableActionButtons: function() {
		if ($chk(this.options.addButtons)) {
			for(var dItem in this.options.addButtons) {
				this.removeActionButtons(dItem, $type(this.options.addButtons[dItem]) == 'array' ? this.options.addButtons[dItem] : [this.options.addButtons[dItem]]) ;
			}
		}
	},

	enableActionButtons: function() {
		if ($chk(this.options.addButtons)) {
			for(var dItem in this.options.addButtons) {
				this.addActionButtons(dItem, $type(this.options.addButtons[dItem]) == 'array' ? this.options.addButtons[dItem] : [this.options.addButtons[dItem]]) ;
			}
		}
	},

	addCountElements: function(item) {
		for (it=0; it<this.options.framecount_screen; it++) {	
			var tmpSpan = new Element('span').injectInside(this.btnCntConfig['holder']).setProperty('value', it).set('html', '&#149;') ;
			tmpSpan.addClass(this.btnCntConfig['offClass']) ;
		}
	},

	previous: function() {
		// alert('prev() = ' + (this.currentIndex > 0 ? this.currentIndex - 1 : this.options.framecount_screen - 1)) ;
		this.walk((this.currentIndex > 0 ? this.currentIndex - 1 : this.options.framecount_screen - 1)) ;
	},

	next: function() {
		// alert('next() = ' + (this.currentIndex < this.options.framecount_screen - 1 ? this.currentIndex + 1 : 0)) ;
		this.walk((this.currentIndex < this.options.framecount_screen - 1 ? this.currentIndex + 1 : 0)) ;
	},

	walk: function(item) {
		if (item != this.currentIndex) {
			this.lastIndex = this.currentIndex || this.options.startitem ;
			this.currentIndex = item ;
			this.previousIndex = this.currentIndex + (this.currentIndex > 0 ? -1 : this.options.framecount_screen - 1) ;
			this.nextIndex = this.currentIndex + (this.currentIndex < this.options.framecount_screen - 1 ? 1 : 1 - this.options.framecount_screen) ;
			// alert('screensize * -currentIndex = ' + (this.screensize * -this.currentIndex)) ;
			this.slideFX.start((this.screensize * -this.currentIndex)) ;
			if (this.btnCntConfig) {
				this.btnCntConfig['holder'].getElementsByTagName('span')[this.lastIndex].removeClass(this.btnCntConfig['onClass']) ;
				this.btnCntConfig['holder'].getElementsByTagName('span')[this.currentIndex].addClass(this.btnCntConfig['onClass']) ;
			}
		}
	}
}) ;
