/*
// 2005-12-04
// Copyright (c) Art. Lebedev | http://www.artlebedev.ru/
// Author - Vladimir Tokmakov
*/

var ttOptions = new Array();
ttOptions.sLeft_white = window.attachEvent ? '' : ' ';
ttOptions.sLeft_white_inline = '';
ttOptions.sLeft_hangs = '\\' + String.fromCharCode( 8222 ) + "|'" + '|"|\\x84|\\x91|\\x93|\\xab|\\(|\\[';
ttOptions.pLeft_replace = new RegExp( '(^|[ \\n\\t])((' + ttOptions.sLeft_hangs + ')+)(\\S|$)', 'g' );
ttOptions.pLeft_match = new RegExp( '^(' + ttOptions.sLeft_hangs + ')' ); 
ttOptions.pLeft_match_html = new RegExp( '(' + ttOptions.sLeft_hangs + '|&(quot|(l[ad]|bd|ls|sb)quo|#(34|132|14[57]|171|82(1[68]|2[02])));)' ); 

function ttParagraph_composer( aeText, bRecursion ){
	if( !aeText.length ){ aeText = new Array( aeText ); }
	if( !bRecursion ){ ttOptions.dtStart = new Date(); }
	for( var i = 0, ii = 0, j, jj, bInline ; i < aeText.length ; i++ ){
		if( bRecursion || ( aeText[i].innerHTML && aeText[i].innerHTML.match( ttOptions.pLeft_match_html ) ) ){
			bInline = aeText[i].tagName && aeText[i].tagName.toLowerCase().match( /^(a(bbr|cronym)?|b(ig)?|c(ite|enter|code)|d(t|el)|em|font|i(ns)?|label|nobr|q|s(pan|trong|trike|ub|up)?|tt|u|var)$/ ) ? true : false;
			if( aeText[i].childNodes ){
				for( j = 0 ; j < aeText[i].childNodes.length ; j++ ){
					if( aeText[i].childNodes[j].nodeType == 1 ){
						j += ttParagraph_composer( aeText[i].childNodes[j], true );
					}else if( aeText[i].childNodes[j].nodeType == 3 ){
						jj = ttOptical_margin_alignment( aeText[i].childNodes[j], bInline );
						ii += jj;
						j += jj;
					}
				}
			}
			if( !bRecursion && ( new Date() - ttOptions.dtStart ) > 20000 ){ return ii; }
		}
	}
	return ii;
}
function ttOptical_margin_alignment( eText, bInline ){
	var iNew_element_count = 0;
	var sNode_value = eText.nodeValue.replace( ttOptions.pLeft_replace, '$1_vlalek_$2_vlalek_$4' ).replace( /^(_vlalek_)+/g, '' );
	asText = sNode_value.split( '_vlalek_' );
	if( asText.length > 1 ){
		for( var i = 0, eSPAN1, eSPAN2, eBefore ; i < asText.length ; i++ ){
			if( asText[i].match( ttOptions.pLeft_match ) ){
				if( i || bInline ){
					eBefore = bInline && !i ? eText.parentNode : eText;
					if( eBefore.previousSibling && eBefore.previousSibling.nodeValue ){
						eBefore.previousSibling.nodeValue = eBefore.previousSibling.nodeValue.replace( /\s+$/, '' );
					}
					var eSPAN1 = document.createElement( 'span' );
					eSPAN1.appendChild( document.createTextNode( bInline ? ttOptions.sLeft_white_inline : ttOptions.sLeft_white ) );
					eBefore.parentNode.insertBefore( eSPAN1, eBefore );
					iNew_element_count++;
					eBefore.parentNode.insertBefore( document.createTextNode( ' ' ), eBefore );
					iNew_element_count++;
				}

				eSPAN2 = document.createElement( 'span' );
				eSPAN2.appendChild( document.createTextNode( asText[i] ) );
				eText.parentNode.insertBefore( eSPAN2, eText );
				iNew_element_count++;

				iWidth = eSPAN2.offsetWidth * 2.4 / eSPAN2.offsetHeight;
				if( eSPAN1 ){
					eSPAN1.style.marginRight = iWidth + 'ex';
					if( bInline ){
						eSPAN1.style.paddingRight = 1;
					}
				}
				eSPAN2.style.marginLeft = '-' + iWidth + 'ex';
			}else{
				eText.parentNode.insertBefore( document.createTextNode( asText[i] ), eText );
				iNew_element_count++;
			}

		}
		eText.parentNode.removeChild( eText );
	}
	return iNew_element_count;
}

