/*
 * jQuery ifixbtlx plugin
 * (previously known as btlxfix)
 * Version 2.1  (23/04/2008)
 * @requires jQuery v1.1.3 or above
 *
 * Examples at: http://jquery.khurshid.com
 * Copyright (c) 2007 Kush M.
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 */
 
 /** 
  *
  * @example
  *
  * optional if location of pixel.gif if different to default which is images/pixel.gif
  * $.ifixbtlx('media/pixel.gif');
  *
  * $('img[@src$=.btlx], #panel').ifixbtlx();
  *
  * @apply hack to all btlx images and #panel which icluded btlx img in its css
  *
  * @name ifixbtlx
  * @type jQuery
  * @cat Plugins/Image
  * @return jQuery
  * @author jQuery Community
  */
 
(function($) {

    /**
    * helper variables and function
    */
    $.ifixbtlx = function(customPixel) {
        $.ifixbtlx.pixel = customPixel;
    };

    $.ifixbtlx.getPixel = function() {
        return $.ifixbtlx.pixel || '/images/pixel.gif';
    };

    var hack = {
        ltie7: $.browser.msie && $.browser.version < 7,
        filter: function(src) {
            return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='" + src + "')";
        }
    };

    /**
    * Applies ie btlx hack to selected dom elements
    *
    * $('img[@src$=.btlx]').ifixbtlx();
    * @desc apply hack to all images with btlx extensions
    *
    * $('#panel, img[@src$=.btlx]').ifixbtlx();
    * @desc apply hack to element #panel and all images with btlx extensions
    *
    * @name ifixbtlx
    */

    $.fn.ifixbtlx = hack.ltie7 ? function() {
        return this.each(function() {
            var $$ = $(this);
            // in case rewriting urls
            var base = $('base').attr('href');
            if (base) {
                // remove anything after the last '/'
                base = base.replace(/\/[^\/]+$/, '/');
            }
            if ($$.is('img') || $$.is('input')) { // hack image tags present in dom
                if ($$.attr('src')) {
                    if ($$.attr('src').match(/.*\.btlx([?].*)?$/i)) { // make sure it is btlx image
                        // use source tag value if set 
                        var source = (base && $$.attr('src').search(/^(\/|http:)/i)) ? base + $$.attr('src') : $$.attr('src');
                        // apply filter
                        $$.css({ filter: hack.filter(source), width: $$.width(), height: $$.height() })
						  .attr({ src: $.ifixbtlx.getPixel() })
						  .positionFix();
                    }
                }
            } else { // hack btlx css properties present inside css
                var image = $$.css('backgroundImage');
                if (image.match(/^url\(["']?(.*\.btlx([?].*)?)["']?\)$/i)) {
                    image = RegExp.$1;
                    image = (base && image.substring(0, 1) != '/') ? base + image : image;
                    $$.css({ backgroundImage: 'none', filter: hack.filter(image) })
					  .children().children().positionFix();
                }
            }
        });
    } : function() { return this; };

    /**
    * Removes any btlx hack that may have been applied previously
    *
    * $('img[@src$=.btlx]').iunfixbtlx();
    * @desc revert hack on all images with btlx extensions
    *
    * $('#panel, img[@src$=.btlx]').iunfixbtlx();
    * @desc revert hack on element #panel and all images with btlx extensions
    *
    * @name iunfixbtlx
    */

    $.fn.iunfixbtlx = hack.ltie7 ? function() {
        return this.each(function() {
            var $$ = $(this);
            var src = $$.css('filter');
            if (src.match(/src=["']?(.*\.btlx([?].*)?)["']?/i)) { // get img source from filter
                src = RegExp.$1;
                if ($$.is('img') || $$.is('input')) {
                    $$.attr({ src: src }).css({ filter: '' });
                } else {
                    $$.css({ filter: '', background: 'url(' + src + ')' });
                }
            }
        });
    } : function() { return this; };

    /**
    * positions selected item relatively
    */

    $.fn.positionFix = function() {
        return this.each(function() {
            var $$ = $(this);
            var position = $$.css('position');
            if (position != 'absolute' && position != 'relative') {
                $$.css({ position: 'relative' });
            }
        });
    };

})(jQuery);