(function ($) {
    $.fn.borderGlow = function (red, green, blue) {
        return this.each(function () {
            var $$ = $(this);
            var r = red, g = green, b = blue;
            
            if (this.fade) {
                clearInterval(this.fade);
            }
            
            var original = $$.css('border-color');
            
            this.style.borderColor = "rgb("+r+","+g+","+b+")";
            if (r == 255 && g == 255 && b == 255) {
                return;
            }
            var newred = r + Math.ceil((255 - r)/10);
            var newgreen = g + Math.ceil((255 - g)/10);
            var newblue = b + Math.ceil((255 - b)/10);
            var repeat = function() {
                fadeUp(element,newred,newgreen,newblue);
            };
            this.fade = setTimeout(repeat,100);
            
        });
    };
    
    $.fn.toggleClassIf = function (v, c) {
        return v ? this.addClass(c) : this.removeClass(c);
    };
    
    // Note - if you change an input field with a hint attached,
    // make sure call .focus() first in the chain to ensure the
    // blur class is removed.
    $.fn.hint = function () {
      return this.each(function (){
        // get jQuery version of 'this'
        var t = $(this); 
        // get it once since it won't change
        var title = t.attr('title'); 
        // only apply logic if the element has the attribute
        if (title) { 
          // on blur, set value to title attr if text is blank
          t.blur(function (){
            if (t.val() == '') {
              t.addClass('blur').val(title);
            }
          });
          // on focus, set value to blank if current value 
          // matches title attr
          t.focus(function (){
            if (t.val() == title) {
              t.val('').removeClass('blur');
            }
          });

          // clear the pre-defined text when form is submitted
          t.parents('form:first()').submit(function(){
              if (t.val() == title) {
                  t.val('').removeClass('blur');
              }
          });

          // now change all inputs to title
          t.blur();
        }
      });
    };
    
    /*
        jQuery Plugin textGrow (leftlogic.com/info/articles/auto_grow_text)
        (c) 2006 Remy Sharp (leftlogic.com)

        Usage: jQuery('input').textGrow({ pad: 25 });
            - pad: trailing padding in pixels - default 25px
            - min_width: minimum width of the text box in pixels
            - max_width: maximum width of the text box in pixels

        If min and max width are not passed in, you can apply this
        via CSS using min-width and max-width attributes.

        See example at: leftlogic.com/info/articles/auto_grow_text  
    */
    $.fn.textGrow = function(options) {
        if (!options.pad) options.pad = 25;
        return this.each(function(i){
            var e = $(this);
            // padding
            var pl, pr, pt, pb;
            // create copy for each element
            var o = { pad: options.pad, min_limit: options.min_limit, max_limit: options.max_limit }; 
            if (e.attr('type') == 'text') {
                var cssminw = parseInt(e.css('min-width'));
                var cssmaxw = parseInt(e.css('max-width'));
                if (cssminw) o.min_limit = parseInt(e.css('min-width'));
                if (cssmaxw) o.max_limit = parseInt(e.css('max-width'));

                // Safari reads a non-existant value as -1 making our calcs a mess
                if (o.min_limit < 0 || isNaN(o.min_limit)) o.min_limit = 0;
                if (o.max_limit < 0 || isNaN(o.max_limit)) o.max_limit = 0;

                pl = e.css('padding-left');
                pr = e.css('padding-right');  // do we care about this?
                pt = e.css('padding-top');
                pb = e.css('padding-bottom');
 
                var html = '<' + 'div style="position:absolute; top:-10000px; visibility:hidden; left:-10000px; padding: ' + pt + ' ' + o.pad + 'px ' + pb + ' ' + pl + '!important; margin:0!important" id="_grow' + i + '"></' + 'div>';
                var c = $('body').append(html);
                // adjust the style of the container to match the font of this element
                var ff = e.css('font-family');
                var fs = e.css('font-size');

                $('div#_grow' + i).css({ 'font-size': fs, 'font-family': ff });

                resize = function(){
                    // put text in hidden div and size appropriately
                    var dl = $('div#_grow' + i).html(e.val().replace(/[ <>]/g,function (m) {
                        var r = '';
                        if (m == ' ') r = '&nbsp;';
                        else if (m == '<') r = '&lt;';
                        else if (m == '>') r = '&gt;';
                        return r;                        
                    })).get(0).offsetWidth;
                    
                    if (o.max_limit && (dl + o.pad) > o.max_limit) {
                        // stop at max width
                        e.css('width', o.max_limit + 'px');
                    } else if (dl <= o.min_limit) {
                        // stop at min midth
                        e.css('width', o.min_limit + 'px');
                    } else { // resize
                        e.css('width', dl + 'px');
                    }
                };

                // auto resize based on current content
                resize();
                e.keypress(resize).keyup(resize);
            }
        });
    };
    
    $.fn.pngFix = function(settings) {

    	// Settings
    	settings = $.extend({
    		blankgif: '/images/blank.gif'
    	}, settings);

    	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
    	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

    	if ($.browser.msie && (ie55 || ie6)) {

    		//fix images with png-source
    		$(this).find("img[@src$=.png]").each(function() {

    			$(this).attr('width',$(this).width());
    			$(this).attr('height',$(this).height());

    			var prevStyle = '';
    			var strNewHTML = '';
    			var imgId = ($(this).attr('id')) ? 'id="' + $(this).attr('id') + '" ' : '';
    			var imgClass = ($(this).attr('class')) ? 'class="' + $(this).attr('class') + '" ' : '';
    			var imgTitle = ($(this).attr('title')) ? 'title="' + $(this).attr('title') + '" ' : '';
    			var imgAlt = ($(this).attr('alt')) ? 'alt="' + $(this).attr('alt') + '" ' : '';
    			var imgAlign = ($(this).attr('align')) ? 'float:' + $(this).attr('align') + ';' : '';
    			var imgHand = ($(this).parent().attr('href')) ? 'cursor:hand;' : '';
    			if (this.style.border) {
    				prevStyle += 'border:'+this.style.border+';';
    				this.style.border = '';
    			}
    			if (this.style.padding) {
    				prevStyle += 'padding:'+this.style.padding+';';
    				this.style.padding = '';
    			}
    			if (this.style.margin) {
    				prevStyle += 'margin:'+this.style.margin+';';
    				this.style.margin = '';
    			}
    			var imgStyle = (this.style.cssText);

    			strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt;
    			strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;'+imgAlign+imgHand;
    			strNewHTML += 'width:' + $(this).width() + 'px;' + 'height:' + $(this).height() + 'px;';
    			strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + $(this).attr('src') + '\', sizingMethod=\'scale\');';
    			strNewHTML += imgStyle+'"></span>';
    			if (prevStyle != ''){
    				strNewHTML = '<span style="position:relative;display:inline-block;'+prevStyle+imgHand+'width:' + $(this).width() + 'px;' + 'height:' + $(this).height() + 'px;'+'">' + strNewHTML + '</span>';
    			}

    			$(this).hide();
    			$(this).after(strNewHTML);

    		});

    		// fix css background pngs
    		$(this).find("*").each(function(){
    			var bgIMG = $(this).css('background-image');
    			if(bgIMG.indexOf(".png")!=-1){
    				var iebg = bgIMG.split('url("')[1].split('")')[0];
    				$(this).css('background-image', 'none');
    				$(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
    			}
    		});

    		//fix input with png-source
    		$(this).find("input[@src$=.png]").each(function() {
    			var bgIMG = $(this).attr('src');
    			$(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
       		    $(this).attr('src', settings.blankgif);
    		});

    	}

    	return $;

    };
    
})(jQuery);

