/******************
   Accessibility
©2008 DividedMedia
******************/

function writeEmail()
{
	var address = $$('.emailaddress');
	
	if(address)
	{
		for(var i = 0; i < address.length; i++)
		{
			address[i].href	  		= address[i].href.split("-at-").join("@"); 
			address[i].innerHTML	= address[i].innerHTML.split("-at-").join("@");
			
			address[i].href	  		= address[i].href.split("-dot-").join("."); 
			address[i].innerHTML	= address[i].innerHTML.split("-dot-").join(".");
		}
	}
}

function setupTargetLinks()
{
	var arrLinks = $$('a.targetblank');
	
	if(arrLinks)
	{ 
		for(var i = 0; i < arrLinks.length; i++)
		{
			arrLinks[i].set('target', 'blank');
		}
	}
}

function setupTextOnly()
{
	var blTextOnly = (Cookie.read('mfonex_textonly') && (Cookie.read('mfonex_textonly') == 'true'));
	
	if(blTextOnly)
	{
		strToLoad = "textonly.css";
	
		var arrBits = $('mainstyle').href.split('/');
		arrBits[ (arrBits.length - 1) ] = strToLoad;
		$('mainstyle').href = arrBits.join('/'); 
		
		$('textonly').innerHTML = "graphical version";
	}

	$('textonly').addEvent('click', function()
							{
								var blTextOnly = (Cookie.read('mfonex_textonly') && (Cookie.read('mfonex_textonly') == 'true'));

								if(blTextOnly)
								{
									Cookie.write('mfonex_textonly', 'false', {duration: 365, path:'/'});									
								}
								else
								{
									Cookie.write('mfonex_textonly', 'true', {duration: 365, path:'/'});									
								}
								
								document.location = document.location;
							});
}

function setupTextSize()
{
	var objBody = $$('html')[0];
	$('textsmall').onclick = function()
							{
								objBody.setStyle('font-size', '100%');
								
								// IE6 nav fix
								if (Browser.Engine.trident4)
								{
									var links = $$('#navigation li a');
									for(var i = 0; i < links.length; i++)
									{
										links[i].setStyle('padding-left', '18px');	
										links[i].setStyle('padding-right', '18px');
									}
								}
							};
	$('textmedium').onclick = function()
							{
								objBody.setStyle('font-size', '110%');
								
								/* IE6 nav fix
								if (Browser.Engine.trident4)
								{
									var links = $$('#navigation li a');
									for(var i = 0; i < links.length; i++)
									{
										links[i].setStyle('padding-left', '11px');	
										links[i].setStyle('padding-right', '11px');
									}
								} */
							};
	$('textlarge').onclick = function()
							{
								objBody.setStyle('font-size', '120%');
								
								/* IE6 nav fix
								if (Browser.Engine.trident4)
								{
									var links = $$('#navigation li a');
									for(var i = 0; i < links.length; i++)
									{
										links[i].setStyle('padding-left', '8px');	
										links[i].setStyle('padding-right', '8px');											
									}
								} */
							};
}


function setupForm()
{		
	var arrTextBoxes = $$('input');
	for(var i = 0; i < arrTextBoxes.length; i++)
	{
		if(arrTextBoxes[i].type == 'text')
		{
			addInputEvents(arrTextBoxes[i]);
		}
	}
	
	var arrTextAreas = $$('textarea');
	if(arrTextAreas.length == 1)
	{	
		addInputEvents(arrTextAreas[0]);	
	}
	
	var objForm = $('frmcontact');
	if(objForm)
	{	
		objForm.addEvent('submit', function(e)
									{
										var strErrors = "";
										var bContinue = true;
										
										if($('strName').value == 'name')
										{
											strErrors += "- Your Name\n";
											bContinue = false;
										}									
										if($('strEmail').value == 'email address')
										{
											strErrors += "- Your Email Address\n";
											bContinue = false;
										}								
										/*if($('strPhone').value == 'phone number')
										{
											strErrors += "- Your Phone Number\n";
											bContinue = false;
										}*/	
										if($('strMessage') && $('strMessage').value == 'message')
										{
											strErrors += "- Your Message\n";
											bContinue = false;
										}								
										
										if(!bContinue)
										{
											alert("Please complete the following fields:\n"+strErrors);
											
											if (e && e.preventDefault)
											{
												e.preventDefault();
											}
										}
										
										return bContinue;
									});
	}
}

function addInputEvents(objInput)
{ 
	objInput.label = objInput.value;
	
	objInput.addEvent('focus', function(){
												
													if(this.value == this.label)
													{
														this.value = '';																
													}
												
												});
	
	objInput.addEvent('blur', function(){
												
													if(this.value == '')
													{
														this.value = this.label;																
													}
												
												});	
}

var changeMasthead = function()
{
	$('imageholder').set('tween',
	{
		duration: 1000,
		onComplete: function(e)
		{
			var strCurrent = $('imageholder').get('class');
			
			var iCurrent = parseInt(strCurrent.substring(5));			
			var iNew = iCurrent;
			
			while(iNew == iCurrent)
			{
				iNew = Math.floor(Math.random() * 8) + 1;
			}
			
			$(e.id).removeClass(strCurrent);
			$(e.id).addClass('image'+iNew.toString());
			
			$(e.id).set('tween', {  duration: 1000, onComplete: Class.empty });
			$(e.id).tween('opacity', 1);
		}
	});
	$('imageholder').tween('opacity', '0');
}

window.addEvent('domready', function()
{
	writeEmail(); 
	setupTargetLinks();
	setupTextOnly();
	setupTextSize();	
	
	if((document.location.toString().indexOf("contact-us.php") > -1) ||
	   (document.location.toString().indexOf("join-our-mailing-list.php") > -1))
	{
		setupForm();
	}
});