/**
 * SwapIt
 *
 * Swapit is a quick and easy plugin designed to make 
 * simple mouseover image swapping easy.
 *
 * @example     $('.classname').swapit('_over');
 */
$.fn.swapit = function(a) {
	return this.each(function() {
		if($(this).get(0).tagName.toUpperCase() == 'IMG') {
			var original = $(this).attr("src");
			if(original.indexOf('_over') == -1) {
				var over = original.split('.');
				if(over.length >= 2) {
					over[over.length-2] += a;
					over = over.join('.');
				} else {
					over = original + a;
				}
				$(this).mouseover(function() { $(this).attr('src', over); }).mouseout(function() { $(this).attr('src', original); });
			}
		}
	});
}

$.preloadimages = function()
{	for(var i = 0; i<arguments.length; i++)
	{	img = new Image();
		img.src = arguments[i];
	}
}

$(document).ready(function(){

    if ($('body#page-home').length) {
		$.preloadimages('images/lo/homenav/classesprograms_over.gif',
			'images/lo/homenav/contactus_over.gif', 
			'images/lo/homenav/employment_over.gif', 
			'images/lo/homenav/events_over.gif', 
			'images/lo/homenav/faq_over.gif',
			'images/lo/homenav/medicallyintegratedservice_over.gif',
            'images/lo/homenav/memberservices_over.gif',
            'images/lo/homenav/ourfacilitydirections_over.gif'
			);
    } else {
    	$.preloadimages('images/lo/nav/classesprograms_over.gif',
			'images/lo/nav/contactus_over.gif', 
			'images/lo/nav/employment_over.gif', 
			'images/lo/nav/events_over.gif', 
			'images/lo/nav/faq_over.gif',
			'images/lo/nav/medicallyintegratedservice_over.gif',
            'images/lo/nav/memberservices_over.gif',
            'images/lo/nav/home_over.gif',
            'images/lo/nav/ourfacilitydirections_over.gif'
			);
    }


	//$('#nav-membership').attr('src', 'images/lo/nav/membership_over.gif'); // must go above swapit() declaration
    
	$('.swap').swapit('_over');


	if ($('div#local ul.menu').length) {
	
		//selected
		var _local = document.location.pathname;
		
		if (_local.indexOf('?') >= 0) {
			_local = _local.substring(0, _local.indexOf('?'));
		}
		
		_local = _local.split('/');
		_local = _local.pop();
		
		if (_local.length == 0) {
			_local = 'index.html';	
		}
		
		$('div#local ul.menu li a').each(function() { 
			var _href = $(this).attr('href');
			if (_href.indexOf('?') >= 0) {
				_href = _href.substring(0, _href.indexOf('?'));
			}
			
			_href = _href.split('/');
			_href = _href.pop();
			
			if (_href == _local) {
				$(this).addClass('selected');
			}
		});
	}
	
	
	if ($('form.contactform').length) {
		$('input#Submit').click(function() {
			
			if ($('input[name="Name"]').val().length == 0) {
				alert('Missing name');
				return false;
			}
			
			if ($('input[name="Email"]').val().length < 5) {
				alert('Missing email address');
				return false;
			}
			
			if ($('textarea[name="Comments"]').val().length < 5) {
				alert('Please add a comment');
				return false;
			}
			
		});
		
	}
	
});