$(document).ready(function(){
	if($('#hgal #gallery div').length){
		
		$('#hgal #gallery div:first').addClass('active').show();
		//$('#homepage_background span').not('.active').css({opacity: 0.0});
		//$('#homepage_background span').show();
		
		var fmRotate = function (){
				//$el = $('.fm image.active').next();
				//$('.fm img.active').removeClass('active');
				//$el.addClass('active');
				//do transition
				var $active = $('#hgal #gallery div.active');
				
				var $next = $('#hgal #gallery div.active').next();
				if (!$next.length){
					$next = $('#hgal #gallery div:first');
				}
				$('img, a, span', $active).animate({
					opacity: 0.0
				}, 1200, function(){
					//$active.removeClass('active last-active');
					$active.hide().removeClass('active');
				});
					$next.show().addClass('active').find('img, a, span').css({
						opacity: 0.0
					}).animate({
						opacity: 1.0}, 4000);
					
				
			return false;
		};
		
		var ssInt = null;
		var ssTo = null;
		
		fmStopShow = function(){
			clearInterval(ssInt);
			clearTimeout(ssTo);
			ssTo = null;
			ssInt = null;
		}
		fmStartShow = function(l){
			fmStopShow();//clear timers
			if (l == 0) {
				fmSlideShow();
				ssInt = setInterval('fmSlideShow()', 5000);
			}else{
				ssTo = setTimeout('fmStartShow(0)',l);
			}
		}
		fmSlideShow = function(){
			fmRotate();
		}
		
		fmStartShow(3000);
		
	}
	
	$('#top_image img:first').addClass('active').show();
	if($('#top_image img').length > 1){
		
		
		//$('#homepage_background span').not('.active').css({opacity: 0.0});
		//$('#homepage_background span').show();
		
		var fmRotate = function (){
				//$el = $('.fm image.active').next();
				//$('.fm img.active').removeClass('active');
				//$el.addClass('active');
				//do transition
				var $active = $('#top_image img.active');
				
				var $next = $('#top_image img.active').next();
				if (!$next.length){
					$next = $('#top_image img:first');
				}
				$active.animate({
					opacity: 0.0
				}, 1200, function(){
					//$active.removeClass('active last-active');
					$active.hide().removeClass('active');
				});
					$next.show().addClass('active').css({
						opacity: 0.0
					}).animate({
						opacity: 1.0}, 4000);
					
				
			return false;
		};
		
		var ssInt = null;
		var ssTo = null;
		
		fmStopShow = function(){
			clearInterval(ssInt);
			clearTimeout(ssTo);
			ssTo = null;
			ssInt = null;
		}
		fmStartShow = function(l){
			fmStopShow();//clear timers
			if (l == 0) {
				fmSlideShow();
				ssInt = setInterval('fmSlideShow()', 5000);
			}else{
				ssTo = setTimeout('fmStartShow(0)',l);
			}
		}
		fmSlideShow = function(){
			fmRotate();
		}
		
		fmStartShow(3000);
		
	}
	
});

