// ===========================
// Search Functionality
// ===========================
$(document).ready(function(){
	var searchactive = ($("li[id='searchlocation']"));
	
	$("#searchwrapper ul a").click(function(){
		//if(searchactive.attr("id") != $(this).parent().attr("id")){
			$(searchactive).find("a").removeClass("active");
			$(this).addClass("active");
			searchactive = $(this).parent();
			
			var id = $(this).parent().attr("id");
			if(id == "searchlocation"){
				initSearchLocation();
			}else if(id == "searchname"){
				initSearchName();
			}else{
				initSearchHood();
			}
	//	}
		return false;
	});
	

	$("#submitsearch").click(checkSubmit);
	$("#searchform").submit(checkSubmit);	
	function checkSubmit(){
		var sid = $(searchactive).attr('id');

		switch (sid){
			case "searchlocation":
				if(!$("#searchfield").val().length || $("#searchfield").val() == "Enter an Address, Intersection, or Zip"){
					// do nothing
				}else{
					window.location.href = "/mug/search.php?address="+$("#searchfield").val();
				}
				break;
			case "searchname":
				if(!$("#searchfield").val().length || $("#searchfield").val() == "Enter the Name of a Cafe or Restaurant"){
					// do nothing
				}else{
					window.location.href = "/mug/search.php?name="+$("#searchfield").val();
				}
				break;
			case "searchhood":
				if($("#selecthood").val() == "Select Neighborhood" || $("#selecthood").val() == ""){
					// do nothing
				}else{
					window.location.href = "/mug/search/"+$("#selecthood").val();
				}
				break;		
		}
		return false;
	}
	
	
});
var selecton = false;
function initSearchHood(){
	$("#searchbox #selecthood").toggle();
	$("#searchbox #searchfield").toggle();
	selecton = true;
}
function initSearchLocation(){
	if(selecton == true){
		$("#searchbox #selecthood").toggle();
		$("#searchbox #searchfield").toggle();
		selecton = false;
	}
	$('#searchfield').val("Enter an Address, Intersection, or Zip");
	$("#searchfield").focus(function() { 
	    if ( $(this).val() == "Enter an Address, Intersection, or Zip"){
	       $(this).val(''); 
	   } 
	});
	$("#searchfield").blur(function() { 
	    if ( !$(this).val().length){
	       $(this).val('Enter an Address, Intersection, or Zip'); 
	   } 
	});	
}
function initSearchName(){
	if(selecton == true){
		$("#searchbox #selecthood").toggle();
		$("#searchbox #searchfield").toggle();
		selecton = false;
	}
	$('#searchfield').val("Enter the Name of a Cafe or Restaurant");
	$("#searchfield").focus(function() { 
	    if ( $(this).val() == "Enter the Name of a Cafe or Restaurant"){
	       $(this).val(''); 
	   } 
	});
	$("#searchfield").blur(function() { 
	    if ( !$(this).val().length){
	       $(this).val("Enter the Name of a Cafe or Restaurant"); 
	   } 
	});	
}

// ===========================
// VALIDATION
// ===========================
function validate(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address = email;
	if(reg.test(address) == false) {
		return false;
	}else{
		return true;
	}
}
function isUrl(s) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}