$(function() {

	$('html').addClass('js');
	
	$('#cboxOverlay').appendTo('#aspnetForm');
	$('#colorbox').appendTo('#aspnetForm');

	if ($('#main').length > 0) {
		$('body').css('overflow', 'scroll');
	}
	
	var slideTimeout = null;
	
	$(window).resize(function () {
		
		$('#slide-wrap .content').css({'min-height': '0px'});
		
		clearTimeout(slideTimeout);
		
		function slide() {
			clearTimeout(slideTimeout);
			if($('body').hasClass('animating')) { } else {
				$('body').addClass('animating');
				$('#carousel #rotate').animate({'left': -ox }, 'slow', function(){
					var theLI = $('#carousel #rotate li:first').html();
					$('#carousel #rotate').append('<li style="width: ' + ox + 'px; height: ' + oy + 'px;">' + theLI + '</li>');
					$('#carousel #rotate li:first').remove();
					$('#carousel #rotate').css({'left': '0'});
					$('body').removeClass('animating');
				});
			}
			slideTimeout = setTimeout(function () {
				slide();
			}, 8000);
		};
		
		function resetSlide() {
			clearTimeout(slideTimeout);
			slideTimeout = setTimeout(function () {
				slide();
			}, 8000);
		}
		
		var ox = $(document).width() - $('header').width() + 10;
		if($('#slide-panel').length > 0) {
			if($('#slide-panel').height() > $(window).height() ) {
				var oy = $('#slide-panel').height();
				$('body').css('overflow', 'auto');
				$('#slide-panel').css('min-height', '0px');
				$('#slide-panel').css('position', 'absolute');
			} else {
				var oy = $(window).height();
			}
		} else {
			if($('body').hasClass('home')) {
				if($(window).height() > 600) {
					var oy = $(window).height();
				} else {
					$('body').css({'overflow': 'auto'});
					$('body').addClass('small');
					var oy = 600;
				}
				if($(window).width() < 952) {
					$('body').css({'overflow': 'auto'});
					$('body').addClass('small');
					var ox = 952;
				}
			} else {
				var oy = $(window).height();
			}
		}
		$(".rss").remove();
		$('#main #rotate img').each(function () {
			var self = this;
			var img = new Image();
			img.onload = function () {
				var cx = this.width;
				var cy = this.height;

				$(self).parent().css({
					'width': ox,
					'height': oy
				});

				var noItems = $(self).parent().parent().find('li').length;
				$(self).parent().parent().css({
					'width': ox * noItems,
					'height': oy
				});

				$(self).parent().parent().parent().css({
					'width': ox,
					'height': oy
				});

				if (ox / oy > cx / cy) {
					$(self).css({
						'width': ox,
						'height': (ox * cy / cx),
						'left': 0,
						'top': 0.5 * (oy - (ox * cy / cx)),
						'position': 'relative'
					});

				} else {
					$(self).css({
						'width': oy * cx / cy,
						'height': oy,
						'top': 0,
						'left': 0.5 * (ox - (oy * cx / cy)),
						'position': 'relative'
					});
				}
			}
			img.src = $(self).attr('src');
		});
		
		$('#carousel-nav li.next').die('click').live('click', function() {
			if($('body').hasClass('animating')) { } else {
				$('body').addClass('animating');
				$('#carousel #rotate').animate({'left': -ox}, 'slow', function(){
					var theLI = $('#carousel #rotate li:first').html();
					$('#carousel #rotate').append('<li style="width: ' + ox + 'px; height: ' + oy + 'px;">' + theLI + '</li>');
					$('#carousel #rotate li:first').remove();
					$('#carousel #rotate').css({'left': '0'});
					$('body').removeClass('animating');
				});
			}
			resetSlide();
			return false;
		});
		
		$('#carousel-nav li.prev').die('click').live('click', function() {
			if($('body').hasClass('animating')) { } else {
				$('body').addClass('animating');
				var theLI = $('#carousel #rotate li:last').html();
				$('#carousel #rotate').prepend('<li style="width: ' + ox + 'px; height: ' + oy + 'px;">' + theLI + '</li>');
				$('#carousel #rotate li:last').remove();
				$('#carousel #rotate').css({'left': -ox});
				$('#carousel #rotate').animate({'left': '0'}, 'slow', function(){
					$('body').removeClass('animating');
				});
			}
			resetSlide();
			return false;
		});
		
		$('#rotate .panel .button').mouseover(function() {
			clearTimeout(slideTimeout);
		}).mouseout(function() {
			resetSlide();
		});
		
		if($('#rotate li').length > 1) {
			resetSlide();
		}
	
		var navHeight = $('header nav').height();
		var socialHeight = $('header ul.social').height();
		var footerHeight = $('footer').height();
		var smallerHeight = 176 + 45 + navHeight + socialHeight + footerHeight + 30;
		if($(document).width() < '951' || $(window).height() < smallerHeight) {
			$('body').addClass('small');
		} else {
			$('body').removeClass('small');
		}
		
		$('#slide-wrap .content').css({'min-height': oy});

		if($('#menus').length > 0) {
			$('body').addClass('small');
		}

	}).trigger('resize');
	

	if($('#slide-panel').length > 0) {
		clearTimeout(slideTimeout);
		$('#carousel-nav').css({'bottom': '-52px'});
	}

	$('#slide-panel .slide-link a').click(function() {
		if($(this).hasClass('closed')) {
			$(this).removeClass('closed').parent().animate({'left': '590px'});
			$(this).parent().parent().parent().animate({'left': '242px'});
			$('#carousel-nav').animate({'bottom': '-52px'});
			clearTimeout(slideTimeout);
		} else {
			$(this).addClass('closed').parent().animate({'left': '115px'});
			$(this).parent().parent().parent().animate({'left': '-230px'});
			$('#carousel-nav').animate({'bottom': '20px'});
			slideTimeout = setTimeout(function () {
				slide();
			}, 8000);
		}
		return false;
	});
	
	$('#rotate li .panel .button').live('click mouseover mouseout', function(event) {
		if ( event.type == 'mouseover' ) {
			var theLink = $(this).find('a').attr('href');
			if(theLink) {
				$(this).addClass('hover');
			}
		} else if ( event.type == 'mouseout' ) {
			$(this).removeClass('hover');
		} else {
			var theLink = $(this).find('a').attr('href');
			if(theLink) {
				window.location = theLink;
			}
		}
	});

	if($('#menus').length > 0) {
		$('body').addClass('small');
	}

	
});
