﻿var cycleseconds = 4;
var homecyc;
var homecycling;
var retoffset;

function homecycle() {
	var on = $('#top ul li.on');
	var next = (on.next().size() ? on.next() : on.siblings('li:first-child'));
	var ul = $(on.children('a').attr('href')).children('ul');
	if(ul.size() > 0) { 
		on = ul.children('li.on').next();
		if(on.size() > 0) next = on;
	}
	homecycling = true;
	homecyc = setTimeout("homecycle();",cycleseconds*1000);
	next.children('a').click();
}

$(function() {
	// Accordion
	$("#accordion").accordion({
		autoHeight: false,
		active: false, 
		alwaysOpen: false
	});
	
	// Tabs
	$("#tabs").tabs({ selected: 1 });
	
	$('#top ul li a').each(function(i) {
		var sib = 'div.tb';
		var ay = i;
		if($(this).parent('li[class^="nav"]').size() == 0) {
			sib = 'img';
			ay -= 3;
		}
		$(this).click(function() {
			if(!homecycling) clearTimeout(homecyc);
			$(this).parent().addClass('on').siblings('.on').removeClass('on');
			$(this).parents('ul').siblings(sib+':eq('+ay+')').fadeIn('fast').siblings(sib+':visible').fadeOut('fast');
			homecycling = false;
			return false;
		}).bind('tellme',function() {
			retoffset = ay;
		});
	});
	$('#top div.tb div.arrows a.p').click(function() {
		var on = $('#latest ul li.on');
		if(on.prev()) on.prev().children('a').click();
		else on.siblings('li:last-child').children('a').click();
		return false;
	}).next().click(function() {
		var on = $('#latest ul li.on');
		if(on.next()) on.next().children('a').click();
		else on.siblings('li:first-child').children('a').click();
		return false;
	});

	homecyc = setTimeout("homecycle();",cycleseconds*1000);
});