
var selectables={delayInit:true,ignore:'a, button, input, option, select, textarea',ignoreAlso:'',mouseStart:{left:0,top:0},nodes:{},nodesSelected:{},selector:null,selectorBackground:null,setMousedown:false,setMousemove:false,setMousemove:false,setResize:false,setSelectStart:false,start:null};(function($){$(function(){selectables.selector=$('<div id="selector"></div>').css({border:'1px solid #0000aa',position:'absolute',zIndex:100}).hide().appendTo(document.body)
selectables.selectorBackground=$('<div></div>').css({backgroundColor:'#0000ff',opacity:0.1}).appendTo(selectables.selector);});var mousedown=function(e){if(e.button==2)return;var ignore=selectables.ignore;if(selectables.ignoreAlso)ignore+=','+selectables.ignoreAlso;if(e.target&&$(e.target).parents().andSelf().is(ignore))return;if(typeof selectables.start=='function'&&selectables.start(e)==false)return;window.startSelecting=true;if(selectables.delayInit){refresh(true);selectables.delayInit=false;}else{refresh(false);}
$.each(selectables.nodes,function(id){if(e.pageX>this.handle.position.left&&e.pageX<this.handle.position.left+this.handle.size.width&&e.pageY>this.handle.position.top&&e.pageY<this.handle.position.top+this.handle.size.height){window.startSelecting=false;if(!selectables.nodesSelected[this.element.selectableid]){unselectAll();}}});selectables.mouseStart.left=e.pageX+2;selectables.mouseStart.top=e.pageY+2;return false;};var mousemove=function(e){if(!window.startSelecting&&!window.isSelecting)return;if(!window.isSelecting){window.startSelecting=false;window.isSelecting=true;selectables.selector.show();}
var height=Math.abs(e.pageY-selectables.mouseStart.top);var width=Math.abs(e.pageX-selectables.mouseStart.left);var left=Math.min(e.pageX,selectables.mouseStart.left);var top=Math.min(e.pageY,selectables.mouseStart.top);var s=selectables.selector.get(0).style;s.left=left+'px';s.top=top+'px';var s2=selectables.selectorBackground.get(0).style;s2.height=height+'px';s2.width=width+'px';$.each(selectables.nodes,function(){var self=this;if(left+width>this.handle.position.left&&left<this.handle.position.left+this.handle.size.width&&top+height>this.handle.position.top&&top<this.handle.position.top+this.handle.size.height){trySelect(this);}else{tryUnselect(this);}});return false;};var mouseup=function(e){if(window.startSelecting){window.startSelecting=false;unselectAll();}
if(!window.isSelecting)return;window.isSelecting=false;selectables.selector.hide();};var getOffset=function(element){return $(element).offset();};var getWidthHeight=function(element){return{height:element.offsetHeight,width:element.offsetWidth};};var refresh=function(hard){$.each(selectables.nodes,function(id){if(!this.element||!this.element.parentNode||!this.element.parentNode.tagName){delete selectables.nodes[id];delete selectables.nodesSelected[id];return;}
if(!hard)return;this.handle.position=getOffset(this.handle.element);this.handle.size=this.handle.element.size||getWidthHeight(this.handle.element);});};var resize=function(e){$.selectable('refresh');};var selectstart=function(e){if(e.target&&$(e.target).is(selectables.ignore))return;return false;};var trySelect=function(node){if(selectables.nodesSelected[node.element.selectableid])return;selectables.nodesSelected[node.element.selectableid]=true;if(typeof node.select=='function')node.select(node.element);};var tryUnselect=function(node){if(!selectables.nodesSelected[node.element.selectableid])return;delete selectables.nodesSelected[node.element.selectableid];if(typeof node.unselect=='function')node.unselect(node.element);};var unselectAll=function(){$.each(selectables.nodes,function(){tryUnselect(this);});selectables.nodesSelected={};};$.extend({selectable:function(mode){if(mode=='refresh'){refresh(true);return;}
if(mode=='selected'){var results=[];$.each(selectables.nodesSelected,function(id){var node=selectables.nodes[id];if(!node||!node.element||!node.element.parentNode||!node.element.parentNode.tagName){delete selectables.nodes[id];delete selectables.nodesSelected[id];return;}
results.push(node.element);});return $(results);}}});$.fn.extend({selectable:function(params){var handle=params.handle;var ignore=params.ignore;var select=params.select;var startFunction=params.start;var unselect=params.unselect;selectables.delayInit=(typeof params.delayInit!='undefined')?params.delayInit:true;selectables.ignoreAlso=ignore;selectables.start=startFunction;unselectAll();selectables.nodes={};this.each(function(){var area=handle?$(handle,this).get(0):this;if(!this.selectableid)this.selectableid=this.id||Math.floor(((Math.random()*100000000)+100000000));selectables.nodes[this.selectableid]={element:this,handle:{element:area},select:select,unselect:unselect};});refresh(!selectables.delayInit);if(!selectables.setMousedown){$(document.body).mousedown(mousedown);selectables.setMousedown=true;}
if(!selectables.setMousemove){$(document.body).mousemove(mousemove);selectables.setMousemove=true;}
if(!selectables.setMouseup){$(document.body).mouseup(mouseup);selectables.setMouseup=true;}
if(!selectables.setSelectStart){$(document.body).bind('selectstart',selectstart);selectables.setSelectStart=true;}
if(!selectables.setResize){$(window).resize(resize);selectables.setResize=true;}
return this;},selectableSelect:function(){return this.each(function(){if(!this.selectableid)return;if(!selectables.nodes[this.selectableid])return;trySelect(selectables.nodes[this.selectableid]);});},selectableUnselect:function(){return this.each(function(){if(!this.selectableid)return;if(!selectables.nodes[this.selectableid])return;tryUnselect(selectables.nodes[this.selectableid]);});}});})(jQuery);