
$(function(){

	if (window.top !== window.self) {
		document.write = "";
		window.top.location = window.self.location;
		return;
	}
	
	var ckYoutube = 'http://www.youtube.com/watch?v=';
	var ckUstream = 'http://www.ustream.tv/channel/';
	if( top_url.indexOf(ckYoutube) === 0 )
	{
		$("#eegFrame").attr("src",top_url.replace(ckYoutube,'http://www.youtube.com/embed/'));
	}
	else if( top_url.indexOf(ckUstream) === 0 )
	{
		var id = top_url.replace(ckUstream,'').replace(/[?#].*/,'').replace(/\/.*/,'');
		var url = 'http://api.ustream.tv/json/channel/'+id+'/getinfo?key=26878187353C03481A4AAE156FE04833&callback=?';
		$.getJSON(url,function(res){
			if( res != undefined && res.embedTagSourceUrl != undefined && res.embedTagSourceUrl != '' )
			{
				if( res.status != 'offline' )
				{
					$("#eegFrame").attr("src",res.embedTagSourceUrl);
				}
				else
				{
					var url2 = 'http://api.ustream.tv/json/user/'+res.user.id+'/listAllVideos?key=26878187353C03481A4AAE156FE04833&callback=?';
					$.getJSON(url2,function(res2){
						if( res2 != null && res2.length > 0 )
						{
							$("#eegFrame").attr("src",res2[(res2.length-1)].url);
						}
						else
						{
							$("#eegFrame").attr("src",res.embedTagSourceUrl);
						}
					});
				}
			}
			else
			{
				location.href = top_url;
			}
		});
	}
	else
	{
		$("#eegFrame").attr("src",top_url);
	}

	if( $.cookie('lastMenu') > 1 )
	{
		if( document.getElementById('topmenu'+$.cookie('lastMenu')) != undefined )
		{
			ToggleSub($.cookie('lastMenu'));
		}
		else if( $.cookie('lastMenu') == '8' )
		{
			ToggleSub(2);
		}
		else
		{
			ToggleSub(0);
		}
	}
	else if( $.cookie('lastMenu') == 1 )
	{
		ToggleSub(0);
	}
	else if( $.cookie('lastMenu') == 0 )
	{
		ToggleSub(0);
	}
	else
	{
		ToggleSub(2);
	}
	
	if( $.cookie('lastMenu') != 2 )
	{
		timer = setTimeout(onReloadComment, 1000);
	}
	setTimeout(commentArrayMng, 1000);

	$("#twtxt").charCount();
	$("#twtxt2").charCount({counterElement:'div',allowed:70});
	$("#favorite").click(function(){
		if( member == 1 )
		{
			var reqUrl = 'json.php?mode=favorites/create&twstatusid='+twstatusid;
			$.getJSON(reqUrl, function(json) {
				alert('お気に入りに追加しました');
			});
		}
	});
	$("#retweet2").click(function(){
		if( member == 1 )
		{
			onRetweet();
		}
		else
		{
			window.location.href = "http://eeg.jp/login.html?directLogin=1";
		}
	});
	updateEEG();
	$(window).resize(function(){
		SetWndSize(bar_w,bar_h);
	});
	
	$("#topmenu2").click(function(){
		ToggleSub(2);
	});

	$("#zapping").click(function(){
		var random = Math.floor( Math.random() * 99 ) + 1;
		var url = $("#zap"+random).attr('href');
		if( url != undefined && url != '' )
		{
			if( -1 === url.indexOf('youtube.com') &&
					-1 === url.indexOf('ustream.tv') &&
					-1 === url.indexOf('twitter.com') 
			)
			{
				window.location.href = url;
			}
		}
	});

	$("#pf_go").click(function(){
		location.href = 'http://pf.eeg.jp/'+$("#pf_twid").val();
	});
	$(".sbt").click(function(){
		$(this).toggleClass("sbt_active");
		Search('resultsDiv6','sbt_active');
	});
	$('.optionWnd2').scroll(scrollToEnd);
});

