(function () {
	/*
	* jQuery Pushbox
	* jQuery Pushbox based on the efocus PushBox class 
	*
	* @author Lee Boonstra, <lee.boonstra@efocus.nl>
	* @since 15 jun 2011
	* @version 1.0
	* @copyright eFocus
	*
	* @uses jQuery 1.4.2, <http://www.jquery.com>
	*/
	$.fn.Pushbox = function (options) {
		return this.each(function () {
			var opt = options || {};
			var arrSlides = jQuery(this).find('li.slide');
			var elViewport = this;
			var duration = 0;
			var isPaused = false;
			var intAnimSource = 0;

			options = {
				'navigation': true, 			// set to true for automatic creation or provide jQuery array with DOM elements, incase false override createNavSpecial
				'navigationclass': 'pushbox_nav', // classname to be given to created navigation // incase navigation false this is the customn class name to search for.
				'interval': 1, 			// interval in seconds, set to 0 to disable automatic rotation
				'transition': 'slide', 		// 'fade', 'slide'
				'freezeonhover': true, 			// enables pausing of automatic rotation on mouseover
				'random': true				// randomize the slide on startup
			};
			for (i in options) {
				if (opt[i] != undefined) {
					options[i] = opt[i];
				}
			}

			init();

			/*
			* Initialize the pushbox
			*/
			function init() {
				var arrNav = null;
				var timer = 0;
				var intTargetSlide = 0;

				if (options.random) {
					intCurrent = getRandomSlide()
				} else {
					intCurrent = 0;
				}

				if (options.transition === 'fade') {
					duration = 400;
				} else if (options.transition === 'slide') {
					duration = 500;
				}

				jQuery(arrSlides[intCurrent]).animate({ 'opacity': 1 }, { 'duration': duration });

				if (options.navigation) {
					setDefaultNavigation();
				} else {
					setCustomNavigation();
				}

				if (options.freezeonhover) setPauseEvent();
				if (options.interval > 0) queueSlide();

				initViewport();
			}

			/*
			* Set Random slide number
			*/
			function getRandomSlide() {
				return intSlideNr = Math.floor(Math.random() * arrSlides.length);
			}

			/*
			* Queues next slide with give interval
			*/
			function queueSlide() {
				timer = setTimeout(jQuery.proxy(function () {
					showSlide("next");
				}, this), options.interval * 1000);
			}

			/*
			* Set pause event on hover
			*/
			function setPauseEvent() {
				jQuery(elViewport).bind({
					'mouseenter': jQuery.proxy(function (event) {
						jQuery(arrSlides).clearQueue();
						if (timer) clearTimeout(timer);
						isPaused = true;
					}, this),

					'mouseleave': jQuery.proxy(function () {
						isPaused = false;
						queueSlide();
					}, this)
				});
			}

			/*
			* Set required styles on viewport element and slide elements
			* //TODO - set are there more styles to set?
			*/
			function initViewport() {
				jQuery(elViewport).css("overflow", "hidden");

				arrSlides.css({
					'position': 'absolute',
					'left': 0,
					'top': 0,
					'z-index': 10
				});

				jQuery(arrSlides[intCurrent]).css({
					'z-index': 20
				});
			}

			/*
			* Create default navigation
			*/
			function setDefaultNavigation() {
				var elNavContainer = jQuery('<ul class="top_rounded boxshadow gradient">').appendTo(elViewport);
				elNavContainer.wrap('<div class=' + options.navigationclass + '/>');

				arrSlides.each(function (intIndex, elSlide) {
					var listItem = document.createElement("li");
					var linkItem = document.createElement("span");

					switch (intIndex) {
						case 0:
							jQuery(listItem).addClass("camper");
							jQuery(linkItem).html("Camper");
							break;
						case 1:
							jQuery(listItem).addClass("camping");
							jQuery(linkItem).html("Camping");
							break;
						case 2:
							jQuery(listItem).addClass("boat");
							jQuery(linkItem).html("Boat");
							break;
						case 3:
							jQuery(listItem).addClass("domestic");
							jQuery(linkItem).html("Domestic");
							break;
						default:
							//add nothing
					}
					jQuery(linkItem).appendTo(listItem);
					jQuery(listItem).appendTo(elNavContainer);
				});

				arrNav = jQuery(elNavContainer).children('li');
				initNavigation();
			}

			/*
			* Set custom navigation
			*/
			function setCustomNavigation() {
				
				arrNav = jQuery('.'+options.navigationclass).find('li');
				initNavigation();

			}

			/*
			* Start navigation
			* Checks if navigation matches number of slides and adds behavior to navigation elements
			* otherwise, remove navigation
			*/
			function initNavigation() {

				if (arrSlides.length != arrNav.length) {
					arrNav.remove();
					return false;
				}

				arrNav.each(jQuery.proxy(function (index, item) {
					jQuery(item).click(jQuery.proxy(function () {
						if (index != intCurrent) showSlide(index);
					}, this));
				}, this));

				updateNavigation(intCurrent);
			}

			/*
			* Update navigation item
			* @param int index number of new slide
			*/
			function updateNavigation(intNewSlide) {
				intCurrent = intNewSlide;

				if (arrNav) {
					arrNav.removeClass('active');
					jQuery(arrNav[intCurrent]).addClass('active');
				}
			}

			/*
			* Show given slide with optional transition
			* @param mixed string or number - 'prev', 'next' or index number
			*/
			function showSlide(slide) {
				if (timer) clearTimeout(timer);
				intAnimSource = jQuery(elViewport).innerWidth();

				if (typeof (slide) === 'number') {
					intTargetSlide = parseInt(slide);
				} else {
					if (slide === 'next') {
						intTargetSlide = getNext();
					} else if (slide === 'prev') {
						intAnimSource = -intAnimSource;
						intTargetSlide = getPrev();
					}
				}

				jQuery(arrSlides[intTargetSlide]).stop();
				jQuery(arrSlides[intTargetSlide]).clearQueue();

				if (options.transition === 'slide') setSlideAnimation();
				if (options.transition === 'fade') setFadeAnimation();



			}

			/*
			* Get next slide number
			*/
			function getNext() {
				var intNext = 0;
				if (intCurrent < arrSlides.length - 1) {
					intNext = intCurrent + 1;
				}

				return intNext;
			}

			/*
			* Get prev slide number
			*/
			function getPrev() {
				var intPrev = arrSlides.length - 1;
				if (intCurrent > 0) {
					intPrev = intCurrent - 1;
				}
				return intPrev;
			}

			/*
			* Set pushbox slide animation
			*/
			function setSlideAnimation() {
				jQuery(arrSlides[intTargetSlide]).css('left', intAnimSource);
				jQuery(arrSlides[intTargetSlide]).css('z-index', 20);
				jQuery(arrSlides[intCurrent]).css('z-index', 15);

				jQuery(arrSlides[intTargetSlide]).animate({ 'left': 0 }, {
					'duration': duration,
					'complete': jQuery.proxy(function () {
						jQuery(arrSlides[intCurrent]).css({
							'z-index': 10
						});
						updateNavigation(intTargetSlide);
						if (!isPaused) queueSlide();
					}, this)
				});
			}

			/*
			* Set pushbox fade animation
			*/
			function setFadeAnimation() {
				jQuery(arrSlides[intTargetSlide]).css('opacity', 0);
				jQuery(arrSlides[intTargetSlide]).css('z-index', 20);
				jQuery(arrSlides[intCurrent]).css('z-index', 15);

				jQuery(arrSlides[intTargetSlide]).animate({ 'opacity': 1 }, {
					'duration': duration,
					'complete': jQuery.proxy(function () {
						jQuery(arrSlides[intCurrent]).css({
							'z-index': 10
						});
						updateNavigation(intTargetSlide);
						if (!isPaused) queueSlide();
					}, this)
				});
			}
		});
	};
})(jQuery);
