var rtaf=new Array();
var rtam=false;
var rtal=false;


function rta(id)
{
    return rta(id,200);
}
function rta(id,height)
{
    rtaf[id]=height;
    if(!rtal)
    {
        if(!rtam)
        {
            window.onload+=new function()
            {
                rtar();
            };
            rtam=true;
        }
    }
    else
    {
        rtar();
    }
}

function rtar()
{
    for(var id in rtaf)
    {
        var ha = new FCKeditor(id);
        ha.BasePath=fckPath;
        ha.Height=rtaf[id];
        ha.ReplaceTextarea();
    }
    rtaf=new Array();
    rtal=true;
}

function ge(id){return document.getElementById(id);}


var fadeInfo=new Array();
function fadeIn(id,from,to,totaltime)
{
 var op=ge(id);
 var step=(to-from)/(totaltime/60);
 fadeInfo[id]={"to":to,"state":1,"op":from,"step":step};
 op.style.display="block";
 op.style.opacity=from/100;
 op.style.filter="alpha(opacity="+from+")";
 fadeTimer(id);
}

function fadeOut(id,from,to,totaltime)
{
 var op=ge(id);
 var step=(to-from)/(totaltime/60);
 fadeInfo[id]={"to":to,"state":2,"op":from,"step":step};
 op.style.display="block";
 op.style.opacity=from/100;
 op.style.filter="alpha(opacity="+from+")";
 fadeTimer(id);
}

function fadeTimer(id)
{
 var cnt=true;
 var info=fadeInfo[id];
 var op=ge(id);
 if(!info)return;
 
 info["op"]+=info["step"];
 if(info["state"]==1 && info["op"]>=info["to"])
 {
  info["op"]=info["to"];
  cnt=false;
 }

 if(info["state"]==2 && info["op"]<=info["to"])
 {
  info["op"]=info["to"];
  if(info["to"]==0)op.style.display="none";
  cnt=false;
 }

 op.style.opacity=info["op"]/100;
 op.style.filter="alpha(opacity="+info["op"]+")";
 
 if(cnt)window.setTimeout('fadeTimer("'+id+'");',60);
}

function moveToBody(id)
{
 var e=ge(id);
 if(!e)return;
 var n=document.createElement("div");
 n.innerHTML=e.innerHTML;
 n.id=e.id;
 e.parentNode.removeChild(e);
 document.body.appendChild(n);
}




var sizeYInfo=new Array();
function sizeYTo(id,from,to,totaltime)
{
 var op=ge(id);
 var step=(to-from)/(totaltime/60);
 var state=step<0?2:1;
 sizeYInfo[id]={"to":to,"state":state,"op":from,"step":step};
 op.style.display="block";
 op.style.height=from+"px";
 sizeYTimer(id);
}

function sizeYTimer(id)
{
 var cnt=true;
 var info=sizeYInfo[id];
 var op=ge(id);
 if(!info)return;
 
 info["op"]+=info["step"];
 
 if(info["state"]==1 && info["op"]>=info["to"])
 {
  info["op"]=info["to"];
  cnt=false;
 }
 
 if(info["state"]==2 && info["op"]<=info["to"])
 {
  info["op"]=info["to"];
  cnt=false;
 }
 op.style.height=info["op"]+"px";
 if(cnt)window.setTimeout('sizeYTimer("'+id+'");',60);
}


var psstate=0;

function pripadovaStudieMove()
{
 var ob=ge("studie");
 var st=ge("sshorttext");
 var lt=ge("slongtext");
 var cspan=ge("stconspan");
 var stspan=ge("studiespan");
 st.style.display="";
 ob.className="full";
 var fromhgt=st.clientHeight==0?96:st.clientHeight+6;
 st.style.display="none";
 lt.style.display="";
 var tohgt=lt.clientHeight==0?stspan.clientHeight-84:lt.clientHeight;
 cspan.style.height=fromhgt+"px";
 stspan.style.height=(tohgt+84)+"px";

 if(psstate==0)
 {
  sizeYTo("stconspan",fromhgt,tohgt,300);
  fadeIn("slongtext",0,100,400);
  //fadeOut("sshorttext",100,0,200);
  psstate=1;
  window.setTimeout('ge("stconspan").style.height="";ge("studiespan").style.height="";ge("studie").className="full";',360);
 }
 else
 {
  st.style.marginBottom=-st.clientHeight+"px";
  sizeYTo("stconspan",tohgt,fromhgt+11,300);
  //fadeIn("sshorttext",0,100,200);
  fadeOut("slongtext",100,0,250);
  psstate=0;
  window.setTimeout('ge("stconspan").style.height="";ge("studiespan").style.height="";ge("sshorttext").style.display="";fadeIn("sshorttext",0,100,400);ge("slongtext").style.display="none";ge("studie").className="mini";',360);
 }
}




function showOverPage(innerHTML)
{
 var op=ge("overpage");
 if(!op)
 {
  op=document.createElement("div");
  op.id="overpage";
  document.body.appendChild(op);
  op.style.position="absolute";
  op.style.top="0px";
  op.style.left="0px";
  op.style.display="block";
 }
 overPageSize(); 
 fadeIn("overpage",20,80,250);
 window.setTimeout('overPageSize();',400);
 return true;
}

function overPageSize()
{
 var op=ge("overpage");
 op.style.height=document.body.clientHeight+"px";
 op.style.width=document.body.clientWidth+"px";
}




function removeWordHtml(html)
{
    html = html.replace(/<o:p>\s*<\/o:p>/g, '') ;
    html = html.replace(/<o:p>[\s\S]*?<\/o:p>/g, '&nbsp;') ;

    // Remove mso-xxx styles.
    html = html.replace( /\s*mso-[^:]+:[^;"]+;?/gi, '' ) ;
 
 // Remove margin styles.
 html = html.replace( /\s*MARGIN: 0cm 0cm 0pt\s*;/gi, '' ) ;
 html = html.replace( /\s*MARGIN: 0cm 0cm 0pt\s*"/gi, "\"" ) ;

    html = html.replace( /\s*TEXT-INDENT: 0cm\s*;/gi, '' ) ;
    html = html.replace( /\s*TEXT-INDENT: 0cm\s*"/gi, "\"" ) ;

    html = html.replace( /\s*TEXT-ALIGN: [^\s;]+;?"/gi, "\"" ) ;

    html = html.replace( /\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/gi, "\"" ) ;

    html = html.replace( /\s*FONT-VARIANT: [^\s;]+;?"/gi, "\"" ) ;

    html = html.replace( /\s*tab-stops:[^;"]*;?/gi, '' ) ;
 html = html.replace( /\s*tab-stops:[^"]*/gi, '' ) ;

    // Remove FONT face attributes.
    html = html.replace( /\s*face="[^"]*"/gi, '' ) ;
 html = html.replace( /\s*face=[^ >]*/gi, '' ) ;
 
 html = html.replace( /\s*FONT-FAMILY:[^;"]*;?/gi, '' ) ;

    // Remove Class attributes
    html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;

    // Remove styles.
    html = html.replace( /<(\w[^>]*) style="([^\"]*)"([^>]*)/gi, "<$1$3" ) ;

    // Remove style, meta and link tags
    html = html.replace( /<STYLE[^>]*>[\s\S]*?<\/STYLE[^>]*>/gi, '' ) ;
    html = html.replace( /<(?:META|LINK)[^>]*>\s*/gi, '' ) ;

    // Remove empty styles.
    html = html.replace( /\s*style="\s*"/gi, '' ) ;

    html = html.replace( /<SPAN\s*[^>]*>\s*&nbsp;\s*<\/SPAN>/gi, '&nbsp;' ) ;

    html = html.replace( /<SPAN\s*[^>]*><\/SPAN>/gi, '' ) ;

    // Remove Lang attributes
    html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;

    html = html.replace( /<SPAN\s*>([\s\S]*?)<\/SPAN>/gi, '$1' ) ;

    html = html.replace( /<FONT\s*>([\s\S]*?)<\/FONT>/gi, '$1' ) ;

    // Remove XML elements and declarations
    html = html.replace(/<\\?\?xml[^>]*>/gi, '' ) ;

    // Remove w: tags with contents.
    html = html.replace( /<w:[^>]*>[\s\S]*?<\/w:[^>]*>/gi, '' ) ;

    // Remove Tags with XML namespace declarations: <o:p><\/o:p>
    html = html.replace(/<\/?\w+:[^>]*>/gi, '' ) ;

    // Remove comments [SF BUG-1481861].
    html = html.replace(/<\!--[\s\S]*?-->/g, '' ) ;

    html = html.replace( /<(U|I|STRIKE)>&nbsp;<\/\1>/g, '&nbsp;' ) ;

    html = html.replace( /<H\d>\s*<\/H\d>/gi, '' ) ;

    // Remove "display:none" tags.
    html = html.replace( /<(\w+)[^>]*\sstyle="[^"]*DISPLAY\s?:\s?none[\s\S]*?<\/\1>/ig, '' ) ;

    // Remove language tags
    html = html.replace( /<(\w[^>]*) language=([^ |>]*)([^>]*)/gi, "<$1$3") ;
    html = html.replace( /<b>(.*?)<\/b>/i, '<strong>$1</strong>' ) ;

    // Remove onmouseover and onmouseout events (from MS Word comments effect)
    html = html.replace( /<(\w[^>]*) onmouseover="([^\"]*)"([^>]*)/gi, "<$1$3") ;
    html = html.replace( /<(\w[^>]*) onmouseout="([^\"]*)"([^>]*)/gi, "<$1$3") ;

    // The original <Hn> tag send from Word is something like this: <Hn style="margin-top:0px;margin-bottom:0px">
    html = html.replace( /<H(\d)([^>]*)>/gi, '<h$1>' ) ;

    // Word likes to insert extra <font> tags, when using MSIE. (Wierd).
    html = html.replace( /<(H\d)><FONT[^>]*>([\s\S]*?)<\/FONT><\/\1>/gi, '<$1>$2<\/$1>' );
    html = html.replace( /<(H\d)><EM>([\s\S]*?)<\/EM><\/\1>/gi, '<$1>$2<\/$1>' );

    html = html.replace( /<font[^>]*>/gi, '' );
    html = html.replace( /<\/font>/gi, '' );
    html = html.replace( /<title>(.*?)<\/title>/gi, '' );

    html = html.replace( /<(|\/)b>/gi, '<$1strong>' ) ;

    html = html.replace( /<p>&nbsp;<\/p>/i, '' ) ;
    html = html.replace( /<p>\s*<\/p>/i, '' ) ;
    html = html.replace( /<h(\d)>\s*<\/h\1>/i, '' ) ;
    html = html.replace( /<p>\s*<\/p>/i, '' ) ;
    html = html.replace( /<p>\s*<\/p>/i, '' ) ;
    html = html.replace( /$\s*/i, '' ) ;
    html = html.replace( /\s*^/i, '' ) ;

    html = html.replace( /<(strong|i|em|u)>\s*<\/\1>/i, '' ) ;
    html = html.replace(/^\s*(.*)\s*/,'$1');

    if(html.substr(0,3)=='<p>')
    {
        html=html.substr(3);
    }
    return html ;
}


function FCKeditor_OnComplete(editor)
{
    editor.Events.AttachEvent('OnPaste',checkWordHtml);
}


var edtTC=new Array();
function checkWordHtml(editor)
{
    edtTC.push(editor);
    window.setTimeout('checkEditors();',100);
    return true;
}

function checkEditors()
{
    var repair=false;
    var editor=edtTC.shift();
    var html=editor.EditorDocument.body.innerHTML;
    if(html.indexOf('\<meta')>=0)repair=true;
    if(html.indexOf('\<title\>')>=0)repair=true;
    if(html.indexOf('class="Mso')>=0 || html.indexOf('class="mso')>=0)repair=true;
    if(repair)
    {
        editor.EditorDocument.body.innerHTML=removeWordHtml(html);
    }
    if(edtTC.length>0)checkEditors();
}
