$(function () {
	ajaxPing();
});

// Make checkout fieldsets equal heights
if ($('fieldset.delivery').height() < $('fieldset.details').height()) {
	$('fieldset.delivery').height($('fieldset.details').height());
} else {
	$('fieldset.details').height($('fieldset.delivery').height());
}
if ($('fieldset.payment').height() < $('fieldset.checkout').height()) {
	$('fieldset.payment').height($('fieldset.checkout').height());
} else {
	$('fieldset.checkout').height($('fieldset.payment').height());
}

// Category Navigation
var container = $('<div class="sub-categories"/>');
$('#sidebar ul.categories ul').each(function (i) {
	var parent = $(this).closest('li');
	if ($(this).outerHeight() > container.height()) container.height($(this).outerHeight());
	var list = $(this).addClass('sub-category').detach();
	
	if ((!parent.siblings().is('.current') && i == 0) || parent.is('.current')) {
		parent.addClass('opened');
	} else {
		list.hide()
	}
	
	//if (parent.not('.current')) ;
	parent.closest('ul').after(container);
	container.append(list);
	
	$('a', parent).first().click(function () {
		$('ul', container).not(list).fadeOut(function () {
			list.fadeIn();
			$('#sidebar ul.categories li').removeClass('opened');
			parent.addClass('opened');
		}); 
		
		return false;
	});
});



$('#footer input[name=e-a]').fieldhint('enter your email address here');


// Slideshow Gallery
$('ul.gallery-slideshow').each(function() {
	$(this).slideshow({
		sleep: $(this).data('sleep') || 5,
		fade: $(this).data('fade') || 2
	});
});


// Homepage Feeds
$('div.home-feed li').each(function() {
	var link = $('a.link', this);
	link.hide();
	$(this).hover(function() {
		$(this).toggleClass('hover');
	}).click(function() {
		document.location.href = link.attr('href');
	});
});


// Category Auto-pagination
$('#content ul.products').autoload({
	paginator: 'div.paginator'
});

// News Auto-pagination
$('#content:has(div.news)').autoload({
	nextContent: 'div.news div.headline',
	appendTo: 'div.news'
});



var ajaxPing = function () {
	setTimeout("$.post(document.location.href, { ajax_ping : '1' }, ajaxPing)", 10 * 60 * 1000);
}

