/* This script adds an "unwatch" link to each entry in your watchlist, using the new
* ajax watchlist thing.
*/

var wlUnwatch = {
	supported: true,
	onLoad: function () {
		var i, entries, links, page, a, span, div;
		
		entries = document.getElementsByClassName( 'mw-changeslist-line-inner' );
		for ( i = entries.length-1; i >= 0; i-- ) {
			page = entries.getAttribute( 'data-target-page' );
			if ( page === null ) {
				continue;
			}
			links = entries.getElementsByClassName( 'mw-changeslist-links' );
			if ( !links ) {
				continue;
			}
			if ( links.nodeName === 'A' ) {
				// Log entries have mw-changeslist-links on the one link. Put it inside a div instead.
				div = document.createElement( 'DIV' );
				div.className = 'mw-changeslist-links';
				links.parentNode.insertBefore( div, links );
				span = document.createElement( 'SPAN' );
				div.appendChild( span );
				span.appendChild( links );
				links.classList.remove( 'mw-changeslist-links' );
				links = div;
			}

			a = document.createElement( 'A' );
			a.href = mw.util.wikiScript() + '?action=unwatch&title=' + encodeURIComponent( page );
			a.title = 'Unwatch ' + page;
			a.unwatchInProgress = false;
			a.onclick = wlUnwatch.onClick;
			a.appendChild( document.createTextNode( 'unw' ) );

			span = document.createElement( 'SPAN' );
			span.appendChild( a );
			links.appendChild( span );
		}
	},

	onClick: function () {
		if ( !wlUnwatch.supported ) {
			wlUnwatch.supported = false;
			return true;
		}
		var link = this;
		if ( link.unwatchInProgress ) {
			return false;
		}
		link.unwatchInProgress = true;
		link.style.color = 'red';
		var timeout = setTimeout( function() {
			timeout = null;
			link.unwatchInProgress = false;
			link.style.color = '';
		}, 10000 );
		var api = new mw.Api();
		api.postWithToken( 'watch', {
			action: 'watch',
			unwatch: 1,
			titles: decodeURIComponent( link.href.match( /&title=(.+)/ ).replace( /_/g,' ' ) )
		} ).done( function( data ) {
			if ( timeout ) {
				clearTimeout( timeout );
			}
			timeout = null;
			link.unwatchInProgress = false;
			link.style.color = '';
			if ( data.watch.unwatched !== undefined ) {
				var li;
				for ( li = link; li && li.nodeName !== 'LI'; li = li.parentNode ); // Empty loop
				if ( li ) {
					var classes = li.className.split(' ');
					var i, len, cl, matches, ns, assocCl;
					for ( i = 0, len = classes.length; i < len; i++ ) {
						cl = classes;
						matches = cl.match( /^watchlist-(\d+)-(.*)/ );
						if ( matches ) {
							break;
						}
					}
					if ( matches ) {
						ns = Number( matches );
						ns ^= 1; // use bitwise XOR to get the associated namespace number
						assocCl = 'watchlist-' + ns + '-' + matches;
						$( '.' + cl ).remove();
						$( '.' + assocCl ).remove();
					} else {
						li.parentNode.removeChild( li );
					}
				}
			}
		} ).fail( function ( error ) {
			if ( timeout ) {
				clearTimeout( timeout );
			}
			timeout = null;
			link.unwatchInProgress = false;
			link.style.color = '';
		} );
		return false;
	}
};

if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Watchlist' && mw.config.get( 'wgAction' ) === 'view' ) {
	mw.loader.using( , function() {
		$( document ).ready( wlUnwatch.onLoad );
	} );
}