// **********************************
// poem_Script v5.01 free software
// ÇáÑÌÇÁ ÚÏã ÅÒÇáÉ æÕÝ ÇáÍÞæÞ
// Please don't remove the copyright note
// åÐÇ ÇáÇÓßÑíÈÊ ÈæÇÓØÉ Úáæí ÍÓíä ÚãÑ ÈÇÚÞíá
// By Alawi Husain Omar Ba Aqeel, http://www.rayaheen.net
// **********************************

var poem_OP = (navigator.userAgent.indexOf('Opera') != -1) ? true : false;
var poem_IE = (navigator.userAgent.indexOf('MSIE') != -1 && !poem_OP) ? true : false;
var poem_GK = (navigator.userAgent.indexOf('Gecko') != -1) ? true : false;
var poem_URL = window.location.href.substr(0, window.location.href.lastIndexOf("/") + 1);

document.write("<pre id=poemMeasure style=\"visibility:hidden;position:absolute\"></pre>");

function poem_getValue(t, p) {
	var r = new RegExp("([ \']|^)" + t + "[ ]*=[ ]*\'[^\']*\'", "i");
	var m = p.match(r);
	if (!m) {
	  r = new RegExp("([ \']|^)" + t + "[ ]*=[ ]*[^\' ]*([ ]|$)", "i");
	  m = p.match(r);
	  if (!m)
		return "";
	}
	m = m[0];
	var v = "";
	while (m!=v) {
		v = m;
		m = m.replace(/[\'\";]/g, "");
		m = m.replace(/&#/g, "&^#");
	}
	m = m.replace(/^[ ]/, "");
	m = m.replace(/[ ]$/, "");
	var i = m.search("=");
	return m.substring(i + 1).replace(/[\(\)\&\[\]\;]/g, "");
}

function poem_toNumber(s) {
	s = parseInt(s);
	if (isNaN(s))
		s = 0;
	return s;
}

function poem_setArg(arg) {
	var par;
	var FontSizeConst = new Array(8, 10, 12, 14, 16, 18, 20);
	par = poem_getValue("font", arg) + ",,,,,";  //comas = number of arrays - 1
	par = par.split(",");
	poem_FontName = par[0];
	par[1] = poem_toNumber(par[1]);
	if (par[1]>= 1 && par[1] <= 7)
		par[1] = FontSizeConst[par[1]-1] ;
	poem_FontSize = par[1] + "pt";
	poem_FontColor = par[2];
	poem_FontBold = (poem_toNumber(par[3])?"bold":"normal");
	poem_FontItalic = (poem_toNumber(par[4])?"italic":"normal");
	poem_FontUnderline = (poem_toNumber(par[5])?"underline":"none");
	par = poem_getValue("border", arg) + ",,,";
	par = par.split(",");
	poem_BorderStyle = par[0];
	poem_BorderSize = poem_toNumber(par[1]);
	poem_BorderColor = par[2];
	if (par[3]=="" || par[3]!=0)
		poem_Shadow = (poem_BorderStyle!="none" && poem_BorderStyle!="");
	else
		poem_Shadow = 0;
	par = poem_getValue("shadow", arg);
	par = poem_toNumber(par);
	poem_Shadow = poem_Shadow && par;
	par = poem_getValue("num", arg) + ",,";
	par = par.split(",");
	poem_Serial = poem_toNumber(par[0]);
	poem_SerialColor = par[1];
	poem_SerialSize = poem_toNumber(par[2]);
	par = poem_getValue("type", arg);
	par = poem_toNumber(par);
	if (par<1 || par>3)
		par = 0;
	poem_Type = par;
	par = poem_getValue("brimage", arg);
	poem_BreakImage = par;
	if (poem_BreakImage) {
		if (poem_BreakImage.search(/^http:\/\//i)==-1)
			poem_BreakImage = poem_URL + poem_BreakImage;
		poem_BreakImage = "<img border=0 src=\"" + poem_BreakImage + "\">";
	}
	else
		poem_BreakImage = "-";
	par = poem_getValue("bkcolor", arg);
	poem_BackColor = par;
	par = poem_getValue("width", arg);
	poem_BorderWidth = poem_toNumber(par);
	par = poem_getValue("marginlr", arg);
	poem_MarginLR = poem_toNumber(par);
	if (poem_MarginLR<=0)
		poem_MarginLR = 30;
	par = poem_getValue("margintb", arg);
	poem_MarginTB = poem_toNumber(par);
	if (poem_MarginTB<=0)
		poem_MarginTB = 30;		
	par = poem_getValue("bkimage", arg);
	poem_BackImage = par;
	if (poem_BackImage.search(/^http:\/\//i)==-1)
		poem_BackImage = poem_URL + poem_BackImage;
}

function doPoem(arg) {
	var poemWidth, poemWidth2, singleHeight, singleWidth, serialWidth;
	var poem_plate, poem_value, w, i, HTML, line, span, serial;
	var poem_PaddingMiddle, poem_PaddingSide;
	var decor_style, font_style, serial_font_style;
	var poem_HTML = "", shadowWidth = 5;
	function charCount(s, c) {
		var t = 0;
		for (var i=0;i<s.length;i++)
			if (s.substr(i,1)==c)
				t++;
		return t;
	}
	function getSerialHTML() {
		if (serialWidth)
			return "<table border=0 cellpadding=0 cellspacing=0 style=\"border-collapse:collapse;margin-left:" + (poemWidth - serialWidth) + ";height:" + singleHeight + ";width:" + serialWidth + "px;position:absolute;" + serial_font_style + "\"><tr><td align=left>" + serial + "&nbsp;&nbsp;&nbsp;</td></tr></table>";
		else
			return "";
	}
	poem_plate = window.document.getElementsByTagName("PRE");
	poem_plate = poem_plate[poem_plate.length - 1];
	poem_value = poem_plate.innerHTML;
	poem_plate.innerHTML = "";
	poem_plate.style.display = "none";
	poem_setArg(arg);
	decor_style = "background-position:top right;border:" + poem_BorderSize + "px " + poem_BorderStyle + " " + poem_BorderColor;
	serial_font_style = "font-family:Arial;font-weight:bold;font-size:" + poem_SerialSize + ";color:" + poem_SerialColor + ";font-weight:" + poem_FontBold;
	font_style = "font-family:" + poem_FontName + ";font-size:" + poem_FontSize + ";color:" + poem_FontColor + ";font-weight:" + poem_FontBold + ";font-style:" + poem_FontItalic + ";text-decoration:" + poem_FontUnderline;
	//change come characters
	poem_value = poem_value.replace(/\n/g, "\r");
	poem_value = poem_value.replace(/[’‘]/g, "");
	poem_value = poem_value.replace(/<@/g, "<");
	poem_value = poem_value.replace(/<br \/>|<br>/gi, "\r");
	//remove all bb code tag
	poem_value = poem_value.replace(/\[[a-z]+[^\]]*\]/gi, "");
	poem_value = poem_value.replace(/\[\/[a-z]+\]/gi, "");
	//allow some html tags
	poem_value = poem_value.replace(/<@/i, "<");
	if (!poem_OP) {
		poem_value = poem_value.replace(/<font[ ]+style=[\"\']background-color:[ ]*([^> ]+)[\"\']>/gi, "<@font style’background-color:$1>");
		poem_value = poem_value.replace(/<font[ ]+style=[\"\']background-color:[ ]*([^ ]+)[\"\'][ ]+color=[ ]*([^> ]+)>/gi, "<@font style’background-color:$1 color’$2>");
		poem_value = poem_value.replace(/<font[ ]+color=[ ]*([^ ]+)[ ]+style=[\"\']background-color:[ ]*([^ ]+)[\"\']>/gi, "<@font style’background-color:$1 color’$2>");
	}
	poem_value = poem_value.replace(/<font[ ]+color=[ ]*([^> ]+)>/gi, "<@font color’$1>");
	poem_value = poem_value.replace(/<\/font>/gi, "<@/font>");
	poem_value = poem_value.replace(/<(s|b|strong|em|i|u|sup|sub|br)>/gi, "<@$1>");
	poem_value = poem_value.replace(/<\/(s|b|strong|em|i|u|sup|sub|br)>/gi, "<@/$1>");
	//remove unused html tags
	poem_value = poem_value.replace(/<[a-z]+[^>]*>/gi, "");
	poem_value = poem_value.replace(/<\/[a-z]+>/gi, "");
	//restore
	poem_value = poem_value.replace(/<@/g, "<");
	poem_value = poem_value.replace(/=/g, "‘")
	poem_value = poem_value.replace(/’/g, "=");
	//remove kasheeda, trim spaces, etc ...
	poem_value = poem_value.replace(/Ü/g, "");
	poem_value = poem_value.replace(/[\t\v]/g, "");
	poem_value = poem_value.replace(/[ ]+/g, " ");
	poem_value = poem_value.replace(/\r\r/g, "\r");
	poem_value = poem_value.replace(/[ ]*‘[ ]*/g, "‘");
	poem_value = poem_value.replace(/[ ]\r/g, "\r");
	poem_value = poem_value.replace(/\r[ ]/g, "\r");
	//start of measurements
	poemMeasure.style.display = "block";
	//set serialWidth
	if (poem_Serial==0)
		serialWidth = 0;
	else {
		poemMeasure.style.fontFamily = "Arial";
		poemMeasure.style.fontSize = poem_SerialSize;
		poemMeasure.style.fontWeight = poem_FontBold;
		w = charCount(poem_value, '\r') + poem_Serial;
		w = w + "";
		w = w.length + 1;
		w = "999999".substr(0, w);
		poemMeasure.innerHTML = "&nbsp;&nbsp;&nbsp;" + w;
		serialWidth = poemMeasure.clientWidth;
	}
	//set singleWidth and singleHeight
	poemMeasure.style.fontFamily = poem_FontName;
	poemMeasure.style.fontSize = poem_FontSize;
	poemMeasure.style.color = poem_FontColor;
	poemMeasure.style.fontWeight = poem_FontBold;
	poemMeasure.style.fontStyle = poem_FontItalic;
	poemMeasure.style.textDecoration = poem_FontUnderline;
	//set singleWdidth
	w = poem_value;
	w = w.replace(/‘/g, " " +"<br>"); // I add one space because I add space to span variable
	w = w.replace(/\r/g, " " + "<br>");
	poemMeasure.innerHTML = w;
	singleWidth = poemMeasure.clientWidth;
	//set singleHeight
	poemMeasure.innerHTML = "&nbsp;";
	singleHeight = poemMeasure.clientHeight;
	//set poemWidth
	function setMeasurementVar() {
		poem_PaddingMiddle = singleWidth / 4;
		poem_PaddingSide = singleWidth / 2;
		if (poem_Type==1)
			poemWidth = singleWidth + poem_PaddingSide + serialWidth
		else if (poem_Type==2)
			poemWidth = 2 * singleWidth + serialWidth;
		else if (poem_Type==3)
			poemWidth = singleWidth + serialWidth;
		else
			poemWidth = 2 * singleWidth + poem_PaddingMiddle + serialWidth;
	}
	setMeasurementVar();
	var poemWidth2 = poem_BorderWidth - 2 * poem_MarginLR - 2 * poem_BorderSize - poem_Shadow * shadowWidth + 3;
	if (poemWidth2>poemWidth) {
		if (poem_Type==1)
			singleWidth = 2 * (poemWidth2 - serialWidth) / 3;
		else if (poem_Type==2)
			singleWidth = (poemWidth2 - serialWidth) / 2;
		else if (poem_Type==3)
			singleWidth = poemWidth2 - serialWidth ;
		else
			singleWidth = 4 * (poemWidth2 - serialWidth) / 9;
		setMeasurementVar();
	}
	else {
		poem_MagrinLR = (poem_BorderWidth - poemWidth) / 2;
		if (poem_MarginLR<0)
			poem_MarginLR = 0;
	}
	poemMeasure.style.display = "none";
	poemMeasure.innerHTML = "";
	//end of measurements
	span = " " + "<img border=0 src=\"space.gif\" height=1px width=\"" + singleWidth + "px\">"; // I add one space to justify will in OPERA browser
	window.document.write("<div align=center dir=rtl>");
	if (poem_Shadow) {
		window.document.write("<table border=0 cellpadding=0 cellspacing=0 bordercolor=#111111 style=\"border-collapse:collapse\"><tr>");
		window.document.write("<td height=100% style=\"padding-top:" + (2 * shadowWidth) + "px\"><div style=\"height:100%;width:" + shadowWidth + "px;background-color:black;font-size:1pt;filter:alpha(opacity=15);-moz-opacity:0.15;opacity:0.15\"></div></td>");
		window.document.write("<td valign=top>");
	}
	window.document.write("<table border=0 bgcolor=\"" + poem_BackColor + "\" background=\"" + poem_BackImage + "\" cellpadding=0 cellspacing=0 style=\"" + decor_style + ";" + font_style + "\">");
	window.document.write("<td style=\"padding-left:" + poem_MarginLR  + "px;padding-right:" + poem_MarginLR  + "px;padding-top:" + poem_MarginTB  + "px;padding-bottom:" + poem_MarginTB  + "px\">");
	serial = poem_Serial - 1;
	poem_value = poem_value.split("\r");
	for (i=0; i<poem_value.length; i++) {
		line = poem_value[i];
		HTML="";
		if (line && line!="-")
			serial++;
		if (line.indexOf("‘")!=-1) {
			line = line.split("‘");
			if (poem_Type==1) {
				HTML += "<div style=\"width:" + poemWidth + "px\">";
				HTML += getSerialHTML();
				HTML += "<div style=\"padding-right:5px;text-align:justify;padding-right:" + serialWidth + "px;padding-left:" + poem_PaddingSide + "px\">" + line[0] + span + "</div>";
				HTML += "<div style=\"padding-right:5px;text-align:justify;padding-right:" + (poem_PaddingSide + serialWidth) + "px\">" + line[1] + span + "</div>";
				HTML += "</div>";
			}
			else if (poem_Type==2) {
				HTML += "<div style=\"width:" + poemWidth + "px\">";
				HTML += getSerialHTML();
				HTML += "<div style=\"padding-right:5px;text-align:justify;padding-right:" + serialWidth + "px;padding-left:" + singleWidth + "px\">" + line[0] + span + "</div>";
				HTML += "<div style=\"padding-right:5px;text-align:justify;padding-right:" + (singleWidth + serialWidth) + "px\">" + line[1] + span + "</div>";
				HTML += "</div>";
			}
			else if (poem_Type==3) {
				HTML += "<div style=\"width:" + poemWidth + "px\">";
				HTML += getSerialHTML();
				HTML += "<div style=\"padding-right:5px;text-align:justify;padding-right:" + serialWidth + "px\">" + line[0] + span + "</div>";
				HTML += "<div style=\"padding-right:5px;text-align:justify;padding-right:" + serialWidth + "px\">" + line[1] + span + "</div>";
				HTML += "</div>";
			}
			else {
				HTML += "<div style=\"text-align:right;width:" + poemWidth + "px\">";
				HTML += getSerialHTML();
				HTML += "<div style=\"padding-right:5px;text-align:justify;margin-left:" + (singleWidth + poem_PaddingMiddle) + "px;position:absolute;margin-right:" + serialWidth + "px;width:" + singleWidth + "px\">" + line[0] + span + "</div>";
				HTML += "<div style=\"padding-right:5px;text-align:justify;padding-right:" + (poem_PaddingMiddle + singleWidth + serialWidth) + "px\">" + line[1] + span + "</div>";
				HTML += "</div>";
			}
		}
		else {
			if (line=="-")
				HTML += "<div align=center style=\"padding-right:" + serialWidth + "px\">" + poem_BreakImage + "</div>";
			else if (line) {
				HTML += getSerialHTML();
				HTML += "<div align=center style=\"padding-right:" + serialWidth + "px\"><div style=\"text-align:justify;width:" + singleWidth + "px\">" + line + span + "</div></div>"
			}
			else HTML += "<br>";
		}
		window.document.write(HTML);
	}
	window.document.write("</td></tr></table>");
	if (poem_Shadow) {
	  window.document.write("</td></tr>");
  	window.document.write("<tr><td rowspan=3 colspan=2 style=\"padding-left:" + (2 * shadowWidth) + "px\"><div style=\"width:100%;height:" + shadowWidth + "px;background-color:black;font-size:1pt;filter:alpha(opacity=15);-moz-opacity:0.15;opacity:0.15;font-size:1pt\"></div></td></tr>");
		window.document.write("</table>");
	}
	poem_plate.innerHTML = "";
}
