
$(document).ready(function(){
	if($('#bimage img').length){
		
		$('#bimage img:first').addClass('active');
		$('#bimage img').not('.active').css({opacity: 0.0});
		$('#bimage').show();
		
		var fmRotate = function (){
				//$el = $('.fm image.active').next();
				//$('.fm img.active').removeClass('active');
				//$el.addClass('active');
				//do transition
				var $active = $('#bimage img.active');
				
				var $next = $('#bimage img.active').next();
				if (!$next.length){
					$next = $('#bimage img:first');
				}
				$active.addClass('last-active');
				
				$next.css({
					opacity: 0.0
				}).addClass('active').animate({
					opacity: 1.0
				}, 4800);
				$active.removeClass('active last-active').animate({
					opacity: 0.0
				}, 1200);
				
				/* remove old one completely first */
				/*
				$active.removeClass('active last-active').animate({
					opacity: 0.0
				}, 1200, function(){
					$next.css({
						opacity: 0.0
					}).addClass('active').animate({
						opacity: 1.0
					}, 1200);
						
				});
				*/
			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()', 10000);
			}else{
				ssTo = setTimeout('fmStartShow(0)',l);
			}
		}
		fmSlideShow = function(){
			fmRotate();
		}
		
		fmStartShow(10000);
	}
	
	/* fix menu position */
	var h = $(window).height();
	var w = $(window).width();
	//alert(h +' xxx ' + w);
	
	if (h > ($('#top_nav').height() + 120) && w > 1100){
		$('#header, #top_nav, #bottom_text').css('position','fixed');
	}
	
	/* homepage bodies */
	if ($('#bodies a').length){
		
		$('#bodies a').mouseenter(function(){
			$('#bodies a').removeClass('bon');
			$(this).addClass('bon');
			$('#binfoh').html($(this).attr('title'));
			$('#binfot').html($(this).find('span').html());
			$('#binfoa').attr('href',$(this).attr('href'));
			
			var t = parseInt($(this).css('top'));
			var l = parseInt($(this).css('left'));
			
			$('#bar1').css('top', t+'px').css('left',l+'px');
			if (l < 150){
				$('#bar3').css('width', '170px');
				var w = 150 - l - 16;
				$('#bar1').css('width',w+'px');
				$('#bar2').css('left','150px');
			}else{
				$('#bar3').css('width', '15px');
				var w = 305 - l - 16;
				$('#bar1').css('width',w+'px');
				$('#bar2').css('left','305px');
			}
			if (t < 81){
				$('#bar2').css('top',t+'px').css('height',(81 - t)+'px');
			}else{
				$('#bar2').css('top','81px').css('height',(t - 81)+'px');
			}
		});
		$('#bar1, #bar2, #bar3').show();
		$('#bodies a:first').trigger('mouseenter');
	
	}
	
	if($('#gallery img').length){
		
		$('#gallery img:first').addClass('active');
		$('#gallery img').not('.active').css({opacity: 0.0}).show();
		
		var gactive = false;
		
		var gRotate = function (last){
				//do transition
			if (!gactive){
			//$('#gallery #control #next, #gallery #control #prev').unbind();
				gactive = true;
				var $active = $('#gallery img.active');
				
				if (last){
					//going backwords
					var $next = $('#gallery img.active').prev('img');
					if (!$next.length){
						$next = $('#gallery img:last');
					}
				}else{
					var $next = $('#gallery img.active').next('img');
					if (!$next.length){
						$next = $('#gallery img:first');
					}
				}
				$active.addClass('last-active');
				
				$next.css({
					opacity: 0.0
				}).addClass('active').animate({
					opacity: 1.0
				}, 1200);
				$active.removeClass('active last-active').animate({
					opacity: 0.0
				}, 1200, function(){
					gactive = false;
				});
			}
				/* remove old one completely first */
				/*
				$active.removeClass('active last-active').animate({
					opacity: 0.0
				}, 1200, function(){
					$next.css({
						opacity: 0.0
					}).addClass('active').animate({
						opacity: 1.0
					}, 1200);
						
				});
				*/
			return false;
		};
		
		var gInt = null;
		var gTo = null;
		
		gStopShow = function(){
			clearInterval(gInt);
			clearTimeout(gTo);
			gTo = null;
			gInt = null;
		}
		gStartShow = function(l){
			gStopShow();//clear timers
			if (l == 0) {
				gSlideShow();
				gInt = setInterval('gSlideShow()', 5000);
			}else{
				gTo = setTimeout('gStartShow(0)',l);
			}
		}
		gSlideShow = function(){
			gRotate(false);
		}
		
		gStartShow(5000);
		
		$('#gallery #control #next').click(function(){
			gStopShow();
			gRotate(false);
		});
		$('#gallery #control #prev').click(function(){
			gStopShow();
			gRotate(true);
		});
	
	}
	
	
})

