$(document).ready(function(){
						   
	$("a.external").attr("target","_blank");

	if(typeof load == 'function'){
		load();
		var gmap = new GoogleMap();
	}
	$("#get_directions").click(function(){
		$("#map_directions").html('');
		if(gmap.directions){
			gmap.directions.clear();
		} else {
			gmap.google_map_1.savePosition();
			var directions = new GDirections(gmap.google_map_1, document.getElementById("map_directions"));
			gmap.directions = directions;
		}
		gmap.directions.load("from: " + $("#from").val() + " to: 4130 Rio Bravo, Suite B El Paso, Texas");
		return false;
	});
	
	$("#form_directions").submit(function(){
		$("#map_directions").html('');
		if(gmap.directions){
			gmap.directions.clear();
		} else {
			gmap.google_map_1.savePosition();
			var directions = new GDirections(gmap.google_map_1, document.getElementById("map_directions"));
			gmap.directions = directions;
		}
		gmap.directions.load("from: " + $("#from").val() + " to: 4130 Rio Bravo, Suite B El Paso, Texas");
		return false;
	})
	
	$("#clear_map").click(function(){
		gmap.directions.clear();
		$("#map_directions").css('padding','0');
		gmap.google_map_1.returnToSavedPosition();
		return false;
	});
	// OnLoad hide all services
	$(".next").each(function(){
		var next = $(this).parent().next();
		var matchHash = new RegExp("#parent(\\d+)");
		urlHash = null;
		current = null;
		if(window.location.hash != ''){
			result = matchHash.exec(window.location.hash);
			urlHash = result[1];
			result = matchHash.exec($(this).attr("href"));
			current = result[1];
		}
		if(urlHash == null || urlHash != current){
			next.toggle();
		} else {
			switchBullets(this);	
		}
	});
	$(".subnext").each(function(){
		var next = $(this).parent().next();
		var matchHash = new RegExp(".*#sub(\\d+)");
		urlHash = null;
		current = null;
		if(window.location.hash != ''){
			result = matchHash.exec(window.location.hash);
			console.log(result);
			if(result != null){
				urlHash = result[1];
				result = matchHash.exec($(this).attr("href"));
				current = result[1];
			}
		}
		if(urlHash == null || urlHash != current){
			next.toggle();
		}
	});
	// Used to show/ hide Our Services
	$(".next").click(function(){
		var next = $(this).parent().next();
		if(next.css('display')!='block'){
			closeCurrentItems(next);
		}
		next.toggle('slow');
		$(this).scrollTo('slow');
		switchBullets(this);
	});
	
	$(".subnext").click(function(){
		var next = $(this).parent().next();
		if(next.css('display')!='block'){
			closeCurrentSubItems(next);
		}
		next.toggle('slow');
		$(this).scrollTo('slow');
	});
});
function switchBullets(obj){
	var bullet = $(obj).find("img");
	if(bullet.attr('src') == "/images/bullet_down.gif"){
		bullet.attr('src', "/images/bullet_right.gif");
	} else {
		bullet.attr('src', "/images/bullet_down.gif");
	}
}
function closeCurrentItems(current){
	$(".next").each(function(){
		var next = $(this).parent().next();
		if(next.css('display') == 'block'){
			next.toggle();
			switchBullets(this);
		}
	});
}
function closeCurrentSubItems(current){
	$(".subnext").each(function(){
		var next = $(this).parent().next();
		if(next.css('display') == 'block'){
			next.toggle();
			switchBullets(this);
		}
	});
}
$(".validate").submit(function(){
	if(hasEmptyRequiredFields()){
		alert("Please complete all the fields");
		return false;
	}
})

function hasEmptyRequiredFields(){
	var empty_fields = false;
	// Validate Required Fields
	$(".validate .required").each(function(){
		if($(this).isEmpty()){
			empty_fields = true;
		}
	});
	
	if(empty_fields){
		return true;
	} else {
		return false;	
	}
}

