var in_cms = false;
$(document).ready(function()
{
	if ($('meta[name = "in-cms"]').size() > 0)
	{
		in_cms = true;
		$('.hide-img').show();
		$('#main-img').hide();
	}

	$('.new-window').click(function(){return !window.open($(this).attr('href'));});
	$('form').validate();
	theRotator();

	$('.subsubnav').each(function(){if ($(this).hasClass('open')) return;$(this).hide();});
	$('.subsubnav').each(function(){$('.item', $(this)).last().addClass('item-last');});
	$('#subnav .main').click(function()
	{
		if (!$('.subsubnav', $(this).parent()).hasClass('open'))
		{
			$('#subnav .open').slideUp('fast').removeClass('open');
			$('.subsubnav', $(this).parent()).slideDown('fast').addClass('open');
		}
		else
		{
			$('#subnav .open').slideUp('fast').removeClass('open');
		}
		return false;
	});
	$('#subnav .item').click(function()
	{
		if ($(this).hasClass('main') || in_cms)
			return;
		var $a = $('a', $(this));
		if ($a != undefined)
		{
			if ($a.hasClass('new-window'))
				window.open($a.attr('href'));
			else
				window.location = $a.attr('href');
			return false;
		}
	});

	var img_counter = 1;
	$('#images .img').each(function(){if (img_counter == 6){img_counter = 0;$(this).addClass('img-last');}img_counter++;});

	var img_src = $('#images .img:first .hide-img img').attr('src');
	$('#main-img img').attr('src',img_src);
	$('#images .img').click(function()
	{
		var img_src = $('.hide-img img', $(this)).attr('src');
		$('#main-img img').hide().attr('src',img_src).fadeIn();
	});
});

function theRotator ()
{
	$('ul#slideshow li').css({opacity: 0.0});
	$('ul#slideshow li:first').css({opacity: 1.0});
	setInterval('rotate()',4000);
}

function rotate ()
{
	var current = ((typeof $('ul#slideshow li.show').attr('class') != 'undefined')?  $('ul#slideshow li.show') : $('ul#slideshow li:first'));
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('ul#slideshow li:first') :current.next()) : $('ul#slideshow li:first'));
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 2500);
	current.animate({opacity: 0.0}, 2500).removeClass('show');
};

