//--------------------------------------------------------
//----------------rssティッカー--------------------
//--------------------------------------------------------
$(function(){
	$.ajax({
		//index.xmlを取得
		url: "index.xml",
		//非同期で処理
		async: true,
		//キャッシュ機構を回避
		cache: false,
		//列に格納されているデータの型(xml)を取得
		dataType:"xml",		
		success: function(xml){
			$(xml).find('item').each(function(i){
                //初期設定で5件出力
				//件数を変更は"i > 2"の部分を修正
                //数値は"出力したい件数 - 1"を入力
                if ( i > 4 ) {
					rss_makelist () ;
                    return false;
                }
				// tittle のテキスト情報の取得
				var title = $(this).find('title').text();
				// title の文字数を取得
				var title_length = title.length;
				// 文字数が 45 文字以上の場合
				if (title_length > 45) {
				// 42 文字以上をカットし、... を追加
					title = title.substr(0,42) + " ...";
				}
				//urlはxmlデーター内の"link"を取得				
				var url = $(this).find('link').text();
				//detaはxmlデーター内の"pubDate"を取得し、日付を整形しdateParseにする
				var date = dateParse($(this).find('pubDate').text());
				//"20xx/xx/xx  テキスト" の形式で出力
				$('#feedList').append('<li class="column">' + date[0] + '<a href="' + url + '">' + title + '</a>' + '</li>');
    		});
		}
	});
});

//dateParse: "20xx/xx/xx" 形式
function dateParse(str){
    var objDate = new Date(str);
    var nowDate = new Date();
    //現在の日付との差を計算
    myDay = Math.floor((nowDate.getTime()-objDate.getTime()) / (1000*60*60*24)) + 1;
    //もし2週間以内なら"new!"マーク
    if (myDay < 15 ){
        var newMsg = '&nbsp;&nbsp;<span style="color:#ff6666; font-weight:bold;">new!</span>';
    } else {
        var newMsg = '';
    }
    var year = objDate.getFullYear();
    var month = objDate.getMonth() + 1;
    var date = objDate.getDate();
    if ( month < 10 ) { month = "0" + month; }
    if ( date < 10 ) { date = "0" + date; }
    str = year + '/' + month + '/' + date;
    rtnValue = new Array(2);
    rtnValue[0] = str;
    rtnValue[1] = newMsg;
    return rtnValue;
}

	 //ティッカーの設定
function rss_makelist(){
	//初期設定
	//#feedListの高さを23×#feedList li.columnの数のサイズにpxをたす
	$("#feedList").css("height",23*$("#feedList li.column").size()+"px");
	//#feedList li.column:lastの中身を#feedListの先頭に挿入する。
	$("#feedList li.column:last").prependTo("#feedList");
	//#feedListに対してcssでmargin-top-23pxを指定
	$("#feedList").css("margin-top","-23px");
	
	//1件送るボタン
	//#rssnewsNextをクリックしたら
	$("#rssnewsNext").click(function(){
		//#feedListにアニメーションの設定
		//marginTopは文字列を整数に変換した#feedListにcssでmargin-top-35pxを適用
		$("#feedList").animate({
			marginTop : parseInt($("#feedList").css("margin-top"))-23+"px"
		},"slow","swing" , 
		function(){
			//#feedListにcssでmargin-top-23pxを適用
			$("#feedList").css("margin-top","-23px");
			//#feedList li.columnのうちから先頭のひとつだけを#feedList追加する。
			$("#feedList li.column:first").appendTo("#feedList");
		});
	});
	
	//定期的に処理を行わせるタイマー処理
	//timerIDは#rssnewsNextをクリックした時、又は5000（5秒たったら）繰り返す。
	var timerID = setInterval(function(){
		$("#rssnewsNext").click();
	},5000);
		//#rssnewsNextボタンを押したら、タイマー処理を止める
	$("#rssnewsPrev img,#rssnewsNext img").click(function(){
		clearInterval(timerID);
	});

	
}
//--------------------------------------------------------
//---------------テニプリトップグラフィック---------------
//--------------------------------------------------------
$(function(){
	
	//初期設定
	//#main-glaphic-boxのwidthは595×.itemの数
	$("#main-glaphic-box").css("width",595*$("#main-glaphic-box .item").size()+"px");

	//タイマーセット
	var timerID_top=setTimeout(function(){
		$("#main-glaphic-btn").click();
	},8000);

	//次のバナー
	$("#main-glaphic-btn").click(function(){
		clearTimeout(timerID_top);
		$("#main-glaphic-btn").hide();
		$("#main-glaphic-box").hide();
		$("#main-glaphic-box .item:first").appendTo("#main-glaphic-box");
		$("#main-glaphic-box").animate({
			marginLeft : parseInt( "0px") , opacity:"toggle"
		},"1000","swing",
		function(){
			$("#main-glaphic-btn").show();
			$("#main-glaphic-box").css("margin-left","0px");
			timerID_top=setTimeout(function(){
				$("#main-glaphic-btn").click();
			},8000);
		});
	});

});


//--------------------------------------------------------
//----------------ランダムピックアップ--------------------
//--------------------------------------------------------
$(function(){
	
	//初期設定
	//#carouselInnerのwidthは620×#carouselInner ul.column数にpxをたす。
	$("#carouselInner").css("width",1000+$("#carouselInner ul li.column").size()+"px");
	//#carouselInner ul li.columnの最後に、#carouselInnerを挿入
	$("#carouselInner ul li.column:last").prependTo("#carouselInner ul:first");
	////#carouselInnerのmargin-leftは-620px
	$("#carouselInner").css("margin-left","-105px");
	
	//前の番組
	//#carouselPrevを押すと、
	$("#carouselPrev").click(function(){
		//#carouselNext,#carouselPrevは消える。
		//$("#carouselNext,#carouselPrev").hide();
		//#carouselInnerのアニメーションは
		$("#carouselInner").animate({
		//"#carouselInnerの	margin-leftは620pxたしたpx分ゆっくり移動					
			marginLeft : parseInt($("#carouselInner").css("margin-left"))+105+"px"
		},"slow","swing" , 
		function(){
			//#carouselInnerのmargin-leftは-620px
			$("#carouselInner").css("margin-left","-105px")
			//#carouselInner ul.columnの最後に、#carouselInnerを挿入
			$("#carouselInner ul li.column:last").prependTo("#carouselInner ul:first");
			//#carouselNext,#carouselPrevを表示。
			$("#carouselNext,#carouselPrev").show();
		});
	});
	
	//次の番組
	//#carouselNextを押すと、
	$("#carouselNext").click(function(){
		 //#carouselNext,#carouselPrevは消える。
		//$("#carouselNext,#carouselPrev").hide();
		//#carouselInnerのアニメーションは
		$("#carouselInner").animate({
		//"#carouselInnerの	margin-leftは620pxたしたpx分ゆっくり移動
			marginLeft : parseInt($("#carouselInner").css("margin-left"))-105+"px"
		},"slow","swing" , 
		function(){
			//#carouselInnerのmargin-leftは-620px
			$("#carouselInner").css("margin-left","-105px");
			//#carouselInner ul.columnの最後に、#carouselInnerを挿入
			$("#carouselInner ul li.column:first").appendTo("#carouselInner ul:last");
			//#carouselNext,#carouselPrevを表示。
			$("#carouselNext,#carouselPrev").show();
		});
	});
	
	//タイマー処理
	//5秒たったら#carouselNextボタンを押した処理をする
	var timerID2 = setInterval(function(){
		$("#carouselNext").click();
	},5000);
	
	//#carouselNextボタンを押したら、タイマー処理を止める
	$("#carouselPrev img,#carouselNext img").click(function(){
		clearInterval(timerID2);
	});
	
});

//--------------------------------------------------------
//----------------フローティング--------------------
//--------------------------------------------------------


$(function(){
	$("a.open").click(function(){
		$("#floatWindow").fadeIn("fast");
		return false;
	});
	
	$("#floatWindow a.close").click(function(){
		$("#floatWindow").fadeOut("fast");
		return false;
	});
	
	
	$("#floatWindow dl dt").mousedown(function(e){
		
		$("body").bind('selectstart', function(){
			return false;
		});
		
		$("#floatWindow")
			.data("clickPointX" , e.pageX - $("#floatWindow").offset().left)
			.data("clickPointY" , e.pageY - $("#floatWindow").offset().top);
		
		$(document).mousemove(function(e){
			$("#floatWindow").css({
				top:e.pageY  - $("#floatWindow").data("clickPointY")+"px",
				left:e.pageX - $("#floatWindow").data("clickPointX")+"px"
			});
		});
		
	}).mouseup(function(){
		$("body").unbind('selectstart');
		$(document).unbind("mousemove");
		
	});
});


$(function(){
	$("a.open2").click(function(){
		$("#floatWindow2").fadeIn("fast");
		return false;
	});
	
	$("#floatWindow2 a.close").click(function(){
		$("#floatWindow2").fadeOut("fast");
		return false;
	});
	
	
	$("#floatWindow2 dl dt").mousedown(function(e){
		
		$("body").bind('selectstart', function(){
			return false;
		});
		
		$("#floatWindow2")
			.data("clickPointX" , e.pageX - $("#floatWindow2").offset().left)
			.data("clickPointY" , e.pageY - $("#floatWindow2").offset().top);
		
		$(document).mousemove(function(e){
			$("#floatWindow2").css({
				top:e.pageY  - $("#floatWindow2").data("clickPointY")+"px",
				left:e.pageX - $("#floatWindow2").data("clickPointX")+"px"
			});
		});
		
	}).mouseup(function(){
		$("body").unbind('selectstart');
		$(document).unbind("mousemove");
		
	});
});



$(function(){
	$("a.open3").click(function(){
		$("#floatWindow3").fadeIn("fast");
		return false;
	});
	
	$("#floatWindow3 a.close").click(function(){
		$("#floatWindow3").fadeOut("fast");
		return false;
	});
	
	
	$("#floatWindow3 dl dt").mousedown(function(e){
		
		$("body").bind('selectstart', function(){
			return false;
		});
		
		$("#floatWindow3")
			.data("clickPointX" , e.pageX - $("#floatWindow3").offset().left)
			.data("clickPointY" , e.pageY - $("#floatWindow3").offset().top);
		
		$(document).mousemove(function(e){
			$("#floatWindow3").css({
				top:e.pageY  - $("#floatWindow3").data("clickPointY")+"px",
				left:e.pageX - $("#floatWindow3").data("clickPointX")+"px"
			});
		});
		
	}).mouseup(function(){
		$("body").unbind('selectstart');
		$(document).unbind("mousemove");
		
	});
});

//--------------------------------------------------------
//----------------ロールオーバ_ing--------------------
//--------------------------------------------------------
function smartRollover() {
	if(document.getElementsByTagName) {
		var images = document.getElementsByTagName("img");

		for(var i=0; i < images.length; i++) {
			if(images[i].getAttribute("src").match("_off."))
			{
				images[i].onmouseover = function() {
					this.setAttribute("src", this.getAttribute("src").replace("_off.", "_on."));
				}
				images[i].onmouseout = function() {
					this.setAttribute("src", this.getAttribute("src").replace("_on.", "_off."));
				}
			}
		}
	}
}

if(window.addEventListener) {
	window.addEventListener("load", smartRollover, false);
}
else if(window.attachEvent) {
	window.attachEvent("onload", smartRollover);
}
