$(document).ready(
	function(){
		
		$('.menu li, .ul-select ul').hover(function(){
			$(this).addClass('hover');
		},function(){
			$(this).removeClass('hover');
		});
		
		// Inline Comments
		$('.inline-comments').each(
			function(i, el) {
				// Hide middle comments
				if ($('.comment', this).length > 3) {
					$('.comment:gt(0)', this).hide();
					$('.comment:last', this).show().before(
						'<a class="show-more-comments" href="#">'+ ($('.comment', this).length - 2) +' More Comments..</a>'
					);
				}
				$('.show-more-comments', this).click(
					function() {
						$('.comment', el).show('fast'); // I love closures!
						$(this).remove();
						return false;
					}
				);
			}
		);
		
		tnRibbon.init($('.view-features'));
		
		derbyUser.init();
	}
);

/* User/Commenting ----------------------------------------------------------*/
var derbyUser = {
	
	username: undefined,
	
	init: function() {
		derbyUser.username = derbyUser.getCookie('drupal_username');
		derbyUser.updateSplash();
		derbyUser.updateComments();
	},
	
	updateSplash: function() {
		if (derbyUser.username) {
			$('#block-block-17 .block-content').html('Welcome, '+ derbyUser.username +' | <a href="/2009/logout">Logout</a>');
		}
	},
	
	updateComments: function() {
		if (derbyUser.username) {
			$('#user-pre-comment').show();
			$('.user-no-comment-access').hide();
		}
		else {
			$('#user-pre-comment').hide();
		}
	},
	
	getCookie: function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	
	setCookie: function(name, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},
	
	delCookie: function(name) {
		createCookie(name,"",-1);
	}
	
}

/* Feature Ribbon Widget ----------------------------------------------------*/
var tnRibbon = {
	
	ribbons: undefined,
		
	init: function(el) {
		this.ribbons = el;
		if (this.ribbons.length < 1) return false; // Exit if no ribbons
		this.ribbons.each(this.initRibbon);
	},
	
	initRibbon: function(i, el){
		// Add DOM attributes
		el.curPos = 0;
		el.count = $('.feature', el).length;
		el.offset = [0];
		
		// Wrap in a container, store offsets for later animation/positioning
		var mHeight = 0;
		var offset = 0;
		$('.feature', el).each(
			function(i) {
				if ($(this).outerHeight() > mHeight){
					// Todo. This is off by several pixels in Safari
					mHeight = $(this).outerHeight();
				}
				offset = offset + $(this).outerWidth();
				el.offset[i + 1] = offset;
			}
		);
		$('.view-content', el).height(mHeight);
		$('.view-content', el).append('<div class="featureContainer"></div>');
		//$('.feature', el).wrapAll('<div class="featureContainer"></div>');
		$('.feature', el).appendTo('.featureContainer');
		
		el.fWidth = 0;
		$('.feature', el).each(function(){el.fWidth += $(this).outerWidth();});
		$('.featureContainer', el).width(el.fWidth);
		
		// Embed pagination links (if necessary) and set their handlers
		if (el.fWidth > $(this).outerWidth()) {
			$(el).append('<a href="#" class="goPrev">Prev</a> <a href="#" class="goNext">Next</a>');
			$('.goPrev', el).click(tnRibbon.goPrev);
			$('.goNext', el).click(tnRibbon.goNext);
		}
	},
	
	changePos: function(el, index){
		// Prevent scrolling beyond first item
		if (index < 0) index = 0;
		
		// Move container into position (if it exists)
		//console.log(index * $(el).eq(0).outerWidth()+' = '+ el.get(0).fWidth);
		if (index * $(el).eq(0).outerWidth() < el.get(0).fWidth) {
			el.get(0).curPos = index;
			var shift = -1 * index * $(el).eq(0).outerWidth();
			// Prevent showing empty edges
			if (shift * -1 > el.get(0).fWidth - 960)
			{
				shift = 960 - el.get(0).fWidth;
			}
			$('.featureContainer', el).animate(
				{
					left: Math.floor(shift)
				},
				'normal', 'swing',
				function(){$('.featureContainer').css('left', Math.floor(shift) +'px');}
			);
			//$('.featureContainer').css('left', Math.floor(shift) +'px');
		}
	},
	
	goNext: function(e){
		var parent = tnRibbon.ribbons;
		targetPos = parent.get(0).curPos + 1;
		tnRibbon.changePos(parent, targetPos);
		
		this.blur();
		return false;
	},
	
	goPrev: function(e){
		var parent = tnRibbon.ribbons;
		targetPos = parent.get(0).curPos - 1;
		tnRibbon.changePos(parent, targetPos);
		
		this.blur();
		return false;
	}
		
}