// ==UserScript==
// @name        Hatena Antenna Open Unvisited
// @description open unvisited links
// @namespace   http://bmky.net/
// @include     http://a.hatena.ne.jp/*
// ==/UserScript==

(function(){
	var openlimit = 10; // 一度に開くページ数
	
	if( gid( "hatena-antenna-normal" ) ) {
		// リダイレクトページを使わない
		for( var i = 0, len = document.links.length; i < len; i++ ) {
			var link = document.links[i];
			
			if( link.href.match( /\d{14}$/ ) ) {
				link.href = link.href.replace( /^[^?]*\?/, "" )
									.replace( /(\d{14})$/, "?$1" );
			}
		}
		
		var h1 = gtg( "h1" )[0];
		
		// デザイン上h1が無いページは作成
		if( h1 == undefined ) {
			var h1 = document.createElement( "h1" );
			h1.innerHTML = document.title;
			gid( "hatena-antenna-normal" ).insertBefore( h1, gid( "hatena-antenna-normal" ).firstChild );
		}
		
		var btn_per = document.createElement( "input" );
		h1.appendChild( btn_per );
		btn_per.type = "button";
		btn_per.value = openlimit + "個ずつ未読ページを開く";
		btn_per.style.marginLeft = "1em";
		btn_per.addEventListener( "click", function( ) {
			open_links( openlimit );
		}, false );
		
		var btn_all = document.createElement( "input" );
		h1.appendChild( btn_all );
		btn_all.type = "button";
		btn_all.value = "全ての未読ページを開く";
		btn_all.style.marginLeft = "1em";
		btn_all.addEventListener( "click", function( ) {
			open_links( 999 );
		}, false );
		
		var btn_test = document.createElement( "input" );
		//h1.appendChild( btn_test );
		btn_test.type = "button";
		btn_test.value = "マーク";
		btn_test.style.marginLeft = "1em";
		btn_test.addEventListener( "click", function( ) {
			mark( 999 );
		}, false );
	}
	
	function open_links( limit ) {
		var links = new Object;
		var sites = new Array;
		var moddates = gtg( "span" );
		
		for( var i = 0, len = moddates.length; i < len; i++ ) {
			if( moddates[i].className == "modified" ) {
				sites.push( moddates[i].nextSibling.nextSibling );
			}
		}
		
		addstyle( 'span.modified + a:visited { display : inline-block !important; height : 32px !important; overflow : hidden !important; }' );
		
		for( var i = 0, len = sites.length; i < len; i++ ) {
			var a = sites[i];
			var name = a.innerHTML;
			var url = a.href;
			
			var siteurl = a.href.replace( /\?\d{14}$/, "" );
			var htndate = parseInt( a.href.replace( /^.*?\?(\d{14})$/, "$1" ) );
			var lastvst = getCookie( siteurl, htndate );
			
			if( htndate <= parseInt( lastvst ) ) continue;
			
			if( a.clientHeight >= 32 ) continue;
			if( links[ name ] != null ) continue;
			links[ name ] = url;
			
			setCookie( siteurl, htndate, 90 );
		}
		
		addstyle( 'span.modified + a:visited { display : inline !important; }' );
		
		var cnt = 0;
		
		for( var i in links ) {
			if( cnt++ < limit ) window.open( links[i] );
			//opera.postError( links[i] );
		}
		
		links = new Object;
	}
	
	function mark( limit ) {
		var links = new Object;
		var sites = new Array;
		var moddates = gtg( "span" );
		
		for( var i = 0, len = moddates.length; i < len; i++ ) {
			if( moddates[i].className == "modified" ) {
				sites.push( moddates[i].nextSibling.nextSibling );
			}
		}
		
		addstyle( 'span.modified + a:visited { display : inline-block !important; height : 32px !important; overflow : hidden !important; }' );
		
		for( var i = 0, len = sites.length; i < len; i++ ) {
			var a = sites[i];
			var name = a.innerHTML;
			var url = a.href;
			
			var siteurl = a.href.replace( /\?\d{14}$/, "" );
			var htndate = parseInt( a.href.replace( /^.*?\?(\d{14})$/, "$1" ) );
			var lastvst = getCookie( siteurl, htndate );
			
			if( htndate <= parseInt( lastvst ) ) {
				a.style.border = "2px solid #000";
			}
			
			if( a.clientHeight >= 32 ) continue;
			if( links[ name ] != null ) continue;
			a.style.background = "yellow";
			links[ name ] = url;
		}
		
		addstyle( 'span.modified + a:visited { display : inline !important; }' );
	}
	
	function gid( id ) { return document.getElementById( id ) }
	function gtg( tag ) { return document.getElementsByTagName( tag ) }
	
	function addstyle( csstext ) {
		var link = document.createElement( "link" );
		link.rel = "stylesheet";
		link.href = 'data:text/css;abbr, ' + csstext;
		link.type = "text/css";
		gtg( "head" )[0].appendChild( link );
	}
	
	function getCookie( name ) {
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ';', len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	}
	
	function setCookie( name, value, expires, path, domain, secure ) {
		var today = new Date();
		today.setTime( today.getTime() );
		if ( expires ) {
			expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = name+'='+escape( value ) +
			( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
			( ( path ) ? ';path=' + path : '' ) +
			( ( domain ) ? ';domain=' + domain : '' ) +
			( ( secure ) ? ';secure' : '' );
	}
	
	function deleteCookie( name, path, domain ) {
		if ( getCookie( name ) ) document.cookie = name + '=' +
				( ( path ) ? ';path=' + path : '') +
				( ( domain ) ? ';domain=' + domain : '' ) +
				';expires=Thu, 01-Jan-1970 00:00:01 GMT';
	}
})();

