// Umschalter WYSIWYG / HTML
var isHTMLMode=false;
// Variable ist ja wenn die news editiert werden sollen
var ref = "",allowedTag=new Array();
var cxbase="/contx/";

// entfernt alle HTML-Tags
function cleanHTML(allowedTags){
 var content;

// alert(editPage.document.getElements);

 if(isHTMLMode)
 {
 content = editPage.document.body.innerHTML;
 content=content.replace(/\&lt\;([^\&]|\n)*\&gt\;/g,"");
 editPage.document.body.innerHTML = content;
 }
 else
 {
 content = editPage.document.body.innerText;
 content=content.replace(/\<([^>]|\n)*\>/g,"");
// content=content.replace(/\<(span|font)([^>]|\n)*\>/g,"");
 editPage.document.body.innerText = content;
 }
}
// An- bzw. Abschalten von Formatierungen
function enableAllowedTags(allowedTags,allowedStyles)
{
 allowedTag=new Array();
 at=allowedTags.split(",");
 for(i=0;i<at.length;i++) allowedTag[at[i]]=true;

// as=allowedStyles.split(",");
// alert(document.URL);
// if(!window.cxStyles)
//  cxStyles=document.getElementById("cxStyles");
// for(i=0;i<cxStyles.length;i++) cxStyles.options.length=null;
//  cxStyles.options[cxStyles.length]=new Option("Style");
// if(as[0]) for(i=0;i<as.length;i++)
// {
//   cxStyles.options[cxStyles.length]=new Option(as[i]);
//   cxStyles.options[cxStyles.length-1].value=as[i];
// }
}

// Funtion zum umschalten WYSIWYG / HTML
// der gesamten aktuellen Seite

function setModeSite(bMode){
 var sTmp;
   isHTMLMode = bMode;
//  base=editPage.document.createElement("base");
//  base.setAttribute("href","http://www.hitmarkt.info/")
//  editPage.document.getElementsByTagName("head")[0].appendChild(base);
//   alert(document.getElementsByTagName("head")[0].nodeName+editPage.document.all.tags("html")[0].innerHTML);
 if(ref=='ja'){
  if(isHTMLMode){
   sTmp=editPage.document.body.innerHTML;
   sTmp=sTmp.replace(/src="http:\/\/[A-Za-z0-9.]*\//g,"src=\"");
//   sTmp=sTmp.replace(/src="/,"src=\"/");
   editPage.document.body.innerText=sTmp;
   editPage.document.body.style.font="10pt Courier";
   editPage.document.body.style.border = "thin solid red";
  } else {
   sTmp=editPage.document.body.innerText;

   editPage.document.body.innerHTML=sTmp;
//   sTmp=sTmp.replace(/http:\/\/[A-Za-z0-9.]*\/contx\/modules\/news/,"");
//   editPage.document.body.insertAdjacentHTML("AfterBegin","<base href='/'>");
//   editPage.document.all.tags("HEAD")[0].insertAdjacentHTML("AfterBegin","<base href='/'>");
   editPage.document.body.style.fontFamily="";
   editPage.document.body.style.fontSize="";
   editPage.document.body.style.border="";
  }
  editPage.focus();
 } else {
  for(i=0;i<document.all.tags("div").length;i++) {
    if(document.all.tags("div")[i].getAttribute("contxid")){
      if (isHTMLMode){
              sTmp=document.all.tags("div")[i].innerHTML
     document.all.tags("div")[i].innerText=sTmp;
    } else {
     sTmp=document.all.tags("div")[i].innerText;
     document.all.tags("div")[i].innerHTML=sTmp;
    }
   }
  }
 }
}

// Fkt. zum einfügen eines Bildes in der WYSIWYG Ansicht
function showDialog(){

    if (isHTMLMode){
  alert("Bitte schalten Sie den HTML Modus aus!");
  return;
 } else {
     bild = window.open(cxbase + 'modules/content/bild.php?ref=' + ref,'bild','width=430,height=280,screenX=0,screenY=0,scrollbars=no');
     bild.focus();
 }
}
function showLinkDialog(){
    if (isHTMLMode){
  alert("Bitte schalten Sie den HTML Modus aus!");
  return;
 } else {
     bild = window.open(cxbase + 'modules/content/link.php?ref=' + ref,'file','width=430,height=300,screenX=0,screenY=0,scrollbars=no,status=no');
     bild.focus();
 }
}

// öffnet fenster zum editieren der news (oder der texte [wenn browser älter MSIE 5.5])
function openEditWindow(id,topic,template_id,width,height){
 adWin = window.open(cxbase+"modules/news/edit.php?id=" + id +"&topic="+topic+"&template_id="+template_id,"adminWin","width="+ width +",height="+ height +",screenX=0,screenY=0,dependent=yes,scrollbars,resizable=yes");
}
function openEditWindowProdukte(id,topic,template_id,width,height){
 adWin = window.open(cxbase+"produkt_create.php?id=" + id +"&topic="+topic+"&template_id="+template_id,"adminWin","width="+ width +",height="+ height +",screenX=0,screenY=0,dependent=yes,scrollbars,status=yes,resizable=yes");
}

var imgId;
var divId = 'no';
var html ='off';
function wechsel(imgId,src) {
 var nr = "divNr" + divId;
 //alert(nr);
 //alert(html[nr][imgId]);
 //if(!html['divNr655'][imgId]){
  document.images(imgId).src=src;
 //}
}
function showLayer(){
 document.all.select_image.style.visibility="visible";
}
// Fügt Stylesheetformatierung ein
function fnStylesheet(strStyle)
{
 if (isHTMLMode){
  alert("Bitte schalten Sie den HTML Modus aus!");
  return;
 } else {
  if (strStyle != null) {
   var objSelection;
   if(ref=='ja')
             objSelection = editPage.document.selection.createRange();
   else
             objSelection = document.selection.createRange();

   var parts=strStyle.split(".");
   if(parts[0]=="") parts[0]="span";
   if(parts[1]!="") parts[1]=" class='"+parts[1]+"'";
    objSelection.pasteHTML("<"+parts[0]+parts[1]+">" + objSelection.text + "</"+parts[0]+"> ");

   objSelection.select();
  }
 }
}
// Führt Formatierungsanweisungen aus
function fnFormat(strCommand, strOption){

 if (isHTMLMode){
  alert("Bitte schalten Sie den HTML Modus aus!");
  return;
 } else {
  if (strOption == 'removeFormat') {
   strCommand = strOption;
   strOption = null;
  }
  if (strOption == null) {
   if(ref=='ja')
    editPage.document.execCommand(strCommand);
   else
    document.execCommand(strCommand);
  } else {
   if(ref=='ja')
    editPage.document.execCommand(strCommand,'',strOption);
   else
    document.execCommand(strCommand,'',strOption);
  }
 }
}
function foreColor()
 {
 var arr = showModalDialog(cxbase + "modules/content/selcolor.htm","","font-family:Verdana; font-size:12; dialogWidth:30em; dialogHeight:34em" );
 if (arr != null) {
  if(ref=='ja')
   editPage.document.execCommand("ForeColor","",arr);
  else
   document.execCommand("ForeColor","",arr);
 }
}
// fügt Bild an
function fnInsertImage(bildname) {
 editPage.document.execCommand("InsertImage",false,bildname);
}
//  die geänderten texte werden in versteckte felder gespeichert
function fnSave(){

 if(document.all){
  // iteriere ueber alle div-tags
  for(i=0;i<document.all.tags("div").length;i++) {
   // extrahiere den inhalt und tue ihn in die variable inhalt
   if(document.all.tags("div")[i].getAttribute("contxid")){
    if (isHTMLMode){
     inhalt=document.all.tags("div")[i].innerText;
    } 
    else {
     inhalt=document.all.tags("div")[i].innerHTML;
    }

    text = getHiddenText(document.all.tags("div")[i],inhalt);

    document.all.saveInhalt.insertAdjacentHTML("BeforeEnd",text);
   }
  }
 }
 else {
  var divs = document.getElementsByTagName('div');
  var saveInhalt = document.saveInhalt;
  for ( var i = 0 ; i < divs.length ; i++ ){ 
   if(divs[i].getAttribute('contxid')){
    inhalt = innerHtml(divs[i]);
    alert(inhalt);
    nodes = getHiddenNodes(divs[i],inhalt);
    for (var j = 0; j < nodes.length ; j++){
     saveInhalt.appendChild(nodes[j]);
    }
   }
  }
 }
}

function getHiddenText(node,inhalt){

 // ersetzte die apostrophe
 inhalt=inhalt.replace(/'/g,"´");   //' um das syntaxhighlighting wieder in takt zu bringen

 // ersetzte die absoluten urls innerhalb des auftrittes durch relative:
 inhalt=inhalt.replace(/http:\/\/(daetz|daetz\.sandstein\.intern|daetz\.sandstein\.de|www\.daetz-centrum\.de)\//g,'/');

 // und mache tags weg usw...
 inhalt = escape(inhalt);
 
 text="<input type='hidden' " +
         "name='templateid["+node.getAttribute("contxid") + "]' value='" + node.getAttribute("templateid") + "'>"+
       "<input type='hidden' " + 
         "name='refid[" + node.getAttribute("contxid") + "]' value='" + inhalt+"'>" +
       "<input type='hidden' " + 
         "name='topicid[" + node.getAttribute("contxid") + "]' value='" + node.getAttribute("topic") + "'>" +
       "<input type='hidden' " + 
         "name='type[" + node.getAttribute("contxid") + "]' value='" + node.getAttribute("type") + "'>";

 return text;
}

function getHiddenNodes(node,inhalt){
 // ersetzte die apostrophe
 inhalt=inhalt.replace(/'/g,"´");   //' um das syntaxhighlighting wieder in takt zu bringen

 // und mache tags weg usw...
 inhalt = escape(inhalt);

 var nodes = new Array();
 for (var i = 0; i < 4; i++){
  nodes[i] = document.createElement('input');
  nodes[i].setAttribute('type','hidden');
 }
 nodes[0].setAttribute('name','templateid[' + node.getAttribute("contxid") + ']' );
 nodes[0].setAttribute('value',node.getAttribute('templateid'));
 nodes[1].setAttribute('name','refid[' + node.getAttribute("contxid") + ']' );
 nodes[1].setAttribute('value',inhalt);
 nodes[2].setAttribute('name','topicid[' + node.getAttribute("contxid") + ']' );
 nodes[2].setAttribute('value',node.getAttribute('topic'));
 nodes[3].setAttribute('name','type[' + node.getAttribute("contxid") + ']' );
 nodes[3].setAttribute('value',node.getAttribute('type'));
 
 return nodes;
}



// Cookie zur Speicherung der Position der schwebenden Menüleiste
function setcookie()  //Position als Array im Cookie speichern
{
        position = new Array();
        position[0];
        position[1];
        if(navigator.appName=="Microsoft Internet Explorer")
        {
        position[0] = parseInt(document.all.editMenue.style.left);                //IE
        position[1] = parseInt(document.all.editMenue.style.top);
        }
        if(navigator.appName=="Netscape")
        {
                if(navigator.appVersion.substring(0,1)<5)                //Netscape vor 6
                {
                position[0] = document.editMenue.left;
                position[1] = document.editMenue.top;
                }
                else
                {
                position[0] = parseInt(document.getElementById("editMenue").style.left);        //Netscape6
                position[1] = parseInt(document.getElementById("editMenue").style.top);
                }
        }
        document.cookie =  "contxpos,"+position + ";path=/";
}
var wert;
function getposition()                        //Den im Cookie gespeicherten Wert auslesen
{
        trenn = document.cookie.split(";");
        for(i=0;i<trenn.length;i++) if(trenn[i].match(/contxpos,/)) wert=trenn[i].split(",");
        if(wert)
        {
                if(navigator.appName=="Microsoft Internet Explorer" && document.all.editMenue)
                {
                document.all.editMenue.style.left = wert[1] + "px";
                document.all.editMenue.style.top = wert[2] + "px";
                }
                else
                {
                        if(navigator.appVersion.substring(0,1)<5 && document.editMenue)
                        {
                        document.editMenue.left = wert[1];
                        document.editMenue.top = wert[2];
                        }
                        else
                        {
                        document.getElementById("editMenue").style.left = wert[1] + "px";
                        document.getElementById("editMenue").style.top = wert[2] + "px";
                        }
                }
        }

}

function makeStatic() {
 if (document.all) {
  document.all['editMenue'].style.pixelTop=document.body.scrollTop+20;
  document.all['editMenue'].style.pixelLeft=document.body.scrollLeft+20;
 } else {
  eval(document.all['editMenue'].top=eval(window.pageYOffset+20));
 }
 setTimeout("makeStatic()",50);
}
//makeStatic();

var ie = document.all ? true : false;
var ns = document.layers ? true : false;
var offset=0;
//setTimeout('glue()',1000);
function glue() {
 if (ns) {
  var pos = document.anchors['dummy'];
  var posi = pos.y;
  var posix = pos.x;
  document.layers['editMenue'].pageX = posix;
  if(window.pageYOffset + offset > posi) {
   document.layers['editMenue'].pageY = window.pageYOffset + offset; //NN
  } else {
   document.layers['editMenue'].pageY = posi;
  }
 }
 if (ie) {
  var pos = document.all.editMenu;
  posi = getTop(pos);
  posx = getLeft(pos);
  document.all['editMenue'].style.posLeft = posx;
  if(document.body.scrollTop  + offset > posi){
   document.all['editMenue'].style.posTop = document.body.scrollTop  + offset; //IE
  } else {
   document.all['editMenue'].style.posTop = posi;
  }
 }
 setTimeout('glue()',50);
}


// um mit dem dom arbeiten zu koennen

function innerHtml(element){
 var nodeName = element.nodeName;
 var nodeValue = element.nodeValue;
 var nodeType = element.nodeType;
 
 var html = getHtml(element);
 html = html.replace(/^(\s*)<(.*?)>(\s*)/i,'');
 html = html.replace(/(\s*)<\/(.*?)>(\s*)$/i,'');
 return html;
}

function getHtml(element){
 var nodeName = element.nodeName;
 var nodeValue = element.nodeValue;
 var nodeType = element.nodeType;
 
 var attributStringArray = new Array();
 var inhalt = '';
 
 if(nodeType == 1){
  for( var i = 0; i < element.attributes.length; i++){
   attributStringArray[attributStringArray.length] = element.attributes[i].nodeName + '="' + element.attributes[i].nodeValue + '"';
  }
  if(element.childNodes.length){
   for(i = 0; i < element.childNodes.length; i++){
    inhalt += getHtml(element.childNodes[i]);
   } 
  }
  if(inhalt != ''){
   return '<' + nodeName + ' '  + attributStringArray.join(' ') + '>' + inhalt + '</' + nodeName + '>';
  }
  else {
   return '<' + nodeName + ' '  + attributStringArray.join(' ') + '>';
  }
 }
 else if (nodeType == 3){
  return nodeValue;
 }
}


