$(function(){
    var duration = 30000;//for index 1024 間格時間
    
    //效果
	//effects = ["cube", "multiflip", "dissolve", "slidein", "fadethrough"];
	if($.browser.safari) {
		effects = ["cube", "multiflip", "dissolve", "slidein", "fadethrough"];
		//effects = ["fadethrough"];
	}else{
		effects= ["dissolve"];	
	}

	var swaping = true;
	
	$('#nav li').click(function(){
		if( timeout_animation ) {
            clearTimeout(timeout_animation);
        }
		//alert(swaping);
		var $this = $(this);
		if ( swaping ) {
			//alert('dd');
			var nextImageNum = $this.index();
			//當前圖片
			var currentImage = $("#poster img").attr("src");

			addaction(currentImage, nextImageNum);
		}
	});
	
	function animation() {

		//隨機圖片數組
		var nextImageNum = parseInt(articles_array.length*Math.random());
	//	alert(articles_array.length);

		//當前圖片
		var currentImage = $("#poster img").attr("src");
		
		//alert(currentImage);
		if(swaping) {
			addaction(currentImage, nextImageNum);
		}
		if( timeout_animation ) {
            clearTimeout(timeout_animation);
        }
	}
	
	function addaction(currentIma, nextImageNum) {
		
		swaping = false;
		//alert(swaping);
		currentImage = currentIma;
		//相關圖片和內容
		nextImage = articles_array[nextImageNum]['images'];
		nextTitle = articles_array[nextImageNum]['title'];
		nextSubtitle = articles_array[nextImageNum]['subtitle'];
		nextSubcategory = articles_array[nextImageNum]['subcategory'];
		nextSubcategory_image = articles_array[nextImageNum]['subcategory_image'];
		//當前內容
		var currentTitle = $("#poster #index_article_title").html();
		var currentSubtitle = $("#poster #index_article_subtitle ").html();
		var currentSubcategory_image = $("#poster #index_article_subcategory_image").attr("src");
		var currentSubcategory = $("#poster #index_article_subcategory_text").html();
		//隨機樣式
		useEffect = effects[parseInt(effects.length*Math.random())];
		
		$thisEffect = $("#" + useEffect);
//alert($thisEffect);
		//moveNav
		$('#nav li').removeClass('active');
		$('#nav .' + nextImageNum).addClass('active');
		
		$("#" + useEffect +" .outgoing .article_show_image").attr("src", currentImage);
		$("#" + useEffect +" .incoming .article_show_image").attr("src", nextImage);
		//添加文章內空
		$("#" + useEffect +" .outgoing #index_article_subtitle").html(currentSubtitle);
		$("#" + useEffect +" .incoming #index_article_subtitle").html(nextSubtitle);
		
		$("#" + useEffect +" .outgoing #index_article_subcategory_text").html(currentSubcategory);
		$("#" + useEffect +" .incoming #index_article_subcategory_text").html(nextSubcategory);
			
		$("#" + useEffect +" .outgoing #index_article_title").html(currentTitle);
		$("#" + useEffect +" .incoming #index_article_title").html(nextTitle);
		
		$("#" + useEffect +" .outgoing #index_article_subcategory_image").attr("src", currentSubcategory_image);
		$("#" + useEffect +" .incoming #index_article_subcategory_image").attr("src", nextSubcategory_image);				

		$("#poster").addClass('inactive');
		$thisEffect.addClass('active');

		timeout_remove = window.setTimeout(removediv, 1200);
	}
		
	function removediv() { //執行完刪除動畫樣式
		//alert(wihchTransitions);
		$thisEffect.removeClass("active");
		$("#poster .article_show_image").attr("src", nextImage);
		//添加最后圖片
		$("#poster #index_article_subtitle").html(nextSubtitle);
		$("#poster #index_article_subcategory_text").html(nextSubcategory);
		$("#poster #index_article_title").html(nextTitle);
		$("#poster #index_article_subcategory_image").attr("src", nextSubcategory_image);	
		
		$("#poster").removeClass("inactive");
		swaping = true;
		if ( timeout_remove ) {
            clearTimeout(timeout_remove);
        }
		//alert(swaping);
		timeout_animation = window.setTimeout(animation, duration);
	}
	
	timeout_animation = window.setTimeout(animation, duration);
	
});

