﻿//阻止浏览器的默认行为
function stopDefault(e) {
//阻止默认浏览器动作(W3C)
if ( e && e.preventDefault )
e.preventDefault();
//IE中阻止函数器默认动作的方式
else
window.event.returnValue = false;
return false;
} 
//清除空白节点
function cleanWhitespaces(elem){
    var elem = elem || document;
    var parentElem = elem; 
    var childElem = parentElem.childNodes;
    var childElemArray = new Array;
    for (var i=0; i<childElem.length; i++){
        if (childElem[i].nodeType==1){
            childElemArray.push(childElem[i]);
        }
    }
    return childElemArray;
}

function cleanPX(num){
	var str1=num.toLowerCase();	
	var reg = new RegExp(/px/gm);
	str2 = str1.replace(reg,"");
	return str2;
}

function redLayoutStr(){
    //alert('测试');
    var father = document.getElementById("page1");
    var column = cleanWhitespaces(father);
    var divArray = new Array;
    
    if (document.all){
        var divArr=leftArr=centerArr=rightArr="";
        for (var i=0; i<column.length; i++){
            //alert(column[i].offsetLeft);
            var col = cleanWhitespaces(column[i]);
            for (var c=0;c<col.length;c++){
            var obj = CurrentStyle(col[c]);
                switch (column[i].id){
                    case "left":
//                        if (c==col.length)
//                            var divH=parseInt(col[c].offsetHeight-5)<0?0:parseInt(col[c].offsetHeight-5);
//                        else
//                            var divH=parseInt(col[c].offsetHeight-33)<0?0:parseInt(col[c].offsetHeight-33);
                          if (col[c].id != "" && col[c].id != "NaN" && col[c].id != "undefined"){
                            var objWidth,objHeight,marTop;
                            if (col[c].style.display=="none"){
                                objWidth = 250;
                                objHeight = 150;
                                marTop = 0;
                            }else{
                                objWidth = col[c].offsetWidth;
                                objHeight = col[c].offsetHeight;
                                marTop = col[c].offsetTop;
                            }
                            leftArr+="[\""+col[c].id+"\","+objWidth+","+objHeight+","+column[i].offsetLeft+","+marTop+","+obj.zIndex+"],";
                          }
                    break;
                    case "center":
//                        if (c==col.length)
//                            var divH=parseInt(col[c].offsetHeight-5)<0?0:parseInt(col[c].offsetHeight-5);
//                        else
//                            var divH=parseInt(col[c].offsetHeight-33)<0?0:parseInt(col[c].offsetHeight-33);
                    //alert(col[i].offsetWidth);
                          if (col[c].id != "" && col[c].id != "NaN" && col[c].id != "undefined"){
                            var objWidth,objHeight,marTop;
                            if (col[c].style.display=="none"){
                                objWidth = 250;
                                objHeight = 150;
                                marTop = 0;
                            }else{
                                objWidth = col[c].offsetWidth;
                                objHeight = col[c].offsetHeight;
                                marTop = col[c].offsetTop;
                            }
                            centerArr+="[\""+col[c].id+"\","+objWidth+","+objHeight+","+column[i].offsetLeft+","+marTop+","+obj.zIndex+"],";
                          }
                    break;
                    case "right":
//                        if (c==col.length)
//                            var divH=parseInt(col[c].offsetHeight-5)<0?0:parseInt(col[c].offsetHeight-5);
//                        else
//                            var divH=parseInt(col[c].offsetHeight-33)<0?0:parseInt(col[c].offsetHeight-33);
                    //alert(col[i].offsetWidth);
                          if (col[c].id != "" && col[c].id != "NaN" && col[c].id != "undefined"){
                            var objWidth,objHeight,marTop;
                            if (col[c].style.display=="none"){
                                objWidth = 250;
                                objHeight = 150;
                                marTop = 0;
                            }else{
                                objWidth = col[c].offsetWidth;
                                objHeight = col[c].offsetHeight;
                                marTop = col[c].offsetTop;
                            }
                            rightArr+="[\""+col[c].id+"\","+objWidth+","+objHeight+","+column[i].offsetLeft+","+marTop+","+obj.zIndex+"],";
                          }
                    break;
                }
            }
        }
        divArr=leftArr+centerArr+rightArr;
    }else{
        //FF字符串处理
        for (var i=0; i<column.length; i++){
            var col = cleanWhitespaces(column[i]);
            for (var c=0; c<col.length; c++){
                divArray.push(col[c]);
            }
        }
        if (divArray!=null || divArray!=undefined){
            var divArr="";
            for (var s=0; s<divArray.length; s++){
                var obj = CurrentStyle(divArray[s]);
                var zIndex=obj.zIndex=="auto"?0:obj.zIndex;
                    if (divArray[s].id != "" && divArray[s].id != "NaN" && divArray[s].id != "undefined"){
                        var objWidth,objHeight;
                        if (divArray[s].style.display=="none"){
                            objWidth = 250;
                            objHeight = 150;
                        }else{
                            objWidth = divArray[s].clientWidth;
                            objHeight = divArray[s].clientHeight;
                        }
                        divArr += "[\""+divArray[s].id+"\","+objWidth+","+objHeight+","+divArray[s].offsetLeft+","+divArray[s].offsetTop+","+zIndex+"],";
                    }
            }
        }
    }
    divArr = "{\"pages\":["+divArr.substring(0,divArr.length-1)+"]}";
    document.getElementById("hidLayOut").value = divArr;
    //return divArr;
    //获取字符串
//	var showV = document.getElementById("getStr");
//	    showV.value=divArr;
}




function getValue(){
	var father = cleanWhitespaces($("father"));
	var stopArr = "{\"pages\":[";
	for (var i=0;i<father.length; i++){
		//alert(father[i].id);
		stopArr+="[\""+father[i].id+"\","+
					father[i].style.width+","+
					father[i].style.height+","+
					father[i].style.left+","+
					father[i].style.top+","+
					father[i].style.zIndex
					+"],";
	}
	$("hidLayOut").value=cleanPX(stopArr).substring(0,cleanPX(stopArr).length-1)+"]}";
	
}
function changeFatherCurrentStyle(){
	var father = cleanWhitespaces($("father"));
	var fatherH = parseInt(CurrentStyle($("father")).height) || 0;
	var childH = new Array;
		//childH.push(fatherH)
	for (var i=0;i<father.length; i++){
		childH.push(((parseInt(father[i].offsetTop)+parseInt(cleanPX(father[i].style.height)))));
	}
	var newH = Math.max.apply(fatherH,childH);
	$("father").style.height = newH + "px";
	$("fatherH").value=$("father").style.height;
}

function startFree(){
	//var divArr = getCookie('divArr');
//	var defaultLayout = '{"pages":[["div1",100,100,10,10,1], ["div2",100,100,150,150,2], ["div3",100,100,280,280,3]]}';
//{"pages":[["div27",238,8,0,456,7],["div28",238,8,24,183,5],["div29",0,0,0,0,0],["div32",238,8,42,304,6],["div888",238,0,712,0,0],["div999",0,0,712,0,0]]}
//	var value = getCookie('divArr') || defaultLayout;
//    var value='{"pages":[["div27",238,8,0,456,7],["div28",238,8,24,183,5],["div29",0,0,0,0,0],["div32",238,8,42,304,6],["div888",238,0,712,0,0],["div999",0,0,712,0,0]]}';
    var value = $("hidLayOut").value;
    //alert(value);
    if(value=="")
    {
        return false;
    }
	var obj = eval('(' + value + ')');
	var divArr = obj;
	//读取父层高度
	var fatherW = parseInt(CurrentStyle($("Container")).width);
	//alert(fatherW);
	//alert(divArr.length);
	for (var i=0; i<divArr["pages"].length; i++){
		//缩放
		var divStr=divArr["pages"][i][0]+"_Handler";
		var rs = new Resize(divArr["pages"][i][0],{
									  			Max:		true,
												//mxContainer:"father",
												mxRight:	fatherW,
									  			onResize:	function(){
																changeFatherCurrentStyle();
															},
									  			onStop:		function(){
																getValue();
															}
									  		});
		rs.Set(divStr, "right-down");
		//拖动	
		new xDrag(divArr["pages"][i][0],{
						   		Limit: true,
								mxContainer: "father", 
								Width: 		divArr["pages"][i][1], 
								Height: 	divArr["pages"][i][2], 
								marginLeft: divArr["pages"][i][3],
								marginTop:	divArr["pages"][i][4], 
								zIndex: 	divArr["pages"][i][5],
								onMove:		function(){
												changeFatherCurrentStyle();
											},
								onStop:		function(){
												getValue();
											}
								});
		//右键菜单
		//new csMenu($(divArr["pages"][i][0]), $("h3RightMenu"));
	}
	//执行一次父层高度设置
	changeFatherCurrentStyle();
}
//window.onload=startFree;


function saveLayoutStr(){
    if(!(confirm("确认保存吗?")))
    {
        return;
    }
    SavePicIndex();
    var status=$("hidLayOutTag").value;
    if (status=='1'){
        
        XDrag.save();
    }else{
       
        getValue();
        alert("保存成功!");
    }
    //alert($("hidLayOut").value);
}


//首页单页图文类调整大小
function testRe(id){
	var handle=id+"_Handler";
var rs = new Resize($(id),{
							Max:		true,
							//mxRight:	fatherW
							onResize:	function(){
											ImgStatus(id);
										}
//							onStop:		function(){
//											getValue();
//										}
						});
		rs.Set($(handle), "right-down");
}
var ImgStatus=function(obj){
	var Width=$(obj).style.width;
	var Height=$(obj).style.height;
	var father=$(obj).parentNode;
	var img=cleanWhitespaces($(obj))[0];
	//console.log(Width,Height);
	//alert(Width+Height);
	img.style.width=father.style.width=Width;
	img.style.height=father.style.height=Height;
}

