function sq_getIEVersion()
{
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    if (MSIEOffset == -1) {
        return 0;
    } else {
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}

function sq_hangP(container,symbols,tag,style)
{	if(typeof(container)=="string")container=document.getElementById(container);
	if(arguments.length<3)tag='span';
	if(arguments.length<4)style="";else style=" "+style;
	var uid=Math.round(Math.random()*10000);
	tag=tag.toLowerCase();
	var symbols=symbols.split(",");
	var ie=sq_getIEVersion();
	ie=(ie>0&&ie<8); 	var ps=container.getElementsByTagName('p');
 	var txt,c,pc,i,j,str,str2,b,ggc,w,el,p_el,ch,is_in_a,op_childs,op_childs_,len,is_bad;
    var gc=0; var prevtag,prevtext; var in_a=false; var in_tag=false;
 	for(i=0;i<ps.length;i++)
 	{ 		txt=ps[i].innerHTML;
 		ggc=gc;
 		prevtag="";
 		prevtext=-1;
 		c=0;
 		while(c<txt.length)
 		{ 			while(txt.charAt(c)=='<')
 			{ 				pc=c;
 				prevtag="!"+txt.charAt(c+1);
 				c+=2;
 				while(c<txt.length&&(ch=txt.charAt(c))!=' '&&ch!='>'&&ch!='/')
 				{ 					prevtag+=ch; 					c++;
 				}
 				while(c<txt.length&&txt.charAt(c)!='>')c++;
 				c++;
 				prevtag=prevtag.toLowerCase();
 				if(c>=txt.length)break;
 			}
 			while(c<txt.length&&((ch=txt.charCodeAt(c))==32||ch==10||ch==13))
 			{ 				prevtext=c; 				c++;
 			}
 			if(c>=txt.length)break;
 			if(txt.charAt(c)=='<')continue;
 			b=false;
 			for(j=0;j<symbols.length;j++)if(txt.substr(c,symbols[j].length)==symbols[j])
 			{ 				if(prevtext==-1||prevtag=="!br")
 				{	 				b=true;	 				str='<'+tag+' style="position: relative; zoom: 1;'+style+'" id="sq_hangP'+uid+'_'+gc+'">'+symbols[j]+'</'+tag+'>';
	 				gc+=2;
	 				txt=txt.substr(0,c)+str+txt.substr(c+symbols[j].length);
	 				c+=str.length;
	 			}else if(prevtext>=0&&((ch=txt.charCodeAt(prevtext))==32||ch==10||ch==13))
	 			{	 				b=true;
	 				str='<'+tag+' id="sq_hangP'+uid+'_'+(gc+1)+'"> </'+tag+'>';
	 				str2='<'+tag+' style="position: relative; zoom: 1;'+style+'" id="sq_hangP'+uid+'_'+gc+'">'+symbols[j]+'</'+tag+'>';
	 				gc+=2;
	 				txt=txt.substr(0,prevtext)+str+txt.substring(prevtext+1,c)+str2+txt.substr(c+symbols[j].length);
	 				c+=str.length-1;
	 			}else if(prevtag=="!a"&&pc>0&&txt.charAt(pc-1)==' ')
	 			{	 				b=true;
	 				str='<'+tag+' id="sq_hangP'+uid+'_'+(gc+1)+'"> </'+tag+'>'+txt.substr(pc,c-pc)+'<'+tag+' style="position: relative; zoom: 1;'+style+'" id="sq_hangP'+uid+'_'+gc+'">'+symbols[j]+'</'+tag+'>';
	 				gc+=2;
	 				txt=txt.substr(0,pc-1)+str+txt.substr(c+symbols[j].length);
	 				c+=str.length+pc-c-1;
	 			}
 			}
 			prevtag=txt.charAt(c);
 			prevtext=c;
 			if(!b)c++;
 		}
 		ps[i].innerHTML=txt;
 		while(ggc<gc)
 		{ 			el=document.getElementById('sq_hangP'+uid+'_'+ggc); 			w=el.offsetWidth;
 			el.style.position='relative';
 			el.style.left='-'+w+'px';
 			el.style.marginRight="-"+w+'px';
 			if(ie)
 			{ 				is_in_a=false;                p_el=el.parentNode;
                while(p_el.tagName)
                {	 				if(p_el.tagName.toLowerCase()=='a')
	 				{	 					is_in_a=true;
	 					break;
	 				}
	 				p_el=p_el.parentNode;
	 			}
 				txt=el.parentNode.innerHTML;
 				c=txt.indexOf('sq_hangP'+uid+'_'+ggc);
 				while(txt.charAt(c)!='<')c++;
 				txt=txt.substr(0,c)+txt.substr(c+tag.length+3);
 				b=false;
 				if(!is_in_a)
 				while(c<txt.length)
 				{		 			while(txt.charAt(c)=='<')
		 			{		 				pc=c;
		 				prevtag="!"+txt.charAt(c+1);
		 				c+=2;
		 				while(c<txt.length&&(ch=txt.charAt(c))!=' '&&ch!='>'&&ch!='/')
		 				{
		 					prevtag+=ch;
		 					c++;
		 				}
		 				while(c<txt.length&&txt.charAt(c)!='>')c++;
		 				c++;
		 				prevtag=prevtag.toLowerCase();
		 				if(prevtag=='!a')in_a=true;else if(prevtag=='!/a')in_a=false;
		 				if(prevtag=='!'+tag)in_tag=true;else if(prevtag=='!/'+tag)in_tag=false;
		 				if(!in_a&&!in_tag&&prevtag=='!br')
		 				{		 					b=true;
		 					txt=txt.substr(0,pc)+'</'+tag+'>'+txt.substr(pc);
		 					break;
		 				}

		 				if(c>=txt.length)break;
		 			}
	 				if(b||c>=txt.length)break;
	 				if(!in_a&&!in_tag&&txt.charAt(c)==' ')
	 				{	 					b=true;
	 					txt=txt.substr(0,c)+'</'+tag+'>'+txt.substr(c);
	 					break;
	 				}
	 				c++;
 				}
 				if(!b)txt+='</'+tag+'>';
                el.parentNode.innerHTML=txt;
                if(is_in_a)document.getElementById('sq_hangP'+uid+'_'+ggc).style.cursor="pointer";
 			}
 			if(el=document.getElementById('sq_hangP'+uid+'_'+(ggc+1)))
 			el.style.marginRight=w+'px';
 			ggc+=2;
 		}
 		if(window.opera)
 		{ 			len=ps[i].innerHTML.length;
 			op_childs_=ps[i].getElementsByTagName('*');
 			op_childs=new Array();
 			is_bad=false;
 			for(j=0;j<op_childs_.length;j++)if(op_childs_[j].id.match(/^sq_hangP.*$/))is_bad=true;else op_childs.push(op_childs_[j]);
 			if(is_bad)for(j=0;j<op_childs.length;j++)
 			if(op_childs[j].tagName.toLowerCase()=='a' || op_childs[j].tagName.toLowerCase()=='span')
			if(op_childs[j].innerHTML.length/len<0.1)
			{ 				op_childs[j].style.display='inline-block';
 				if(op_childs[j].tagName.toLowerCase()=='span' && op_childs[j].parentNode.tagName.toLowerCase()=='a')
	 				op_childs[j].style.textDecoration='underline';
 			}
 		}
 	}
 	return true;
}

var sq_hyphenate_patterns=new Array();
var sq_hyphenate_weights=new Array();
var sq_hyphenate_exceptions=new Array();
var sq_hyphenate_eweights=new Array();
var sq_hyphenate_symbols=new Array();

function sq_hyphenate(container,lang,treshold)
{
	if(typeof(container)=="string")container=document.getElementById(container);
	if(arguments.length<2)lang='eng';
	if(arguments.length<3)treshold=500;
	var i,j,k,l,m,txt,txts,txtn,prevc,prevb,word,breaks,points,pbreak,pchar;
	var tree=new Array();
	for(i=0;i<sq_hyphenate_patterns[lang].length;i++)tree['sqh_'+sq_hyphenate_patterns[lang][i]]=sq_hyphenate_weights[lang][i];
	var etree=new Array();
	for(i=0;i<sq_hyphenate_exceptions[lang].length;i++)etree['.'+sq_hyphenate_exceptions[lang][i]+'.']='00'+sq_hyphenate_eweights[lang][i]+'00';
 	var ps=container.getElementsByTagName('p');
 	for(i=0;i<ps.length;i++)if(ps[i].offsetWidth>=treshold)
 	{ 		txt=ps[i].innerHTML.replace(/&rsquo;/g,"'").replace(/’/g,"'")+'.';
 		txts=txt.toLowerCase();
 		txtn='';
 		prevb=false;
        for(j=0;j<txts.length;j++)
        {
			if(txts.charAt(j).match(sq_hyphenate_symbols[lang]))
	        {	        	if(!prevb)
	        	{	        		prevb=true;
	        		prevc=j;
	        	}
	        }else
	        {	        	if(prevb)
	        	{	        		prevb=false;
	        		word='.'+txts.substr(prevc,j-prevc)+'.';
	                breaks=new Array(word.length+1);
	                if(points=etree[word])
	                {	                	for(k=0;k<etree[word].length;k++)breaks[k]=etree[word].charAt(k)-'0';
	                }else
	                {
		                for(k=0;k<breaks.length;k++)breaks[k]=0;
		                for(k=0;k<word.length;k++)for(l=k;l<word.length;l++)if(points=tree['sqh_'+word.substr(k,l-k+1)])
		                {		                	for(m=0;m<points.length;m++)breaks[k+m]=Math.max(breaks[k+m],points.charAt(m)-'0');
		                }
		         	}
	                pbreak=1;
	                for(k=3;k<word.length-2;k++)if(breaks[k]%2)
	                {	                	txtn+=txt.substr(prevc+pbreak-1,k-pbreak)+'&shy;';
	                	pbreak=k;
	                }
	                k=word.length-1;
	               	txtn+=txt.substr(prevc+pbreak-1,k-pbreak);
	        	}
	        	txtn+=txt.charAt(j);
	        }

	        if(txts.charAt(j)=='&')
	        {
				pchar=j;
	        	while(j<txts.length&&txts.charAt(j)!=';')j++;
	        	if(j==txts.length)j--;
				txtn+=txt.substr(pchar+1,j-pchar);
	        }else if(txts.charAt(j)=='<')
	        {
				pchar=j;
	        	while(j<txts.length&&txts.charAt(j)!='>')j++;
	        	if(j==txts.length)j--;
				txtn+=txt.substr(pchar+1,j-pchar);
	        }
	  	}
		ps[i].innerHTML=txtn.substr(0,txtn.length-1).replace(/'/g,"&rsquo;");
		ps[i].style.textAlign='justify';
 	}
	return true;
}