var offers = {

	itemIndex: 0,
	defaults: {
		itemsPerPage: 1,
		itemsPerTransition: 1,
		transitionSpeed: 0.3,
		numbers: false
	},
	init: function(el) {
	
		if (!$(el)) {return false}
		
		this.elStr = el;
		this.container = $(el);
		this.mask = $(document.createElement('div')).addClassName('js-mask');
		this.runner = this.container.down('ul');
		this.runner.wrap(this.mask);
		this.items = this.runner.childElements();
		this.noOfItems = this.items.length;
		this.noOfPages = Math.ceil((this.noOfItems - this.defaults.itemsPerPage) / this.defaults.itemsPerTransition) + 1;
		
		if (this.defaults.numbers) {
			this.insertNumbers();
			Event.observe(this.noLinksContainer, 'click', this.numberedLinksHandler);
		}
		else {
			this.insertNextPrev();
		}
		
		Event.observe(this.nextBtn, 'click', this.nextHandler);
		Event.observe(this.prevBtn, 'click', this.prevHandler);
		
		var interval = setInterval(this.intervalHandler, 5000);
		
		Event.observe(this.container, 'mouseover', function() {
			clearInterval(interval);
		});
		
		Event.observe(this.container, 'mouseout', function() {
			interval = setInterval(offers.intervalHandler, 5000);
		});
		
		this.buttonStatus();
		this.container.setStyle({'visibility': 'visible'});
		
	},
	insertNumbers: function() {
		var links = [];
		this.noLinksContainer = new Element('ol');
		this.mask.insert({'after': this.noLinksContainer});

		for (var i = 0; i < this.noOfPages; i++) {
			// switched identifyier form href to ID as IE is rubbish
			links[i] = '<li><a id="item-' + i + '">' + (i + 1) + '</a></li>';
		}
		$(this.noLinksContainer).update(links.join(''));
		
		// insert next and prev buttons for carousel w/ numbered links
		this.noLinksContainer.insert({'bottom': '<li><a class="next">Next</a></li>'});
		this.noLinksContainer.insert({'top': '<li><a class="prev">Prev</a></li>'});

		this.nextBtn = this.noLinksContainer.select('.next')[0];
		this.prevBtn = this.noLinksContainer.select('.prev')[0];
		
	},
	insertNextPrev: function() {
		
		this.container.insert({'bottom': '<a class="next">Next</a>'});
		this.nextBtn = this.container.select('.next')[0];
		
		this.container.insert({'bottom': '<a class="prev">Prev</a>'});
		this.prevBtn = this.container.select('.prev')[0];
		
	},
	numberedLinksHandler: function(event) {
		
		var el = Event.element(event);
		if ('A' === el.tagName) {
			// get href value
			offers.itemIndex = $(event.target).readAttribute('id').split('-')[1] * offers.defaults.itemsPerTransition;
			offers.slide();
		}
		Event.stop(event);

	},
	nextHandler: function(event) {
		offers.itemIndex = offers.itemIndex + offers.defaults.itemsPerTransition;
		offers.slide();
		Event.stop(event);
	},
	prevHandler: function(event) {
		offers.itemIndex = offers.itemIndex - offers.defaults.itemsPerTransition;
		offers.slide();
		Event.stop(event);
	},
	intervalHandler: function() {
		offers.itemIndex = offers.itemIndex + offers.defaults.itemsPerTransition;
		//if (offers.itemIndex >= offers.noOfItems) {
		if (offers.itemIndex > (offers.noOfItems - offers.defaults.itemsPerPage)) {
			offers.itemIndex = 0;
		}
		offers.slide();
	},
	buttonStatus: function() {
	
		if (this.defaults.numbers) {
			// highlight numbered link
			var n = Math.ceil(this.itemIndex / this.defaults.itemsPerTransition) + 2; // add 2 instead of 1 to make up for prev buttons
			this.noLinksContainer.select('a').each(function(el) {
				$(el).removeClassName('active');
			});
			//$$('#' + this.elStr + ' ol li:nth-child(' + n + ') a')[0].addClassName('active');
			this.noLinksContainer.select('li:nth-child(' + n + ') a')[0].addClassName('active');
		}
		
		// highlight prev next button
		// reset opacity class
		this.nextBtn.setStyle({opacity: 1});
		this.prevBtn.setStyle({opacity: 1});
		
		if (this.itemIndex === (this.noOfItems - this.defaults.itemsPerPage)) {
			this.nextBtn.setStyle({'opacity': 0.5});
		}
		else if (this.itemIndex === 0) {
			this.prevBtn.setStyle({'opacity': 0.5});
		}	
	},
	slide: function() {
		
		Number(this.itemIndex);

		// check whether there are enough items to slide to
		if (this.itemIndex > (this.noOfItems - this.defaults.itemsPerPage)) {
			this.itemIndex = this.noOfItems - this.defaults.itemsPerPage; // go to last panel - items per transition
		}
		else if (this.itemIndex < 0) {
			// go to first
			this.itemIndex = 0;
		}
		
		var n = this.itemIndex + 1; // nth chiild isn't zero based
		var nextItem = this.runner.select('li:nth-child(' + n + ')');
		
		var pos = nextItem[0].positionedOffset();
		//new Effect.Move(this.runner, { x: -pos.left, mode: 'absolute', duration: this.defaults.transitionSpeed });
		new Effect.Move(this.runner, { x: -pos.left, mode: 'absolute', duration: this.defaults.transitionSpeed, transition: Effect.Transitions.EaseFromTo });
		
		this.buttonStatus();
		
	}
}

// IE fires dom loaded event to early so load is used
Event.observe(window, 'load', function() { 


	$(document.body).addClassName('js-enabled');

	// inititate single offers carousel
	if ($('carousel')) { offers.init('carousel'); }
	
	// inititate multiple offers carousel
	if ($('large-carousel')) {
		offers.defaults.itemsPerPage = 3;
		offers.defaults.itemsPerTransition = 3;
		offers.defaults.transitionSpeed = 0.6;
		offers.defaults.numbers = true;
		offers.init('large-carousel');
	}


});
