$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */
	
	var totWidth=0;
	var positions = new Array();
	
	$('#notice_content li').each(function(i){
		
		/* Traverse through all the slides and store their accumulative widths in totWidth */
		
		positions[i]= totWidth;
		totWidth += $(this).width();
		
		/* The positions array contains each slide's commulutative offset from the left part of the container */
		
		if(!$(this).width())
		{
			alert("Please, fill in width & height for all your images!");
			return false;
		}
		
		//增加 tab 事件處理
		$(this).find(" > a").focus(function(){
			$('#notice_menu li a:eq(' + i + ')').focus();
		});
		
	});
	
	$('#notice_content').width(totWidth);
	
	//複製成選單
	$('#notice_content').clone().attr("id", "notice_menu").removeAttr("style").appendTo($("#notice_slideshow")).children().each(function(i){
		$(this).attr("class", "menuItem").find("a").text(i + 1);
	});

	/* Change the cotnainer div's width to the exact width of all the slides combined */

	$('#notice_menu li a').click(action).focus(function(){
		$(this).click();
	}).keypress(function(e){ //當按下enter時
		if(e.keyCode == 13){
			window.open(this.href);
			e.preventDefault();
		}
	}).bind("action", action);
	
	$('#notice_menu li.menuItem:first').addClass('act').siblings().addClass('inact');
	/* On page load, mark the first thumbnail as active */
	
	function action(e,keepScroll){
		/* On a thumbnail click */
		
		$('li.menuItem').removeClass('act').addClass('inact');
		$(this).parent().addClass('act');
		
		var pos = $(this).parent().prevAll('.menuItem').length;
		
		$('#notice_content').stop().animate({marginLeft:-positions[pos]+'px'},450);
		/* Start the sliding animation */
		if(e)
		e.preventDefault();
		/* Prevent the default action of the link */
		
		// Stopping the auto-advance if an icon has been clicked:
		if(!keepScroll) clearInterval(itvl);
		
	}
	
	/*****
	 *
	 *	Enabling auto-advance.
	 *
	 ****/
	 
	var current=1;
	function autoAdvance()
	{
		if(current==-1) return false;
		
		//$('#notice_menu li a').eq(current%$('#notice_menu li a').length).trigger('click',[true]);	// [true] will be passed as the keepScroll parameter of the click function on line 28
		$('#notice_menu li a').eq(current%$('#notice_menu li a').length).trigger('action', true);
		//event(null, true);
		current++;
	}

	// The number of seconds that the slider will auto-advance in:
	
	var changeEvery = 5;

	var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

	/* End of customizations */
});
