/**
 * Extended Type, Javascript file
 * 
 * Extended Type is an innovative web technology that extends
 * the range of fonts available for use on a web site.
 *
 * @author Rob Stanford <rob.stanford@airlock.com>
 * @version 0.9.7
 * @copyright Copyright (c) 2008 Airlock Limited <http://www.airlock.com>
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public Licnese
 * @link http://opensource.airlock.net/projects/extended-type/
 *
 * Influenced by work from Lalit Patel <http://www.lalit.org/>
 */

var ExtendedType=function(params){this.params=params};ExtendedType.prototype.initialise=function(){this.params=this.params||new Array();this.params.path=this.params.path||'et.php';this.params.maxLength=this.params.maxLength||200;this.params.compareFont=this.params.compareFont||'arial';this.properties={fontSize:'',fontWeight:'',fontStyle:'',color:'',paddingTop:'',paddingBottom:'',paddingLeft:'',paddingRight:'',lineHeight:'',textTransform:''};this.browserFonts=new Object();this.replaced=new Array();var fonts=['serif','sans-serif','cursive','fantasy','monospace'].concat(this.params.exclude||new Array(),this.params.compareFont);for(var i=0;i<fonts.length;++i){this.browserFonts[fonts[i].toLowerCase()]=''}this.test=document.createElement('span');this.test.style.fontFamily=this.params.compareFont;this.test.style.fontSize='72px';this.test.innerHTML='mmmmmmmmmml';document.body.appendChild(this.test);this.defaultWidth=this.test.offsetWidth;if(this.params.resize==true){var self=this;this.resizeTimeout;this.windowWidth=document.body.clientWidth;window.onresize=function(){self.resize()}}};ExtendedType.prototype.replace=function(){this.initialise();var elements=document.body.getElementsByTagName('*');for(var i=0;i<elements.length-1;++i){if(elements[i].childNodes.length==1&&elements[i].firstChild.nodeType==3){if(EtElement.prototype.et_inspect(elements[i],this.params.maxLength,this)){this.extend(elements[i],EtElement.prototype);elements[i].et_initialise(this.properties);elements[i].et_replace(this.params.path);this.replaced.push(elements[i])}}}document.body.removeChild(this.test)};ExtendedType.prototype.extend=function(object,prototype){for(var name in prototype){object[name]=prototype[name]}return object};ExtendedType.prototype.testForFont=function(font){if(font in this.browserFonts)return true;this.test.style.fontFamily=font+','+this.params.compareFont;if(this.test.offsetWidth!=this.defaultWidth){this.browserFonts[font]='';return true}};ExtendedType.prototype.resize=function(){if(this.windowWidth==document.body.clientWidth){return}this.windowWidth=document.body.clientWidth;var self=this;var replace=function(){for(var i=0;i<self.replaced.length;++i){self.replaced[i].et_replace(self.properties,this.params.path)}};clearTimeout(this.resizeTimeout);this.resizeTimeout=setTimeout(replace,800)};var EtElement=function(){};EtElement.prototype.et_inspect=function(element,maxLength,et){if(!element.innerHTML||element.innerHTML>maxLength){return false}var fonts=EtElement.prototype.et_getFonts(element);if(!et.testForFont(fonts[0])){return true}};EtElement.prototype.et_initialise=function(cssProperties){this.et={};this.et.properties=this.et_getComputedStyles(cssProperties);this.et.properties.backgroundColor=this.et_getBackgroundColor(this);this.et.properties.fonts=this.et_getFonts(this);this.et.properties.text=this.innerHTML;if(this.et.properties.backgroundColor=='transparent'){this.et.ir=new RundlePharkIR(this)}else{this.et.ir=new GilderLevinIR(this,this.et.properties.backgroundColor)}};EtElement.prototype.et_replace=function(path){this.et.ir.ready(this,false);this.et.properties.width=this.et_getDimensions('width');this.et.properties.height=this.et_getDimensions('height');this.et.ir.ready(this,true,this.et.properties);var url=path+'?';for(var name in this.et.properties){url+=name+'='+this.et_encode(this.et.properties[name])+'&'}this.et.image=ExtendedType.prototype.extend(new Image(),EtImage.prototype);this.et.image.et_load(url,this.et_irReplace,this)};EtElement.prototype.et_encode=function(string){if(typeof document.body.style.maxHeight=="undefined"){string=string.toString().replace(/#/g,'%23').replace(/&/g,'%26')}return escape(string)};EtElement.prototype.et_irReplace=function(){this.et.ir.replace(this,this.et.image,this.et.properties)};EtElement.prototype.et_getStyle=function(element,property){if(element.style[property]){return element.style[property]}else if(element.currentStyle){return element.currentStyle[property]}else if(document.defaultView&&document.defaultView.getComputedStyle){property=property.replace(/([A-Z])/g,"-$1").toLowerCase();return document.defaultView.getComputedStyle(element,"").getPropertyValue(property)}};EtElement.prototype.et_getFonts=function(element){return EtElement.prototype.et_getStyle(element,'fontFamily').toLowerCase().split(',')};EtElement.prototype.et_getBackgroundColor=function(element){var image=EtElement.prototype.et_getStyle(element,'backgroundImage');var color=EtElement.prototype.et_getStyle(element,'backgroundColor');if(image!='none'){return'transparent'}else if(color!='transparent'&&color!='rgba(0, 0, 0, 0)'){return color}else if(element.parentNode.style){return EtElement.prototype.et_getBackgroundColor(element.parentNode)}return'white'};EtElement.prototype.et_getDimensions=function(dimension){var measure=(dimension=='width')?this.offsetWidth:this.offsetHeight;var overflow=this.style.overflow;this.style.overflow='hidden';if(dimension=='width'){var width=this.style.width;this.style.width=0;var value=(measure-this.offsetWidth)+'px';this.style.width=width}else{var height=this.style.height;this.style.height=0;var value=(measure-this.offsetHeight)+'px';this.style.height=height}this.style.overflow=overflow;return value};EtElement.prototype.et_getComputedStyles=function(properties){var result={};var left=this.style.left;for(name in properties){var value=this.et_getStyle(this,name);var quantity=parseFloat(value)||value;var units=isNaN(quantity)?'':value.toString().substr(quantity.toString().length);if('|em|%|in|cm|mm|pt|pc'.indexOf(units)>0){if(units=='%'&&name=='lineHeight'){value=quantity/100*parseFloat(value)}else{if(name=='fontSize'){quantity=1;units='em'}this.style.left=quantity+units;value=this.style.pixelLeft}value+='px'}result[name]=value}this.style.left=left;return result};var EtImage=function(){};EtImage.prototype.et_load=function(src,callback,element){this.et={};this.src=src;if(this.complete){callback.call(element)}else{this.et.callback=callback;this.et.element=element;this.onload=this.et_onLoad}};EtImage.prototype.et_onLoad=function(){this.et.callback.call(this.et.element);this.et.callback=null;this.et.element=null};Function.prototype.inherits=function(superclass){var x=function(){};x.prototype=superclass.prototype;this.prototype=new x()};var GilderLevinIR=function(element,backgroundColor){this.span=document.createElement('span');this.span.style.backgroundColor=backgroundColor;this.span.style.backgroundRepeat='no-repeat';this.span.style.position='absolute';this.span.style.display='block';this.span.style.top=0;this.span.style.left=0;element.style.display='block';element.appendChild(this.span)};GilderLevinIR.prototype.ready=function(element,toggle,properties){element.style.position=toggle?'relative':'';element.style.letterSpacing=toggle?'normal':'';element.style.overflow=toggle?'hidden':'';element.style.height=toggle?properties.height:'';this.span.style.height=toggle?parseInt(properties.height)*4+'px':'';this.span.style.width=toggle?parseInt(properties.width)*2+'px':''};GilderLevinIR.prototype.replace=function(element,image,properties){if(image.height===1&&image.width===1){this.ready(element,false,properties);element.removeChild(this.span);return false}if(typeof document.body.style.maxHeight=="undefined"){this.span.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+image.src+"', sizingMethod='crop')"}else{this.span.style.backgroundImage='url('+image.src+')'}var paddingTop=parseFloat(properties.paddingTop);var elementHeight=parseFloat(properties.height)+paddingTop;var lineHeight=parseFloat(properties.lineHeight);var difference=Math.abs(image.height-elementHeight)+lineHeight*0.05;if(difference>=lineHeight){element.style.height=image.height-paddingTop+'px'}};var RundlePharkIR=function(element){GilderLevinIR.call(this,element)};RundlePharkIR.inherits(GilderLevinIR);RundlePharkIR.prototype.ready=function(element,toggle,properties){GilderLevinIR.prototype.ready.call(this,element,toggle,properties);element.style.textIndent=toggle?'-9999em':'';this.span.style.height=toggle?'100%':'';this.span.style.width=toggle?'100%':''};