// jQuery(function($) {
$(document).ready(function() {
	
	if ($('div#map').length) {
		$('div#map').googleMap(43.453238, -96.807725, 10, {
			controls: ["GSmallMapControl", "GMapTypeControl"],
			markers:$(".geo")
		});
	}
	
	
	// Arrange Subnav
	if($('ul.nav ul.sub').length) {
		var l = '';
		$('ul.sub').siblings('a').each(function() {
			l = $(this).position().left+'px';
		})
		$('ul.sub').css('left',l);
	}
	
	// Activate BG on Product Lists
	if($('div#content div.products ul li').length)
	{
		$('div#content div.products ul li').each(function()
			{
				if($('img',this).length)
					$(this).css('background-image','url('+$('img',this).attr('src')+')');
					
				if ($('span.caption',this).length)
				{
					$(this).hover(
						function() {showNewBubble($(this));},
						function() {hideNewBubble($(this));}
						);
				}
			});
	}
	
	// Place Input Masks & Validators on Forms
	if ($('form#request_call').length)
	{
		$('form#request_call').validate({
			success: 'valid',
			rules: {
				name: 'required',
				phone1: 'required',
				comments: 'required'
			},
			messages: {
				name: 'Please let us know your name.',
				phone1: 'Please leave a number we can reach you at.',
				comments: 'Please leave a comment.'
			}
		});
		$('form#request_call input#phone1').mask('(999) 999-9999');
	}
	if ($('form#contact').length)
	{
		$('form#contact').validate({
			success: 'valid',
			rules: {
				name: 'required',
				comments: 'required'
			},
			messages: {
				name: 'Please let us know your name.',
				comments: 'Please leave a comment.'
			}
		});
	}
	if ($('form#email-list').length)
	{
		$('form#email-list').validate({
			rules: {
				name: 'required',
				email: {
					required: true,
					email: true
				}
			},
			messages: {
				name: 'Please let us know your name.',
				email: {
					required: 'Please leave an email address.',
					email: 'Your email address does not appear to be valid. (Example: name@domain.com)'
				}
			},
			submitHandler: function(form) {
				var subOpt = {
					target: '.success',
					success: showResponse
				}
				$(form).ajaxSubmit(subOpt);
				return false;
			}
		});
		/*
		$('form#email-list').validate({
			onkeyup: true,
			success: 'valid',
			rules: {
				name: 'required',
				email: {
					required: true,
					email: true
				}
			},
			messages: {
				name: 'Please let us know your name.',
				email: {
					required: 'Please leave an email address.',
					email: 'Your email address doesn\'t appear to be valid. (Example: name@domain.com)'
				}
			},
			submitHandler: function(form)
			{
				ajaxSubmit($(form));
			}
		});
		*/
	}
	
	// Form Effects
	if ($('form div.form').length)
	{
		$('form div.form table td.field input,form div.form table td.field textarea')
			.focus(function()
				{
					$(this).parent('td').addClass('focus');
				})
			.blur(function()
				{
					$(this).parent('td').removeClass('focus');
				});
	}
	
	// Product Filters
	$('a[rel="filter"]').click(function() {
		var filter = this.className;
		$('div#content div.products ul li.'+filter).show(500);
		$('div#content div.products ul li').not('.'+filter).hide(500);
	})
	
	
	
	
	
	
	
	
	/* Functions */
	function showNewBubble(i)
	{
		var newBubble = $('<div class="bubble"><p>'+$('span.caption',i).text()+'</p><img src="/media/images/structure/bubble_point.png" class="point" /></div>');
		$(i).append(newBubble);
		var h = $('div.bubble',i).height()+50;
		var mt = '-'+h+'px';
		var ml = '-70px'
		$('div.bubble',i)
			.css({position:'absolute',top:mt,left:ml})
			.fadeIn('fast');
			//.css({top:$(i).offset().top-$('div.bubble',i).height()-80,left:$(i).offset().left-40,display:'none'})
			
	}
	function hideNewBubble(i)
	{
		$('div.bubble',i).fadeOut('fast',function(){
			$(this).remove();
		});
	}
	
	function showBubble(i)
	{
		$('div#bubble')
			.html('<p>'+$('span.caption',i).text()+'</p><img src="http://scherer.clickrain.com/media/images/structure/bubble_point.png" class="point" />')
			.css({top:$(i).offset().top-$('div#bubble').height()-80,left:$(i).offset().left-40})
			.fadeIn('fast');
			/*
			.hide()
			.html('<p>'+$('span.caption',i).text()+'</p><img src="http://scherer.clickrain.com/media/images/structure/bubble_point.png" class="point" />')
			.css({top:$(i).offset().top-$('div#bubble').height()-80,left:$(i).offset().left-40})
			.fadeIn('fast');
			*/
	}
	function hideBubble(i)
	{
		$('div#bubble')
			.stop()
			.html('<img src="http://scherer.clickrain.com/media/images/structure/bubble_point.png" class="point" />')
			.fadeOut('fast');
			/*
			.html('<img src="http://scherer.clickrain.com/media/images/structure/bubble_point.png" class="point" />')
			.fadeOut('fast');
			*/
	}
	/*
	function ajaxSubmit(f)
	{
		var name = $('input#name').val();
		var email = $('input#email').val();
		var comments = $('textarea#comments').val();
		$.post('http://scherer.clickrain.com/email-signup.php',{name:name,email:email,comments:comments},function(d)
		{
			if (d.success)
			{
				$('form#email-list').hide('fast');
				$('div.info').hide('fast');
				$('div.success').show('fast');
			}
			if (!d.success)
			{
				$('div.error').show('fast');
			}
		},'json');
	}
	*/
	function showResponse() {
		$('div.success').slideDown(400);
		$('form#email-list').hide('fast');
		$('div.info').hide('fast');
	}
	
	
	
	
	
});
