$(document).ready(function() {
	theCarousel = new Carousel();
	theCarousel.init();
	//Tip();
})


function Carousel() {
	this.s = { /* Selectors */
		carousel: "#dCarousel",
		controls: {
			prev: "#dCarouselPrev",
			next: "#dCarouselNext"
		},
		column: ".dcColumn",
		previewCol: ".dcColumnPreview",
		contentCol: ".dcColumnContent",
		activeCol: ".dcColumn.active",
		collapser: ".collapser",
		dcBlurb: ".dcBlurb",
		caseStudy: ".dcCaseStudy"
	};
	this.animation = {
		duration: {
			expand: 1000,
			collapse: 1000,
			disabledControls: 1100
		},
		easing: {
			expand: 'easeInQuint',
			expandDown: 'easeOutQuad',
			collapse: 'easeOutQuint',
			collapseUp: 'easeInOutQuad'
		}
	};
	this.width = {
		previewCol: 190,
		contentCol: 374
	};
	this.height = {};

	this.init = function() {
		this.width.dcBlurb = $(this.s.dcBlurb, this.s.carousel).width();
		$(this.s.column + ":last", this.s.carousel).addClass("active");
		this.doCarouselControls();
		this.activateActive();
		this.collapsersActivate();
		this.Gallery.init(this);
	};
	this.activateActive = function() {
		$(this.s.activeCol).find(this.s.previewCol).hide();
		$(this.s.activeCol).find(this.s.contentCol).show();
	};

	this.prev = function() {
		this.disableCarouselControls(this.animation.duration.disabledControls)
		easing = this.animation.easing;
		duration = this.animation.duration;
		width = this.width;

		$col = $(this.s.activeCol, this.s.carousel); //.closest(this.s.column);
		$prevCol = $col.prev();
		if ($prevCol.attr('id') == undefined || !$prevCol.hasClass(this.s.column)) {
			$prevCol = $col.closest(this.s.carousel).find(this.s.column + ":last");
		}

		$cc = $col.find(this.s.contentCol);
		$cc.animate({ width: "0" }, duration.collapse, easing.collapse);
		$cc.prev().animate({ width: width.previewCol + "px" }, duration.expand, easing.expand);
		$col.removeClass('active');

		$cc = $prevCol.find(this.s.contentCol);
		$cc.prev().animate({ width: "0" }, duration.collapse, easing.collapse);
		$cc.animate({ width: width.contentCol + "px" }, duration.expand, easing.expand);
		$prevCol.addClass('active');
	};

	this.next = function() {
		this.disableCarouselControls(this.animation.duration.disabledControls)
		easing = this.animation.easing;
		duration = this.animation.duration;
		width = this.width;

		$col = $(this.s.activeCol, this.s.carousel); //.closest(this.s.column);
		$nextCol = $col.next();
		if ($nextCol.attr('id') == undefined) {
			$nextCol = $col.closest(this.s.carousel).find(this.s.column + ":first");
		}
		$cc = $col.find(this.s.contentCol);
		$cc.animate({ width: "0" }, duration.collapse, easing.collapse);
		$cc.prev().animate({ width: width.previewCol + "px" }, 1000, 'swing');
		$col.removeClass('active');

		$cc = $nextCol.find(this.s.contentCol);
		$cc.prev().animate({ width: "0" }, duration.collapse, easing.collapse);
		$cc.animate({ width: width.contentCol + "px" }, duration.expand, easing.expand);
		$nextCol.addClass('active');

	};

	this.doCarouselControls = function() {
		t = this;
		$(this.s.controls.next + " a, " + this.s.controls.prev + " a").attr('href', 'javascript:;');
		$(this.s.controls.next).click(function() {
			t.next();
			return false;
		});
		$(this.s.controls.prev).click(function() {
			t.prev();
			return false;
		});
	};
	this.disableCarouselControls = function(duration) {
		//disable the controls
		$(this.s.controls.next, this.s.carousel).unbind('click');
		$(this.s.controls.prev, this.s.carousel).unbind('click');
		if (duration > 0) { //if we have a duration, reenable the controls after [duration]
			t = this;
			$.timer(duration, function() {
				t.doCarouselControls();
			});
		}
	};
	this.collapsersActivate = function() {
		$(this.s.caseStudy, this.s.carousel + " " + this.s.contentCol).each(function(k, v) {
			$(v).attr("initialHeight", $(v).innerHeight());
		});
		t = this;
		$(this.s.collapser, this.s.carousel).click(function() {
			var $t = $(this);
			var $caseStudy = $t.closest(t.s.contentCol).find(t.s.caseStudy);
			var cc = "collapsed";
			if ($caseStudy.is(":animated")) { return false; }
			if ($t.hasClass(cc)) {
				$caseStudy.animate({ height: $caseStudy.attr("initialHeight") + "px" }, t.animation.duration.expand, t.animation.easing.expandDown)
				.closest(t.s.dcBlurb).animate({ top: "26px" }, t.animation.duration.expand / 2, t.animation.easing.expandDown);
				$t.removeClass(cc)
			}
			else {
				$caseStudy.animate({ height: "0px" }, t.animation.duration.collapse, t.animation.easing.collapseUp)
				.closest(t.s.dcBlurb).animate({ top: "4px" }, t.animation.duration.expand / 2, t.animation.easing.expandDown);
				$t.addClass(cc);
			}
			return false;
		});
	};

	this.Gallery = {
		s: {
			mainImg: ".dcGalleryImage",
			thumbsContainer: ".dcGallery",
			thumbs: ".dcGallery li a"
		},
		init: function(parent) {
			this.parent = parent;
			theParent = this.parent;
			g = this;
			$(g.s.thumbs, theParent.s.carousel).click(function() {
				//$thumb = $(this);
				g.setImage(this);
				return false;
			});
		},
		setImage: function(clicker) {
			//get the details of the current image
			//current column:
			$clicker = $(clicker);
			$col = $clicker.closest(this.parent.s.column);
			$main = $(this.s.mainImg, "#" + $col.attr("id"));
			curBig = $main.find("img").attr("src");
			curSmall = $main.find("img").attr("longdesc");
			newBig = $clicker.attr("href");
			newSmall = $clicker.find("img").attr("src");
			$clicker.attr("href", curBig);
			$clicker.find("img").attr("src", curSmall);
			$main.find("img").attr("src", newBig);
			$main.find("img").attr("longdesc", newSmall);
		}



	};

}


function Tip() {
	$("#tip #hideTip a").click(function() {
		$(this).closest("#tip").slideUp(400);
		return false;
	});

}
