/*=======================================
 File Name: jquery.nah.js
 Author: Richard Scarrott
 Copyright: Tangent One
 Created (updated): 30/07/10 (30/07/10)
=======================================*/
$.nah = new Object();

if (typeof Object.create !== 'function') {
	function F() {}
	Object.create = function (o) {
		F.prototype = o;
		return new F();
	};
}

(function($) {
	// product carousel
	$.nah.carousel = {
		itemIndex: 0,
		itemsPerPage: 1,
		itemsPerTransition: 1,
		speed: 750,
		pause: 3000,
		init: function($el) {
			if (!$el.length) {return false;}
			this.$container = $el;
			this.setUp();
		},
		setUp: function() {
		
			this.$runner = this.$container.find('ul');
			this.$items = this.$runner.children().eq(0).addClass('current').end();
			this.noOfItems = this.$items.length;
			this.noOfPages = Math.ceil((this.noOfItems - this.itemsPerPage) / this.itemsPerTransition) + 1;

			if (this.noOfItems <= this.itemsPerPage) {return false;}
			
			this.insertNumberedLinks();
			this.buttonStatus();
			
			this.startInterval();
			this.$container.hover($.proxy(this, 'stopInterval'), $.proxy(this, 'startInterval'));
			this.$noLinksContainer.find('a').click($.proxy(this, 'numberedLinksHandler'));
		
		},
		insertNumberedLinks: function() {

			// insert numbered links
			var i, links = [];
			this.$noLinksContainer = $('<ol />').appendTo(this.$container);
			for (i = 0; i < this.noOfPages; i++) {

				if (i === this.noOfPages - 1) {
					links[i] = '<li class="last-child"><a href="#item--' + i + '">' + (i + 1) + '</a></li>';
				}
				else {
					links[i] = '<li><a href="#item--' + i + '">' + (i + 1) + '</a></li>'; // using double hyphen as IE sticks full URL in place so error can occur if href is split by single '-', prob should use a somthing better than double hyphen
				}
			}
			this.$noLinksContainer.append(links.join('')).wrap('<div class="nav">');
		
		},
		numberedLinksHandler: function(e) {
			this.itemIndex = $(e.target).attr('href').split('--')[1] * this.itemsPerTransition;
			this.switchPanel();
			return false;
		},
		buttonStatus: function() {
			this.$noLinksContainer
				.children().removeClass('current')
					.eq(Math.ceil(this.itemIndex / this.itemsPerTransition)).addClass('current');
		},
		switchPanel: function(animate) {
		
			var $currentItem, $nextItem;
		
			$currentItem = $('.current', this.$container).addClass('previous').removeClass('current');
			$nextItem = this.$items.eq(this.itemIndex);
			
			if (animate) {
				$nextItem.css({'opacity': 0}).addClass('current').animate({'opacity': 1}, this.speed, function() {
					$currentItem.removeClass('previous');
				});
			}
			else {
				$nextItem.addClass('current');
				$currentItem.removeClass('previous');
			}
			
			this.buttonStatus();
		
		},
		intervalHandler: function() {
			this.itemIndex = this.itemIndex + this.itemsPerTransition;
			if (this.itemIndex > (this.noOfItems - this.itemsPerPage)) {
				this.itemIndex = 0;
			}
			this.switchPanel(true);
		},
		startInterval: function() {
			this.interval = setInterval($.proxy(this, 'intervalHandler'), this.pause);
		},
		stopInterval: function() {
			clearInterval(this.interval);
		}
	},
    $.nah.calendarPicker = {
        init: function(){
            if (!$('input.datepicker-box').length) {return false;}

            $('input.datepicker-box').datepicker({ dateFormat: 'dd-mm-yy' });
            $('input.datetimepicker-box').datepicker({
                dateFormat: 'dd-mm-yy',
                duration: '',
                showTime: true,
                constrainInput: false,
                stepMinutes: 1,
                stepHours: 1,
                altTimeField: '',
                time24h: true  });
        }
    };

})(jQuery);
	
jQuery(document).ready(function($) {
	$('body').addClass('js-enabled');
	$.nah.carousel.init($('#intro'));
    $.nah.calendarPicker.init(); 
});


