// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function toggleSubmit() {
  Element.toggle("process");
  Element.toggle("processing");
}

// IE background image flicker fix
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

// IE PNG Transparency Fix for IE5.x - 6
if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent)
	Event.observe(window, 'load', function(){ 
  	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
  	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
  	if (itsAllGood)
  		for (var i = 0; i < document.images.length; i++)
  			pngLoad(document.images[i]);
  })

// loads an image, src is optional // public
function pngLoad(img,src) {
	if (typeof img == "object" && typeof img.tagName == "string" && img.tagName == "IMG") {
		// this is an image
		if (typeof src == "string") {
			// src parameter is present
			if (src.match(/\.png$/i) != null) {
				// this is png image
				img.style.width = null;
				img.style.height = null;
				img.style.filter = null;
				img.src = src;
				pngSwapPrepare(img);
			} else {
				// its not a png
				img.src = src;
			}
		}
		else if (img.src.match(/\.png/i) != null) {
				// no src arameter, but its png -> simply swap
				pngSwapPrepare(img);
		}
	}
	// swap (if complete) or shedule it to onload event // private
	function pngSwapPrepare(img) {
		if (img.complete)
			pngSwap(img);
		else
			img.attachEvent("onload",pngOnLoadSwap);
	}
	// supposed to be called when image is loaded into memory //private
	function pngOnLoadSwap() {
		event.srcElement.detachEvent("onload",pngOnLoadSwap);
		pngSwap(event.srcElement)
	}
	// Swaps img and background //private
	function pngSwap(img) {
		with (img) {
			style.width = width + "px";
			style.height = height + "px";
			style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale') "
			src = "i/blank.gif";
		}
	}
}

// Live Comment Preview
var m_author_default='';
function m_preview_author(e) {
	var p = document.getElementById('m_preview_author');
	var a = e.form.comment_author.value;
	var u = e.form.comment_author_url.value;
	if (m_author_default=='') { m_author_default = p.innerHTML; }
	p.innerHTML = (!m_empty(a))?a:m_author_default; 
	p.href = (!m_empty(u))?u:'#';
	p.title = u;
}

function m_preview_comment(e) { document.getElementById('m_preview_comment').innerHTML = '<p>'+e.value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'/p><p>')+'<'+'/p>'; }

/**************************************************
 m_empty()
 
 Similar to the php function empty()
 Returns true if val contains an empty string
 or contains only whitespace.
 ie. spaces or returns
 **************************************************/
function m_empty(val) { var empty = /^\s*$/; return empty.test(val); }