/*
Copyright (c)  2008-2012, www.redips.net  All rights reserved.
Code licensed under the BSD License: http://www.redips.net/license/
http://www.redips.net/javascript/table-td-merge-split/
Version 1.1.0
May 15, 2012.
https://github.com/dbunic/REDIPS_table
hedy对此源码有改动 希望能以数据的形式返回 而不是直接操作结构
*/

/*jslint white: true, browser: true, undef: true, nomen: true, eqeqeq: true, plusplus: false, bitwise: true, regexp: true, strict: true, newcap: true, immed: true, maxerr: 14 */
/*global window: false */

/* enable strict mode */
"use strict";var REDIPS=REDIPS||{};REDIPS.table=(function(){var y,v,h,g,x,r,m,n,e,k,d,i,w,f,o,p,a,u,b,c,l=[],j,t,s={cell:false,row:false,column:false},q=true;y=function(E,z,D){if(true){return}var G,F,C,B,A;A=function(K){var H=[],I,J;I=K.getElementsByTagName("table");for(J=0;J<I.length;J++){H.push(I[J])}return H};j=z;if(typeof(E)==="string"){if(D==="classname"){l=A(document);for(C=0;C<l.length;C++){if(l[C].className.indexOf(E)===-1){l.splice(C,1);C--}}}else{E=document.getElementById(E)}}if(E&&typeof(E)==="object"){if(E.nodeName==="TABLE"){l[0]=E}else{l=A(E)}}for(B=0;B<l.length;B++){F=l[B].getElementsByTagName("th");for(C=0;C<F.length;C++){e(F[C])}G=l[B].getElementsByTagName("td");for(C=0;C<G.length;C++){e(G[C])}}o()};e=function(z){if(z.className.indexOf("ignore")>-1){return}if(j===true){}else{}};p=function(z){if(typeof(z)==="string"){z=document.getElementById(z)}};v=function(C){var z=C||window.event,D=a(z.target||z.srcElement),A,B;if(!D){return}B=(/^\s*$/.test(D.innerHTML))?true:false;if(REDIPS.table.mark_nonempty===false&&B===false){return}if(z.which){A=z.which}else{A=z.button}if(A===1){D.redips=D.redips||{};if(D.redips.selected===true){n(false,D)}else{n(true,D)}}};a=function(z){if(!z){return null}if(z.nodeName=="TD"||z.nodeName=="TH"){return z}return a(z.parentNode)};h=function(J,I,P){var E={};var G,L,K,C,B,z,M,A,N,O,F,D,H={index:-1,span:-1};f();G=(P===undefined)?l:m(P);for(O=0;O<G.length;O++){N=i(G[O]);L=G[O].rows;C=(J==="v")?x(G[O]):L.length;B=(J==="v")?L.length:x(G[O]);for(F=0;F<C;F++){H.index=H.span=-1;for(D=0;D<=B;D++){A=(J==="v")?(D+"-"+F):(F+"-"+D);if(N[A]){K=N[A];K.redips=K.redips||{};z=K?K.redips.selected:false;M=(J==="v")?K.colSpan:K.rowSpan}else{z=false}if(z===true&&H.index===-1){H.index=D;H.span=M}else{if((z!==true&&H.index>-1)||(H.span>-1&&H.span!==M)){g(N,F,H.index,D,J,I,E);H.index=H.span=-1;if(z===true){if(I===true||I===undefined){n(false,K)}z=false}}}if(N[A]){D+=(J==="v")?K.rowSpan-1:K.colSpan-1}}if(z===true){g(N,F,H.index,D,J,I,E)}}}o();return E};u=function(z){var A=SINGLETON.getJsonFactory().api().getMagicalCoderIdAttrName();return z.attr(A)};g=function(M,L,J,I,E,D,B){var K=0,z,A,H,C;A=(E==="v")?M[J+"-"+L]:M[L+"-"+J];var N=[];var G=null;var F=null;for(C=J+1;C<I;C++){z=(E==="v")?(C+"-"+L):(L+"-"+C);if(M[z]){H=M[z];K+=(E==="v")?H.rowSpan:H.colSpan;w(H,A);$(A).append($(H).html());N.push(u($(H)));H.parentNode.deleteCell(H.cellIndex)}}if(A!==undefined){if(E==="v"){A.rowSpan+=K}else{A.colSpan+=K}b(B,A,N);if(D===true||D===undefined){n(false,A)}}};b=function(D,A,C){var B=u($(A));if(!D[B]){D[B]={needMergedCellIds:[],rowSpan:null,colSpan:null}}if(D[B].needMergedCellIds.length>0){if(C.length>0){for(var z=0;z<C.length;z++){result.needMergedCellIds.push(C[z])}}}else{D[B].needMergedCellIds=C}D[B].rowSpan=A.rowSpan;D[B].colSpan=A.colSpan};x=function(D){var E=D.rows,C,z=0,B,A;if(typeof(D)==="string"){D=document.getElementById(D)}for(B=0;B<E.length;B++){C=0;for(A=0;A<E[B].cells.length;A++){C+=E[B].cells[A].colSpan||1}if(C>z){z=C}}return z};r=function(F,O){var L=[];var E,I,H,M,B,z,K,J,N,D,C,P;P=function(V,U,R){var Q,T,S;Q=0;T=U+V.rowSpan-1;for(S=R-1;S>=0;S--){if(M[T+"-"+S]===undefined){Q++}}return Q};f();E=(O===undefined)?l:m(O);for(N=0;N<E.length;N++){M=i(E[N]);J=x(E[N]);I=E[N].rows;for(D=0;D<I.length;D++){K=(F==="v")?J:I[D].cells.length;for(C=0;C<K;C++){if(F==="v"){H=M[D+"-"+C];if(H!==undefined){H.redips=H.redips||{}}if(H!==undefined&&H.redips.selected===true&&H.rowSpan>1){B=P(H,D,C);var A=D+H.rowSpan-1;var G=C-B;z=I[A].insertCell(G);z.colSpan=H.colSpan;H.rowSpan-=1;c(I[A],H,z,G,L);e(z);M=i(E[N])}}else{H=I[D].cells[C];H.redips=H.redips||{};if(H.redips.selected===true&&H.colSpan>1){K++;var A=D;var G=C+1;z=I[A].insertCell(G);z.rowSpan=H.rowSpan;H.colSpan-=1;c(I[A],H,z,G,L);e(z)}}if(H!==undefined){n(false,H)}}}}o();return L};c=function(E,F,D,A,B){var z={};var C="<"+D.tagName.toLowerCase()+(D.colSpan&&D.colSpan>1?" colspan='"+D.colSpan+"'":"")+(D.rowSpan&&D.rowSpan>1?" rowspan='"+D.rowSpan+"'":"")+"></"+D.tagName.toLowerCase()+">";z.add={trMcId:u($(E)),index:A,html:C};z.exist={keepId:u($(F)),colSpan:F.colSpan,rowSpan:F.rowSpan};B.push(z)};m=function(z){var A=[];if(z!==undefined){if(typeof(z)==="string"){z=document.getElementById(z)}if(z&&typeof(z)==="object"&&z.nodeName==="TABLE"){A[0]=z}}return A};k=function(N,F,G){var z=[];var E,L=null,H,I,M,J=0,D,C,B;f();if(typeof(N)!=="object"){N=document.getElementById(N)}if(G===undefined){G=-1}if(F==="insert"){H=N.rows[0];for(D=0;D<H.cells.length;D++){J+=H.cells[D].colSpan}L=N.insertRow(G);for(D=0;D<J;D++){E=L.insertCell(D);e(E)}o();var K=["<tr>"];for(var A=0;A<J;A++){K.push("<td></td>")}K.push("</tr>");z.push({parentMcId:u($(L).parent()),index:G,html:K.join("")})}else{if(N.rows.length===1){return}N.deleteRow(G);M=i(N);G=N.rows.length-1;J=x(N);for(D=0;D<J;D++){I=M[G+"-"+D];if(I===undefined){for(C=G,B=1;C>=0;C--,B++){I=M[C+"-"+D];if(I!==undefined){I.rowSpan=B;break}}}else{if(I.rowSpan>1){I.rowSpan-=1}}D+=I.colSpan-1}}return z};d=function(I,D,E){var G=[];var F,H,B,C;f();if(typeof(I)!=="object"){I=document.getElementById(I)
}if(E===undefined){E=-1}if(D==="insert"){for(C=0;C<I.rows.length;C++){var A=I.rows[C].cells[0];B=I.rows[C].insertCell(E);var J={html:"<"+A.tagName.toLowerCase()+"></"+A.tagName.toLowerCase()+">",trMcId:u($(B).parent()),index:E};G.push(J);e(B)}o()}else{if(I==null){return G}F=I.rows[0].cells;if(F.length===1&&(F[0].colSpan===1||F[0].colSpan===undefined)){return G}for(C=0;C<I.rows.length;C++){if(E===-1){H=I.rows[C].cells.length-1}else{H=E}F=I.rows[C].cells[H];var J={};if(typeof F=="undefined"){F=I.rows[C].cells[I.rows[C].cells.length-1]}if(F.colSpan>1){F.colSpan-=1;J.update={mcId:u($(F)),colSpan:F.colSpan}}else{if(E<I.rows[C].cells.length){var z=u($(I.rows[C].cells[E]));I.rows[C].deleteCell(E);J.deleteMcId=z}}G.push(J);C+=F.rowSpan-1}}return G};n=function(A,C,D,B){var z;if(typeof(A)!=="boolean"){return}if(typeof(C)==="string"){C=document.getElementById(C)}else{if(typeof(C)!=="object"){return}}if(C.nodeName==="TABLE"){z=i(C);C=z[D+"-"+B]}if(!C||(C.nodeName!=="TD"&&C.nodeName!=="TH")){return}C.redips=C.redips||{};if(typeof(REDIPS.table.color.cell)==="string"){if(A===true){C.redips.background_old=C.style.backgroundColor;C.style.backgroundColor=REDIPS.table.color.cell}else{C.style.backgroundColor=C.redips.background_old}}C.redips.selected=A};f=function(){if(window.getSelection){window.getSelection().removeAllRanges()}else{if(document.selection&&document.selection.type==="Text"){try{document.selection.empty()}catch(z){}}}};i=function(M){var K=[],z,B={},J,L,E,A,I,H,G,F,D,C;H=M.rows;for(G=0;G<H.length;G++){for(F=0;F<H[G].cells.length;F++){J=H[G].cells[F];L=J.parentNode.rowIndex;E=J.rowSpan||1;A=J.colSpan||1;K[L]=K[L]||[];for(D=0;D<K[L].length+1;D++){if(typeof(K[L][D])==="undefined"){I=D;break}}B[L+"-"+I]=J;for(D=L;D<L+E;D++){K[D]=K[D]||[];z=K[D];for(C=I;C<I+A;C++){z[C]="x"}}}}return B};w=function(D,C){var B,A,z;if(D===C){return}B=D.childNodes.length;for(A=0,z=0;A<B;A++){if(D.childNodes[z].nodeType===1){C.appendChild(D.childNodes[z])}else{z++}}};o=function(A){if(A===undefined&&t!==true){return}if(A!==undefined){t=A}var E,G,z,F,D,B,C;for(C=0;C<l.length;C++){E=l[C].rows;F=x(l[C]);z=i(l[C]);for(D=0;D<E.length;D++){for(B=0;B<F;B++){if(z[D+"-"+B]){G=z[D+"-"+B];G.innerHTML=(t)?D+"-"+B:""}}}}};return{color:s,mark_nonempty:q,onmousedown:y,mark:n,merge:h,split:r,row:k,column:d,cell_index:o,cell_ignore:p}}());