//First lets initiate some variables

var isEditable= false;
var isIE;
var isGecko;
var isSafari;
var isKonqueror;
var browser = navigator.userAgent.toLowerCase();

function initiateEditor() {
 //check what browser is in use

 isIE = ((browser.indexOf("msie") != -1) && (browser.indexOf("opera") == -1) && (browser.indexOf("webtv") == -1)); 
 isGecko = (browser.indexOf("gecko") != -1);
 isSafari = (browser.indexOf("safari") != -1);
 isKonqueror = (browser.indexOf("konqueror") != -1);
 
 //enable designMode if the browser is not safari or konqueror.
 if (document.getElementById && document.designMode && !isSafari && !isKonqueror) {
   isEditable= true;
 } 
}
//Javascript function dislpayEditor will create the textarea. 

function displayEditor(editor, html, width, height) {
   if(isEditable){
       document.writeln('<iframe id="' + editor + '" name="' + editor + '" width="' + width + 'px" height="' + height + 'px" style="border:1px solid #aaaaaa;background-color:#ffffff"></iframe>');
//create a hidden field that will hold everything that is typed in the textarea
       document.writeln('<input type="hidden" id="hidden' + editor + '" name="hidden' + editor + '" value="">');
//assign html (textarea value) to hiddeneditor 
      document.getElementById('hidden' + editor).value = html;
//call function designer
      designer(editor, html);
   }else{
     document.writeln('<textarea name="' + editor + '" id="' + editor + '" cols="120" rows="3" class=input>' + html + '</textarea>');
   }
}

//this is designer function that enables designMode and writes defalut text to the text area
function designer(editor, html) {
     var mainContent= "<html id=" + editor + "><head></head><body>" + html + "</body></html>" ;
//assign the frame(textarea) to the edit variable using that frames id
     var edit = document.getElementById(editor).contentWindow.document;
//write the content to the textarea
      edit.write(mainContent);
	  edit.close();
//enable the designMode	
      edit.designMode =  "On" ;
	  //document.getElementById(content).contentDocument.designMode = "on";
}

//To execute command we will use javascript function editorCommand. 
function editorCommand(editor, command, option) {

   var mainField;
   mainField = document.getElementById(editor).contentWindow;
   mainField.focus();
	
	
	//alert(mainField.name+'_'+command+'_'+ option);
  
   if(command == "createlink"){
   	
	var szURL = prompt("Enter a URL:", "");
	if(szURL){if(szURL.search("http://")==-1){szURL = "http://" + szURL;}}
	mainField.document.execCommand("Unlink",false,null);
	mainField.document.execCommand("CreateLink",false,szURL);   	
   }	


//   else if(command == "InsertImage"){
 
// 	mainField.document.execCommand("InsertImage",false,option); 
 	
//   }	
   else{
   
   try {
	/*
	  if(!nn){
	  	
		
	  	var oSel = mainField.document.selection.createRange();
		var sType = document.selection.type;
		var oTarget = (sType == "None" ? mainField.document : oSel);	
	  	//oSel.execCommand(command, false, option);
	  	oTarget.execCommand(command, false, option);
	  }		
          else{
	
		*/
          	mainField.document.execCommand(command, false, option);

          //}

          mainField.focus();

	  if(command=='forecolor'){cc_hide('fontcolorspan1');}
	  else if(command=='fontsize'){cc_hide('fontsizespan1');}
	  else if(command=='fontname'){cc_hide('fontspan1');}

          
   } catch (e) {alert('execCommand error');}

   }	
}

function updateEditor(editor) {
	if (!isEditable) return;
	//assign the value of the textarea to the hidden field. 
	var hiddenField = document.getElementById('hidden' + editor);
	if (hiddenField.value == null){ hiddenField.value = "";}
	hiddenField.value = document.getElementById(editor).contentWindow.document.body.innerHTML;
	document.edit.blogcontent.value = hiddenField.value;


}


//-------------------------------------------------------------------------------------------------------
function image_gallery(editor,entryUID,blogUID){

	updateEditor('txteditor');
	content = document.getElementById('hiddentxteditor').value;


	if(content.search("galleryicon.gif")==-1){						//check if one gallery already exists
		parent.editorCommand('txteditor','InsertImage','images/galleryicon.gif');
	}
	
	document.getElementById('entrytop').style.visibility='hidden';
	sizevedit(700,(v00height-100));
	ajax_gallery('entryUID='+entryUID+'&blogUID='+blogUID);
	naviswitch('edit');

}

//-------------------------------------------------------------------------------------------------------
function image_addbub(editor){



  	var mainField = document.getElementById(editor).contentWindow;
   	mainField.focus();
	//if ( oSel.anchorNode){alert(oSel.anchorNode.childNodes[ oSel.anchorOffset ] );}
   
   	if(nn){
   		//-- Gecko
   		var oSel = mainField.getSelection();
		if (oSel.anchorNode){
			//if (oSel.anchorNode){alert(oSel.anchorNode.childNodes[ oSel.anchorOffset ].tagName +'_'+ oSel.anchorNode.childNodes[ oSel.anchorOffset ].src);}
   			oSel = oSel.anchorNode.childNodes[oSel.anchorOffset];
	
   		}
   	}
   	else{
   		//-- IE
		var oSel = mainField.document.selection.createRange();
		//alert(oSel.item +'_'+ oSel.item(0).tagName +'_'+ oSel.item(0).src);	
		if(oSel.item){oSel = oSel.item(0);}	
	

	}

	if((oSel) && (oSel.tagName=="IMG")){		
		//alert(oSel.tagName +'_'+ oSel.src);	
		if(document.getElementById('entrytop')){document.getElementById('entrytop').style.visibility='hidden';}
		javascript:sizevedit(700,(v00height-100));ajax_admbubles('imgsrc='+oSel.src);naviswitch('edit');
	}
	else{alert(tnotimage);}	
}	