//top menu
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

$(function(){
	$("#innquery").mouseover(function(){$(this).attr("src","/images/top_item1a.gif");});
	$("#innquery").mouseout(function(){$(this).attr("src","/images/top_item1s.gif");});
	$("#scenicspots").mouseover(function(){$(this).attr("src","/images/top_item2a.gif");});
	$("#scenicspots").mouseout(function(){$(this).attr("src","/images/top_item2s.gif");});
	$("#discussion").mouseover(function(){$(this).attr("src","/images/top_item3a.gif");});
	$("#discussion").mouseout(function(){$(this).attr("src","/images/top_item3s.gif");});
	$("#recommend").mouseover(function(){$(this).attr("src","/images/top_item4a.gif");});
	$("#recommend").mouseout(function(){$(this).attr("src","/images/top_item4s.gif");});
});

//add count for inn adver
$(function(){
	$(".tab_content img, .tab_content a, .listby3 img, .cyclepics img, .coverflowAd img").bind('click',this,function(){
		var	ienid = $(this).attr('id');
		
		if ( ienid !== undefined && ienid !== null && ienid!=="")
		{	
			$('#adcount').attr("src","adcount.php?ienid="+ienid);
		}
	});
});

//left menu
$(function(){
	// 幫 div.qa_title 加上 hover 及 click 事件
	// 同時把兄弟元素 div.qa_content 隱藏起來
	$('#qaContent div.qa_title').click(function(){
		// 當點到標題時，若答案是隱藏時則顯示它；反之則隱藏
		$(this).next('div.qa_content').slideToggle();
	}).siblings('div.qa_content').hide();

	var $ad = $('a.fadein'),
		showIndex,
		fadeOutSpeed = 1000,	// 淡出的速度
		fadeInSpeed = 600;		// 淡入的速度
	
	// 先把其它圖片的變成透明
	for(var i=0;i<$ad.length;i++){
		$ad.eq(i).css({opacity: 0});
	}
	
	$ad.mouseover(function(){
		// 取得目前點擊的號碼
		showIndex = $(this).attr('tabindex') * 1 - 1;
		$ad.eq(showIndex).stop().fadeTo(fadeInSpeed, 1);
		return false;
	}).focus(function(){
		$(this).blur();
	});

	$ad.mouseout(function(){
		// 取得目前點擊的號碼
		showIndex = $(this).attr('tabindex') * 1 - 1;
		$ad.eq(showIndex).stop().fadeTo(fadeOutSpeed, 0);
		return false;
	});
});


//news
$(function(){
	// 先取得 div#abgne_marquee ul
	// 接著把 ul 中的 li 項目再重覆加入 ul 中(等於有兩組內容)
	// 再來取得 div#abgne_marquee 的高來決定每次跑馬燈移動的距離
	// 設定跑馬燈移動的速度及輪播的速度
	var $marqueeUl = $('div#abgne_marquee ul'),
		$marqueeli = $marqueeUl.append($marqueeUl.html()).children(),
		_height = $('div#abgne_marquee').height() * -1,
		scrollSpeed = 600,
		timer,
		speed = 2000 + scrollSpeed,
		direction = 0,	// 0 表示往上, 1 表示往下
		_lock = false;

	// 先把 $marqueeli 移動到第二組
	$marqueeUl.css('top', $marqueeli.length / 2 * _height);
	
	// 幫左邊 $marqueeli 加上 hover 事件
	// 當滑鼠移入時停止計時器；反之則啟動
	$marqueeli.hover(function(){
		clearTimeout(timer);
	}, function(){
		timer = setTimeout(showad, speed);
	});
	
	
	
	// 控制跑馬燈上下移動的處理函式
	function showad(){
		_lock = !_lock;
		var _now = $marqueeUl.position().top / _height;
		_now = (direction ? _now - 1 + $marqueeli.length : _now + 1)  % $marqueeli.length;
		
		// $marqueeUl 移動
		$marqueeUl.animate({
			top: _now * _height
		}, scrollSpeed, function(){
			// 如果已經移動到第二組時...則馬上把 top 設回到第一組的最後一筆
			// 藉此產生不間斷的輪播
			if(_now == $marqueeli.length - 1){
				$marqueeUl.css('top', $marqueeli.length / 2 * _height - _height);
			}else if(_now == 0){
				$marqueeUl.css('top', $marqueeli.length / 2 * _height);
			}
			_lock = !_lock;
		});
		
		// 再啟動計時器
		timer = setTimeout(showad, speed);
	}
	
	// 啟動計時器
	timer = setTimeout(showad, speed);

	$('a').focus(function(){
		this.blur();
	});
});


//footer
$(function(){
function adjfooter(){
	var adjtimer,rbt,lbt,tmprbt,tmplbt;
	rbt=$('#rightdiv').offset().top+$('#rightdiv').height();
	lbt=$('#leftdiv').offset().top+$('#leftdiv').height();
	if(rbt>lbt){
		$('#bottomdiv').css("top",rbt+"px").show();	
		if(rbt==tmprbt) clearTimeout(adjtimer);
		tmprbt=rbt;
	}
	else{
		$('#bottomdiv').css("top",lbt+"px").show();
		if(lbt==tmplbt) clearTimeout(adjtimer);
		tmplbt=lbt;
	}
}
	adjtimer=setTimeout(adjfooter, 5000);
});


