$(function(){
	$(".film").each(function(){
		new Film(this)
	})
});


function Film (a) {
	this.viewport = $(a);
	this.navItems = this.viewport.find('.navigation li');
	this.reel = this.viewport.find('.reel');
	this.frames = this.reel.find('.frame');
	this.expandAllLink = this.viewport.find('.expandAll');
	this.collapseAllLink = this.viewport.find('.collapseAll');
	this.tween = null;
	this.selectedIndex = 0;
	this.previous = this.viewport.find('.previous');
	this.next = this.viewport.find('.next');
	this.prepare();
}
Film.prototype = {
	prepare: function () {
		var c = this;
		this.navItems.each(function(b){
			$(this).click(function(){
				this.blur();
				return c.activate(b)
			});
			$(this).keydown(function(a){
				this.blur();
				return a.keyCode != 13 || c.activate(b)
			})
		});
		this.previous.click(function(){
			this.blur();
			return c.activate(c.selectedIndex - 1);
		});
		this.previous.keydown(function(a){
			this.blur();
			return a.keyCode != 13 || c.activate(c.selectedIndex -1);
		});
		this.next.click(function(){
			this.blur();
			return c.activate(c.selectedIndex + 1);
		});
		this.next.keydown(function(a){
			this.blur();
			return a.keyCode!=13||c.activate(c.selectedIndex+1);
		});
		this.expandAllLink.click(function(){
			this.blur();
			return c.toggleAll();
		});
		this.collapseAllLink.click(function(){
			this.blur();
			return c.toggleAll();
		});
		this.previous.hide();
		return this;
	},
	num: function (a, b) {
		return parseInt($.css(a.jquery ? a[0] : a, b), 10) || 0;
	},
	activate: function (a) { 
		if(a >= 0 && a < this.frames.length && a != this.selectedIndex) {
			var b = this;
			this.navItems.eq(this.selectedIndex).removeClass('selected');
			this.navItems.eq(a).addClass('selected');
			this.checkNav(a);
			this.moveTo(a, null);
			this.selectedIndex = a;
		}
		return this;
	},
	moveTo: function (a, b) {
		/*this.reel.queue("fx", []);
		this.reel.stop()
		var c = -100 * a;
		var obj = this.reel;
		obj.animate({left: c + '%'}, function() {
			obj.css('left', c + '%');
		});*/
		if (this.tween) {
			this.tween.stop()
		}
		var c = -100 * a;
		this.tween = new Tween(this.reel, 'left', EEQ.Quartic.easeInOut, this.num(this.reel, 'left'), c, 30, '%');
		this.tween.onMotionFinished = function(){ b() };
		return this;
	},
	toggleAll: function () {
		var a = this;
		var b = -100 * this.selectedIndex;
		var c = 'expanded';
		if (a.reel.hasClass(c)) {
			a.expandAllLink.css('opacity', 1);
			a.collapseAllLink.css('opacity', 0.5);
			a.reel.slideUp(200, function(){
				$(this).css('left', b + '%').removeClass(c).fadeIn(300);
				a.showNav();
				a.collapseAllLink.hide();
				a.expandAllLink.show();
			});
		} else {
			a.expandAllLink.css('opacity', 0.5);
			a.collapseAllLink.css('opacity', 1);
			a.reel.fadeOut(300, function() {
				$(this).css('left', 0).addClass(c).slideDown(200);
				a.expandAllLink.hide();
				a.collapseAllLink.show();
			});
			a.hideNav();
		}
		return false;
	},
	hideNav: function() {
		this.navItems.parent().hide();
		this.next.hide();
		this.previous.hide();
	},
	showNav: function() {
		this.navItems.parent().show();
		this.next.show();
		this.previous.show();
		this.checkNav(this.selectedIndex);
	},
	checkNav: function (a) {
		if (a > 0) {
			this.previous.show();
		} else {
			this.previous.hide();
		}
		if (a == this.frames.length - 1){
			this.next.hide();
		} else {
			this.next.show();
		}
	}
};