jQuery.fn.jqSelecta=function(K){var T=this,R=jQuery(this),O=R.attr("id"),C="cell.ref",S="opt.ref",I={multiSelect:null,autoSelected:null,title:null,showHeader:true,showHeaderCloseIcon:true,showFooter:true,showMultiSelectFooterIcons:true,footerCancelButtonText:"отмена",footerOkButtonText:"выбрать",showFooterOkCancelButtons:true,onOpen:function(){},onClose:function(){},labelCallback:function(V){if(F.multiSelect){return ((V!=0)?"выбрано "+V:"ничего не выбрано")
}else{var W=R.find("[selected=true]");
if(W==null){W=jQuery(R.children()[0])
}return W.html().replace(/\s+/g," ")
}},height:null,width:"300px",buttonWidth:null,autoScrollOverflowY:true,autoScrollOverflowX:true,columns:2,fadeSpeed:300,valueSetFrom:"value",titleSetFrom:"title",textSetFrom:"text",persistAsCookie:false,debug:false,trace:false,shadow:false},F=jQuery.extend(I,K),H=null,U=null,G,Q,B,M,J,A;
if(F.multiSelect===null){F.multiSelect=R.attr("multiple");
if(F.multiSelect===undefined){F.multiSelect===false
}}this.cancelAndClose=function(){if(F.multiSelect===true){T.revertState()
}T.hideSelecta()
};
this.refreshLabel=function(){if(typeof A!=="undefined"){A.html(F.labelCallback(R.find("[selected=true]").size()))
}};
this.revertState=function(){if(U!==null&&U.length>0){this.selectNone();
jQuery(U).attr("selected",true)
}this.reloadState();
this.refreshLabel()
};
this.reloadState=function(){R.find("[selected=true]").each(function(V){L(jQuery(this).data(S))
})
};
this.showSelecta=function(){M.css("z-index",1000);
G.fadeIn(F.fadeSpeed);
U=R.find("[selected=true]");
H=jQuery('<iframe style="border:0; overflow:hidden; position:absolute;top:0px'+((F.trace===true)?";border: solid blue 1px;":"")+";left:0px;width:"+G.width()+"px;height:"+G.height()+'px;z-index:-1"></iframe>');
jQuery("#"+O+"_jqselecta-wrap").prepend(H);
if(F.shadow===true){jQuery("#"+O+"_jqselecta-shadow").fadeIn(F.fadeSpeed)
}J.removeClass("ui-icon-carat-1-s").addClass("ui-icon-carat-1-n");
F.onOpen()
};
this.hideSelecta=function(){var V=(F.multiSelect===true)?F.fadeSpeed:(F.fadeSpeed*3);
G.fadeOut(V,function(){H.remove();
J.removeClass("ui-icon-carat-1-n").addClass("ui-icon-carat-1-s");
F.onClose()
});
if(F.shadow===true){jQuery("#"+O+"_jqselecta-shadow").hide()
}M.css("z-index",0);
if(F.persistAsCookie===true){dehydrateAsCookie()
}};
this.dehydrateAsCookie=function(){var V="";
R.find("[selected=true]").each(function(W){if(W>0){V+="&"
}V+=escape(jQuery(this).attr("value"))+"="+escape(jQuery(this).html())
});
if(V!==""){jQuery.cookie(O+"_jqSelecta",V)
}};
this.rehydrateFromCookie=function(){var Y=jQuery.cookie(O+"_jqSelecta"),Z,X,V,W;
if(Y!==null){Z=Y.split(/\&/);
if(Z.length>0){this.selectNone()
}for(X=0;
X<Z.length;
X++){V=Z[X].split(/\=/);
R.find("[value="+unescape(V[0])+"]").each(function(a){W=jQuery(this);
if(unescape(V[1])===W.html()){jQuery(this).attr("selected",true)
}})
}this.reloadState()
}};
this.toggleSelecta=function(){if(G.is(":visible")){T.hideSelecta()
}else{T.showSelecta()
}};
this.selectAll=function(){Q.find(".cellectable").each(function(V){L(this)
});
T.refreshLabel()
};
this.selectNone=function(){Q.find(".cellectable").each(function(V){D(this)
});
T.refreshLabel()
};
this.selectInverse=function(){Q.find(".cellectable").each(function(V){if(jQuery(this).data(C).attr("selected")===true){D(this)
}else{L(this)
}});
T.refreshLabel()
};
this.getSelectedValues=function(){var V=[];
jQuery(T).find("[selected=true]").each(function(W){V[V.length]=jQuery(this).attr("value")
});
return V
};
function D(V){var W=jQuery(V);
if(W.data(C).attr("disabled")){return 
}W.data(C).removeAttr("selected");
W.css("font-weight","normal");
W.find("span").removeClass("ui-icon-check").addClass("ui-icon-close")
}function L(V){var W=jQuery(V);
if(W.data(C).attr("disabled")){return 
}W.data(C).attr("selected",true);
W.css("font-weight","bold");
W.find("span").removeClass("ui-icon-close").addClass("ui-icon-check")
}function E(V){var W=jQuery(V);
if(W.data(C).attr("disabled")){return 
}if(W.data(C).attr("selected")===true&&F.multiSelect){D(V)
}else{L(V)
}T.refreshLabel()
}function N(V){if(F.debug){var W="[jqSelecta]  "+((typeof (V)==="Array")?Array.prototype.join.call(V,""):V);
if(window.console&&window.console.log){window.console.log(W)
}else{alert(W)
}}}function P(){G=jQuery('<div id="'+O+'_jqselecta" class="ui-widget ui-helper-clearfix ui-widget-content ui-corner-all" style="display:none;width:'+F.width+'"></div>');
Q=jQuery('<table class="ui-helper-clearfix" style="text-align:left;border:0;padding:0;margin:0;width:'+F.width+";z-index:19;height:"+F.height+'"></table>');
B=jQuery("<tbody></tbody>").appendTo(Q);
var a=R.children(),l="px",x=F.width.replace(/[^\d]+/g,""),c=(x/F.columns),W=Math.round(a.length/F.columns),g,u,w,h,d,Y,b,n,y,AB,p,s,V,f,e,v,r,o,t,X,Z,q,m,AA,k,z=null;
if(F.multiSelect===false&&R.find("[selected=true]").size()>0){z=R.find("[selected=true]")[0].index;
if(z===null){z=0
}}for(w=0;
w<W;
w++){g=jQuery("<tr></tr>");
for(u=0;
u<F.columns;
u++){h=w+u*W;
if(h>=a.length){for(;
u<F.columns;
u++){jQuery("<td> </td>").appendTo(g)
}break
}d=jQuery(a[h]);
n=b=d.text();
AB=y=d.attr("title");
s=p=d.attr("value");
if(F.textSetFrom==="value"){n=p
}else{if(F.textSetFrom==="title"){n=y
}}if(F.valueSetFrom==="text"){s=b
}else{if(F.valueSetFrom==="title"){s=y
}}if(F.titleSetFrom==="text"){AB=b
}else{if(F.titleSetFrom==="value"){AB=p
}}V=d.attr("selected");
if(!(F.multiSelect===false||F.autoSelected===null)){V=F.autoSelected
}if(z!==null&&z!==h){V=false
}d.attr("selected",V);
Y=jQuery('<td title="'+AB+'" style="border:0px;cursor:pointer;'+((true===V)?"font-weight:bold":"")+'" class="cellectable widget-content"><div style=white-space:nowrap;text-overflow:ellipsis;width:'+c+l+';overflow:hidden><div style="float:left"><span class="ui-icon '+((true===V)?"ui-icon-check":"ui-icon-close")+'"/></div><div> '+n+"</div></div></td>");
jQuery(Y).data(C,d);
jQuery(d).data(S,Y);
jQuery(Y).appendTo(g);
if(d.attr("disabled")===true){Y.addClass("ui-state-disabled")
}else{Y.hover(function(){jQuery(this).addClass("ui-state-hover")
},function(){jQuery(this).removeClass("ui-state-hover")
}).click(function(){var AC=jQuery(this),j,i;
if(F.multiSelect===false){j=AC.data(C);
i=j.attr("selected");
Q.find(".cellectable").each(function(AD){jQuery(this).css("font-weight","normal");
jQuery(this).find("span").removeClass("ui-icon-check").addClass("ui-icon-close");
jQuery(this).data(C).removeAttr("selected")
});
j.attr("selected",i)
}E(this);
if(F.multiSelect===false){T.hideSelecta()
}return false
})
}}jQuery(g).appendTo(B)
}jQuery(G).bind("mouseaway",function(){jQuery(this).find("ui-state-hover").removeClass("ui-state-hover")
});
if(true!==F.showHeader){if(F.showHeaderCloseIcon===true){f=jQuery('<div title="Cancel &amp; Close" id="'+O+'_jqselecta-header-close" class="ui-state-default ui-corner-all" style="float:right;cursor:pointer"><span class="ui-icon ui-icon-close"/></div>');
f.hover(function(){jQuery(this).addClass("ui-state-hover")
},function(){jQuery(this).removeClass("ui-state-hover")
}).click(T.cancelAndClose);
v=jQuery('<div id="'+O+'_jqselecta-header" class="ui-widget-header ui-helper-clearfix" style="padding:1px"></div>');
v.append(f)
}if(F.title!==null){v.append('<div id="'+O+'_jqselecta-header-text">'+F.title+"</div>")
}v.appendTo(G)
}e=jQuery('<div style="height:'+F.height+"!important;width:"+F.width+";overflow-y:"+((F.autoScrollOverflowY===true)?"scroll":"hidden")+"!important;overflow-x:"+((F.autoScrollOverflowX===true)?"auto":"hidden")+'!important"></div>');
Q.appendTo(e);
e.appendTo(G);
if(F.showFooter===true){r=jQuery('<div id="'+O+'_jqselecta-footer" class="ui-widget-header ui-helper-clearfix" style="padding:1px"></div>');
o=new Array();
if(F.multiSelect===true&&F.showMultiSelectFooterIcons===true){Z=jQuery('<div title="Выбрать все" id="'+O+'_jqselecta-select-all" class="ui-state-default ui-corner-all" style="float:left;cursor:pointer"><span class="ui-icon ui-icon-circle-check"/></div>');
q=jQuery('<div title="Снять выделение" id="'+O+'_jqselecta-select-none" class="ui-state-default ui-corner-all" style="float:left;cursor:pointer"><span class="ui-icon ui-icon-circle-close"/></div>');
m=jQuery('<div title="Инвертировать" id="'+O+'_jqselecta-select-inverse" class="ui-state-default ui-corner-all" style="float:left;cursor:pointer"><span class="ui-icon ui-icon-circle-triangle-s"/></div>');
Z.click(T.selectAll);
q.click(T.selectNone);
m.click(T.selectInverse);
r.append(Z);
r.append(q);
r.append(m);
o.push(m,Z,q)
}if(F.showFooterOkCancelButtons===true){AA=jQuery('<button class="ui-state-default ui-corner-all" style="text-overflow:ellipsis;overflow:hidden;float:right;cursor:pointer" type="button">'+F.footerOkButtonText+"</button>");
k=jQuery('<button class="ui-state-default ui-corner-all" style="text-overflow:ellipsis;overflow:hidden;float:right;cursor:pointer" type="button">'+F.footerCancelButtonText+"</button>");
AA.click(T.hideSelecta);
k.click(T.cancelAndClose);
AA.appendTo(r);
k.appendTo(r);
o.push(AA,k)
}r.appendTo(G);
jQuery(o).each(function(){jQuery(this).hover(function(){jQuery(this).addClass("ui-state-hover")
},function(){jQuery(this).removeClass("ui-state-hover")
})
})
}if(F.persistAsCookie===true){rehydrateFromCookie()
}X=jQuery('<button id="'+O+'_jqselecta-button" class="ui-state-default ui-corner-all" style="width:'+((F.buttonWidth===null)?(R.width()+"px"):F.buttonWidth)+';cursor:pointer" type="button"></button>');
t=jQuery("<nobr></nobr>");
J=jQuery('<span class="ui-icon ui-icon-carat-1-s" style="float:right"/>');
t.append(J);
A=jQuery('<span style="width:'+((F.buttonWidth===null)?(R.width()+"px"):F.buttonWidth)+'text-overflow:ellipsis;overflow:hidden">'+F.labelCallback(R.find("[selected=true]").size())+"</span>");
t.append(A);
X.append(t);
X.bind("click",T.toggleSelecta);
X.hover(function(){jQuery(this).addClass("ui-state-hover")
},function(){jQuery(this).removeClass("ui-state-hover")
});
M=jQuery('<span  id="'+O+'_jqselecta-holder" style="margin:0;padding:0;overflow:visible;position:relative;width:'+F.width+((F.trace===true)?";border: dashed green 1px;":"")+'"></span>');
X.appendTo(M);
G.appendTo(M);
R.after(M);
R.hide();
G.wrap('<div  id="'+O+'_jqselecta-wrap" style="position:absolute;z-index:19;display:block;width:'+F.width+((F.trace===true)?";border: dotted red 1px":"")+";top:"+X.height()+'px;left:0px"></div>');
if(F.shadow===true){jQuery("#"+O+"_jqselecta-wrap").prepend(jQuery('<div id="'+O+'_jqselecta-shadow" class="ui-widget-shadow" style="position:absolute;z-index:29;top:0;width:'+G.width()+"px;top:"+X.height()+"px;left:0px;height: "+G.height()+'px;display:none"></div>'))
}return T
}return P()
};

