﻿
/******************************
 .flowTxt update: 100220
*********************************/

$(function(){
	
	$.each($(".flowable li"), function(i,item){
		var flowTxtWidth=$(item).parent().innerWidth() - $(item).children(".head").outerWidth();
		$(item).children(".flowTxt").css("width",flowTxtWidth+"px");
		var dist=$(item).children(".flowTxt").children("span").dist=$(item).children(".flowTxt").children("span").innerWidth()-flowTxtWidth;
		var moveItem=$(item).children(".flowTxt").children("span");
		if(dist>0){
			moveItem.css("position","relative");
			moveItem.css("left","0px");
			$(item).children(".flowTxt").mouseover(function(){
				move(moveItem,-dist-20);
			});
			$(item).children(".flowTxt").mouseout(function(){
				moveDefault(moveItem);
				
			});
		}
	});	
	function move(obj,toX){
		var str = String($(obj).css("left"));
		clearInterval(obj.intervalID);
		
		obj.intervalID=setInterval(function(){
			_move(obj,toX);//IEでのobjの参照を保持
		},30);
	}
	function moveDefault(obj){
		move(obj,0);
	}
	function _move(obj,toX){
		//alert(["_move",toX]);
		var str = String($(obj).css("left"));
		if(!str)clearInterval(obj.intervalID);
		var x = Number(str.substr(0,str.length-2));
		var toLeft=toX<x;
		if(toLeft){
			if(x-2<=toX){
				$(obj).css("left",toX+"px")
				clearInterval(obj.intervalID);
			}else{
				$(obj).css("left",(x-2)+"px")
			}
		}else{
			if(x+8>=toX){
				$(obj).css("left",toX+"px")
				clearInterval(obj.intervalID);
			}else{
				$(obj).css("left",(x+16)+"px")
			}
		}
		
	}

});


/******************************
 changeTab
*********************************/


$(function(){
	$("#newInfo #mobile").hide();
	$("#newInfo #consumer .mbBtn a").click(function(){
		$("#newInfo #consumer").hide();
		$("#newInfo #mobile").show();
	});
	$("#newInfo #mobile .csBtn a").click(function(){
		$("#newInfo #consumer").show();
		$("#newInfo #mobile").hide();
	});
});

/********************************
 * nicoChannel
 * IE6、IE7、FF3、Safari3、Chrome4、Opera9で検証済
 **************************/


$(function(){
//jQuery.event.add(window, "load", function(){
	var feedURL="http://ch.nicovideo.jp/video/ch217?rss=2.0";
	//サムネURL（末尾にIDを付加）
	var imgURL="http://tn-skr4.smilevideo.jp/smile?i=";
	
	var listWrap=$("#nicoChannel .nicoList .wrap");
	var movieWrap=$("#nicoChannel .nicoMovie .movieBox");
	
	//１ページの表示件数
	var displayAmount=3;
	//読み込むフィードのエントリ数
	var entoryAmount=20;
	var pageAmount=1;
	var currentPage=1;
	
	//エントリをオブジェクトの配列で格納
	var entries;
	
    google.setOnLoadCallback(loadRss);
	init();
	
	function init(){
		$("#nicoChannel .nicoMovie .closeBtn a").click(function(){
			showList(currentPage)
		});	
	}
	function showLoading(){
		listWrap.html('<p class="loading">Loading...</p>');
	}
	function loadRss(){
		showLoading();
	  	var feed = new google.feeds.Feed(feedURL);
		feed.setNumEntries(entoryAmount);
		pageAmount=Math.ceil(entoryAmount/displayAmount)
      	feed.load(function(result) {
			//console.log("onLoad");
	        if (!result.error) {
				entries=result.feed.entries;
				setPager();
				if(nicoMovieId && nicoMovieId!="0"){
					showMovie(nicoMovieId);
				}else{
					showList(1);
				}
	        }else{
				listWrap.text("Load Error");
			}
		});
	}
	function setPager(){
		
		//console.log("setPager",pageAmount)
		var tag="<ul>"
		for(var i=1; i< pageAmount+1; i++){
			tag+='<li class="no0'+i+'"><a href="javascript:void(0);" title="'+i+'">'+i+'</a></li>';
			
		}
		tag+="</ul>";
		$("#nicoChannel .nicoPager").html(tag);
		
	}
	function showList(_page){
		//console.log("showList",_page)
		$("#nicoChannel .movieBox").html(' ');
		$("#nicoChannel .nicoMovie").hide(200);
		$("#nicoChannel .nicoList").show();
		listWrap.html("")
		//console.log()
		for(var i=0;i<displayAmount; i++){
			var entry=entries[i+displayAmount*(_page-1)];
			//動画IDを格納
			if (entry) {
				if (!entry.movieID) 
					entry.movieID = entry.link.split("/watch/")[1];
				if (!entry.imgURL) {
					$(entry.content).find("img").each(function(){
						entry.imgURL = this.src;
					});
				}
				if (!entry.dateTxt) {
					var date = new Date(entry.publishedDate);
					entry.dateTxt = (date.getFullYear() + "/" + String(date.getMonth() + 1 + 100).substr(1, 2) + "/" + String(date.getDate() + 100).substr(1, 2));
				}
				var tag='<div class="nicoItem clearfix" id="id'+entry.movieID+'">';
				tag+='<a href="javascript:void(0);" title="'+entry.title+'"><img src="'+entry.imgURL+'" alt="" width="65" height="50" /></a>';
				tag+='<p><span>'+entry.dateTxt+'</span><br />';
				tag+='<a href="javascript:void(0);" title="'+entry.title+'">'+entry.title.substr(0,29)+'…</a></p>';
				tag+='</div>';
			
				listWrap.append(tag);
				listWrap.children("#id"+entry.movieID).children("a").click(function(){
					showMovie($(this).parent().attr("id").split("id")[1])
				});
				listWrap.children("#id"+entry.movieID).children("p").children("a").click(function(){
					showMovie($(this).parent().parent().attr("id").split("id")[1])
				});
			}else{
				var tag='<div class="nicoItem clearfix"><p> </p></div>';
				listWrap.append(tag);
			}
			
			

		}
		currentPage=_page;
		changePager();
		function changePager(){
			//console.log("changePager");
			for(var i=1;i < pageAmount+1; i++){
				if(i==_page){
					$("#nicoChannel .nicoPager .no0"+i).html('<strong>'+i+'</strong>');
				}else{
					$("#nicoChannel .nicoPager .no0"+i).html('<a href="javascript:void(0);" title="'+i+'">'+i+'</a>');
					$("#nicoChannel .nicoPager .no0"+i+" a").click(function(){
						
						showList(Number($(this).parent().attr("class").split("no")[1]))
					});
				}
				//console.log(i,$("#nicoChannel .nicoPager .no0"+i+" a"));
			}
		}
	}
	function changePager(_page){
		movieWrap.text="Loading..."
		
	}
	var _showMovieCnt=0;
	function showMovie(_id){
		//console.log("showMovie",_id)
		var tag='<iframe id="nicomeve" name="nicomeve" src="nicomovie.html?id='+_id+'" width="0" height="0" marginheight="0" marginwidth="0" frameborder="0" scrolling="no" \/>';
		$("#nicoChannel .nicoList").hide();
		$("#nicoChannel .nicoMovie").show();
		//ie6でiframe内が表示されないバグを回避、なぜか時間差付けると表示される
		setTimeout(function(){
			$("#nicoChannel #movieBox").html(tag);
		},100);
		_showMovieCnt=0;
		setTimeout(_showMovie,400);
	}
	function _showMovie(){
		_showMovieCnt++;
		//iframe内のタグを取得して表示
		var iframeContent=document.getElementById("nicomeve").contentWindow.document.getElementById("nico");
		//alert(iframeContent.innerHTML);
		
		var movieTag=iframeContent.innerHTML.split("</script>")[2];
		if(!movieTag)movieTag=iframeContent.innerHTML.split("</SCRIPT>")[2];
		//console.log("_showMovie");
		if(movieTag){
			//console.log("movieTag",movieTag);
			var tag="";
			if ($.browser.msie) {
				//for IE
				var list = iframeContent.childNodes;
				for (var i = 0; i < list.length; i++) {
					//alert(i+"\n"+list.item(i).nodeName)
					if (list.item(i).nodeName == "OBJECT" || list.item(i).nodeName == "EMBED" || list.item(i).nodeName == "object" || list.item(i).nodeName == "embed") {
						//OBJECTタグの調査
						//alert([list.item(i).nodeName, list.item(i).childNodes.length, list.item(i).attributes.length]);
						tag = '<object classid="' + list.item(i).getAttribute('classid') + '" width="252" height="200">\n'
						if (list.item(i).childNodes.length) {
							for (var j = 0; j < list.item(i).childNodes.length; j++) {
								//paramタグの調査
								var node = list.item(i).childNodes[j];
								//alert([j, node.nodeName, node.getAttribute("name"), node.getAttribute("value")]);
								if(node.getAttribute('name')=="movie"){
									//キャッシュが残らないように、IEでキャッシュが残るとscaleMode=noScaleになるのを回避
									tag += '<param name="' + node.getAttribute('name') + '" value="' + node.getAttribute('value') + Math.random()+ '" />\n';
								}else{
									tag += '<param name="' + node.getAttribute('name') + '" value="' + node.getAttribute('value') + '" />\n';
								}
							}
						}
						tag+='<param name="scale" value="showall" />';
						tag += '</object>\n';
					}
				}
			}else{
				//その他のブラウザ
				tag=movieTag
			}
			//$("#flash").text(tag);
			$("#nicoChannel #movieBox").html(tag);
			//$("#nicoChannel #movieBox").text($("#nicoChannel #movieBox").html());
			//document.write(tag)
			
			//alert(tag);
			
		}else{
			//console.log("_showMovie ",_showMovieCnt)
			//iframe内のタグを取得できない場合は再帰処理
			if(_showMovieCnt<10)setTimeout(_showMovie,500);
			
		}
	}
})


/********************************
 * flash表示
 **************************/
function onResizeWindow(){
	//console.log("onResizeWindow");
	var minW=960;
	$("#flash").css("width",Math.max($(window).width(),minW)+"px");
}
$(window).resize(function(){onResizeWindow()});

$(function(){
	onResizeWindow();
	//var isSafari=/a/.__proto__=='//';
	var flashvars = {};
	var params = {
		//allowScriptAccess: "always"
		menu: "false"
	};
	var attributes = {
		id: "swfContent",
		name: "swfContent"
	};
	swfobject.embedSWF("swf/top.swf","flashContent","100%","415","9.0.0","common/js/swfobject/expressinstall.swf",flashvars,params, attributes);

});

