/* Live Search */
var LiveSearch = {
	search_text: "Live Search",
	orig_content: null, // default for orig content
	
	init: function()
	{
		this.onBlur()
		
		Event.observe('ls_field', 'focus', this.onFocus)
		Event.observe('ls_field', 'blur', this.onBlur)
		Event.observe('ls_form', 'submit', this.onSubmit)
		
		// set observer for search method
		new Form.Element.DelayedObserver($('ls_field'), 0.5, function(){ LiveSearch.search()})
	},
	
	search: function(url)
	{
		// live search input
		var ls = $('ls_field')

		// if live search isn't blank, meaning neither empty nor containing any white space
		// and it's not this.search_text
		if (!ls.value.blank() && this.search_text != ls.value)
		{
			// if url is undefined
			if (undefined == url)
			{
				// set it
				var url = '/?s=' + encodeURIComponent(ls.value) + '&ajax=true'
			}

			// update content
			new Ajax.Request(url,
			{
				method: 'get',
				onSuccess: function(transport)
				{
					var content = $('content')

					// use importNode for Ajax xml data documentElement
					// https://developer.mozilla.org/en/DOM/document.importNode
					var res_xml = document.importNode(transport.responseXML.documentElement, true)

					// change posts links to javascript method call
					var posts_links = $(res_xml).select('div.ajax_search_link > a')

					// iterate over each link and change it
					posts_links.each(function(item)
					{
						item.setAttribute('href',
						'javascript: LiveSearch.search("' + item.getAttribute('href') + '")')
					})

					// if LiveSearch.orig_content is null
					if (null == LiveSearch.orig_content)
					{
						// save orig content in it then
						LiveSearch.orig_content = content
					}

					// replace content
					content.parentNode.replaceChild(res_xml, content)
				}
			})
		}
	},
	
	onFocus: function(e)
	{
		var ls = $('ls_field')
		
		// if live_s is this.search_text
		if (ls.value == LiveSearch.search_text)
		{
			// null it up
			ls.value = ''
			ls.className = 'ls_onfocus'
		}
	},
	
	onBlur: function(e)
	{
		var ls = $('ls_field')
		
		// if live_s value is blank
		if (ls.value.blank())
		{
			// set live search message
			ls.value = LiveSearch.search_text
			ls.className = 'ls_onblur'
			
			// if we have orig content
			if (null != LiveSearch.orig_content)
			{
				// set it
				var content = $('content')
				
				content.parentNode.replaceChild(LiveSearch.orig_content, content)
			}
		}
	},
	
	onSubmit: function(e)
	{
		// don't submit the form
		e.returnValue = false
	}
}

/* Obfuscate Data */
var ObfuscateData = {
	addData: function(id, data)
	{
		// get text node for span
		var tn = $(id).childNodes[0]

		// replace text with data
		tn.nodeValue = this.obfuscate(data)
	},
	
	obfuscate: function(data)
	{
		return this.rot5(this.rot13(data))
	},
	
	// latin abc
	rot13: function(str)
	{
		// we take each character and rotate it
		return str.replace(/[a-zA-Z]/g, function(c)
		{
			return String.fromCharCode( (c <= "Z" ? 90: 122) >=
			(c = c.charCodeAt(0) + 13) ? c : c - 26 )
		})
	},
	
	// for digits
	rot5: function(str)
	{
		return str.replace(/[0-9]/g, function(d)
		{
			d = Number(d) // cast to integer
			return (d < 5) ? d + 5: d - 5
		})
	},
}

/* Preload Images */
function preloadImages()
{
	var prelImages = [
	"/wp-content/themes/dennisbb/images/home-hover.png",
	"/wp-content/themes/dennisbb/images/home-active.png",
	"/wp-content/themes/dennisbb/images/portfolio-hover.png",
	"/wp-content/themes/dennisbb/images/portfolio-active.png",
	"/wp-content/themes/dennisbb/images/services-hover.png",
	"/wp-content/themes/dennisbb/images/services-active.png",
	"/wp-content/themes/dennisbb/images/contact-hover.png",
	"/wp-content/themes/dennisbb/images/contact-active.png",
	"/wp-content/themes/dennisbb/images/about-hover.png",
	"/wp-content/themes/dennisbb/images/about-active.png",
	"/wp-content/themes/dennisbb/images/free-quote-close.png",
	],
	preLoaded = new Array
	
	for (var i = 0, imagesLength = prelImages.length; i < imagesLength; ++i)
	{
		preLoaded[i] = new Image();
		preLoaded[i].src = prelImages[i]
	}
}

// runs when DOM is loaded, but content is not yet
Event.observe(document, 'dom:loaded', function()
{
	LiveSearch.init()
})

// runs when full page, including images, is loaded
Event.observe(window, 'load', function()
{
	preloadImages()
})