// Hides the definition titles and prepends the definition descriptions instead
function inlineDefinitions(elm) {
	$("dt", elm).each(function(){
		var self = $(this);
		var title = self.text();
		self.addClass("structural").next().prepend("<strong>"+title+":</strong> ");
	})
}

// Insert navigation buttons for the jCarousel Lite when there are more than a certain number of products
function addCarousel(max, next, prev) {
	var sMaxProducts = max ? max : 6;
	var oProductList = $("#product-list .jcarousel");
	var sNoOfProducts = $("ul", oProductList).children().length; /* number of products in list */
	var sSelProductId = $("li.sel", oProductList).attr("id"); /* id of selected product */
	var sSelProduct = $("li", oProductList).index( $("#"+sSelProductId) ); /* number in list of selected product */
	var startNo = sSelProduct;
	if(sNoOfProducts <= 6) { startNo = 0; }
	var sNext = next ? next : "Next";
	var sPrev = prev ? prev : "Previous";
	if(sNoOfProducts > sMaxProducts) {
		if(sSelProduct >= (sMaxProducts - 1)) {
			var sOverflow = sNoOfProducts - sMaxProducts; /* number of product off-list */
			sSelProduct -= sOverflow;
		}
		$("#product-list .jcarousel").before('<button class="jcarousel-prev" title="'+sPrev+'">'+sPrev+'</button>');
		$("#product-list .jcarousel").after('<button class="jcarousel-next" title="'+sNext+'">'+sNext+'</button>');
	}
	$("#product-list .jcarousel").jCarouselLite({
		start: startNo,
		visible: sMaxProducts,
		scroll: 1,
		animation: "fast",
		speed: 200,
		circular: false,
		btnNext: ".jcarousel-next",
		btnPrev: ".jcarousel-prev"
    });
}

function enableHighslide(elm, type) {
	var ajax = type ? type : null;
	elm.click(function(){
		hs.graphicsDir = '/i/highslide/';
		hs.showCredits = false;
		hs.showResize = false;
		hs.allowMultipleInstances = false;
		if(ajax){
			hs.Expander.prototype.onFocus = function(sender){
				// set focus on the close link in the last list item of the navigation list
				sender.innerContent.getElementsByTagName("UL")[0].lastChild.getElementsByTagName("A")[0].focus();
			}
			hs.outlineType = null;
			hs.outlineWhileAnimating = false;
			hs.align = 'center';
			hs.dimmingOpacity = 0.6;
			hs.width = 380;
			hs.minHeight = 240;
			return hs.htmlExpand(this, {objectType: ajax});
		} else {
			hs.Expander.prototype.onFocus = null;
			hs.outlineType = null;
			hs.outlineWhileAnimating = true;
			hs.dimmingOpacity = null;
			hs.useBox = false;
			return hs.expand(this);
		}
	});
}

$(document).ready(function() {
	var homeText = $('#nav-home a').text();
	var loginText = $('#nav-login a').text();
	$('#nav-home.sel a').html('<img src="/i/home_sel.png" alt="'+homeText+'" \/>');
	$('#nav-home:not(".sel") a').html('<img src="/i/home.png" alt="'+homeText+'" \/>').hover(function(){
		$(this).find('img').attr('src', '/i/home_sel.png');
	},function() {
		$(this).find('img').attr('src', '/i/home.png');
	});
	$('#nav-login.sel a').html('<img src="/i/key_sel.png" alt="'+loginText+'" \/>');
	$('#nav-login:not(".sel") a').html('<img src="/i/key.png" alt="'+loginText+'" \/>').hover(function(){
		$(this).find('img').attr('src', '/i/key_sel.png');
	},function() {
		$(this).find('img').attr('src', '/i/key.png');
	}).attr('href',function(){return this.href + '#content'});
	// Login trigger
	enableHighslide($('#nav-login a'), 'ajax');
	// Product page
	$('.product-features').addClass('inline').css('cursor','pointer').click(function() {
		$(this).toggleClass('inline');
	});
	$('tr:even').addClass('even');
});