jQuery.noConflict(); 

var ifrdoc;
var browser="";

var bold_a = false;
var italic_a = false;
var underline_a = false;
var delete_a = false;
var color_a = false;
var color_bg_a = false;
var orderArray_a = false;
var disorderArray_a = false;
var left_a = false;
var center_a = false;
var right_a = false;
var indentation_a = false;
var outdent_a = false;
var code_a = false;
window.onload = function(){
	browser_version();//判断浏览器
	init();
}

function init(){

	
	var ifr = document.createElement("iframe");
	var dom_id = document.getElementById("editor").appendChild(ifr);
	
	ifrdoc = ifr.contentWindow.document;
	ifrdoc.designMode="on";     //文档进入可编辑模式
	ifrdoc.contentEditable=true;
	ifrdoc.open();                        //打开流
	ifrdoc.writeln("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><style type=\"text/css\" media=\"all\">body{ font-size:12px; margin:0px; padding:5px;} p{ margin:6px; padding:0px;}</style><title></title></head><body></body></html>"); 
	if(browser == "ie7" || browser == "ie6" || browser == "ie8")
		window.frames(0).focus()
	if(browser == "ff")
		dom_id.focus();
	ifrdoc.close();
	

}

function test(){
	//var txt=ifrdoc.getSelection();  //ff
	var txt=ifrdoc.selection.createRange();  //ie
	alert(txt.text); 

}
function browser_version(){
	if(jQuery.browser.msie && jQuery.browser.version ==6){
		browser = "ie6";
	}
	
	if(jQuery.browser.msie && jQuery.browser.version ==7){
		browser = "ie7";
	}
	
	if(jQuery.browser.msie && jQuery.browser.version ==8){
		browser = "ie8";
	}
	
	if(jQuery.browser.mozilla){
		browser = "ff";
	}
}
//判断是否选择了文字
function choose_decide(){
	var sel = ifrdoc.selection != null ? ifrdoc.selection.createRange().text : ifrdoc.getSelection();
	if(sel.length < 0 || sel.length == 0){
		alert("请选择文字");
		return false;
	}else{
		return true;
	}
}

function bold(){
		if(choose_decide()){
			ifrdoc.execCommand("bold",false,null);
			if(bold_a == true){
				jQuery(".bold").find("a").css({
						background:'url(../../editor/images/editor_bold.png)'
				})
				bold_a = false;
			}else{
				jQuery(".bold").find("a").css({
						background:'url(../../editor/images/editor_bold_a.png)'
				})
				bold_a = true;
			}
		}
	
}

function italic(){
	if(choose_decide()){
		ifrdoc.execCommand("italic",false,null);
		if(italic_a == true){
			jQuery(".italic").find("a").css({
					background:'url(../../editor/images/editor_italic.png)'
			})
			italic_a = false;
		}else{
			jQuery(".italic").find("a").css({
					background:'url(../../editor/images/editor_italic_a.png)'
			})
			italic_a = true;
		}
	}
	
}

function underline(){
	if(choose_decide()){
		ifrdoc.execCommand("underline",false,null);
		if(underline_a == true){
			jQuery(".underline").find("a").css({
					background:'url(../../editor/images/editor_underline.png)'
			})
			underline_a = false;
		}else{
			jQuery(".underline").find("a").css({
					background:'url(../../editor/images/editor_underline_a.png)'
			})
			underline_a = true;
		}
	}
	
}

function Delete(){
	if(choose_decide()){
		ifrdoc.execCommand("StrikeThrough",false,null);
		if(delete_a == true){
			jQuery(".delete").find("a").css({
					background:'url(../../editor/images/editor_delete.png)'
			})
			delete_a = false;
		}else{
			jQuery(".delete").find("a").css({
					background:'url(../../editor/images/editor_delete_a.png)'
			})
			delete_a = true;
		}
	}
	
}

function colorChoose(){
	if(choose_decide())
	jQuery(".color_choose").toggle();
}

function colorChooseBg(){
	if(choose_decide())
	jQuery(".color_choose_bg").toggle();
}

function foreColor(color){
	ifrdoc.execCommand("ForeColor",false,color);
	jQuery(".color_choose").toggle();
}

function backColor(color){
		if(browser == "ie7" || browser == "ie6" || browser == "ie8")
			ifrdoc.execCommand("BackColor",false,color);
		else
			ifrdoc.execCommand("hilitecolor",false,color);
		jQuery(".color_choose_bg").toggle();

}

function orderArray(){
	if(choose_decide()){
		ifrdoc.execCommand("InsertOrderedList",false,null);
		if(orderArray_a == true){
			jQuery(".orderArray").find("a").css({
					background:'url(../../editor/images/editor_orderArray.png)'
			})
			orderArray_a = false;
		}else{
			jQuery(".orderArray").find("a").css({
					background:'url(../../editor/images/editor_orderArray_a.png)'
			})
			orderArray_a = true;
			
			jQuery(".disorderArray").find("a").css({
					background:'url(../../editor/images/editor_disorderArray.png)'
			})
			disorderArray_a = false;
		}
	}
}

function disorderArray(){
	if(choose_decide()){
		ifrdoc.execCommand("InsertUnorderedList",false,null);
		if(disorderArray_a == true){
			jQuery(".disorderArray").find("a").css({
					background:'url(../../editor/images/editor_disorderArray.png)'
			})
			disorderArray_a = false;
		}else{
			jQuery(".disorderArray").find("a").css({
					background:'url(../../editor/images/editor_disorderArray_a.png)'
			})
			disorderArray_a = true;
			
			jQuery(".orderArray").find("a").css({
					background:'url(../../editor/images/editor_orderArray.png)'
			})
			orderArray_a = false;
		}
	}

}

function left(){
	if(choose_decide()){
		ifrdoc.execCommand("JustifyLeft",false,null);
		if(left_a == true){
			jQuery(".left").find("a").css({
					background:'url(../../editor/images/editor_left.png)'
			})
			left_a = false;
		}else{
			jQuery(".left").find("a").css({
					background:'url(../../editor/images/editor_left_a.png)'
			})
			left_a = true;
			
			jQuery(".center").find("a").css({
					background:'url(../../editor/images/editor_center.png)'
			})
			center_a = false;
			
			jQuery(".right").find("a").css({
					background:'url(../../editor/images/editor_right.png)'
			})
			right_a = false;
		}
	}
}

function center(){
	if(choose_decide()){
		ifrdoc.execCommand("JustifyCenter",false,null);
		if(center_a == true){
			jQuery(".center").find("a").css({
					background:'url(../../editor/images/editor_center.png)'
			})
			center_a = false;
			
		}else{
			jQuery(".center").find("a").css({
					background:'url(../../editor/images/editor_center_a.png)'
			})
			center_a = true;
			
			jQuery(".left").find("a").css({
					background:'url(../../editor/images/editor_left.png)'
			})
			left_a = false;
			
			jQuery(".right").find("a").css({
					background:'url(../../editor/images/editor_right.png)'
			})
			right_a = false;
		}
	}
}

function right(){
	if(choose_decide()){
		ifrdoc.execCommand("JustifyRight",false,null);
		if(right_a == true){
			jQuery(".right").find("a").css({
					background:'url(../../editor/images/editor_right.png)'
			})
			right_a = false;
		}else{
			jQuery(".right").find("a").css({
					background:'url(../../editor/images/editor_right_a.png)'
			})
			right_a = true;
			
			jQuery(".left").find("a").css({
					background:'url(../../editor/images/editor_left.png)'
			})
			left_a = false;
			
			jQuery(".center").find("a").css({
					background:'url(../../editor/images/editor_center.png)'
			})
			center_a = false;
		}
	}
}

function indentation(){
	if(choose_decide())
		ifrdoc.execCommand("Indent",false,null);
}

function outdent(){
	if(choose_decide())
		ifrdoc.execCommand("Outdent",false,null);
}

function link(){
	if(choose_decide())
		var sURL=window.prompt("请输入链接的目标地址 (e.g. http://www.hellophper.com):", "http://");
	ifrdoc.execCommand("CreateLink");
}

function code(){
	if(code_a == true){
			var text = jQuery("#edit_text")[0].value;//获取隐藏框内容
			ifrdoc.body.innerHTML = text;//复制到编辑框
			jQuery("#editor").show();
			jQuery("#edit_text").hide();
			jQuery(".code").find("a").css({
					background:'url(../../editor/images/editor_code.png)'
			})
			
			code_a = false;
		}else{
			var html = jQuery("iframe").contents()[0].body.innerHTML;//获取编辑框内容
			jQuery("#edit_text")[0].value = html;//复制到隐藏框
			jQuery("#editor").hide();
			jQuery("#edit_text").show();
			jQuery(".code").find("a").css({
					background:'url(../../editor/images/editor_code_a.png)'
			})
			code_a = true;
	}
}

function editor_save(){
	var html = jQuery("iframe").contents()[0].body.innerHTML;
	jQuery("#edit_text")[0].value = html;
	document.comment.submit();
}