jQuery.extend(jQuery.easing, {
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158; var p=0; var a=c;
		if (t==0) return b;
		if ((t/=d)==1) return b+c;
		if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	}
});

var Finder = {
	
	chosen: [],
	possible: [],
	programs: [],
	matches: [],
	
	// Finder initialization
	init: function() {
		// intialize possible matrix values
		$('fieldset a').each(function() {
			Finder.possible.push(this.id);
		})
		// initialize actual matrix values
		$('#programs tr').each(function() {
			Finder.programs.push({slug: this.id, conditions: $(this).attr('class').split(' ')});
		})
		// setup buttons
		$('.option').click(function() {
			Finder.select($(this));
			return false;
		});
		// attach page links
		$('a[href^=#page]').click(function() {
			var page = this.href.slice(this.href.indexOf('#'));
			// video links
			if ($(this).attr('rel')) {
				var params = $(this).attr('rel').split(' ');
				var video = {file: params[0], width: parseInt(params[1]), height: parseInt(params[2])+20}
				Finder.page(page, {video: video, width: video.width+50, height: video.height+60});
				$('#videoContainer').css('height', video.height);
			}
			// all other links
			else {
				// close link
				if (this.id == 'close') {
					$('#page4 #video').empty();
				}
				// reset link
				else if (this.id == 'reset') {
					Finder.chosen = Finder.matches = [];
					$('.chosen, .disabled').removeClass('chosen disabled').css('opacity', 1);
					$('#programs tr').show();
				}
				Finder.page(page); 
			}
		});
		// go to first page
		Finder.page('#page1', {width: 400, duration: 1000, easing: 'easeOutElastic'});
	},
	
	// moves from page to page
	page: function(to, options) {
		if (!$('#content').hide().is(':empty')) {
			$('#content div:first').appendTo('#tmp');
		}
		$(to).appendTo('#content');
		options = $.extend({
			width: 480,
			height: $('#content').height(),
			duration: 300,
			easing: 'swing'
		}, options);
		if (!Finder.maxHeight && to == '#page2') {
			Finder.maxHeight = options.height;
		} else if (options.height > Finder.maxHeight) {
			options.height = Finder.maxHeight;
		}
		$('#page').animate({
			width: options.width, marginLeft: options.width / -2,
			height: options.height, marginTop: options.height / -2
		}, options.duration, options.easing, function() {
			if (to == '#page4') {
				swfobject.embedSWF('mediaplayer.swf', 'video', options.video.width, options.video.height, '8.0.0', false, {
					file: options.video.file, volume: 100, autostart: true, enablejs: true
				}, {allowfullscreen: true});
				$('#content').show();
			} else {
				$('#content').fadeIn();
			}
		});
	},
	
	// adds or removes option
	select: function(el) {
		if (!el.hasClass('chosen') && !el.hasClass('disabled')) {
			el.addClass('chosen');
			Finder.chosen.push(el.attr('id'));
		} else {
			el.removeClass('chosen');
			Finder.chosen = Finder.remove(el.attr('id'), Finder.chosen);
		}
		Finder.search();
	},
	
	// hides all programs that don't match all chosen conditions
	search: function() {
		Finder.matches = Finder.programs.slice();
		$.each(Finder.programs, function(i, program) {
			$.each(Finder.chosen, function(j, condition) {
				if ($.inArray(condition, program.conditions) == -1) {
					//Finder.matches = Finder.remove(program, Finder.matches);
					delete Finder.matches[$.inArray(program, Finder.matches)];
				}
				$('#'+program.slug).hide();
			});
		});
		$.each(Finder.matches, function(i, program) {
			if (program) $('#'+program.slug).show();
		});
		Finder.relevance();
	},
	
	// toggles options based on matrix availability
	relevance: function() {
		var toDisable = Finder.possible.slice();
		$.each(Finder.matches, function(i, program) {
			$.each(Finder.possible, function(j, condition) {
				if (program && $.inArray(condition, program.conditions) != -1) {
					$('#'+condition).removeClass('disabled').css('opacity', 1);
					//toDisable = Finder.remove(condition, toDisable);
					delete toDisable[$.inArray(condition, toDisable)];
				}
			});
		});
		$.each(toDisable, function(i, condition) {
			$('#'+condition).addClass('disabled').css('opacity', .7);
		});
	},
	
	// returns array with item removed
	remove: function(item, array) {
		for (var i = array.length; i--; i) {
			if (array[i] === item) {
				array.splice(i, 1);
			}
		}
		return array;
	}
  
};

$(document).ready(Finder.init);