﻿/*  Systematic - javascript for featurelist By: Steen F. Tøttrup / Illumi a/s */
var FeatureList = new Class({ Implements: Options, options: { speed: 1000, containerId: '', subDivClass: '', nextId: '', prevId: '', direction: 'vertical', clickableClass: '', unclickableClass: '', delay: 5000 },
	initialize: function(options) {
		this.setOptions(options);
		if ($(this.options.containerId)) {
			this.container = $(this.options.containerId);
		}

		this.items = this.container.getChildren('div');
		var width = 0;
		var height = 0;
		this.direction = this.options.direction.toLowerCase();
		if (this.direction == 'horizontal') {
			height = this.container.getSize().y;
			this.items.each(function(li, index) { width += li.getSize().x; });
		}
		else {
			width = this.container.getSize().x;
			this.items.each(function(li, index) { height += li.getSize().y; });
		}

		if (height < this.container.getParent().getSize().y) {
			$(this.options.nextId).set('style', 'display: none');
			$(this.options.prevId).set('style', 'display: none');
		}

		this.container.setStyles({ position: 'absolute', top: 0, left: 0, width: width, height: height });

		this.move = new Fx.Morph(this.container, { duration: this.options.speed });

		this.current = 0;

		$(this.options.nextId).addEvent('click', function() { this.Next(); return false; } .bind(this));
		$(this.options.nextId).addEvent('focus', function() { $(this.options.nextId).blur(); return false; } .bind(this));
		$(this.options.prevId).addEvent('click', function() { this.Prev(); return false; } .bind(this));
		$(this.options.prevId).addEvent('focus', function() { $(this.options.prevId).blur(); return false; } .bind(this));
		$(this.options.prevId).set('class', 'dimmed');

		featureDelay = this.AutoScroll.bind(this).delay(this.options.delay + this.options.speed);
	},
	AutoScroll: function() {
		this.Next();
		featureDelay = this.AutoScroll.bind(this).delay(this.options.delay + this.options.speed);
	},
	ClearAuto: function() {
		$clear(featureDelay);
		featureDelay = null;
	},
	Next: function() {
		this.ClearAuto();

		//var pos1 = this.items[this.items.length - 1];
		//alert(pos1.getSize().y + ' ' + this.container.offsetTop + ' ' + this.container.getSize().y + ' ' + this.container.getParent().getSize().y);

		if (this.container.getSize().y + this.container.offsetTop > this.container.getParent().getSize().y) {
		//if (this.current < this.items.length - 1) {
			this.current++;
			$(this.options.prevId).set('class', this.options.clickableClass);
			var pos = this.items[this.current];
			this.move.start({
				top: -pos.offsetTop,
				left: -pos.offsetLeft
			});
		}
		//if (this.current == this.items.length - 1) {
		if (this.container.getSize().y + this.container.offsetTop < this.container.getParent().getSize().y) {
			$(this.options.nextId).set('class', this.options.unclickableClass);
		}
	},
	Prev: function() {
		this.ClearAuto();
		if (this.current > 0) {
			this.current--;
			if (this.current == 0) {
				$(this.options.prevId).set('class', this.options.unclickableClass);
			}
			$(this.options.nextId).set('class', this.options.clickableClass);
			var pos = this.items[this.current];
			this.move.start({
				top: -pos.offsetTop,
				left: -pos.offsetLeft
			});
		}
	}
});
var featureDelay = null;
window.addEvent('domready', function() {if ($('feature-products-inner')) {var feature = new FeatureList({ containerId: 'feature-products-inner', subDivClass: 'feature-product', nextId: 'rightbutton', prevId: 'leftbutton', direction: 'horizontal', clickableClass: '', unclickableClass: 'dimmed'});}});
window.addEvent('domready', function() {if ($('splash-header-news-inner')) {var feature = new FeatureList({ containerId: 'splash-header-news-inner', subDivClass: 'splash-news-item', nextId: 'nextbutton', prevId: 'prevbutton', clickableClass: '', unclickableClass: 'dimmed'});}});
