//     Griddle.js 0.1
//     (c) 2011 Jeremy Singer-Vine
//     Griddle may be freely distributed under the MIT license.
//     Details and documentation at: http://jsvine.github.com/griddle
(function(){var a={VERSION:0.1};window.Griddle=window.Griddle||a;function e(h){var g;for(g in h){this.style[g]=h[g]}return this}function f(g,i,j){var h=document.createElement(g);if(i){h.className=i}if(j){h.innerHTML=j}return h}function d(){var h,g,j;for(h=0;h<this.tiles.length;h++){g=h%this.n_col;j=Math.floor(h/this.n_col);this.tiles[h].x=g;this.tiles[h].y=j;this.tiles[h].index=h;this.tiles[h].adjacent={left:g>0?h-1:false,right:g<(this.n_col-1)?h+1:false,up:j>0?h-this.n_col:false,down:j<((this.tiles.length/this.n_col)-1)?h+this.n_col:false}}}function c(g,h){this.el=f(g.tile_element_type,"griddle_tile");e.call(this.el,{cssFloat:"left",styleFloat:"left",width:g.tile_width?g.tile_width+"px":"auto",height:g.tile_height?g.tile_height+"px":"auto"});this.data=h;this.index=null;this.x=null;this.y=null;this.adjacent={}}function b(g){var h=g.transition||"left 0.25s linear, top 0.25s linear";this.config=g;this.container=g.container;this.grid_element_type=g.grid_element_type||"div";this.tile_element_type=g.tile_element_type||"div";this.el=f(g.grid_element_type||"div","griddle");this.render=g.render;this.n_col=g.n_col||g.data.length;this.n_col_visible=g.n_col_visible||g.n_col;this.n_row_visible=g.n_row_visible||g.data.length/g.n_col;this.current_index=0;this.tile_width=g.tile_width;this.tile_height=g.tile_height;this.container_height=g.container_height;this.tiles=[];e.call(this.el,{position:"relative",overflow:"visible",top:0,left:0,WebkitTransition:h,MozTransition:h,OTransition:h,MsTransition:h,Transition:h})}b.prototype.add=function(m,h,g){var l,n=[],j,k=g||this.render;if(Object.prototype.toString.call(m)!=="[object Array]"){m=[m]}for(j=0;j<m.length;j++){l=new c(this,m[j]);n.push(l);if(h===undefined){this.el.appendChild(l.el);this.tiles.push(l)}else{this.el.insertBefore(this.el,this.tiles[h].el);this.tiles.splice(h,0,l)}}d.call(this);for(j=0;j<n.length;j++){k.call(n[j])}return this};b.prototype.remove=function(g,j,i){var h=j||1;while(h--){if(i){i.push(this.tiles.slice(g,1))}this.el.removeChild(this.tiles[g].el);this.tiles.splice(g,1)}d.call(this);return this};b.prototype.go=function(g){var h=this.tiles[g];this.el.style.left=-(h.el.offsetLeft)+"px";this.el.style.top=-(h.el.offsetTop)+"px";if(this.config.onexit){this.config.onexit.call(this)}this.current_index=g;if(this.config.onenter){this.config.onenter.call(this)}return this};b.prototype.shift=function(h,j){var g=this.current_index;j=j||1;while(j--){g=this.tiles[g].adjacent[h];if(!this.tiles[g]){return false}}return this.go(g)};b.prototype.setDimensions=function(g){this.n_col=g.n_col||this.n_col;this.n_col_visible=g.n_col_visible||this.n_col_visible;this.n_row_visible=g.n_row_visible||this.n_row_visible;e.call(this.el,{width:this.tile_width*this.n_col+"px"});e.call(this.container,{width:this.tile_width*this.n_col_visible+"px",height:(this.tile_height&&this.n_row_visible)?this.tile_height*this.n_row_visible+"px":(this.container_height?this.container_height+"px":"auto")});d.call(this);return this};a.create=function(g){var h=new b(g);e.call(g.container,{overflow:"hidden",position:"relative"});h.setDimensions({n_col:h.n_col,n_col_visible:h.n_col_visible,n_row_visible:h.n_row_visible});if(g.data){h.add(g.data)}g.container.appendChild(h.el);return h}}());