/*
 * flugrid for jQuery - simple fluid Grid
 *
 * Copyright (c) 2008 Joe Rivard (Red Orbit Systems LLC)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2008-07-16$
 */
(function($) {
  $.fn.flugrid = function(options,callback) {
      var settings = {
          width: "100%",
          height: 'auto',
          hmin: 100,
          hmax: null,
          stripe: true,
          vresize: true
      };
      if ( options && typeof(options) != 'string' )
          $.extend(settings, options);
          
      var drag={        
        start:function(hdl,e){        
          //initialize drag
          drag.y= e.pageY;
          drag.h = grid.body.height()
          hdl.hover(function(){},function(ev){if(Math.abs(ev.pageY-drag.y) > 20){drag.stop(hdl,ev)}});
          $(document).bind("mousemove", function(ev){            
            drag.h = (drag.h + (ev.pageY - drag.y));drag.y= ev.pageY;
            //make sure it is > min height and < max height
            if((!settings.hmax||drag.h < settings.hmax) && (!settings.hmin||drag.h > settings.hmin)){grid.body.css({height: drag.h })};
          }).bind("mouseup",function(e){drag.stop(hdl,e)})
        },
        stop: function(hdl,e){$(document).unbind("mousemove");hdl.unbind("hover")}
      };
      //Original Table Parts
      var table={};
      //Generate Grid Parts
      var grid = {
        resize:function(grid, table){
          widths = new Array();        
          cols = table.header.find("td,th").each(function(){
            //w=$(this).css("width") || $(this).attr("width") || $(this).width();
            widths.push($(this).width())            
          }).size();
          //check to see if scroll bar is showing
          diff = grid.body.width()-grid.body.attr("scrollWidth");
          widths[cols-1] = parseInt(widths[cols-1]) - parseInt(diff);
          grid.self.find("tr").each(function(i){
            $(this).find("td").each(function(i){              
              $(this).css({width: widths[i]})
              $(this).find("div").css({width: widths[i]-10})
            })
          });          
        }
      };
      
      
      return this.each(function() {
        //Check to see if grid was already generated
        if($(this).attr("fg"))return false;
        grid.self = $("<div class='sgrid'></div>");
        // generate table wrapper
        grid.wrap = $("<div class='twrap stretch'></div>");
        // generate table head wrapper
        grid.header = $("<div class='thwrap wstretch'></div>");
        // generate table body wrapper
        grid.body = $("<div class='tbwrap wstetch'></div>");
        //genreate table footer
        grid.footer = $("<div class='tfwrap wstretch'></div>");        
        //grab table
        table.self = $(this).addClass("wstretch").attr("fg","1");
        //Grab table header
        table.header = $("<table class='wstretch'></table>").append(table.self.find("tr:first"));
        //render sgrid at position of table to be transformed
        grid.self.insertBefore(table.self);
        //append the header to the new grid
        grid.header.append(table.header);
        //append grid header to grid
        grid.wrap.append(grid.header);
        //append table body to grid body
        grid.body.append(table.self);
        //append grid body to grid
        grid.wrap.append(grid.body);
        //append grid footer to grid
        grid.wrap.append(grid.footer);
        //Generate handle if hresize
        if(settings.vresize){
          grid.handle = $("<div class='hdl'></div>").appendTo(grid.footer).bind("mousedown", function(ev){
            drag.start($(this),ev)
            grid.handle.bind("mouseup",function(ev){
              drag.stop($(this),ev)
            })
          });
        }

        //append grid to container
        grid.self.append(grid.wrap);
        //set width of grid
        grid.self.css({width:settings.width})
        //set height of grid
        grid.body.css({height: settings.height});        
        $(".sgrid").resize(function(){grid.resize(grid, table);});
        //generate stripes
        if(settings.stripe){
          grid.body.find("tr").each(function(i){if(i%2 > 0){$(this).attr("class","alt");}})
        }
        //Grab Min/Max Height if not specified
        if(!settings.height)settings.hmax = (settings.hmax || grid.self.height()-grid.header.height()-4);
        settings.hmin.toString().replace("px","")
        //place div's inside <td>'s
        $(".sgrid").find("td,th").each(function(){
          $("<div></div>").html($(this).html()).appendTo($(this).html(""))  
        })
        //generate widths
        grid.resize(grid, table);
        //fix safari bug
        if($.browser.safari){ setTimeout(function(){grid.resize(grid, table)},1)}
        $(window).resize(function(){grid.resize(grid, table);});
      });  
  }  
})(jQuery);
