/*
 * news typing list - jquery.newsTyping.js
 * 
 * The author is Manabu Kushimoto.
 * And URL is http://web-park.org.
 * 
 * jQuery version 1.4.2 later is required.
 * 
 * Update is 2011.07.19.
 * 
 * License is GPL(GNU General Public License).
 * Copy right © WEB-PARK.ORG All rights reserved.
 * 
 */

jQuery(function($){
	$.fn.newsTyping=function(options){
		var op=$.extend($.fn.newsTyping.defaults,options);
		var $t=this;
		var autoType="autoType",autoTypeElm='<div id="'+autoType+'"></div>';
		var nextImg='<img src="img/index/btn_next.gif" />';
		var prevImg='<img src="img/index/btn_prev.gif" />';
		var controlElm='<ul id="typingControl"><li class="next"><a href="#">'+nextImg+'</a></li><li class="prev"><a href="#">'+prevImg+'</a></li></ul>'
		$t.prepend(autoTypeElm)/*.append(controlElm)*/;
		
		var $autoType=$("div#autoType"),$typingList=$("ul#typingList"),typingListLength=$("ul#typingList").children().length;
		
		$("<a />").appendTo($autoType);
		
		$("ul#typingControl li.next a").click(function(){
			$typingList.children().eq(0).appendTo($typingList).show(function(){
				autoSwitch();
			});
			$(this).blur();
			return false;
		});

		$("ul#typingControl li.prev a").click(function(){
			$typingList.children().eq(typingListLength-1).prependTo($typingList).show(function(){
				autoSwitch();
			});
			$(this).blur();
			return false;
		});

		function autoTxt(){
			var href=$typingList.children().eq(0).children().attr("href");
			var text=new String($typingList.children().eq(0).children().text()).replace(/&/,"＆");
			var textLength=text.length;
			var textArray=new Array(textLength);
			var textNumber=new Number();
			var textReplace;
			textArray=text.split('');
			$autoType.children().attr("href",href);
			setTxt=setInterval(function(){
				if(textNumber>=textLength){
					textNumber=0;
					$typingList.children().eq(0).appendTo($typingList);
					autoSwitch("on");
				}else{
					textNumber=textNumber+1;
					$autoType.children().append("_").delay(80).show(function(){
						textReplace=new String($(this).text()).replace(/_/,"");
						$autoType.children().html(textReplace+textArray[textNumber-1]);
					});
				}
			},200);
		}
		autoTxt();

		function autoSwitch(timeout){
			clearInterval(setTxt);
			if(timeout=="on"){
				timoutTxt=setTimeout(function(){
					$autoType.children().html("");
					autoTxt();
				},6000);
			}else{
				timoutTxt=setTimeout(function(){
					$autoType.children().html("");
					autoTxt();
				},100);
			}
		}

	};
	$.fn.newsTyping.defaults = {
	};
	
	window.onload=function(){
		$("div#typingInfo").newsTyping();
	};

});
