/*! Clusterize.js - v0.7.0 - 2015-05-21
* http://NeXTs.github.com/Clusterize.js/
* Copyright (c) 2015 Denis Lukov; Licensed MIT */

;(function(n,l){"undefined"!=typeof module?module.exports=l():"function"==typeof define&&"object"==typeof define.amd?define(l):this[n]=l()})("Clusterize",function(){function n(b,a,c){return a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent("on"+b,c)}function l(b){return"[object Array]"===Object.prototype.toString.call(b)}var g=function(){for(var b=3,a=document.createElement("b"),c=a.all||[];a.innerHTML="\x3c!--[if gt IE "+ ++b+"]><i><![endif]--\x3e",c[0];);return 4<b?b:document.documentMode}(),
m=function(b){if(!(this instanceof m))return new m(b);var a=this,c={item_height:0,block_height:0,rows_in_block:50,rows_in_cluster:0,cluster_height:0,blocks_in_cluster:4,tag:null,content_tag:null,show_no_data_row:!0,no_data_class:"clusterize-no-data",no_data_text:"No data",keep_parity:!0,verify_change:!1};a.options={};for(var d="rows_in_block blocks_in_cluster verify_change show_no_data_row no_data_class no_data_text keep_parity tag".split(" "),f=0,k;k=d[f];f++)a.options[k]="undefined"!=typeof b[k]&&
null!=b[k]?b[k]:c[k];c=["scroll","content"];for(f=0;d=c[f];f++)if(a[d+"_elem"]=b[d+"Id"]?document.getElementById(b[d+"Id"]):b[d+"Elem"],!a[d+"_elem"])throw Error("Error! Could not find "+d+" element");var e=l(b.rows)?b.rows:a.fetchMarkup(),h={data:""};b=a.scroll_elem.scrollTop;a.exploreEnvironment(e);a.insertToDOM(e,h);a.scroll_elem.scrollTop=b;var p=!1,g=function(){p!=(p=a.getClusterNum())&&a.insertToDOM(e,h)};n("scroll",a.scroll_elem,g);a.destroy=function(b){var c=a.scroll_elem;c.removeEventListener?
c.removeEventListener("scroll",g,!1):c.detachEvent("onscroll",g);a.html((b?a.generateEmptyRow():e).join(""))};a.update=function(b){e=l(b)?b:[];b=a.scroll_elem.scrollTop;a.insertToDOM(e,h);a.scroll_elem.scrollTop=b};a.clear=function(){a.update([])};a.getRowsAmount=function(){return e.length};var q=function(b,c){var d=l(c)?c:[];d.length&&(e="append"==b?e.concat(d):d.concat(e),a.insertToDOM(e,h))};a.append=function(a){q("append",a)};a.prepend=function(a){q("prepend",a)}};m.prototype={constructor:m,fetchMarkup:function(){for(var b=
[],a=this.getChildNodes(this.content_elem);a.length;)b.push(a.shift().outerHTML);return b},exploreEnvironment:function(b){var a=this.options;a.content_tag=this.content_elem.tagName.toLowerCase();if(!a.item_height||!a.tag){if(!b.length)return;g&&9>=g&&(a.tag=b[0].split("<")[1].split(" ")[0].split(">")[0]);this.html(b[0]+b[0]+b[0]);b=this.content_elem.children[1];a.tag||(a.tag=b.tagName.toLowerCase());a.item_height=b.offsetHeight}a.block_height=a.item_height*a.rows_in_block;a.rows_in_cluster=a.blocks_in_cluster*
a.rows_in_block;a.cluster_height=a.blocks_in_cluster*a.block_height},getClusterNum:function(){var b=this.options;return Math.floor(this.scroll_elem.scrollTop/(b.cluster_height-b.block_height))},generateEmptyRow:function(){var b=this.options;if(!b.tag||!b.show_no_data_row)return[];var a=document.createElement(b.tag),c=document.createTextNode(b.no_data_text),d;a.className=b.no_data_class;"tr"==b.tag&&(d=document.createElement("td"),d.appendChild(c));a.appendChild(d||c);return[a.outerHTML]},generate:function(b,
a){var c=this.options,d=b.length;if(d<c.rows_in_block)return{rows_above:0,rows:d?b:this.generateEmptyRow()};c.cluster_height||this.exploreEnvironment(b);var f=a*c.rows_in_cluster-c.rows_in_block*a,f=0<f?f:0,k=f+c.rows_in_cluster,e=f*c.item_height,d=(d-k)*c.item_height,h=[],g=f;0<e?(c.keep_parity&&h.push(this.renderExtraTag("keep-parity")),h.push(this.renderExtraTag("top-space",e))):g++;for(c=f;c<k;c++)b[c]&&h.push(b[c]);0<d&&h.push(this.renderExtraTag("bottom-space",d));return{rows_above:g,rows:h}},
renderExtraTag:function(b,a){var c=document.createElement(this.options.tag);c.className=["clusterize-extra-row","clusterize-"+b].join(" ");a&&(c.style.height=a+"px");return c.outerHTML},insertToDOM:function(b,a){var c=this.generate(b,this.getClusterNum()),d=c.rows.join("");if(!this.options.verify_change||this.options.verify_change&&this.dataChanged(d,a))this.html(d),"ol"==this.options.content_tag&&this.content_elem.setAttribute("start",c.rows_above)},html:function(b){var a=this.content_elem;if(g&&
9>=g&&"tr"==this.options.tag){var c=document.createElement("div");for(c.innerHTML="<table><tbody>"+b+"</tbody></table>";b=a.lastChild;)a.removeChild(b);for(c=this.getChildNodes(c.firstChild.firstChild);c.length;)a.appendChild(c.shift())}else a.innerHTML=b},getChildNodes:function(b){b=b.children;for(var a=[],c=0,d=b.length;c<d;c++)a.push(b[c]);return Array.prototype.slice.call(a)},dataChanged:function(b,a){var c=JSON.stringify(b);return c!==a.data&&(a.data=c)}};return m});