/* This loads in a stylesheet if JavaScript is enabled.  This gets rid of the flicker from the dtr and allows the site to be viewed right if js is disabled */
var myCSS = new Asset.css('http://www.clarkconstruction.com/?css=site/javascript_enabled.css.v.1237892068');

var DynamicTextReplacement = new Class({
	Implements: Options,
	options: {
		scriptURL: 'http://www.clarkconstruction.com/gdfont-renderer-20/font.php',
		classArray: ['home_callout', 'timeline_headline', 'content_headline', 'quote', 'section_headline', 'featured_project_headline', 'page_headline', 'news_headline', 'release_headline']
	},
	initialize: function(options) 
	{
		this.setOptions(options);
		this.options.classArray.each( function(dtr_class, class_index) 
		{
			$$('.' + dtr_class).each( function(element, index) 
			{
				/*get the text inside the element*/
				var theText = element.get('text');
				
				if (theText)
				{
					/*removes the existing text from the element*/
					element.set('text', '');
					
					/*create an image object and set its source and alt to the text, and give the image a class*/
					/*
					var theImage = new Element('img', {'src':this.options.scriptURL + '?text=' + encodeURIComponent(theText) + '&style=' + dtr_class});
					theImage.store('altText', theText);
					theImage.addEvent('load', function(theText)
					{
						this.setProperty('alt', this.retrieve('altText'));
						this.setProperty('width', this.width);
						this.setProperty('height', this.height);
					});
					*/
					
					/*append the image to the original object*/
					/*element.grab(theImage);*/
					
					
					/*
					var imageRequest = new Request({method: 'get', url: this.options.scriptURL + '?text=' + encodeURIComponent(theText) + '&style=' + dtr_class});
					imageRequest.addEvent('success', function(responseText, responseXML)
					{
						element.set('html', responseText);
					}.bind(this));
					imageRequest.send();
					*/
					var imageURL =  (Browser.Engine.trident4) ? this.options.scriptURL + '?text=' + encodeURIComponent(theText) + '&style=' + dtr_class + '&dimensions=true': this.options.scriptURL + '?text=' + encodeURIComponent(theText) + '&style=' + dtr_class;
					var jsonRequest = new Request.JSON({
						url: imageURL,
						onSuccess: function(responseJSON) 
						{
							var imageSource = responseJSON.name;
							var imageWidth = (responseJSON.width) ? responseJSON.width : "";
							var imageHeight = (responseJSON.height) ? responseJSON.height : "";
							
							//alert('source: http://www.clarkconstruction.com/gdfont-renderer-20/' + responseJSON.name + ', width: ' + responseJSON.width + ', height: ' + responseJSON.height);
							
							var theImage = new Asset.image( 'http://www.clarkconstruction.com/gdfont-renderer-20/' + imageSource, {
														   	onload: function() 
															{
																this.setProperty('alt', theText);
																if (imageWidth != "")
																{
																	this.setProperty('width', imageWidth);
																}
																if (imageHeight != "")
																{
																	this.setProperty('height', imageHeight);
																}
																/*append the image to the original object*/
																element.grab(theImage);
															}	   
							});
						}
					}).get();
				}
				
				/*set the objects css to display*/
				element.setStyle('display', 'block');
				element.removeClass(dtr_class);
			}, this);
		}, this);
	}					   
});

function searchBox()
{
	if ( $('keywords') )
	{
		if (undefined!==window.searchResults) 
		{
			$('keywords').setProperty('value', searchResults);
		}
		else
		{
			$('keywords').setProperty('value', 'SEARCH CLARK');
		}
		$('keywords').addEvents(
		{
			'blur': function() {
				if (this.getProperty('value') == '')
				{
					this.setProperty('value', 'SEARCH CLARK');
				}
			},
			'focus':function() {
				var textBoxValue = this.getProperty('value');
				if ( textBoxValue.trim() == 'SEARCH CLARK')
				{
					this.setProperty('value', '');
				}
			}
		});
	}
	
	/*gives the left nav a js class if it exists*/
	if ($('leftnav'))
	{$('leftnav').addClass('js_enabled');}
}

window.addEvent('domready', function() {
    searchBox();
	var dtr = new DynamicTextReplacement();
});