/**
 * SlideShow
 *
 * @author Boz
 * @classDescription A class based slide show UI component
 * @css
 *      .navitem - navitems
 *      #nav00 - convention for mapping navitems to slides
 *      .slide - slides
 *      #slide00 - convetion for mapping slides to navitems
 *      .current - denotes the currently visible slide
 *      .pre - elements that invoke getPrev()
 *      .nxt - elements that invoke getNext()
 *
 * Assumptions:
 *  - There will be the same # of navitmes as there are slides
 *
 **/

mdp.app.SlideShow = function(slideShowId, options) {
	/* ---[ CLASS VARIABLES ]--- */
	var self = this;
	this.slideshow;
	this.length;
	this.index = 1;

	/* ---[ CONSTRUCTOR ]--- */
	function init() {

		/* initialization code */
		setupEventListeners();

		/* display slideshow length */
		$(".lengthDisplay").html(self.length);
	}

	/* ---[ PUBLIC METHODS ]--- */
	this.getSlide = function(index) {
		/* hide current slide */
		var lastSlide = self.getCurrent().removeClass("current").addClass("hide");

		/* show slide by index */
		var current = $("#slide" + index).addClass("current");
		if (!$.browser.msie || ($.browser.msie && $.browser.version == '6.0' && $.browser.version != '7.0')) {
			lastSlide.css('opacity', 0);
			var fx = current.animate({"opacity":1}, self.getDuration(), function() {
				current.removeClass("hide");
			});
		}
		else {
			current.removeClass("hide");
		}

		/* update navigation */
		if (typeof(options) != 'undefined' && typeof(options.onUpdateNav) != 'undefined') {
			options.onUpdateNav(index);
		} else {
			self.updateNavigation(index);
		}

		/* sendPageEvent */
		if (typeof(options) != 'undefined' && options != null) {
			if (typeof(options.onReport) == 'undefined') {
				self.report(index, options);
			} else {
				options.onReport(index, options);
			}
		}

		/* check the state of the slideshow */
		if (typeof(options) == 'undefined' || options != null) {

			if (index == 1) {
				(typeof(options.onPrevDisable) == 'undefined') ? null : options.onPrevDisable();
				(typeof(options.onNextEnable) == 'undefined') ? null : options.onNextEnable();
			}

			else if (index == self.length) {
				(typeof(options.onPrevEnable) == 'undefined') ? null : options.onPrevEnable();
				(typeof(options.onNextDisable) == 'undefined') ? null : options.onNextDisable();
			}

			else {
				(typeof(options.onPrevEnable) == 'undefined') ? null : options.onPrevEnable();
				(typeof(options.onNextEnable) == 'undefined') ? null : options.onNextEnable();
			}

		}

		/* display index */
		self.index = index;
		$(".indexDisplay").html(index.replace(/[^0-9]*0?/, ""));
	};

	this.getNext = function() {
		/* get the current index */
		var current = self.getCurrent();
		var index = current.attr('id').replace(/[^0-9]/g, "");
		if (index < self.length) {
			index = parseInt(index, 10) + 1;
			if (index < 10) {
				index = "0" + index;
			}
		}

		/* update navigation */
		if (typeof(options) != 'undefined' && typeof(options.onUpdateNav) != 'undefined') {
			options.onUpdateNav(index);
		} else {
			self.updateNavigation(index);
		}

		/* show the slide */
		if ($("#slide" + index).length > 0) {
			/* hide current slide */
			var lastSlide = current.removeClass("current").addClass("hide");

			/* show next slide */
			current = $("#slide" + index).addClass("current");
			if (!$.browser.msie || ($.browser.msie && $.browser.version == '6.0' && $.browser.version != '7.0')) {
				lastSlide.css('opacity', 0);
				current.animate({"opacity":1}, self.getDuration(), function() {
					current.removeClass("hide");
				});
			}
			else {
				current.removeClass("hide");
			}

			/* sendPageEvent */
			if (typeof(options) != 'undefined' && options != null) {
				if (typeof(options.onReport) == 'undefined') {
					self.report(index, options);
				} else {
					options.onReport(index, options);
				}
			}

			/* check the state of the slideshow */
			index = parseInt(index, 10);
			if (typeof(options) == 'undefined' || options != null) {

				if (index == 2) {
					(typeof(options.onPrevEnable) == 'undefined') ? null : options.onPrevEnable();
				}

				if (index == self.length) {
					(typeof(options.onNextDisable) == 'undefined') ? null : options.onNextDisable();
				}

				(typeof(options.onPreload) == 'undefined') ? null : options.onPreload();

				(typeof(options.getTargeter) == 'undefined') ? null : options.getTargeter();
			}

			/* display index */
			self.index = index;
			$(".indexDisplay").html(parseInt(self.index));
		}
	};

	this.getPrev = function() {
		/* get current index */
		var current = self.getCurrent();
		var index = current.attr('id').replace(/[^0-9]/g, "");
		if (index > 1) {
			index = parseInt(index, 10) - 1;
			if (index < 10) {
				index = "0" + index;
			}
		}

		/* update navigation */
		if (typeof(options) != 'undefined' && typeof(options.onUpdateNav) != 'undefined') {
			options.onUpdateNav(index);
		} else {
			self.updateNavigation(index);
		}

		/* show the slide */
		if ($("#slide" + index).length > 0) {
			/* hide current slide */
			var lastSlide = current.removeClass("current").addClass("hide");

			/* show previous slide */
			current = $("#slide" + index).addClass("current");

			if (!$.browser.msie || ($.browser.msie && $.browser.version == '6.0' && $.browser.version != '7.0')) {
				lastSlide.css('opacity', 0);
				current.animate({"opacity":1}, self.getDuration(), function() {
					current.removeClass("hide");
				});
			}
			else {
				current.removeClass("hide");
			}

			/* sendPageEvent */
			if (typeof(options) != 'undefined' && options != null) {
				if (typeof(options.onReport) == 'undefined') {
					self.report(index, options);
				} else {
					options.onReport(index, options);
				}
			}

			/* check the state of the slideshow */
			index = parseInt(index, 10);
			if (typeof(options) == 'undefined' || options != null) {

				if (index == 1) {
					(typeof(options.onPrevDisable) == 'undefined') ? null : options.onPrevDisable();
				}

				if (index == self.length - 1) {
					(typeof(options.onNextEnable) == 'undefined') ? null : options.onNextEnable();
				}

				(typeof(options.getTargeter) == 'undefined') ? null : options.getTargeter();
			}

			/* display index */
			self.index = index;
			$(".indexDisplay").html(parseInt(self.index));
		}
	};

	this.updateNavigation = function(index) {
		var current = $("#" + slideShowId).find(".selected:first");
		if (current.length > 0) {
			current.removeClass("selected");
			var navitem = $("#nav" + index).addClass("selected");
		}
	};

	this.getCurrent = function() {
		return $("#" + slideShowId).find(".current:first");
	};

	this.findParent = function(el, _class) {
		if (el.hasClass(_class)) {
			return el;
		}
		else {
			return findParent(el.parent(), _class);
		}
	};

	this.getDuration = function() {
		if (typeof(options) == 'undefined' || options == null) {
			return 300;
		}
		else {
			return (typeof(options.duration) == 'undefined') ? 300 : options.duration;
		}
	};

	this.report = function(index, options) {

		if (typeof(options) != "undefined" && !options.disableAdRefresh) {

			if (typeof(s) == "object") {
				var pagename = "";
				var pagenamevals = s.pageName.split(":");

				/* insert new slide index */
				pagenamevals[2] = "Slide" + parseInt(index, 10);

				/* rebuild page name string */
				for (var i = 0; i < pagenamevals.length; i++) {
					pagename += pagenamevals[i];
					if (i != pagenamevals.length - 1) {
						pagename += ":";
					}
				}

				/* set omniture values */
				s.eVar9 = pagename;
				if (typeof(sendPageEvent) == 'function') {
					sendPageEvent('', pagename);
				}
			}

		}

	};

	/* ---[ EVENT LISTENERS ]--- */
	function setupEventListeners() {

		/* find the slideshow */
		this.slideshow = $("#" + slideShowId);

		/* find pre within the slideshow */
		$(this.slideshow).find(".pre").each(function(i, pre) {

			/* attach getPrev() to pre click events */
			$(pre).click(self.getPrev);

		});

		/* find nxt within the slideshow */
		$(this.slideshow).find(".nxt").each(function(i, nxt) {
			/* attach getNext() to nxt click events */
			$(nxt).click(self.getNext);

		});

		/* find all nav items within slideshow */
		$(this.slideshow).find(".navitem").each(function(i, nav) {

			/* attach getSlide() to nav item click events */
			$(nav).click(function() {
				var index = this.id.replace(/[^0-9]/g, "");
				self.getSlide(index);
				$("#slidenav").removeClass("hide");
			});

		});

		/* get slideshow length */
		var slides = $(this.slideshow).find(".slide");
		self.length = slides.length;

		/* set opacity for smooth animation */
		if (!$.browser.msie || ($.browser.msie && $.browser.version == '6.0' && $.browser.version != '7.0')) {
			slides.each(function(index, slide) {
				if (index != 0) {
					$(slide).css('opacity', 0);
				}
			});
		}
	}

	/* ---[ RUN ]--- */
	init();
};

