

SocialShareSlider = new Class({
	Implements: [ Options ],

	options: {
		offset: 200,
		tabwidth: 50,
		width: 400,
		transition: Fx.Transitions.Quad.easeInOut,
		duration: 500,
		timeout: 1000,
		position: 'left',
		twitterUser: ''
	},

	initialize: function(container, options) {
		this.setOptions(options);
		this.container = $(container);
		if(!this.container) return;

		this.timer = null;
		this.tweens = [];

		if($('social_share_slider_twitter_followers')) {
			new Request({
				method: 'get',
				url: '/index.php?request=main.ajax-twitter-info&user='+this.options.twitterUser,
				onSuccess: function(response) {
					if(response) {
						response = JSON.decode(response).shift();
						$('social_share_slider_twitter_followers').innerHTML = response.user.followers_count + ' Followers';
					}
				}
			}).send();
		}


		this.container.setStyles({
			'display': 'block',
			'opacity': 0
		});


		this.container.setStyles({
			'top': $(document.body).getScroll().y + this.options.offset,
			'left': (this.options.position == 'right') ? $(document.body).getWidth()  - this.options.tabwidth  : (this.container.getWidth() - this.options.tabwidth) * -1
		});

		new Fx.Tween(this.container, {
			duration: 1000
		}).start('opacity', 1);


		this.container.addEvent('mouseover', function() {
			this.show();
		}.bind(this));

		this.container.addEvent('mouseout', function() {
			this.hide();
		}.bind(this));

		setInterval(function() {
			this.windowScroller();
		}.bind(this), 300);
	},

	windowScroller: function() {
		if(this.tweens['windowScroller']) this.tweens['windowScroller'].cancel();

		var targetTop = $(document.body).getScroll().y + this.options.offset;

		if(this.container.getTop() != targetTop) {
			this.tweens['windowScroller'] = new Fx.Tween(this.container, {
				duration: 300,
				transition: Fx.Transitions.Quad.easeOut
			}).start('top', targetTop);
		}
	},

	show: function() {
		clearTimeout(this.timer);

		if(this.tweens.show && !this.tweens.show.isComplete) return;
		if(this.tweens.doHide) this.tweens.doHide.cancel();

		var targetLeft = (this.options.position == 'right') ? $(document.body).getWidth() - this.options.width : this.options.width - this.container.getWidth();

		if(this.container.getLeft() != targetLeft) {
			this.tweens['show'] = new Fx.Tween(this.container, {
				transition: Fx.Transitions.Quad.easeInOut,
				duration: 500,
				onComplete: function() {
					this.isComplete = true;
				},
				onCancel: function() {
					this.isComplete = true;
				}
			}).start('left', targetLeft);
		}
	},

	hide: function() {
		clearTimeout(this.timer);
		this.timer = setTimeout(function() {
			this.doHide();
		}.bind(this), this.timeout);
	},

	doHide: function() {
		clearTimeout(this.timer);
		if(this.tweens.doHide && !this.tweens.doHide.isComplete) return;
		if(this.tweens.show) this.tweens.show.cancel();


		var targetLeft = (this.options.position == 'right') ? $(document.body).getWidth()  - this.options.tabwidth  : (this.container.getWidth() - this.options.tabwidth) * -1;

		this.tweens['doHide'] = new Fx.Tween(this.container, {
			transition: Fx.Transitions.Quad.easeInOut,
			duration: 500,
			onComplete: function() {
				this.isComplete = true;
			},
			onCancel: function() {
				this.isComplete = true;
			}
		}).start('left', targetLeft);
	}

});


