smartphone = ((navigator.appVersion).indexOf("Mobile") > 0);

function switchFixed(targetClass,fixedClass){
	var msie6 = $.browser == 'msie' && $.browser.version < 7;
	if (!msie6) {
		var target = $('.'+targetClass);
		var top = $(target).offset().top - parseFloat($(target).css('margin-top').replace(/auto/, 0));

		$(window).scroll(function (event) {
		var y = $(this).scrollTop();
			if (y >= top-200) {
				$(target).addClass(fixedClass);
			} else {
				$(target).removeClass(fixedClass);
			}
		});
	}
}

function newsticker()
{
	var ul = $('#newsticker ul');
	var len = $('li',ul).length;
	var n = 1;
	$('li:first',ul).fadeIn('slow');
	var interval = setInterval(function(){
		$('li:nth-of-type(' + n + ')',ul).hide();
		if(n==len){
			n = 1;
		}else{
			n++;
		}
		$('li:nth-of-type(' + n + ')',ul).filter(':hidden').fadeIn('slow');
	},5500);
}

$(function(){

	if(smartphone)
	{
		$('body').prepend('<div id="for-smartphone"><a href="http://matome-blog.jp/">スマートフォン用ページに切替</a></div>').css('background','transparent');
		$('#for-smartphone a').click(function(){
			$.cookie('pc_version',0,{expires:-1});
		});
	}
		
	//はてなブックマーク取得、リツイート数の取得
	$('#content tr').each(function(){
		if($('h3 a',this).size() > 0){
			var thisDom = $(this);
			var eUrl = $('h3 a',this).attr('href');
			$.getJSON(
				'http://api.b.st-hatena.com/entry.count?callback=?',
				{
					url : eUrl
				},
				function(data){
					if(data>1){
						$("td.hatena-column",thisDom).append("<a href='http://b.hatena.ne.jp/entry/"+eUrl+"' target='_blank'>"+data+" users</a>");
					}
				}
			);
			$.getJSON(
				'http://urls.api.twitter.com/1/urls/count.json?callback=?',
				{
					url : eUrl
				},
				function(data){
					if(data.count>=1){
						$("td.twitter-column",thisDom).append("<a href=\"http://topsy.com/tb/"+eUrl.substring(7)+"\" target='_blank'>"+data.count+" tweets</a>");
					}
				}
			);
		}
	});

	//外部リンクにtarget="_blank"
	var inLink = $('#logo a').attr('href');
	$("a").click(function(){
		if($(this).attr('href').indexOf(inLink)){
			this.target="_blank";
		}
	});
	
	//表示形式変更ボタン
	$('#style select').removeAttr('disabled');
	if($.cookie('ts')==1) {
		$('#body table .description').show();
		$('#style select option').removeAttr('selected');
		$('#style select option[value="1"]').attr('selected','selected');
	} 
	$('#style select').change(function(){
		if($('option:selected',this).val()==0){
			$('#body table .description').hide();
			$.cookie("ts",0,{ path:'/',expires:30 });
		}else {
			$('#body table .description').show();
			$.cookie("ts",1,{ path:'/',expires:30 }); 
		}
	});
	
	//日付から絞り込み
	$('a.link-date').click(
		function(){
			if($(this).text()=='日時指定'){
				$('#date-box').show();
				$('a.link-date').text('閉じる');
			}else{
				$('#date-box').hide();
				$('a.link-date').text('日時指定');
			}
			return false;
		}
	);
	
	//ニュースティッカー
	newsticker();
	
	//現在の閲覧者数
	$.get("http://matome-blog.jp/scounter/scounter.txt", function(data){
		if(data.match(/[^0-9]+/)===null){
			$('#scounter').text(data);
		}
	});
	
	if($('#socialbox').size() > 0){
		switchFixed('socialbox','socialbox-fixed');
	}
});

