/*
 *
 * Copyright (c) 2006 Sam Collett (http://www.texotela.co.uk)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 */

/*
 * Converts image and link elements to thumbnails
 *
 * @name     jThumb
 * @author   Joan Piedra (http://www.joanpiedra.com)
 * @example  $("a.thumb, img.thumb").thumbs();
 *
 */
jQuery.fn.thumbs = function() {
    this.each(function() {
        var img = new Image();
        img.src = $(this).attr("src");
        var width = $(this).css('width');
        var height = $(this).css('height');

        var photoAspectRatio = img.width / img.height;
        var canvasAspectRatio = width.replace("px", "") / height.replace("px", "");

        if (photoAspectRatio < canvasAspectRatio) {
            $(this).css('width', width);
            $(this).css('height', 'auto');

            var intHeight = height.replace("px", ""); //tirar o PX
            $(this).css('marginTop', (-Math.floor(intHeight / 2)));
        }
        else {
            $(this).css('width', 'auto');
            $(this).css('height', height);
        }

        $(this).wrap('<div class="thumb-img" style="width:' + width + ' ;height:' + height + ';"><div class="thumb-inner">' + '</div></div>');
    });
}

jQuery.fn.thumbsSingle = function() {
        var img = new Image();
        img.src = $(this).attr("src");
        var width = $(this).css('width');
        var height = $(this).css('height');

        var photoAspectRatio = img.width / img.height;
        var canvasAspectRatio = width.replace("px", "") / height.replace("px", "");

        if (photoAspectRatio < canvasAspectRatio) {
            $(this).css('width', width);
            $(this).css('height', 'auto');

            var intHeight = height.replace("px", ""); //tirar o PX
            $(this).css('marginTop', (-Math.floor(intHeight / 2)));
        }
        else {
            $(this).css('width', 'auto');
            $(this).css('height', height);
        }

        $(this).wrap('<div class="thumb-img" style="width:' + width + ' ;height:' + height + ';"><div class="thumb-inner">' + '</div></div>');
}


/*
 * Absolute positions the image in the middle of the thumbnail frame
 *
 * @name     jThumbImg
 * @author   Joan Piedra (http://www.joanpiedra.com)
 * @example  $("a.thumb img, img.thumb").thumbsImg();
 *
 */
jQuery.fn.thumbsImg = function()
{
	return this.each(
		function()
		{
			jQuery(this).css('position','absolute');
			jQuery(this).left( '-' + ( parseInt( $(this).width() ) / 2 ) + 'px' );
			jQuery(this).top( '-' + ( parseInt( $(this).height() ) / 2 ) + 'px' );
			jQuery(this).css('margin-left', '50%' );
			jQuery(this).css('margin-top', '50%');
		}
	)
}
