// Location JS functions
$(function() {
	// Takes a string and makes address parts of the string 
	function getBranch(string, piece) {
		// This expects something like: Corporate Branch: 60 Berry Drive - Pachecho, CA 94553
		stopOne = string.indexOf(':');
		string = string.replace(':', '');
		stopTwo = string.indexOf(' - ');
		string = string.replace(' - ', '');
		
		// Now fetch and return what is being asked for
		if (piece == 'branch') {
			return string.substring(0, stopOne);
		} else if (piece == 'address') {
			return string.substring(stopOne+1, stopTwo);
		} else if (piece == 'locale') {
			return string.substring(stopTwo, string.length);
		}  
	}
	
	// Handle the replacement of values in the show box
	function showBranch(branchString) {
		$('#branch-location').removeClass('hide-location').addClass('show-location');
		$('#branch-location-name').html(getBranch(branchString, 'branch'));
		$('#branch-location-address').html(getBranch(branchString, 'address'));
		$('#branch-location-locale').html(getBranch(branchString, 'locale'));
	}
	
	// Styling of the zipcode searchbox
	var elem = '#zipcode';
	if ($(elem).val() == '') {
		$(elem)
		.val($(elem).attr('title'))
		.removeClass('zipcode-text-heavy')
		.addClass('zipcode-text-light');
	}

	// Now handle what happens when focus is given to it or removed from it
	$(elem).focus(function() {
		/**
		 * If the value of the box is the same as the title clear the box
		 */
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
		
		/**
		 * Now handle some stylistic stuff
		 */
		$(this)
		.removeClass('zipcode-text-light')
		.addClass('zipcode-text-heavy')
		.attr('maxlength', 5);
	})
	.blur(function() {
		var entered = $(this).val();
		entered = jQuery.trim(entered);
		
		/**
		 * If the value is empty make it look like when it rendered originally
		 */
		if (entered == '') {
			$(this)
			.attr('maxlength', '')
			.val($(this).attr('title'))
			.removeClass('zipcode-text-heavy error')
			.addClass('zipcode-text-light');
			
			$("#zipcode-form-notices").removeClass('error').text('');
		} else {
			if ($(this).val() != $(this).attr('title')) {
				if (entered.length != 5 || isNaN(entered)) {
					$("#zipcode-form-notices").addClass('error').text('Zip codes must be a five digit number');
					$(this).addClass('error');
					$('#zipcode').focus();
				} else {
					$("#zipcode-form-notices").removeClass('error').text('');
					$(this).removeClass('error');
				}
			}
		}
	});
	
	/**
	 * Handle the area map for links to branches
	 * 
	 * This is hella not doing what we want it to do. Needs to be improved in 
	 * order to roll with it.
	 */
	$("area.location-center").mouseover(function() {
		/** 
		 * Find out if we have a search or not because we don't want to override
		 * what is being looked for.
		 */
		searched = jQuery.trim($("#location-list-header").text());
		
		/**
		 * As long as there is no current search we can safely add to the search
		 * results box.
		 */
		if (!searched) {
			var branchdata = $(this).attr('title');
			var branchname = getBranch(branchdata, 'branch');
			var branchinfo = '<strong>' + branchname + '</strong><br />';
			branchinfo += getBranch(branchdata, 'address') + '<br />';
			branchinfo += getBranch(branchdata, 'locale') + '<br />';
			branchinfo += sitetext.contactphone + '<br />';
			branchinfo += '<a href="/contact/email/branch/' + encodeURI(branchname.toLowerCase()) + '/" title="' + sitetext.sendusanemail +'">' + sitetext.sendusanemail + '</a>';
			branchinfo = '<p id="location-list-detail-info" class="' + branchname.replace(/\s/g, "-").toLowerCase() +'">' + branchinfo + '</p>';
			$("#location-list-detail").html(branchinfo);
		}
	});
});