// Create array of onload events
var onloadEvents = new Array();
window.onload = function()
{
	for(var i = 0; i < onloadEvents.length; i++)
		onloadEvents[i]();
}

// Google Analytics
if(config.googleAnalyticsId)
{
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', config.googleAnalyticsId]);
	_gaq.push(['_trackPageview']);
	
	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
}

// Init. drop-down menu system
onloadEvents.push(function()
{
	var ids = config.dropDownMenuIds;

	for(var i = 0; i < ids.length; i++)
	{
		var element = document.getElementById(ids[i]);
		if(element)
		{
			var sfEls = element.getElementsByTagName("LI");
			for(var i = 0; i < sfEls.length; i++)
			{
				sfEls[i].oldClassName = sfEls[i].className ? sfEls[i].className : "";
				sfEls[i].onmouseover = function()
				{
					this.className = this.oldClassName + " hover";
				}
				sfEls[i].onmouseout = sfEls[i].onclick = function()
				{
					this.className = this.oldClassName;
				}
	
				var sfEls2 = sfEls[i].getElementsByTagName("A");
				for(var j = 0; j < sfEls2.length; j++)
				{
					sfEls2[j].classResetElement = sfEls[i];
					sfEls2[j].onclick = function()
					{
						if(this.href.substr(this.href.length-1) == "#")
							return false;
	
						this.classResetElement.className = this.classResetElement.oldClassName;
						if(this.className.match(/^(.*[ ]+)?newwindow([ ]+.*)?$/))
						{
							window.open(this.href);
							return false;
						}
					}
				}
			}
		}
	}
});

// Init. google map
if(config.googleMapConfig)
{
	onloadEvents.push(function()
	{
		var mapOptions = {
			zoom: 8,
			center: new google.maps.LatLng(39.83, 98.58),
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}

		var infoWindow = new google.maps.InfoWindow({
			content: ""
		});

		var googleMap = new google.maps.Map(document.getElementById("google-map"), mapOptions);
		var bounds = new google.maps.LatLngBounds();
		for(var i = 0; i < config.googleMapConfig.points.length; i++)
		{
			var location = new google.maps.LatLng(config.googleMapConfig.points[i].latitude, config.googleMapConfig.points[i].longitude);
			bounds.extend(location);
			createMarker(
				googleMap,
				infoWindow,
				location,
				config.googleMapConfig.points[i].city,
				config.googleMapConfig.points[i].text
			);
		}

		if(bounds.getNorthEast().equals(bounds.getSouthWest()))
		{
			var extendPoint1 = new google.maps.LatLng(bounds.getNorthEast().lat() + 0.01, bounds.getNorthEast().lng() + 0.01);
			var extendPoint2 = new google.maps.LatLng(bounds.getNorthEast().lat() - 0.01, bounds.getNorthEast().lng() - 0.01);
			bounds.extend(extendPoint1);
			bounds.extend(extendPoint2);
		}
		googleMap.fitBounds(bounds);
	});
}

function createMarker(googleMap, infoWindow, point, title, text)
{
	var marker = new google.maps.Marker({
		position: point,
		map: googleMap,
		title: title,
		html: text
	});

	if(text.length)
	{
		google.maps.event.addListener(marker, "click", function() {
			infoWindow.setContent(createImprovedTextNode(marker.html));
			infoWindow.open(googleMap, marker);
		});
	}
	else
	{
		google.maps.event.addListener(marker, "click", function() {
			infoWindow.close();
			googleMap.panTo(marker.position);
		});
	}
}

function createImprovedTextNode(t)
{
	var nbsp = "\u00a0";

	var o = document.createElement("span");
				
	t = strReplace("\r\n", "\n", t);
	t = strReplace("\r", "\n", t);
	t = strReplace("\t", "    ", t);
	t = strReplace("  ", nbsp + " ", t);
	var parts = t.split("\n");
	for(var j = 0; j < parts.length; j++)
	{
		if(j)
			o.appendChild(document.createElement("br"));
		o.appendChild(document.createTextNode(parts[j]));
	}
	return o;
}

function strReplace(search, replace, subject)
{
    var s = subject;
    var ra = r instanceof Array, sa = s instanceof Array;
    var f = [].concat(search);
    var r = [].concat(replace);
    var i = (s = [].concat(s)).length;
    var j = 0;
    
    while(j = 0, i--)
	{
        if(s[i])
		{
            while(s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    }

    return sa ? s : s[0];
}


// Misc. functions

function clickToCall(ctcId, ctcKey)
{
	var phone = document.getElementById('area').value + document.getElementById('pre').value + document.getElementById('suff').value;
    ctcUrl = "http://secure.ifbyphone.com/clickto_status.php?click_id=" + escape(ctcId) + "&phone_to_call=" + escape(phone) + "&key=332e589fc8488352eb0754c4d6ee4a958a7388fc&click_key=" + escape(ctcKey);
	window.open(ctcUrl, 'Clicktocall' , 'width=200,height=200,toolbar=no,location=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no');
}

function submitSignup(form)
{
	if(checkSignup(form))
		form.submit();
}

function checkSignup(form)
{
	if(!validate_required(form.email_address) || !validate_expression(form.email_address, "email"))
	{
		alert("Please enter a valid email address");
		return false;
	}
	return true;
}

function validate_required(field)
{
	return (field.value == null || field.value == "") ? false : true;
}

function validate_expression(field, expr)
{
	var ex = '';
	switch (expr) {
		case 'number':
			ex  = /^[0-9]+$/;
			break;
		case 'float':
			ex = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
			break;
		case 'alphanum':		
			ex = /^[0-9a-zA-Z ]+$/;
			break;
		case 'email':
			ex = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/;	
			break;
		case 'alpha':
		default:
			ex = /^[a-zA-Z ]+$/;
			break;
	}

	var val = field.value.replace(/ /,'');
	return val.match(ex) ? true : false;
}

/* BEGIN GALLERY FUNCTIONS */
function updatePhoto(id, c)
{
	var image = document.getElementById('photoMain');
	var caption = document.getElementById('caption');
	var fullimage = document.getElementById('fullImage');
	var fullcaption = document.getElementById('fullCaption');
	
	if(image)
	{
		image.src = './assets/std/' + id + '/';
		image.alt = c;
	}

	if(caption)
	{
		while(caption.firstChild)
			caption.removeChild(caption.firstChild);
		caption.appendChild(document.createTextNode(c));
	}
	
	if(fullimage)
	{
		fullimage.src = './assets/lg/' + id + '/';
		fullimage.alt = c;
	}
	
	if(fullcaption)
	{
		while(fullcaption.firstChild)
			fullcaption.removeChild(fullcaption.firstChild);
		fullcaption.appendChild(document.createTextNode(c));
	}
}
function showFullImage()
{
	document.getElementById('fullImageUberContainer').style.display = 'block';
}
function hideFullImage()
{
	document.getElementById('fullImageUberContainer').style.display = 'none';
}
/* END GALLERY FUNCTIONS */

/* BEGIN CONTACT US FUNCTIONS */
function checkContactForm()
{
	var msg = '';
	var name = document.getElementById('contactname');
	var email = document.getElementById('contactemail');
	var phone = document.getElementById('contactphone');
    var city = document.getElementById('contactcity');
    var state = document.getElementById('contactstate');
    var zip = document.getElementById('contactpostalcode');
	
	if(!validate_required(name)) {
		msg = msg + '  Your Name\n';
		name.focus();
	}
	if(!validate_required(email) || !validate_expression(email,'email')) {
		msg = msg + '  Your Email Address\n';
		email.focus();
	}
    if(!validate_required(city)) {
		msg = msg + '  Your City\n';
		city.focus();
	}
    if(!validate_required(state)) {
		msg = msg + '  Your State\n';
		state.focus();
	}
    if(!validate_required(zip)) {
		msg = msg + '  Your Zip Code\n';
		zip.focus();
	}
	if(msg.length > 0)
	{
		alert('Please complete or correct the following:\n' + msg);
		return false;
	}
	
	return true;
}
/* END CONTACT US FUNCTIONS */

/* BEGIN TESTIMONIALS FUNCTIONS */
function toggleTestimonial(elem)
{
	var el = document.getElementById('testimonial' + elem);
	if(el.style.display == 'block')
		el.style.display = 'none';
	else
		el.style.display = 'block';
}
/* END TESTIMONIALS FUNCTIONS */

/* BEGIN FUN ANIMAL TRIVIA FUNCTIONS */
function toggleTrivia(elem, answer)
{
	if(document.getElementById(elem + '_' + answer).className == '')
	{
		document.getElementById(elem + '_' + answer).className = 'on';
		document.getElementById(elem + '_answer').style.display = 'block';
	}
	else
	{
		document.getElementById(elem + '_' + answer).className = '';
		document.getElementById(elem + '_answer').style.display = 'none';
	}
}
/* END FUN ANIMAL TRIVIA FUNCTIONS */

