(function () {
	function TwitterFeed () {

		this.init = function () {
			// check for necessary elements
			if (!$("#footer > .content .feas .twitter").length) {
				return false;
			}

			// bind events
			$("#footer > .content .feas .twitter .paging li a").click(function() {
				if ($(this).parents("li").hasClass("prev")) {
					alert("Previous");
				} else if ($(this).parents("li").hasClass("next")) {
					alert("Next");
				}
				
				return false;
			});
		};
		
		this.init();
	}
	new TwitterFeed();
	
	function PromoPlayer () {
		this.els = {};
		this.els.player = "#promo";
		this.els.slides = "#promo .slides > li";
		this.els.nav = "#promo .paging";
		this.els.tabs = "#promo .paging > li";

//		this.photos = [];

		this.currentSlide = 0;
		this.numSlides = $(this.els.slides).length;

		this.animation = {};
		this.animation.duration = 500;
		this.animation.interval = 18000;

		this.timer = false;

		this.init = function () {
			if (!$(this.els.player).length) {
				return false;
			}

			// prepare the slides for animation
			this.arrangeSlides();
			// post-load the photos in the slides
//			this.loadPhotos();

			// bind events
			var self = this;
			$(this.els.player + " .paging li").each(function (id) {
				$(this).children("a").mousedown(function () {
					self.changeSlides(id);
					return false;
				});
			});
			$(this.els.player + " .paging li a").click(function () {
				return false;
			});

			// Set a timer to automatically change slides
			// use window.setTimeout instead of window.setInterval - timer will have to be
			// reset during animation in case of manual invocation of slide changes anyway.
			this.timer = window.setTimeout(function () {
				self.advanceSlides();
			}, this.animation.interval);
		};

		this.arrangeSlides = function () {
			// put the first slide in the appropriate position
			$(this.els.slides).eq(0).addClass("active").css({
				top : 38,
				left : 0
			});

			// place all other slides just off the bottom of the container
			$(this.els.slides).not(":eq(0)").removeClass("active").show().css({
				top : 402			// height of viewer (364) + height of overlap with nav (38)
			});
		};
/*
		this.loadPhotos = function () {
			// collect attributes of all the images
			var self = this;
			$(this.els.player).find(".slides li").each(function () {
				self.photos.push({
					attributes : {
						src : $(this).find("[name=\"src\"]").val(),
						alt : $(this).find("[name=\"alt\"]").val(),
						width : $(this).find("[name=\"width\"]").val(),
						height : $(this).find("[name=\"height\"]").val()
					}
				});
			});

			// start pre-loading all the images
			for (i in this.photos) {
				this.photos[i].image = new Image();
				this.photos[i].image.id = i;

				this.photos[i].image.onload = function () {
					var attributes = self.photos[this.id].attributes;
					$("#promo .slides li:eq(" + this.id + ") .photo").html("<img src=\"" + attributes.src + "\" alt=\"" + attributes.alt + "\" width=\"" + attributes.width + "\" height=\"" + attributes.height + "\"/>");
				};
				this.photos[i].image.src = this.photos[i].attributes.src;
			}
		};
*/

		this.changeSlides = function (id) {
			// if slide specified is the slide currently being viewed, do nothing
			if (id == this.currentSlide) {
				return;
			}

			// stop any current animation on any slides and clear the animation queue
			$(this.els.slides).stop(true, true);
			// The slide down animation has a callback to slide up the next slide.
			// .stop() ends the slide down animation and triggers the callback.
			// Call stop again to stop this secondary animation.
			$(this.els.slides).stop(true, true);

			// destroy timer in case this change was manually invocated
			// Timer will be reset when animation is complete
			window.clearTimeout(this.timer);

			// set this slide as the current slide
			this.currentSlide = id;

			// move current slide out of view
			var self = this;
			$(this.els.slides + ".active").animate({
				top : 402
			}, this.animation.duration, function () {
				// remove the active flag from the slide
				$(this).removeClass("active");

				// change which navigation tab is highlighted
				$(self.els.tabs).eq(id).siblings().removeClass("active");
				$(self.els.tabs).eq(id).addClass("active");

				// move new slide into view andgive it the active flag.
				$(self.els.slides).eq(id).addClass("active").animate({
					top : 38
				}, self.animation.duration, function () {
					// reset/recreate timer
					self.timer = window.setTimeout(function () {
						self.advanceSlides();
					}, self.animation.interval);
				});
			});
		};

		this.advanceSlides = function () {
			// identify next slide
			var nextSlide = this.currentSlide + 1;
			if (nextSlide >= this.numSlides) {
				nextSlide = 0;
			}

			this.changeSlides(nextSlide);
		};

		// put things in motion
		this.init();
	}
	new PromoPlayer();

}());
