/* jQuery Image Text Replacement Alpha 1
 * Author: Jamie Thompson (jamie_at_themagictorch_d0t_org) 
 * Website: http://jamazon.co.uk
 * 
 * Copyright (c) 2008 Jamie Thompson
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

var ONE_WORD = false;

(function($) {

	$.fn.jitr = function(font, bgcolor){
		
		var hex = function(N) {
			if(N==null) return "00";
			N = parseInt(N);
			if(N==0 || isNaN(N)) return "00";
			N = Math.max(0, N);
			N = Math.min(N, 255);
			N = Math.round(N);
			return "0123456789ABCDEF".charAt((N - N%16) / 16) + "0123456789ABCDEF".charAt(N%16);
		};
		
		function hex2(s) {
	        var s = parseInt(s).toString(16);
	        return ( s.length < 2 ) ? '0'+s : s;
	    };
		
	    function gpc(node) {
	        for ( ; node && node.nodeName.toLowerCase() != 'html'; node = node.parentNode  ) {
	            var v = jQuery.css(node,'backgroundColor');
	            if ( v.indexOf('rgb') >= 0 ) { 
	                rgb = v.match(/\d+/g); 
	                return hex2(rgb[0]) + hex2(rgb[1]) + hex2(rgb[2]);
	            }
	            if ( v && v != 'transparent' )
	                
					return v;
	        }
	        return 'ffffff';
	    };
		
		var hexed = function(color) {
			if(!color) { return false; };
			if(color.search('rgb') > -1) {
				color = color.substr(4,color.length-5).split(', ');
				color = hex(color[0]) + hex(color[1]) + hex(color[2]);
			};
			color = color.replace('#','');
			if(color.length < 6) {
				color = color.substr(0, 1) 
					+ color.substr(0, 1) 
					+ color.substr(1, 1) 
					+ color.substr(1, 1) 
					+ color.substr(2, 1) 
					+ color.substr(2, 1);
			};
			return color;
		};
		
		return $(this).each(function(){
			if( !$(this).children().length ){
				bgcolor = bgcolor ? bgcolor : gpc(this.parentNode);
				var color = hexed($(this).css('color'));
				var size = parseInt($(this).css('font-size'));
				if( ONE_WORD == true ) {
  				var words = $(this).text();
  				$(this).html('');
  				$(this).append('<img alt="'+words+'" src="http://superficelle.fr/wp-content/js/jitr.php?text='
  				+escape(words)+'&color='+color+'&bgcolor='+bgcolor+
  				'&size='+size+'&font='+font+'">');
				  
				} else {
  				//if($.browser.msie) size = Math.round(size / 1.7) /* why microsoft WHY? */
  				var words = $(this).text().split(' ');
  				$(this).html('');
  				for (i = 0; i < words.length; i++){
  					//$(this).append('<img alt="'+words[i]+'" src="/heading.php?text='
  					$(this).append('<img alt="'+words[i]+'" src="http://superficelle.fr/wp-content/js/jitr.php?text='
  					+escape(words[i])+'&color='+color+'&bgcolor='+bgcolor+
  					'&size='+size+'&font='+font+'">');
  				};
				}

			};
			$(this).css('visibility','visible');
		});
	};
})(jQuery);