/* Font replacement */
Cufon.replace('.univers', { 'fontFamily': 'Univers' });
/* Encapsulation */
(function($) {
	/* jScrollPane */
	$('#map-list').jScrollPane({
		showArrows: true,
		verticalGutter: 0
	});
	/* Input Defaults */
	$('.replace-default').each(
		function() {
			if ($(this).val() != '') {
				$(this).data('default-value', $.trim($(this).val()));
			}
			$(this).bind(
				'focusin focusout',
				function() {
					var currentValue = $.trim($(this).val()).toUpperCase();
					if (currentValue == $(this).data('default-value').toUpperCase()) {
						$(this).addClass('focus').val('');
					}
					else if (currentValue == '' || currentValue == $(this).data('default-value').toUpperCase()) {
						$(this).removeClass('focus').val($(this).data('default-value'));
					}
				}
			);
		}
	);
	
	/* Tabbox tabs */
	$('#tabbox nav a').click(function(){
		
		$(this)
			.closest('li')
				.siblings('li')
					.each(function() {
						$('#' + $(this).find('a').data('content')).hide();
					})
					.removeClass('active')
				.end()
					.each(function() {
						$('#' + $(this).find('a').data('content')).fadeIn();
					})
				.addClass('active');
		if ( $('body').hasClass('ie6') ) {
			DD_belatedPNG.fix('#tabbox nav .active');
		}
		
		return false;
	
	}).closest('.active').find('a').click();
	
	/* Flash */
	if ( $('#flash-map').length > 0 ) {
		$('#flash-map').flash(
		{
				swf: 'http://media.hbfuller.com/designvideo/map.swf',
				width: "375",
				height: "400",
				allowScriptAccess: "always",
				flashvars: {
					paLink: path + '?state=Pennsylvania',
					ohLink: path + '?state=Ohio',
					wvLink: path + '?state=West+Virginia',
					vaLink: path + '?state=Virginia',
					njLink: path + '?state=New+Jersey',
					deLink: path + '?state=Delaware',
					mdLink: path + '?state=Maryland'
				}
			}
		);
	}
	

	/* FAQ page */
	$('#faq-content').find('a').click(function() {
		var isActive = $(this).hasClass('active');
		$('#faq-content').find('a').removeClass('active').end().find('.answer').slideUp();
		if (!isActive) {
			$(this).addClass('active').next().find('.answer').slideDown();
		}		
		return false;
	});

	$('.questions').hide(); 
	$('.questions:first').show();
	
	function change(obj) {
		$('#faq-nav').find('a').removeClass('active');
		$(obj).addClass('active');
		$('.questions').hide();
		Cufon.refresh();

		var activeTab = $(obj).attr('href'); 
		$(activeTab).fadeIn();
	}

	$('#faq-nav').find('a').click(function() {
		change($(this))
		return false;
	});

	$('.faq .prev, .faq .next').find('a').click(function() {

		if ( $(this).parent().hasClass('next') ) {			
			var next =  $('#faq-nav').find('a.active').next();

			if ( $(next).index() == -1)   {
			    next = $('#faq-nav a:first');
			}
			change(next)

		} else {
			var prev =  $('#faq-nav').find('a.active').prev();

			if ( $(prev).index() == -1)   {
			    prev = $('#faq-nav a:last');
			}
			change(prev)
		}
		return false
	});
	
	/*	Contact Form validation	*/
	if ( $('#contactUs').length > 0 ) {

		$('#contactUs').submit(function() {
			var message = "";
			var error = false;

			// Check the text fields
			$(this).find('.text').each(function() {
				if ( $.trim($(this).val()) == "" ) {
					if ( $(this).attr('id') == "email" && $("#emailCheckbox").attr("checked") == false ) {
						// Things are fine
					} 
					else if( $(this).attr('id') == "phone" && $("#phoneCheckbox").attr("checked") == false ) {
						// Things are fine
					} else {
						$(this).addClass('error');
						message = "Please fill in the required fields";
						error = true;
					}
				} 
				else if ( $(this).hasClass('error') ) {
					$(this).removeClass('error');
				}
			});

			if( $('#state').val() == false || $('#state').val() == 'false' ) {
				message = message + ", and please select a state";
				$('#form-content .notifications').append(message);
				error == true;	
			}

			//If email, check and validate
			if ( $('#email').val() != '' && $("#emailCheckbox").attr("checked") == true ) {
				if ( !validateEmail($('#email').val()) ) {
					$('#email').addClass('error');
					error == true;
				}	
			}

			if ( error == true ) {
				$('#form-content .notifications').html(message+'.').fadeIn();
			} else {
				$('#form-content .notifications').css({'display':'none'});
				return true;
			}	

			return false;
		});	
	}

	/*	 Google Maps */
	if($('#map-canvas').length > 0 && stores.length > 0) {

		for ( i in stores ) {
			setMarker(i);
		}

		var latlng = new google.maps.LatLng(-34.397, 150.644);
	    var myOptions = {
			zoom: 8,
			center: latlng,
	      	mapTypeId: google.maps.MapTypeId.ROADMAP
	    };
	    var map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);

	}

	function setMarker(i) {
		var store = stores[i];
		var geocoder = new google.maps.Geocoder();

		// Geocode Request
		geocoder.geocode( { 'address': store[1]}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				map.setCenter(results[0].geometry.location);


				// Create Content for the Window
				var content = '<div class="mapContent"><h1>' + store[0] + '</h1><p class="address">' + store[1] + '</p><div class="info-foot"><a href="http://maps.google.com/maps?daddr='+ store[1].replace(" ", "+") +'&hl=en" target="_blank">Directions</a></div></div>'; 

				// Set the Marker Down
				var marker = new google.maps.Marker({
					map: map, 
					position: results[0].geometry.location,
					title: store[0]
				});

				// Create the info Window
				var infowindow = new google.maps.InfoWindow({
			        content: content
			    });

				// Se the click event for the window. 
				google.maps.event.addListener(marker, 'click', function() {
				 	infowindow.open(map,marker);
				});


				$('#map-list li[rel="'+ store[2] +'"]').click(function() {
					map.setCenter(results[0].geometry.location);
					infowindow.open(map,marker);
				});

			} else {
				return false;
			}
		});
	}


	if ( $('#map-list-city').length > 0 ) {
		$('#map-list-city').change(function() {
		
			var state = window.location.search.get('state');
			window.location = 'http://' + window.location.hostname + path + '?state='+ state +'&city='+$(this).val();
		});
	}

	/* Search Results Page	*/
	if ( $('.search .results div').length > 0 ) {
		$('.search .results div').each(function() {

			$(this).hover(
				function() {
					$(this).css({"background-color":'#efefef'});
				},
				function() {
					$(this).css({"background-color":'#fff'});
				}
			);
		});
	}

})(window.jQuery);

function validateEmail(email) { 
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
	return email.match(re);
}

String.prototype.get = function(p){
  return(this.match(new RegExp("[?|&]?" + p + "=([^&]*)"))[1]);
}

