﻿String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "");}
function radioCheck(checkId){$(checkId).checked = true;}
function getXY(obj){
var a = new Array();var t = obj.offsetTop;var l = obj.offsetLeft;var w = obj.offsetWidth;var h = obj.offsetHeight;
while(obj=obj.offsetParent){t+=obj.offsetTop;l+=obj.offsetLeft;}
a[0] = t;a[1] = l;a[2] = w;a[3] = h;return a;
}
function convArray(array){
var nArray = [];
for (var i=0;i<array.length;i++) nArray.push(array[i]);
return nArray;
}
function hasClass(w,Name){
	var hasClass = false;
	w.className.split(' ').each(function(s){	if (s == Name) hasClass = true;	});
	return hasClass;
}
Array.prototype.each=function(func){for(var i=0,l=this.length;i<l;i++) {func(this[i],i);};};
function getElementsByClassName(cn,tag,root){
var elems;
if(!root||!tag)	 elems =document.getElementsByTagName("*")||document.all;
else elems=root.getElementsByTagName(tag);
var elemList = [];
convArray(elems).each(function(e){if(hasClass(e,cn)){elemList.push(e);}});
    return convArray(elemList);
}
function $(A){return document.getElementById(A)}
String.prototype.checkSpec = function(){
if(this.search(/[~'"@#$?&<>\/]/g)!=-1){
return false;
}else{return true}
}
function checkTextInput(el,len,defaultValue,title) {
if (!el) return;
el.value = el.value.trim();
if (el.value.length == 0) {alert(title + "不能为空");el.value = defaultValue;return;}
if (el.value.length > len) {alert(title + "不能超过"+len+"个字");el.value = el.value.substring(0,len);return;}
if (!el.value.checkSpec()) {alert(title + "不能包含特殊字符~'\"\/@#$?&<>");el.value = defaultValue;return;}
}
function checkInputSpecStr(el){
if(!el.value.checkSpec()){alert("不能包含特殊字符~'\"\/@#$?&<>");el.value = el.value.substring(0,el.value.length-1);return;}
}

function openDesignDiv(fatherDivId,openId,firstBrotherId){
document.onclick();
ControlManager.open(fatherDivId);
qiehuan(getElementsByClassName("check","div",$(openId).parentNode)[0],openId);
}

function qiehuan(currentDiv,toCheckId){
currentDiv.className = "uncheck";
$(toCheckId).className = "check";
}

function showTips(imgObj){
var tips = $('tipsDiv');
tips.style.display = "block";
tips.innerHTML = imgObj.getAttribute("topicName");
}

function changeXBg(radioId,className,imgSrc,desId,desX,desY){
radioCheck(radioId);
ControlManager.backgroundImgSet(className,imgSrc);
if(desId&&desX&&desY){
ControlManager.heightSet(className,'200px');
ControlManager.descriptionSet(desId,desX,desY);
}
}

function bgSetChange(type){
if(type==1){
$("bgSetTitle").className = "bgSetTitleIn";
$("bgSetContIn").style.display = "block";
$("bgSetContOut").style.display = "none";
$("bgSetTitle1").className = "check3";
$("bgSetTitle2").className = "uncheck3";
}else{
$("bgSetTitle").className = "bgSetTitleOut";
$("bgSetContOut").style.display = "block";
$("bgSetContIn").style.display = "none";
$("bgSetTitle1").className = "uncheck3";
$("bgSetTitle2").className = "check3";
}
}
/**
* 选择背景图片的时候当前选中状态的切换
* @param {Object} thisImg
*/
function setBackgroundImgCheck(thisImg,root){
if(!thisImg)	return;
var els=getElementsByClassName("check2","img",root);
if(els.length>0){
var nowCheck = els[0];
if(nowCheck!=thisImg){
nowCheck.className = "uncheck2";
thisImg.className = "check2";
}
}else	 thisImg.className = "check2";
}
function createUploadImg(uploadImgRoot,iWidth,iHeight,className,checkClassName,path,imgHeight,id){
try{
var imgObj = document.createElement("img");
if(id)	imgObj.id=id;
imgObj.src = path;
imgObj.className = checkClassName;
imgObj.width = iWidth;
imgObj.height = iHeight;
uploadImgRoot.innerHTML = "";
uploadImgRoot.appendChild(imgObj);
uploadImgRoot.style.height = iHeight + "px";
uploadImgRoot.style.width = iWidth + "px";
imgObj.onclick=function(){
ControlManager.backgroundImgSet(className,imgObj.src);
if(imgHeight){
ControlManager.heightSet(className,imgHeight+"px");
}
}
if(checkClassName.substring(0,2)!="un"){
	/*if(imgHeight){
	ControlManager.heightSet(className,imgHeight+"px");
	}
	ControlManager.backgroundImgSet(className,path);*/
	imgObj.onclick();
}
}catch(e){
//alert("上传后初始化出错！");
}

}

function moveToNowTopic(sTopic,nTopicId){
	$(nTopicId).src=sTopic.src;	
}

function defCheckAndChangeBg(checkRoot,className,checkClassName){
var nowCheckImg = getElementsByClassName(checkClassName,"*",checkRoot)[0];
if(!nowCheckImg){return;}
nowCheckImg.onclick();
nowCheckImg.parentNode.onclick();
}

function defCheckAndChangeBg2(checkRoot,className,checkClassName){
var nowCheckImg = getElementsByClassName(checkClassName,"*",checkRoot)[0];
if(!nowCheckImg){return;}
var backgroundValue = nowCheckImg.style.background;
if(backgroundValue.indexOf("#")!=-1){
ControlManager.backgroundSet(className,backgroundValue);
}else{
backgroundValue = backgroundValue.split(')')[0].split('(')[1];//截取URL部分
ControlManager.backgroundImgSet(className,backgroundValue);
}
}

function setLayoutImgCheck(thisImg){
var root = thisImg.parentNode;
var nowCheck = getElementsByClassName("aCheck","a",root)[0];
if(nowCheck!=thisImg){
nowCheck.className = "fl aUnCheck";
thisImg.className = "fl aCheck";
}
}

function doSave(isFormal, doneCallback, formid) {
	showLoading();
	var form = $(formid);
	callback = doneCallback;
	form.submit();
}

function switchPageDone(url) {
	return function() {location.href = url;}
}

function refresh() {
	var url = location.href;
	if (url.charAt(url.length - 1) == "#") { url = url.substring(0, url.length-1);}
	location.href = url;
}

function changelibg(obj,type){
var tempArr = obj.getElementsByTagName("li")
if(type==1){
for(var i=0;i<tempArr.length;i++){
tempArr[i].style.background = "#EAEAEA";
}
}else{
for(var i=0;i<tempArr.length;i++){
tempArr[i].style.background = "#fff";
}
}
}

function moveup(id){
var innerStr1 = $(id).innerHTML;
var innerStr2 = "";
var index = id.split("_")[1];
if(index>1){
var idName = "move_"+(index-1);
innerStr2 = $(idName).innerHTML;
$(id).innerHTML = innerStr2;
$(idName).innerHTML = innerStr1;
}
}

function movedown(id){
var innerStr1 = $(id).innerHTML;
var innerStr2 = "";
var index = parseInt(id.split("_")[1]);
if(index<moveMaxNum){
var tempIndex = index+1;
var idName = "move_"+ tempIndex;
innerStr2 = $(idName).innerHTML;
$(id).innerHTML = innerStr2;
$(idName).innerHTML = innerStr1;
}
}

function init_editList(){
try{
//editList = getElementsByClassName("editList");
for(var i=0;i<editList.length;i++){
editList[i].onclick=function(){
event.cancelBubble=true;
}
}
var editCtrlList = getElementsByClassName("editCtrl");
for(var i=0;i<editCtrlList.length;i++){
editCtrlList[i].onclick=function(){
document.onclick();
event.cancelBubble=true;
for(var i=0;i<editList.length;i++){
editList[i].style.display = "none";
}
var passiveDiv = getElementsByClassName("editList","div",this.parentNode.parentNode.parentNode.parentNode)[0];
passiveDiv.style.display = "block";
}
}
}catch(e){
alert("初始化编辑按钮的事件绑定出错！");
}
}

function moveModuleUp(currentDiv,blockMargin){
BLOCKMARGIN = blockMargin;
var root = currentDiv.parentNode;
currentMoveList = getElementsByClassName("moveChild","div",root);
if(getUpDiv(currentDiv)){
var upDiv = getUpDiv(currentDiv);
var tempDivUp = createTempDiv(currentDiv,0);
var tempDivdown = createTempDiv(upDiv,0);
var absoultDivToDown = modifyDiv(upDiv,'relative');
upDiv.parentNode.insertBefore(tempDivdown ,upDiv);
absoultDivToDown.style.position = 'absolute';
//YAHOO.util.Dom.addClass(absoultDivToDown, 'filter2');//将要移动的层设为半透明
var absoultDivToUp = this.modifyDiv(currentDiv,'relative');
currentDiv.parentNode.insertBefore(tempDivUp ,currentDiv);
absoultDivToUp.style.position = 'absolute';
//YAHOO.util.Dom.addClass(absoultDivToUp, 'filter2');//将要移动的层设为半透明
var father = this;
var newDivUp = absoultDivToUp;    //复制移动层;
var newDivDown = absoultDivToDown;    //复制移动层;
newDivUp.style.position = "";
newDivUp.style.width = "";
newDivUp.style.height = "";
newDivUp.style.marginTop = '0px'
newDivUp.style.marginLeft = '0px'
newDivUp.style.marginBottom = BLOCKMARGIN + 'px'
newDivDown.style.position = "";
newDivDown.style.width = "";
newDivDown.style.height = "";
newDivDown.style.marginTop = '0px'
newDivDown.style.marginLeft = '0px'
newDivDown.style.marginBottom = BLOCKMARGIN + 'px'
absoultDivToDown.parentNode.removeChild(absoultDivToDown);
absoultDivToUp.parentNode.removeChild(absoultDivToUp);
tempDivUp.parentNode.insertBefore(newDivDown,tempDivUp);
tempDivdown.parentNode.insertBefore(newDivUp,tempDivdown);
tempDivUp.parentNode.removeChild(tempDivUp);//删除原来的临时占位层；
tempDivdown.parentNode.removeChild(tempDivdown);
}else{
return false;
}
}

function moveModuleDown(currentDiv,blockMargin){
BLOCKMARGIN = blockMargin;
var root = currentDiv.parentNode;
currentMoveList = getElementsByClassName("moveChild","div",root);
if(getDownDiv(currentDiv)){
var downDiv = getDownDiv(currentDiv);
var tempDivUp = createTempDiv(currentDiv,0);
var tempDivdown = createTempDiv(downDiv,0);
var absoultDivToDown = modifyDiv(currentDiv,'relative');
currentDiv.parentNode.insertBefore(tempDivUp ,currentDiv);
absoultDivToDown.style.position = 'absolute';
//YAHOO.util.Dom.addClass(absoultDivToDown,'filter2');//将要移动的层设为半透明
var absoultDivToUp = modifyDiv(downDiv,'relative');
downDiv.parentNode.insertBefore(tempDivdown ,downDiv);
absoultDivToUp.style.position = 'absolute';
//YAHOO.util.Dom.addClass(absoultDivToUp,'filter2');//将要移动的层设为半透明
var newDivUp = absoultDivToUp;    //复制移动层;
var newDivDown = absoultDivToDown;    //复制移动层;
newDivUp.style.position = "";//还原为相对定位
newDivUp.style.width = "";
newDivUp.style.height = "";
newDivUp.style.marginTop = '0px'
newDivUp.style.marginLeft = '0px'
newDivUp.style.marginBottom = BLOCKMARGIN + 'px'
newDivDown.style.position = "";
newDivDown.style.width = "";
newDivDown.style.height = "";
newDivDown.style.marginTop = '0px'
newDivDown.style.marginLeft = '0px'
newDivDown.style.marginBottom = BLOCKMARGIN + 'px'
absoultDivToDown.parentNode.removeChild(absoultDivToDown);
absoultDivToUp.parentNode.removeChild(absoultDivToUp);
tempDivUp.parentNode.insertBefore(newDivUp,tempDivUp);
tempDivdown.parentNode.insertBefore(newDivDown,tempDivdown);
tempDivUp.parentNode.removeChild(tempDivUp);//删除原来的临时占位层；
tempDivdown.parentNode.removeChild(tempDivdown);
}else{
return false;
}
}

function getUpDiv(currentDiv){
for(var i=0;i<currentMoveList.length;i++){
if(currentMoveList[i] == currentDiv){
if(i>0){
return currentMoveList[i-1];
}
}
}
return null;
}

function getDownDiv(currentDiv){
for(var i=0;i<currentMoveList.length;i++){
if(currentMoveList[i] == currentDiv){
if(currentMoveList[i+1]){
return currentMoveList[i+1];
}
}
}
return null;
}

function createTempDiv(formerObject,hasBorder){
var objHeight = formerObject.offsetHeight;
var containerTempObject= document.createElement("div");
containerTempObject.id = "tempContentDiv";
if(hasBorder == 1){
containerTempObject.style.height = objHeight -2 +"px";
//if(Browser.isIE)
containerTempObject.style.marginBottom = BLOCKMARGIN + 'px';
/*else
containerTempObject.style.marginBottom = BLOCKMARGIN + 'px';
containerTempObject.className = 'tmpbd'*/
}else{//生成不带虚线边框占位层；
containerTempObject.style.height = objHeight +"px";
containerTempObject.style.marginBottom = BLOCKMARGIN + 'px';
}
return containerTempObject;
}

function modifyDiv(formerObject,p){
var referenceO = getXY(formerObject)
formerObject.style.position = p;
formerObject.style.marginTop = '0px'
formerObject.style.marginLeft = '0px'
formerObject.style.zIndex = 1000;
var offsetBoderWidth=0,offsetBoderHeight=0;
var boderWidth = parseInt(formerObject.style.borderWidth);
if(boderWidth>0){
offsetBoderWidth = -2*boderWidth;
offsetBoderHeight = -2*boderWidth;
}
formerObject.style.width = referenceO[2] + offsetBoderWidth + 'px';
formerObject.style.height = referenceO[3] + offsetBoderHeight + 'px';
formerObject.style.top = referenceO[0] + 'px';
formerObject.style.left = referenceO[1] + 'px';
return formerObject;
}

function editContent(url) {
if(url.indexOf("http") < 0) {
	openDesignDiv('setAdvance',url,'editLog');
} else {
	doSave(false, openPageDone(url), 'editContent');
}
}

function openPageDone(url) {
	return function() {hideLoading(); window.open(url, "newWindow");}
}

function editModuleTitle(currentDiv){
var oTitle = getElementsByClassName("titleName","span",currentDiv)[0];//获取当前标题对象
var tempValue = oTitle.innerHTML;
var inputDiv = document.createElement("div");
inputDiv.className = "rel";
inputDiv.innerHTML = "<div class=\"abs\" style=\"top:3px;left:-5px;z-index:999;\"><input type=\"text\" maxlength=\"6\" class=\"titleLinkColor\" style=\"height:20px;line-height:20px;font-size:14px;font-weight:bold;color:#000;\" size=\"14\"/></div>";
oTitle.parentNode.insertBefore(inputDiv,oTitle);
var oInput = inputDiv.getElementsByTagName("input")[0];
oInput.focus();
oInput.value = oTitle.innerHTML;

oInput.attachEvent("onblur",function(){setValue(oInput,oTitle,inputDiv)});
oInput.attachEvent("onkeyup",function(){check(oInput)});

}

function setValue(oInput,oTitle,inputDiv){
if(oInput.value.trim()==''){
oInput.value = tempValue;
}
oTitle.innerHTML = oInput.value;
inputDiv.parentNode.removeChild(inputDiv);
}

function check(oInput){
if(!oInput.value.checkSpec()){
oInput.value = oInput.value.substring(0,oInput.value.length-1);
}
}

function removeModule(currentDiv){
var ml =  getMoveBoxLength(currentDiv);
var emptyDiv;
if(ml==1){
var className="",className2="";
if(currentDiv.parentNode.id=="content1"){
className ='edit_left';
className2 = 'editListLeft';
emptyDiv = createEmptyDiv(className,className2,true);
}else{
className ='edit_right';
className2 = 'editListRight';
emptyDiv = createEmptyDiv(className,className2,false);
}
}

var nowClassName = getElementsByClassName("editdiv","div",currentDiv)[0].parentNode.className;
var tempDiv = document.createElement("div");
tempDiv.style.height = getXY(currentDiv)[3] + "px";
tempDiv.style.overflow = "hidden";

currentDiv.parentNode.insertBefore(tempDiv,currentDiv);
currentDiv.parentNode.removeChild(currentDiv);
if(emptyDiv){
tempDiv.parentNode.insertBefore(emptyDiv,tempDiv);
init_editList();
}
tempDiv.parentNode.removeChild(tempDiv);
}

function getMoveBoxLength(currentDiv){
return getElementsByClassName("moveChild","div",currentDiv.parentNode).length;
}

function createEmptyDiv(className,className2,isLeft){
var emptyDiv = document.createElement("div");
emptyDiv.className = "bodyCont moveChild empty";
emptyDiv.style.marginBottom = "7px";
emptyDiv.innerHTML ="<div class=\"clr\"></div>"+
"<div class=\"rel fl zindex900\">"+
"<div class=\""+className+"\">"+
"<div class=\"editdiv\"><a href=\"javascript:void(0);\" class=\"editCtrl\"><img border=\"0\" src='"+contextPath+"/resource/css/images/edit.gif'/></a></div>"+
"</div>"+
"</div>"+
"<div class=\"rel fl size0 zindex999\">"+
"<div class=\"editList "+className2+"\">"+
"<ul>"+
"<li onmouseover=\"this.className='onHover';\" onmouseout=\"this.className='';\"><a href=\"javascript:void(0);\" class=\"moveUp block ml10px\" onclick=\"moveModuleUp(this.parentNode.parentNode.parentNode.parentNode.parentNode,7);\" onfocus=\"this.blur()\">上移板块</a></li>"+
"<li onmouseover=\"this.className='onHover';\" onmouseout=\"this.className='';\"><a href=\"javascript:void(0);\" class=\"movedown block ml10px\" onclick=\"moveModuleDown(this.parentNode.parentNode.parentNode.parentNode.parentNode,7);\" onfocus=\"this.blur()\">下移板块</a></li>"+
"<li onmouseover=\"this.className='onHover';\" onmouseout=\"this.className='';\"><a href=\"javascript:void(0);\" class=\"editCont block ml10px\" onclick=\"showAddListDiv("+isLeft+",this);event.cancelBubble=true;\" onfocus=\"this.blur()\">编辑内容</a></li>"+
"<li onmouseover=\"this.className='onHover';\" onmouseout=\"this.className='';\"><a href=\"javascript:void(0);\" class=\"add block ml10px\" onclick=\"insertModule(this.parentNode.parentNode.parentNode.parentNode.parentNode,'"+className+"');\" onfocus=\"this.blur()\">插入板块</a></li>"+
"<li onmouseover=\"this.className='onHover';\" onmouseout=\"this.className='';\"><a href=\"javascript:void(0);\" class=\"del block ml10px\" onclick=\"removeModule(this.parentNode.parentNode.parentNode.parentNode.parentNode);\" onfocus=\"this.blur()\">删除板块</a></li>"+
"</ul>"+
"</div>"+
"</div>"+
"<div style=\"height:80px;\"></div>";
return emptyDiv;
}

function insertModule(currentDiv,className){
var className2;
var isLeft;
if(className=='edit_left'){
className2 = 'editListLeft';
isLeft = true;
if(getMoveBoxLength(currentDiv)>=leftMaxLength){
return;
}
}else{
className2 = 'editListRight';
isLeft = false;
if(getMoveBoxLength(currentDiv)>=rightMaxLength){
return;
}
}
var tempDiv = document.createElement("div");
tempDiv.style.height = "0px";
tempDiv.style.overflow = "hidden";
var emptyDiv = createEmptyDiv(className,className2,isLeft)
currentDiv.parentNode.insertBefore(tempDiv,currentDiv);
currentDiv.parentNode.insertBefore(emptyDiv,currentDiv);
tempDiv.parentNode.removeChild(tempDiv);
init_editList();
}

function showAddListDiv(isLeft,thisA){
nowEditDiv = thisA.parentNode.parentNode.parentNode.parentNode.parentNode;
var thisPos = getXY(thisA);
var offsetX = 0;
if(thisPos[1]>390){
offsetX = -295;
}else{
offsetX = 105;
}
if(isLeft==true){
$('editcontentindexmain').style.display = "none";
$('editcontentside').style.top = thisPos[0]-60+"px";
$('editcontentside').style.left = thisPos[1]+offsetX+"px";
$('editcontentside').style.display = "block";
$('editcontentside').style.zIndex = "999";
}else{
$('editcontentside').style.display = "none";
$('editcontentindexmain').style.top = thisPos[0]-60+"px";
$('editcontentindexmain').style.left = thisPos[1]+offsetX+"px";
$('editcontentindexmain').style.display = "block";
$('editcontentindexmain').style.zIndex = "999";
}
}
function setBlockId(insertId){
if(nowEditDiv.id==''){
nowEditDiv.id = insertId;
}
}

function isExisted(insertId,contentId){
var list = getElementsByClassName("moveChild","div",$(contentId));
for(var i=0;i<list.length;i++){
if(list[i].id==insertId){
return true;
break;
}
}
return false;
}

function uploadLog(url,height){
if($('logTable')){
$('logEditImg').src=url;
$('log_img').src=url;
}else{
$('log').innerHTML ="<table id=\"logTable\" width=\"120\" height=\"90\"><tr><td align=\"center\" valign=\"middle\"><img id=\"log_img\" height=\""+height+"\" src=\""+url+"\"/></td></tr></table>"
$('logEdit').innerHTML = "<img id=\"logEditImg\" src=\""+url+"\" style=\"border:1px solid #DDDDDD;\"/><a href=\"#\" onclick=\"deleteLog();\" onfocus=\"this.blur();\" onmousedown=\"return aliclick(this,'?tracelog=dndiy_wrd_dellogo')\">删除标志</a>";
}
$('logEditImg').height = height;
$('log_img').height = height;
}

function deleteLog(){
if(confirm("是否真要删除该LOGO？")) {}else{return false;}
if($('logTable')){
$('logTable').parentNode.removeChild($('logTable'));
}
$('logEdit').innerHTML = "";
}
function addModule(id,title,isLeft){
	var cm;
	if(isLeft)	cm=new CommonModule(id,title);
	else	cm=new CommonModule(id,title,"Right",true);
	var pe=nowEditDiv.parentElement;
	pe.insertBefore(cm.getUI(),nowEditDiv);
	pe.removeChild(nowEditDiv);
	init_editList();
}
function loading_show(el,el_mask){
el_mask.style.display='block';
el.style.display='block';
el_mask.style.height=document.body.clientHeight+'px';
el_mask.style.width=document.body.clientWidth+'px';
el_mask.style.zIndex='999';
el.style.zIndex='999';
}
function loading_hidden(el,el_mask){
el_mask.style.display='none';
el.style.display='none';
}
