
/* =====================================================================
Created By: Jordan Miller - LexJet Corporation
Summary:	   Cuts large images down and displays only a small portion
               in a thumbnail window.  
Includes:      jquery.lj.thumbs-1.0.js
=======================================================================*/

jQuery.thumbnail = function(container, options) {
    
    //Create the Wrapper
    var _thumbWrapper = $(document.createElement('div'));
    _thumbWrapper.addClass("ThumbnailWrapper");
    _thumbWrapper.css("width", options.ThumbnailWidth);
    _thumbWrapper.css("height", options.ThumbnailHeight);
    _thumbWrapper.css("padding", options.ThumbnailPadding);

    //Create the Thumbnail
    var _thumb = $(document.createElement('div'));
    _thumb.addClass("Thumbnail");

    //Set the properties of the Image
    if ($(container).width() != null) {
        var _w = parseInt($(container).width());
        if (_w - (options.ThumbnailWidth / 2) > options.ThumbnailWidth) {
            _w = (_w - (options.ThumbnailWidth / 2)) / 2;
            $(container).css('margin-left', '-' + _w + 'px');
        } else {
            if (_w > options.ThumbnailWidth) {
                _w = _w / 2;
                $(container).css('margin-left', '-' + _w + 'px');
            } else if (_w == options.ThumbnailWidth) {
                _w = 0;
                $(container).css('margin-left', _w + 'px');
            } else {
                _w = (options.ThumbnailWidth - _w) / 2;
                $(container).css('margin-left', _w + 'px');
            }
        }

    }
    if ($(container).height() != null) {
        var _h = parseInt($(container).height());
        if (_h - (options.ThumbnailHeight / 2) > options.ThumbnailHeight) {
            _h = (_h - (options.ThumbnailHeight / 2)) / 2;
            $(container).css('margin-top', '-' + _h + 'px');
        } else {
            if (_h > options.ThumbnailHeight) {
                _h = (_h - options.ThumbnailHeight) / 2;
                $(container).css('margin-top', '-' + _h + 'px');
            } else if (_h == options.ThumbnailHeight) {
                _h = 0;
                $(container).css('margin-top', _h + 'px');
            } else {
                _h = (options.ThumbnailHeight - _h) / 2;
                $(container).css('margin-top', _h + 'px');
            }
        }
    }
    $(container).css("display", "block");
    $(container).wrap(_thumbWrapper).wrap(_thumb); //Wrap the Image
    $(container).find("IMG").show(); 

}

jQuery.fn.thumbnail = function(options) {
    options = options || {};
    options.ThumbnailPadding = options.ThumbnailPadding || '6px';
    options.ThumbnailWidth = options.ThumbnailWidth || '130';
    options.ThumbnailHeight = options.ThumbnailHeight || '130';
    
    this.each(function() {
        var container = this;
        new jQuery.thumbnail(container, options);
    });
    return this;
}

