/**
 *  Author: Robin Pan (htmlor [at] gmail.com)
 *
 *
 *  通用方法（平台无关）
 *
/*----------------------------------------------------------------*/


/* ===== functions ===== */

// 浏览器检查
function Browser(){
    this.isIE = ((document.all && window.ActiveXObject) ? true : false);
    
    this.isIE5Up = ((this.isIE && document.getElementById) ? true : false);
    
    this.isFirefox = ((document.implementation && document.implementation.createDocument && !window.opera) ? true : false);
    
    this.isOpera = (window.opera ? true : false);
    
    this.isMozilla = ((navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) ? true : false);
}

// 取得当前时间戳
function getTimeStamp(){
    var d = new Date();
    return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds());
}

// 取得字符串的实际长度（全角算2个字符）
function getStrActualLen(sChars){
    return sChars.replace(/[^\x00-\xff]/g,"xx").length;
}

// 占用空间计算
function getFriendlyFileSize(bytes){
    var str;
    if(bytes >= 1024 * 1024){
        str = formatFloat(bytes / (1024 * 1024), 1) + " MB";
    }
    else if(bytes >= 1024){
        str = formatFloat(bytes / 1024, 1) + " KB";
    }
    else{
        str = formatFloat(bytes / 1024, 2) + " KB";
    }
    return str;
}

// 得到固定小数位的数字
function formatFloat(fNumber, iLength){
    if(iLength == null){
        return Math.round(fNumber);
    }
    
    // 是整数，不操作
    if(Math.round(fNumber) == fNumber){
        return fNumber;
    }
    
    // 小数位数刚刚好，不操作
    var pattern = eval("/^\\d*?\\.?\\d{0,"+ iLength +"}$/");
    if(pattern.test(fNumber.toString())){
        return fNumber;
    }
    
    var P, x, y;
    P = Math.pow(10, iLength);
    // 乘以10的n次方，再取整
    x = Math.round(fNumber * P);
    // 
    if(x < P){
        y = parseFloat(x / P);
    }
    // 
    else{
        // 在要取的小数位前加小数点，再转换为数字
        pattern = eval("/(\\d{"+ iLength +"})$/");
        y = parseFloat(x.toString().replace(pattern, ".$1"));
    }
    return y;
}
// 得到两个数字之间的随机数
function getRandomNumber(iMin, iMax){
    var num = Math.floor(Math.random() * (iMax-iMin+1)) + iMin;
    return num;
}

// 
function getInterceptedStr(sSource, iLen){
    if(getStrActualLen(sSource) <= iLen){
        return sSource;
    }
    var ELIDED = "...";
    var str = "";
    var l = 0;
    var schar;
    for(var i=0; schar=sSource.charAt(i); i++){
        str += schar;
        l += (schar.match(/[^\x00-\xff]/) != null ? 2 : 1);
        if(l >= iLen - ELIDED.length){
            break;
        }
    }
    str += ELIDED;
    return str;
}

// 
function getCharsFromHtml(sHtml){
    var browser = new Browser();
    var chars = sHtml;
    // 转换html标记要在前面
    chars = chars.replace(/<br(\s?\/)?>/ig, (browser.isFirefox ? "\n" : "\r\n"));
    chars = chars.replace(/&nbsp;/g, " ");
    chars = chars.replace(/(&nbsp;){4}/g, " ");
    chars = chars.replace(/&lt;/g, "<");
    chars = chars.replace(/&gt;/g, ">");
    chars = chars.replace(/&quot;/g, "\"");
    chars = chars.replace(/&#039;/g, "'");
    // 转换&符号要在后面（防止把类似于&amp;nbsp;的过度转换）
    chars = chars.replace(/&amp;/g, "&");
    return chars;
}
// 
function getHtmlFromChars(sChars){
    var browser = new Browser();
    var html = sChars;
    // 转换&符号要在前面（防止把类似于&nbsp;的过度转换）
    html = html.replace(/&(?!#\d+;|[a-zA-Z]+;)/g, "&amp;");
    html = html.replace(/ /g, "&nbsp;");
    html = html.replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;");
    html = html.replace(/</g, "&lt;");
    html = html.replace(/>/g, "&gt;");
    html = html.replace(/"/g, "&quot;");
    html = html.replace(/'/g, "&#039;");
    // 转换html标记要在后面
    html = html.replace((browser.isFirefox ? /\n/g : /\r\n/g), "<br>");
    return html;
}

// 比较两个数组是否完全一致
function isIdenticalArrays(aSourceA, aSourceB){
    if(aSourceA.length != aSourceB.length){
        return false;
    }
    for(var i=0; i<aSourceA.length; i++){
        if(aSourceA[i] != aSourceB[i]){
            return false;
        }
    }
    return true;
}
// 得到一个元素在数组中的索引值
function getIndexInArray(oValue, aSource){
    for(var i=0; i<aSource.length; i++){
        if(aSource[i] == oValue){
            return i;
        }
    }
    return -1;
}
// 得到一个元素在对象中的键值
function getKeyInObject(oValue, oSource){
    for(var key in oSource){
        if(oSource[key] == oValue){
            return key;
        }
    }
    return null;
}
// 得到一个元素在对象中的键值
function keyExists(sKey, oSource){
    for(var key in oSource){
        if(key == sKey){
            return true;
        }
    }
    return false;
}

// 执行脚本代码
function execScript(sText){
    var str = sText.replace(/\n+|\r+|\t+/g, "");
    
    eval(str);
}

// 是否合法的格式
function isValidFormat(sSource, sFormat){
    var re = getFormatRegExp(sFormat);
    var valid = re.test(sSource);
    
    return valid;
}

// 得到匹配通用格式的正则表达式
function getFormatRegExp(sFormat){
    var re;
    
    switch(sFormat){
        case "email":
            re = /^[\w\.\-]+?@[0-9a-zA-Z\-]+?(\.\w+?)+$/;
            break;
        case "phone":
            re = /^\d{3,4}\-\d{7,8}$/;
            break;
        case "cellphone":
            re = /^\d{11}$/;
            break;
        case "zipcode":
            re = /^\d{6}$/;
            break;
        case "idcard":
            re = /^\d{15}|\d{18}$/;
            break;
        default:
            var matches;
            if(sFormat.indexOf("digit") == 0){
                matches = sFormat.match(/^digit(\d+)(\-(\d+))?$/);
                re = new RegExp("^\\d{" + matches[1] + (isEmpty(matches[2]) ? "" : "," + matches[3]) + "}$");
                //window.alert(re);
            }
            else if(sFormat.indexOf("pass") == 0){
                matches = sFormat.match(/^pass(\d+)(\-(\d+))?$/);
                re = new RegExp("^\\S{" + matches[1] + (isEmpty(matches[2]) ? "" : "," + matches[3]) + "}$");
                //window.alert(re);
            }
            else if(sFormat.indexOf("char") == 0){
                matches = sFormat.match(/^char(\d+)(\-(\d+))?$/);
                re = new RegExp("^.{" + matches[1] + (isEmpty(matches[2]) ? "" : "," + matches[3]) + "}$");
                //window.alert(re);
            }
            else{
                re = null;
            }
    }
    
    return re;
}

function isNumeric(vValue){
    var pattern = /^[\.\d]+$/;
    return (pattern.test(vValue));
}
function isEmpty(vValue){
    return (vValue == "" || vValue == null || vValue == [] || vValue == {});
}


/* ===== elements ===== */

// 
function $(sText, eParent){
    var parent = (eParent ? eParent : document);
    
    var pattern = /^(#([\w\-]+)\s?)?(([a-z1-6]+)(\.([\w\-]+))?)?$/;
    var matches = sText.match(pattern);
    
    if(!matches){
        return null;
    }
    
    // 有目标容器
    if(matches[1]){
        var id = matches[2];
        parent = document.getElementById(id);
    }
    
    if(!matches[3]){
        return parent;
    }
    
    // 有给定标记
    var tagname = matches[4];
    var eles = parent.getElementsByTagName(tagname);
    
    if(!matches[5]){
        return eles;
    }
    
    // 有给定classname
    var classname = matches[6];
    var specified = [];
    for(var i=0; i<eles.length; i++){
        if(eles[i].className == classname){
            specified.push(eles[i]);
        }
    }
    
    return specified;
}

// 消除dom节点中的函数对象（通常是在删除节点之前，以解决ie的内存泄漏问题）
function dissolveFunctions(eNode){
    if(eNode.attributes){
        var attr, aname;
        for(var i=0; attr=eNode.attributes[i]; i++){
            aname = attr.name;
            if(typeof(eNode[aname]) === "function"){
                eNode[aname] = null;
            }
        }
    }
    if(eNode.childNodes){
        var cnode = eNode.firstChild;
        while(cnode != null){
            dissolveFunctions(cnode);
            cnode = cnode.nextSibling;
        }
    }
}

function getPreloadedImage(sSrc){
    var img = new Image();
    img.src = sSrc;
    return img;
}

function setLinkUrl(eLink, sHref){
    eLink.href = sHref;
}

// 创建一个button元件（firefox和ie兼容的）
function createButtonElement(sLabel, sType){
    var browser = new Browser();
    var type = (sType != null) ? sType : "button";
    var btn = document.createElement(browser.isIE ? '<button type="'+ type +'"></button>' : "button");
    if(!browser.isIE){
        btn.type = type;
    }
    btn.innerHTML = sLabel;
    return btn;
}

function showElement(eTarget, bVisible){
    if(bVisible == null){
        bVisible = true;
    }
    eTarget.style.visibility = (bVisible ? "visible" : "hidden");
}
function displayElement(eTarget, bVisible){
    if(bVisible == null){
        bVisible = true;
    }
    eTarget.style.display = (bVisible ? "block" : "none");
}
function disableElement(eTarget, bDisabled){
    if(bDisabled == null){
        bDisabled = true;
    }
    eTarget.disabled = (bDisabled ? true : false);
}

/* ===== dom ===== */

function getFirstChildOf(eNode){
    if(!eNode.hasChildNodes()){
        return false;
    }
    var n = eNode.firstChild;
    return (isEmptyTextNode(n) ? n.nextSibling : n);
}
function getLastChildOf(eNode){
    if(!eNode.hasChildNodes()){
        return false;
    }
    var n = eNode.lastChild;
    return (isEmptyTextNode(n) ? n.previousSibling : n);
}

function getPrevSiblingOf(eNode){
    if(eNode.previousSibling == null){
        return false;
    }
    var n = eNode.previousSibling;
    return (isEmptyTextNode(n) ? (n.previousSibling == null ? false : n.previousSibling) : n);
}
function getNextSiblingOf(eNode){
    if(eNode.nextSibling == null){
        return false;
    }
    var n = eNode.nextSibling;
    return (isEmptyTextNode(n) ? (n.nextSibling == null ? false : n.nextSibling) : n);
}

function getTextIn(eNode){
    if(eNode.nodeType == 3){
        return eNode.nodeValue;
    }
    if(!eNode.hasChildNodes()){
        return false;
    }
    var n = eNode.firstChild;
    while(n.hasChildNodes()){
        n = eNode.firstChild;
    }
    return (n.nodeType == 3 ? n.nodeValue : false);
}

function isEmptyTextNode(eNode){
    return (eNode.nodeType == 3 && eNode.nodeValue.replace(/[\s\n]+/g, "") == "");
}



/* ===== events ===== */


function getMouseButton(e){
    return (e.which ? e.which : e.button);
}
function getEventTarget(e){
    return (e.target ? e.target : e.srcElement);
}
function getMouseOffset(e){
    return (e.layerX ? {x: e.layerX, y: e.layerY} : {x: e.offsetX, y: e.offsetY});
}
function getMouseCoor(e){
    return {x: e.clientX, y: e.clientY};
}

function addEventHandler(eTarget, sEvent, fHandler, bCapture){
    if(eTarget.addEventListener){
        return eTarget.addEventListener(sEvent, fHandler, (bCapture == null ? false : bCapture));
    }
    if(eTarget.attachEvent){
        return eTarget.attachEvent("on" + sEvent, fHandler);
    }
    var act = eTarget['on' + sEvent];
    eTarget['on' + sEvent] = function(e){
        act(e);
        fHandler(e);
    };
    return true;
}

function getBodyDim(){
    var dim = {};
    if(document.documentElement){
        dim.w = document.documentElement.scrollWidth;
        dim.h = document.documentElement.scrollHeight;
        dim.cw = document.documentElement.clientWidth;
        dim.ch = document.documentElement.clientHeight;
        return dim;
    }
    dim.w = document.body.scrollWidth;
    dim.h = document.body.scrollHeight;
    if(window.innerWidth){
        dim.cw = window.innerWidth;
        dim.ch = window.innerHeight;
        return dim;
    }
    dim.cw = document.body.clientWidth;
    dim.ch = document.body.clientHeight;
    return dim;
}
function getBodyScroll(){
    var bsroll;
    if(window.innerWidth){
        return {x: window.pageXOffset, y: window.pageYOffset};
    }
    if(document.documentElement){
        return {x: document.documentElement.scrollLeft, y: document.documentElement.scrollTop};
    }
    return {x: document.body.scrollLeft, y: document.body.scrollTop};
}

// 得到页面元件的绝对位置
function getCoor(eTarget){
    var coor ={x: 0, y: 0};
    var node = eTarget;
    while(node != null){
        if(node.offsetLeft){
            coor.x += node.offsetLeft;
        }
        if(node.offsetTop){
            coor.y += node.offsetTop;
        }
        node = node.offsetParent;
    }
    return coor;
}
function setCoor(eTarget, vX, vY, iZ){
    if(vX != null){
        eTarget.style.left = (typeof(vX) == "number" ? vX + "px" : vX);
    }
    if(vY != null){
        eTarget.style.top = (typeof(vY) == "number" ? vY + "px" : vY);
    }
    if(iZ != null){
        eTarget.style.zIndex = iZ;
    }
}

function getDim(eTarget){
    var dim = {};
    try{
        dim.w = eTarget.offsetWidth;
        dim.h = eTarget.offsetHeight;
    }
    catch(ex){
        dim.w = parseInt(eTarget.style.width);
        dim.h = parseInt(eTarget.style.height);
    }
    return dim;
}
function setDim(eTarget, vW, vH){
    if(vW != null){
        eTarget.style.width = (typeof(vW) == "number" ? vW + "px" : vW);
    }
    if(vH != null){
        eTarget.style.height =(typeof(vH) == "number" ? vH + "px" : vH);
    }
}
function setImageDim(eImage, vW, vH){
    if(vW != null){
        eImage.width = (typeof(vW) == "number" ? vW + "px" : vW);
    }
    if(vH != null){
        eImage.height =(typeof(vH) == "number" ? vH + "px" : vH);
    }
}



/* ===== cookies ===== */


function setCookie(sName, sValue, dExpires, sPath, sDomain, bSecure){
    document.cookie = sName + "=" + escape(sValue) + 
        (dExpires ? "; expires=" + dExpires.toUTCString() : "") + 
        (sPath ? "; path=" + sPath : "") + 
        (sDomain ? "; domain=" + sDomain : "") + 
        (bSecure ? "; secure" : "");
}
function getCookie(sName){
    sName = sName.replace(/([\+\-\*\/\=\?\!\$\.\(\)\[\]\{\}\\])/g, "\\$1");
    var pattern = new RegExp(sName + "=" + "([^=;]*);?");
    var matches = document.cookie.match(pattern);
    if(matches == null){
        return null;
    }
    return matches[1];
}



/* ===== paginate ===== */


function get2dData(aData, iCapacity){
    var a = [];
    
    if(aData.length <= iCapacity){
        a[0] = [];
        var i;
        for(i=0; i<aData.length; i++){
            a[0].push(aData[i]);
        }
        return a;
    }
    
    var s;
    var i, j;
    for(i=0; i<Math.ceil(aData.length/iCapacity); i++){
        s = [];
        for(j=i*iCapacity; j<Math.min((i+1)*iCapacity, aData.length); j++){
            s.push(aData[j]);
        }
        a.push(s);
    }
    return a;
}

// 得到分页后的页码显示代码
function getPageClusters(iCurr, iTotal, sTemplate){
    if(iTotal == 1){
        return "";
    }
    if(iCurr > iTotal){
        return "";
    }
    
    var TAIL_PAGES_NUM = 2;
    var SIBLING_PAGES_NUM = 3;
    var ELIDED_PAGES_NUM = 3;
    var ELIDED_PAGES_CHARS = '...';
    var SEPARATOR_CHAR = ' ';
    var PAGE_VAR_REGEXP = /%PAGE%/g;
    
    var html = '';
    var i;
    var begin = 1, end = iTotal;
    
    // 总页数的常规临界点（本页+两倍相邻页数+省略页数+末端页数）
    // 达不到临界点则直接显示全部页码
    var crit_pages = 1 + 2*SIBLING_PAGES_NUM + ELIDED_PAGES_NUM + TAIL_PAGES_NUM;
    if(iTotal < crit_pages){
        for(i=begin; i<=end; i++){
            html += sTemplate.replace(PAGE_VAR_REGEXP, i) + SEPARATOR_CHAR;
        }
        return html;
    }
    
    // 靠前临界点（首端页数+省略页数+相邻页数+1）
    var fore_crit_page = TAIL_PAGES_NUM + ELIDED_PAGES_NUM + SIBLING_PAGES_NUM + 1;
    // 靠后临界点（总页数-尾端页数-相邻页数-省略页数）
    var back_crit_page = iTotal - TAIL_PAGES_NUM - ELIDED_PAGES_NUM - SIBLING_PAGES_NUM;
    
    // 当前页靠前则不显示省略号
    if(iCurr < fore_crit_page){
        for(i=begin; i<=iCurr-1; i++){
            html += sTemplate.replace(PAGE_VAR_REGEXP, i) + SEPARATOR_CHAR;
        }
    }
    else{
        // 首端
        for(i=begin; i<=begin+TAIL_PAGES_NUM-1; i++){
            html += sTemplate.replace(PAGE_VAR_REGEXP, i) + SEPARATOR_CHAR;
        }
        html += ELIDED_PAGES_CHARS + SEPARATOR_CHAR;
        // 当前页太接近尾页则补全前相邻页
        // 确保每页周围有足够的相邻页
        if(iCurr > end - SIBLING_PAGES_NUM){
            for(i=end-2*SIBLING_PAGES_NUM; i<=iCurr-SIBLING_PAGES_NUM-1; i++){
                html += sTemplate.replace(PAGE_VAR_REGEXP, i) + SEPARATOR_CHAR;
            }
        }
        // 左邻
        for(i=iCurr-SIBLING_PAGES_NUM; i<=iCurr-1; i++){
            html += sTemplate.replace(PAGE_VAR_REGEXP, i) + SEPARATOR_CHAR;
        }
    }

    // 当前页
    html += sTemplate.replace(PAGE_VAR_REGEXP, iCurr) + SEPARATOR_CHAR;

    // 当前页靠后则不显示省略号
    if(iCurr > back_crit_page){
        for(i=iCurr+1; i<=end; i++){
            html += sTemplate.replace(PAGE_VAR_REGEXP, i) + SEPARATOR_CHAR;
        }
    }
    else{
        // 右里
        for(i=iCurr+1; i<=iCurr+SIBLING_PAGES_NUM; i++){
            html += sTemplate.replace(PAGE_VAR_REGEXP, i) + SEPARATOR_CHAR;
        }
        // 当前页太接近首页则补全后相邻页
        // 确保每页周围有足够的相邻页
        if(iCurr < begin + SIBLING_PAGES_NUM){
            for(i=iCurr+SIBLING_PAGES_NUM+1; i<=begin+2*SIBLING_PAGES_NUM; i++){
                html += sTemplate.replace(PAGE_VAR_REGEXP, i) + SEPARATOR_CHAR;
            }
        }
        html += ELIDED_PAGES_CHARS + SEPARATOR_CHAR;
        // 尾端
        for(i=end-TAIL_PAGES_NUM+1; i<=end; i++){
            html += sTemplate.replace(PAGE_VAR_REGEXP, i) + SEPARATOR_CHAR;
        }
    }
    
    return html;
}

//查看用户是否登录.
function isLogin()
{
    var ck = getCookie('3SEYU_SESS[main]');
    if(ck == null || ck == "") return false;
    else return true;
}

//去空函数,相当于VB中的trim
function trim(s) 
{
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

//给图片做标记的通用方法
function refreshDroppedPhotos(_waitPhotos, _PhotosData){
    var sPrefix = "drpdstmp";
    removeDroppedStamps(sPrefix);

    var i, image, pinfo, pid;
    for(i=0; image=_waitPhotos.images[i]; i++){
        pinfo = getPhotoInfo(image.src);
        pid = pinfo.photoid;
        _waitPhotos.coors[pid] = getCoor(image);
        for(var key in _PhotosData){
            if(typeof(_PhotosData[key].photoid)!='undefined'){
                if(_PhotosData[key] && image.alt == _PhotosData[key].photoid){
                    setDroppedStamp(_waitPhotos.coors[pid].x, _waitPhotos.coors[pid].y, true);
                }
            }else{
                if(typeof(_PhotosData[key].photos)!='undefined'){
                    for(var _key in _PhotosData[key].photos){
                        if( _PhotosData[key].photos[_key] && image.alt == _PhotosData[key].photos[_key].photoid ){
                            setDroppedStamp(_waitPhotos.coors[pid].x, _waitPhotos.coors[pid].y, true);
                        }
                    }
                }else if(typeof(_PhotosData[key])!='undefined'){
                    for(var _key in _PhotosData[key]){
                        if( _PhotosData[key][_key] && image.alt == _PhotosData[key][_key].photoid ){
                            setDroppedStamp(_waitPhotos.coors[pid].x, _waitPhotos.coors[pid].y, true);
                        }
                    }
                }
            }
        }
    }
}

// 设置记号图片
function setDroppedStamp(iX, iY, bDisplay){
    var sPrefix = "drpdstmp";
    var _opacity = "75";
    var eStamp;
    // 坐标为0或者正数，sID同时不为空，则创建记号图片
    if( iX>=0 && iY>=0 ){
        eStamp = document.createElement("div");
        eStamp.innerHTML = "<img src=\"http://img1.3seyu.com/images/have_dropped.gif\"/>";
        eStamp.id = sPrefix+iX+iY;
        eStamp.className = sPrefix;
        eStamp.style.cssText = "left:"+iX+"px;top:"+iY+"px;width:17px;height:18px;position:absolute;display:none;z-index:3;filter:alpha(opacity="+_opacity+");-moz-opacity:0."+_opacity+";opacity:0."+_opacity+";";
        document.body.appendChild(eStamp);
    }
    eStamp = $("#"+sPrefix+iX+iY);
    if(!isEmpty(eStamp)){
        eStamp.style.display = ""+(bDisplay?"block":"none");
    }
    return;
}

function removeDroppedStamps(_className){
    var _tmp = $("div."+_className);
    for(var key in _tmp){
        document.body.removeChild(_tmp[key]);
    }
}

function hideDroppedStamp(_sX, _sY){
    var _x = 1*_sX.substring(0,_sX.length-2);
    var _y = 1*_sY.substring(0,_sY.length-2);
    var sPrefix = "drpdstmp";
    var _eTmp = $("#"+sPrefix+_x+_y);
    if(_eTmp){
        displayElement(_eTmp, 0);
    }
}

function showDroppedStamp(_x, _y){
    var sPrefix = "drpdstmp";
    var _eTmp = $("#"+sPrefix+_x+_y);
    if(_eTmp){
        displayElement(_eTmp);
    }
}

// 判断拖拽区域是否位于出血位
function isCutMargin(_layout, _trend, _itray, _product){
    var nc = {};
    var cc = {};
    var oc = {};
    // 出血边界（像素）
    var CUTMARGIN = 6;
    // 允许提示框显示的次数
    var TIP_CUT_ALLOW = 5;
    var bShowTip = true;
    
    // 显示提示框超过3次则不再显示
    if(gCutTipCount!=null){
        if(gCutTipCount>TIP_CUT_ALLOW){
            $('#margstmp').style.display='none';
            bShowTip = false;
        }
    }
    // 针对不同产品有不同的出血尺寸
    switch(_product){
        case "calendar":
            // 原尺寸
            oc = {'w':gCanvasDesignDim.w, 'h':gCanvasDesignDim.h, 'x':0, 'y':0};
            // 除去出血后的尺寸
            cc = {'w':gCanvasDesignDim.w-2*CUTMARGIN, 'h':gCanvasDesignDim.h-2*CUTMARGIN, 'x':CUTMARGIN, 'y':CUTMARGIN};
            // 整理后的裁切尺寸
            nc = {'x':0, 'y':0, 'cpw':0, 'cph':0, 'rzw':0, 'rzh':0};
            break;
        default:
            return nc;
            break;
    }
    
    if(_trend!='photo'){
            nc = {'x':0, 'y':0, 'cpw':0, 'cph':0, 'rzw':0, 'rzh':0};
            return nc;
    }
    
    // 根据出血位计算裁切坐标
    var _CMARGIN = 1;
    var _oTable = gCanvasDesignDim;
    var _oTray = gModelDesignData[_layout]['photos'][_itray];
    if(_oTray.x <= _CMARGIN
    || _oTray.y <= _CMARGIN
    || Math.abs(_oTray.x+_oTray.w-_oTable.w) <= _CMARGIN
    || Math.abs(_oTray.y+_oTray.h-_oTable.h) <= _CMARGIN){
        setMarginDiv(_oTray.w, _oTray.h, _oTray.x, _oTray.y, bShowTip);

        var _tray;
        for(var i=0; i<$("#photos-table").childNodes.length; i++){
            if(_itray==i && $("#photos-table").childNodes[i].tagName=='DIV'){
                _tray = $("#photos-table").childNodes[i];
                break;
            }
        }
        if(_tray){
            var _x = 1*_tray.style.left.substring(0,_tray.style.left.length-2);
            var _y = 1*_tray.style.top.substring(0,_tray.style.top.length-2);
            var _w = 1*_tray.style.width.substring(0,_tray.style.width.length-2);
            var _h = 1*_tray.style.height.substring(0,_tray.style.height.length-2);
            
            // 确定裁切框左上角坐标
            nc.x = _x;
            nc.y = _y;
            nc.x = (_x<cc.x || _x>cc.x+cc.w)? cc.x : _x;
            nc.y = (_y<cc.y || _y>cc.y+cc.h)? cc.y : _y;
            // 确定裁切框宽度
            nc.cpw  = (_oTray.x<nc.x)? _oTray.w-cc.x : _oTray.w;
            nc.cpw  = (_oTray.x==nc.x && _oTray.x+_oTray.w>cc.x+cc.w)? _oTray.w-(_oTray.x+_oTray.w-cc.x-cc.w) : nc.cpw;
            // 确定裁切框高度
            nc.cph = (_oTray.y<nc.y)? _oTray.h-cc.y : _oTray.h;
            nc.cph  = (_oTray.y==nc.y && _oTray.y+_oTray.h>cc.y+cc.h)? _oTray.h-(_oTray.y+_oTray.h-cc.y-cc.h) : nc.cph;

            nc.rzw = _oTray.w;
            nc.rzh = _oTray.h;

            if( !(nc.x+nc.cpw<=nc.rzw) ){
                nc.x = 0;
            }

            if( !(nc.y+nc.cph<=nc.rzh) ){
                nc.y = 0;
            }
            
            if( nc.cpw==nc.rzw && nc.cph==nc.rzh ){
                nc = {'x':0, 'y':0, 'cpw':0, 'cph':0, 'rzw':0, 'rzh':0};
            }

            if(Math.abs(_w-oc.w)<=1){
                nc.cpw = cc.w;
            }

            if(Math.abs(_h-oc.h)<=1){
                nc.cph = cc.h;
            }

        }
//        alert("x,y: " + nc.x + ", " + nc.y + " cpw,cph: " + nc.cpw + ", " + nc.cph + " rz: " + nc.rzw + ", " + nc.rzh);
        
        return nc;
    }else{
        return null;
    }
}
function setMarginDiv(iW, iH, iX, iY, bDisplay){
    var msPrefix = "margstmp";
    if(iW==null && iH==null && iX==null && iY==null || !bDisplay){
        var eMStamp = $("#"+msPrefix);
        if(eMStamp){
            eMStamp.style.display = 'none';
            return;
        }
        return;
    }
    // 出血警告层固定ID
    var _opacity = "80";
    var eMStamp;
    // tip arrow dims, 提示框的大小尺寸, x,y为指向定位点；w,h为图片长高
    var tad = {x:42, y:0, w:138, h:101};
    // 提示框箭头指向的点
    var posxy = {x:iW/2, y:10};
    // photo page wrapper
    var ppw = getCoor($("#photopage-wrapper"));
    // 坐标为(0,0)或者正数，sID同时不为空，则创建记号图片
    if($("#"+msPrefix)){
        removeMarginDiv(msPrefix);
    }
    if( iX>=0 && iY>=0 ){
        var emstap_x;
        var emstap_y = iY;
        if(emstap_y<=6){
            emstap_x = ppw.x+iX-tad.x+posxy.x;
            emstap_y = ppw.y+iY-tad.y+posxy.y;
            eMStamp = document.createElement("div");
            eMStamp.id = msPrefix;
            eMStamp.className = msPrefix;
            document.body.appendChild(eMStamp);
            eMStamp.style.cssText = "width:"+tad.w+"px;height:"+tad.h+"px;position:absolute;left:"+emstap_x+"px;top:"+emstap_y+"px;filter:alpha(opacity="+_opacity+");-moz-opacity:0."+_opacity+";opacity:0."+_opacity+";";

            gCutTipCount ++;
        }
    }
    eMStamp = $("#"+msPrefix);
    if(!isEmpty(eMStamp) && bDisplay){
        eMStamp.style.display = ""+(bDisplay?"block":"none");
    }
    return;
}

function removeMarginDiv(_className){
    var _tmp = $("#margstmp");
    if(_tmp){
        document.body.removeChild(_tmp);
    }
    return;
}

