// pr_init [javascript]
function pr_init() {
	if( document.getElementById( 'pr_container' ) ) {
		return;
	}

	if( document.URL.indexOf( 'action=protect' ) > 0 || document.URL.indexOf( 'action=unprotect' ) > 0 ) {
		return;
	}
	if( document.URL.indexOf( 'action=delete' ) > 0 || document.URL.indexOf( 'action=undelete' ) > 0 ) {
		return;
	}
	if( document.URL.indexOf( 'action=watch' ) > 0 || document.URL.indexOf( 'action=unwatch' ) > 0 ) {
		return;
	}
	if( document.URL.indexOf( 'action=history' ) > 0 ) {
		return;
	}

	/* check if external URL is provided */
	if( !self.proofreadPageThumbURL ) {
		var text = document.getElementById( 'wpTextbox1' );
		if ( text ) {
			var proofreadPageIsEdit = true;
			re = /<span class="hiddenStructure" id="pageURL">\[http:\/\/(.*?)\]<\/span>/;
			m = re.exec( text.value );
			if( m ) {
				self.proofreadPageExternalURL = 'http://' + m[1];
			}
		} else {
			var proofreadPageIsEdit = false;
			text = document.getElementById( 'bodyContent' );
			try {
				var a = document.getElementById( 'pageURL' );
				var b = a.firstChild;
				self.proofreadPageExternalURL = b.getAttribute( 'href' );
			} catch( err ) {
			};
		}
		// set to dummy values, not used
		self.proofreadPageWidth = 400;
		self.proofreadPageHeight = 400;
	}

	if( !self.proofreadPageThumbURL ) {
		return;
	}

	if( self.proofreadpage_setup ) {
		proofreadpage_setup(
			proofreadPageWidth,
			proofreadPageHeight,
			proofreadPageIsEdit
		);
	} else {
		pr_setup();
	}

	// add CSS classes to the container div
	var c = document.getElementById( 'pagequality' );
	if( c ) {
		c = c.nextSibling;
		if( c.className == 'pagetext' ) {
			c.className += ' ' + self.proofreadPageCss;
		}
	}
}

$(document).ready( pr_init );
$(document).ready( pr_init_tabs );
$(document).ready( pr_initzoom );


/* Quality buttons */
self.pr_add_quality = function( form, value ) {
	self.proofreadpage_quality = value;
	self.proofreadpage_username = proofreadPageUserName;
	var text = '';
	switch( value ) {
		case 0:
			text = mediaWiki.msg( 'proofreadpage_quality0_category' );
			break;
		case 1:
			text = mediaWiki.msg( 'proofreadpage_quality1_category' );
			break;
		case 2:
			text = mediaWiki.msg( 'proofreadpage_quality2_category' );
			break;
		case 3:
			text = mediaWiki.msg( 'proofreadpage_quality3_category' );
			break;
		case 4:
			text = mediaWiki.msg( 'proofreadpage_quality4_category' );
			break;
	}
	form.elements['wpSummary'].value = '/* ' + text + ' */ ';
	form.elements['wpProofreader'].value = self.proofreadpage_username;
};

function pr_add_quality_buttons() {
	var ig = document.getElementById( 'wpWatchthis' );
	if( !ig ) {
		ig = document.getElementById( 'wpSummary' );
	}
	if( !ig ) {
		return;
	}
	var f = document.createElement( 'span' );
	ig.parentNode.insertBefore( f, ig.nextSibling.nextSibling.nextSibling );

	if( !proofreadPageAddButtons ) {
		f.innerHTML =
			' <input type="hidden" name="wpProofreader" value="' + self.proofreadpage_username + '">' +
			'<input type="hidden" name="quality" value=' + self.proofreadpage_quality + ' >';
		return;
	}

	f.innerHTML =
' <input type="hidden" name="wpProofreader" value="' + self.proofreadpage_username + '">'
+'<span class="quality0"> <input type="radio" name="quality" value=0 onclick="pr_add_quality(this.form,0)" tabindex=4> </span>'
+'<span class="quality2"> <input type="radio" name="quality" value=2 onclick="pr_add_quality(this.form,2)" tabindex=4> </span>'
+'<span class="quality1"> <input type="radio" name="quality" value=1 onclick="pr_add_quality(this.form,1)" tabindex=4> </span>'
+'<span class="quality3"> <input type="radio" name="quality" value=3 onclick="pr_add_quality(this.form,3)" tabindex=4> </span>'
+'<span class="quality4"> <input type="radio" name="quality" value=4 onclick="pr_add_quality(this.form,4)" tabindex=4> </span>';
	f.innerHTML = f.innerHTML + '&nbsp;' + escapeQuotesHTML( mediaWiki.msg( 'proofreadpage_page_status' ) );

	if( !( ( self.proofreadpage_quality == 4 ) || ( ( self.proofreadpage_quality == 3 ) && ( self.proofreadpage_username != proofreadPageUserName ) ) ) ) {
		document.editform.quality[4].parentNode.style.cssText = 'display:none';
		document.editform.quality[4].disabled = true;
	}
	switch( self.proofreadpage_quality ) {
		case 4:
			document.editform.quality[4].checked = true;
			break;
		case 3:
			document.editform.quality[3].checked = true;
			break;
		case 1:
			document.editform.quality[2].checked = true;
			break;
		case 2:
			document.editform.quality[1].checked = true;
			break;
		case 0:
			document.editform.quality[0].checked = true;
			break;
	}