(function($){
    // Extend the jQuery fn namespace
    $.fn.extend({
        newsTicker: function(options) {
            // 今回使う変数たち
            var self = $(this),
                c = $.extend({
                    whois: null,
                    whoname:null
            }, options || {});
            if( !c.whois ) {
	            alert("whoisの指定をしてください");
                return false;
            }
            if( !c.whoname ) {
	            alert("whonameの指定をしてください");
                return false;
            }
	$.ajax({
		url: "news.xml",
		type: 'GET',
         dataType: ($.browser.msie) ? "text" : "xml",
         error: function(xml){
	        alert("xmlファイルの読み込みに失敗しました");
	     },
		success: function(xml){
//			alert("xmlファイルの読み込みに成功しました");
			var xmll;
            if (typeof xml == "string") {
                xmll = new ActiveXObject("Microsoft.XMLDOM");
                xmll.async = true;
                xmll.loadXML(xml);
            } else {
                xmll = xml;
            }
//               alert($(xmll).text());//alertを表示　←①
			
			var item_text = "";
			var whois_text = c.whois;
			var whois_n = 0;
//				alert(whois_text);
			$(xmll).find("item").each(function(i){
				if( $(whois_text,this).text() ==1){
					item_text +=  "<li";
					item_text +=  " id='news-";
					item_text +=  whois_n;
					item_text +=  "'>";
					
					if ($("top",this).text() == "1" ){
						item_text +=  "<a href='";
						item_text += $("link",this).text();
						item_text +=  "'>";
						item_text += $("title",this).text();
						item_text +=  "</a></li>";
					}else{
						if ($("url",this).text() == "" ){
							item_text += $("title",this).text();
						}else{
							item_text +=  "<a href='";
							item_text += $("url",this).text();
							item_text +=  "' target='_blank'>";
							item_text += $("title",this).text();
							item_text +=  "</a></li>";
						}
					}
					whois_n += 1;
				};
			});
//				alert(item_text);

			if (whois_n > 0){
				var ticker_text = "";
				ticker_text += "<div class='newsTitle'>";
				ticker_text += c.whoname;
				ticker_text += " インフォ</div>";
				ticker_text += "<div id='newsTicker'><ul>";
				ticker_text += item_text;
				ticker_text += "</div><img src='../images/talent/share/ticker_foot.jpg' class='floatL mgT10'><br class='clear'>";

			
				$("div#ticker").html(ticker_text);

				function newsFader(){
					if($("li","div#newsTicker ul").length==1) return false;
					$("li","div#newsTicker ul").each(function(i){
						if(i!=0) $(this).hide();
					});
					var start = count = 0,
					end = $("li","div#newsTicker ul").length-1,
					interval = 2500,
					fadeTimer = setInterval(function(){
						$("li#news-"+(start==1 ? count==0 ? end : count-1 : count)).hide();
						$("#news-"+(start==1 ? (++count)-1 : ++count)).fadeIn("slow");
						if(start==0 && count>=end){
							count = 0;
							start = 1;
						}else if(start==1 && count>end){
							count = 0;
						}
					},interval);
				}
			newsFader();
				}
			}
		});
        }
    });

})(jQuery);
