
$(document).ready(function(){

	var htsEvenedOut = {};

		// TABS
	$('ul.video-tabs li').click(function(){
		var rel = $(this).attr('rel');
		$('ul.video-tabs li').removeClass('active');
		$(this).addClass('active');
		$('.video-category-wrap .video-category').removeClass('active');
		$('.video-category-wrap .video-category-'+rel).addClass('active');

		if (typeof htsEvenedOut[rel] == 'undefined') {
			htsEvenedOut[rel] = true;
			// even out heights
			var maxVidHt = 0;
			$('.video-category-wrap .video-category-'+rel+' .video').each(function(){
				maxVidHt = Math.max(maxVidHt, $(this).height());
			});
			$('.video-category-wrap .video-category-'+rel+' .video').each(function(){
				$(this).height(maxVidHt+'px');
			});
		}

		return false;
	});

	var catIndex = 0;
	$('.video-category-wrap .video-category').each(function(){
		var self = this; // sustainable reference
		var index = 0;
		var count = $('div.video', this).length;
		var activePage = 0;

		var max = count - 6; // the furthest index we allow scrolling to

		var go = function(i){
			index = i;
				// all but the first video need to scroll an offset of 9px
			var offset = (i===0) ? 0 : 9;

			$('.video-category-wrap-inner', self)
				.scrollTo( $('div.video:eq('+i+')', self),
				    {duration:700, axis:"x", offset:{left:offset}}
				);

			var pg = Math.floor(i/6);
			if (pg == (pages-1) && i > (max-1)) {
				pg = pages;
			}

			activePage = pg;
			$('.pagination-inner a', self).removeClass('active');
			$('.pagination-inner a:eq('+pg+')', self).addClass('active');
		};

			// on page at a time
		var next = function(){
			var pg;

			if (activePage < pages) {
				pg = activePage + 1;
			} else {
				pg = 0;
			}

			$('.pagination-inner a:eq('+pg+')', self).click();
			return false;
		};

		var previous = function(){
			var pg;

			if (activePage == 0) {
				pg = pages;
			} else {
				pg = activePage - 1;
			}

			$('.pagination-inner a:eq('+pg+')', self).click();
			return false;
		};

		/* // One video at a time
		var next = function(){
			if (index >= max) {
				index = 0;
			} else {
				index++;
			}
			go(index);
			return false;
		};

		var previous = function(){
			if (index == 0) {
				index = max;
			} else {
				index--;
			}
			go(index);
			return false;
		};*/

			// add pagination bubbles
		var pages = (count % 6) ? Math.floor(count/6) : Math.floor((count/6)-1);
		if (pages > 0) {
			for (var i=0; i<pages; i++) {
				$('.pagination-inner', this).append('<a rel="'+(i+2)+'" href="#pg'+(i+2)+'">'+(i+2)+'</a>');
			}
		}

			// pagination events
		$('.pagination-inner a', this).click(function(){
			var i = ($(this).attr('rel')-1)*6;
			if (i > max) {
				i = max;
			}

			go(i);
			return false;
		});

			// prev, next navigation
		$('a.previous', this).click(previous);
		$('a.next', this).click(next);

			// allow scrolling with mousewheel
		/*$(this).mousewheel(function(event, delta){
			if (delta > 0) { // up
				previous();
			} else { // down
				next();
			}
			return false;
		});*/


		// even out heights
		/*var maxVidHt = 0;
		htsEvenedOut[ $('.video-category-wrap .video-category.active').attr('rel') ] = true;
		$('.video-category-wrap .video-category.active .video').each(function(){
			maxVidHt = Math.max(maxVidHt, $(this).height());
		});
		$('.video-category-wrap .video-category.active .video').each(function(){
			$(this).height(maxVidHt+'px');
		});*/

		catIndex++;
	});

});
