/*------------------------------------------------------------------------
# JA Zeolite for Joomla 1.5 - Version 1.0 - Licence Owner JA90689
# ------------------------------------------------------------------------
# Copyright (C) 2004-2008 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
# @license - Copyrighted Commercial Software
# Author: J.O.O.M Solutions Co., Ltd
# Websites:  http://www.joomlart.com -  http://www.joomlancers.com
# This file may not be redistributed in whole or significant part.
-------------------------------------------------------------------------*/
Fx.Scroll = Fx.Base.extend({

	options: {
		overflown: [],
		offset: {'x': 0, 'y': 0}
	},

	initialize: function(element, options){
		this.now = [];
		this.element = $(element);
		this.bound = {'stop': this.stop.bind(this, false)};
		this.addEvent('onStart', function(){
			document.addEvent('mousewheel', this.bound.stop);
		}.bind(this));
		this.removeEvent('onComplete', function(){
			document.removeEvent('mousewheel', this.bound.stop);
		}.bind(this));
		this.parent(options);
	},

	setNow: function(){
		for (var i = 0; i < 2; i++) this.now[i] = this.compute(this.from[i], this.to[i]);
	},

	/*
	Property: scrollTo
		Scrolls the chosen element to the x/y coordinates.

	Arguments:
		x - the x coordinate to scroll the element to
		y - the y coordinate to scroll the element to
	*/

	scrollTo: function(x, y){
		if (this.timer && this.options.wait) return this;
		var el = this.element.getSize();
		var values = {'x': x, 'y': y};
		for (var z in el.size){
			var max = el.scrollSize[z] - el.size[z];
			if ($chk(values[z])) values[z] = ($type(values[z]) == 'number') ? values[z].limit(0, max) : max;
			else values[z] = el.scroll[z];
			values[z] += this.options.offset[z];
		}
		return this.start([el.scroll.x, el.scroll.y], [values.x, values.y]);
	},

	/*
	Property: toTop
		Scrolls the chosen element to its maximum top.
	*/

	toTop: function(){
		return this.scrollTo(false, 0);
	},

	/*
	Property: toBottom
		Scrolls the chosen element to its maximum bottom.
	*/

	toBottom: function(){
		return this.scrollTo(false, 'full');
	},

	/*
	Property: toLeft
		Scrolls the chosen element to its maximum left.
	*/

	toLeft: function(){
		return this.scrollTo(0, false);
	},

	/*
	Property: toRight
		Scrolls the chosen element to its maximum right.
	*/

	toRight: function(){
		return this.scrollTo('full', false);
	},

	/*
	Property: toElement
		Scrolls the specified element to the position the passed in element is found.

	Arguments:
		el - the $(element) to scroll the window to
	*/

	toElement: function(el){
		var parent = this.element.getPosition(this.options.overflown);
		var target = $(el).getPosition(this.options.overflown);
		return this.scrollTo(target.x - parent.x, target.y - parent.y);
	},

	increase: function(){
		this.element.scrollTo(this.now[0], this.now[1]);
	}

});

window.addEvent('domready', function() {
	var fx = new Fx.Style($('ja-contentslider-nav-knob'), 'left', {duration: 300, wait: false});
	var myScrollFx = new Fx.Scroll($('ja-contentslider-center'), {
		wait: false
	});

	var x = $('ja-contentslider-center').scrollWidth - $('ja-contentslider-center').offsetWidth;
	var mySlide = new Slider($('ja-contentslider-nav-slider'), $('ja-contentslider-nav-knob'), {	
		steps: x,

		onTick: function(pos){
			fx.start(pos);
			myScrollFx.scrollTo(this.toStep(pos), 0);
		}

	}).set(0);

	mySlide.drag.addEvent('onDrag', function() {
		mySlide.step = mySlide.toStep(mySlide.drag.value.now[mySlide.z]);
		myScrollFx.set([mySlide.step, 0]);
	});

	$('ja-contentslider-nav-left').addEvent ('click', function() {
		mySlide.step -= 100;
		mySlide.step = mySlide.step.limit(0, mySlide.options.steps);
		fx.start(mySlide.toPosition(mySlide.step));
		myScrollFx.scrollTo(mySlide.step, 0);
	});

	$('ja-contentslider-nav-right').addEvent ('click', function() {
		mySlide.step += 100;
		mySlide.step = mySlide.step.limit(0, mySlide.options.steps);
		fx.start(mySlide.toPosition(mySlide.step));
		myScrollFx.scrollTo(mySlide.step, 0);
	});

});
