$(function() {
	
	if ($('#home-alert').length)
	{
		var aShows = getCookie('homeAlert04142010');
		if (aShows===null || aShows < 3)
		{
			aCount = (aShows===null)? 1: Number(aShows) + 1;
			$('a#home-alert-trigger').fancybox({
				hideOnContentClick:false,
				overlayOpacity:0.7,
				onClosed:function() {
					var today = new Date();
					var expire = new Date();
					expire.setTime(today.getTime() + 360000*24*30);
					document.cookie = "homeAlert04142010="+aCount
									+ ";expires="+expire.toGMTString();
					}
			}).click();
		}
	}
	
	if ($.browser.msie && $.browser.version < 7) {
	$('form button').click(function() {
		$(this).parents('form').submit();
	});
	}
	
	$('a.new-window').attr('target','_blank');
	
	if ($('div#bubble ul.slides').length) {
		$('div#bubble ul.slides').cycle({
			//fx:'scrollLeft',
			pager:$('div#bubble div.control'),
			next:$('div#bubble a.next'),
			prev:$('div#bubble a.prev')
		});
	}
	
	$('form:not(#comment_form) :text').each(function() {
		var pre = $(this).siblings('span.title').text();
		if ($(this).val()=='') $(this).val(pre).addClass('pre');
		$(this).focus(function() {
			if ($(this).val()==pre) $(this).val('').removeClass('pre');
		});
		$(this).blur(function() {
			if ($(this).val()=='') $(this).val(pre).addClass('pre');
		});
	});
	$('form:not(#comment_form) textarea').each(function() {
		var pre = $(this).siblings('span.title').text();
		if ($(this).text()=='') $(this).text(pre).addClass('pre');
		$(this).focus(function() {
			if ($(this).text()==pre) $(this).text('').removeClass('pre');
		});
		$(this).blur(function() {
			if ($(this).text()=='') $(this).text(pre).addClass('pre');
		});
	});
	
	if ($('div#header ul li.sel a img').length) {
		$('div#header ul li.sel a img').attr('src',$('div#header ul li.sel a img').attr('src').replace(/.png$/,'_o.png'));
	}
	$('div#header ul li:not(.sel) a img,div#footer div.locations a img').hover(function() {
		$(this).attr('src',$(this).attr('src').replace(/.png$/,'_o.png'));
	},function() {
		$(this).attr('src',$(this).attr('src').replace(/_o.png$/,'.png'));
	});
	
	if ($('button,p.read-more a').length) {
		$('button,p.read-more a').hover(function() {
			$(this).addClass('hov');
		},function() {
			$(this).removeClass('hov');
		});
	}
	
	if ($.browser.msie && $.browser.version <= 7) {
		$('form button.sub').click(function() {
			$(this).parents('form').submit();
		});
	}
	
	if ($('a.callout img.button,a.callout-h img.button').length) {
		$('a.callout img.button,a.callout-h img.button').hover(function() {
			$(this).attr('src',$(this).attr('src').replace(/.png$/,'_o.png'));
		},function() {
			$(this).attr('src',$(this).attr('src').replace(/_o.png$/,'.png'));
		});
	}
	
	if ($('a.colorbox').length) {
		$('a.colorbox').colorbox({width:'320px',height:'400px',iframe:true});
	}
	
	
	
	
	if ($('form.contact').length && $('div.loc').length) {
		var loc = $('div.loc').text();
		$('form.contact').prepend('<input type="hidden" name="loc" id="contact-loc" value="'+loc+'">');
	}
	$('form.contact').submit(function() {
		$(':text',this).each(function() {
			var pre = $(this).siblings('span.title').text();
			if ($(this).val() == pre) {
				$(this).not('.n-req').val('');
			}
		});
		$('textarea',this).each(function() {
			var pre = $(this).siblings('span.title').text();
			if ($(this).text() == pre) {
				$(this).not('.n-req').text('');
			}
		});
		return false;
	});
	var cFormOpt = {
		debug:true,
		onkeyup:false,
		rules: {
			name:'required',
			email: {
				required:true,
				email:true
			}
		},
		errorClass:'invalid',
		highlight:function(e,ec) {
			$(e).addClass(ec);
			$(e).not('.n-req').val($(e).siblings('span.title').text());
		},
		unhighlight:function(e,ec) {
			$(e).removeClass(ec);
			$(e).siblings('span.error').remove();
		},
		submitHandler:function(f) {
			formShade($('div.content-box'));
			$.ajax({
				type:'POST',
				url:'/cr/php/processor.php',
				dataType:'json',
				data:{
					action:$('input#contact-action',$(f)).val(),
					affiliate:$('input#contact-affiliate',$(f)).val(),
					affiliateinfo:($('input#contact-affiliate-info:checked').length)?1:0,
					name:$('input#contact-name').val(),
					company:$('input#contact-company').val(),
					address:$('input#contact-address').val(),
					city:$('input#contact-city').val(),
					state:$('input#contact-state').val(),
					zip:$('input#contact-zip').val(),
					phone:$('input#contact-phone').val(),
					fax:$('input#contact-fax').val(),
					email:$('input#contact-email').val(),
					comments:$('textarea#contact-comments').val(),
					signup:($('input#contact-signup:checked').length)?1:0,
					js:$('input#contact-js').val()
				},
				success:function(json) {
					var m = '';
					if (json.success) {
						window.location = "/contact/success/?msg="+json.success;
						m = '<h1>Thanks for contacting Milo Belle.</h1><p>'+json.success+'</p><!-- Google Code for Contact Form Submission Conversion Page --><script type="text/javascript"><!--var google_conversion_id = 1035376585;var google_conversion_language = "en_US";var google_conversion_format = "3";var google_conversion_color = "ffffff";var google_conversion_label = "xN6lCOvtswEQya_a7QM";var google_conversion_value = 0;//--></script><script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js"></script><noscript><div style="display:inline;"><img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/1035376585/?label=xN6lCOvtswEQya_a7QM&amp;guid=ON&amp;script=0"/></div></noscript>';
					} else {
						m = '<p><strong>An error has occurred.</strong></p>';
					}
					formReveal($('div.content-box'),m);
				},
				error:function(x,t,e) {
					formReveal($('div.content-box'),'<p><strong>An error has occurred.</strong> Please try again later.</p>');
				}
			});
			return false;
		},
		invalidHandler: function(f,v) {
			$('input:text:not(.invalid)',v).each(function() {
				$(this).not('.n-req').val($(this).siblings('span.title').text());
			});
		},
		errorPlacement: function(err,el) {
			var fn = $(el).siblings('span.title').text();
			$(el).siblings('span.error').remove();
			if(!$(el).siblings('span.error').length) {
				$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
			}
		}
	}
	$('form.contact').validate(cFormOpt);
	$('form.contact').prepend('<input type="hidden" name="js" id="contact-js" value="1">');
	
	
	
	var resFormOpt = {
		debug:true,
		onkeyup:false,
		rules: {
			name:'required',
			phone:'required',
			email: {
				required:true,
				email:true
			}
		},
		errorClass:'invalid',
		highlight:function(e,ec) {
			$(e).addClass(ec);
			$(e).not('.n-req').val($(e).siblings('span.title').text());
		},
		unhighlight:function(e,ec) {
			$(e).removeClass(ec);
			$(e).siblings('span.error').remove();
		},
		submitHandler: function(f) {
			f.submit();
		},
		invalidHandler: function(f,v) {
			$('input:text:not(.invalid)',v).each(function() {
				$(this).not('.n-req').val($(this).siblings('span.title').text());
			});
		},
		errorPlacement: function(err,el) {
			var fn = $(el).siblings('span.title').text();
			$(el).siblings('span.error').remove();
			if(!$(el).siblings('span.error').length) {
				$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
			}
		}
	}
	$('form.resume').submit(function() {
		$(':text',this).each(function() {
			var pre = $(this).siblings('span.title').text();
			if ($(this).val() == pre) {
				$(this).not('.n-req').val('');
			}
		});
		// return $(this).valid(resFormOpt);
	});
	$('form.resume').validate(resFormOpt);
	
	
	
	
	
	$('form.wp-dl').submit(function() {
		$(':text',this).each(function() {
			var pre = $(this).siblings('span.title').text();
			if ($(this).val() == pre) {
				$(this).not('.n-req').val('');
			}
		});
		return false;
	});
	var dlFormOpt = {
		debug:true,
		onkeyup:false,
		rules: {
			fname:'required',
			lname:'required',
			email: {
				required:true,
				email:true
			}
		},
		errorClass:'invalid',
		highlight:function(e,ec) {
			$(e).addClass(ec);
			$(e).not('.n-req').val($(e).siblings('span.title').text());
		},
		unhighlight:function(e,ec) {
			$(e).removeClass(ec);
			$(e).siblings('span.error').remove();
		},
		submitHandler:function(f) {
			formShade($('div#colorbox-content'));
			$.ajax({
				type:'POST',
				url:'/cr/php/processor.php',
				dataType:'json',
				data:{
					action:$('input.action',$(f)).val(),
					file:$('input#file').val(),
					fname:$('input#fname').val(),
					lname:$('input#lname').val(),
					cname:$('input#cname').val(),
					email:$('input#email').val(),
					js:$('input#js').val()
				},
				success:function(json) {
					var m = '';
					if (json.success) {
						m = '<h1>Thanks for your interest.</h1><p><a href="'+json.link+'" class="wp"></a></p><p>Click the icon above<br>to download the whitepaper.</p>';
					} else {
						m = '<p><strong>An error has occurred.</strong></p>';
					}
					formReveal($('div#colorbox-content'),m);
				},
				error:function(x,t,e) {
					formReveal($('div#colorbox-content'),'<p><strong>An error has occurred.</strong> Please try again later.</p>');
				}
			});
			return false;
		},
		invalidHandler: function(f,v) {
			$('input:text:not(.invalid)',v).each(function() {
				$(this).not('.n-req').val($(this).siblings('span.title').text());
			});
		},
		errorPlacement: function(err,el) {
			var fn = $(el).siblings('span.title').text();
			$(el).siblings('span.error').remove();
			if(!$(el).siblings('span.error').length) {
				$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
			}
		}
	}
	$('form.wp-dl').validate(dlFormOpt);
	$('form.wp-dl').prepend('<input type="hidden" name="js" id="js" value="1">');
	
	
	
	$('form.em-sub').submit(function() {
		$(':text',this).each(function() {
			var pre = $(this).siblings('span.title').text();
			if ($(this).val() == pre) {
				$(this).not('.n-req').val('');
			}
		});
		return false;
	});
	var formOpt = {
		onkeyup:false,
		rules: {
			fname:'required',
			lname:'required',
			email: {
				required:true,
				email:true
			}
		},
		errorClass:'invalid',
		highlight:function(e,ec) {
			$(e).addClass(ec);
			$(e).not('.n-req').val($(e).siblings('span.title').text());
		},
		unhighlight:function(e,ec) {
			$(e).removeClass(ec);
			$(e).siblings('span.error').remove();
		},
		submitHandler:function(f) {
			formShade($(f));
			$.ajax({
				type:'POST',
				url:'/cr/php/processor.php',
				dataType:'json',
				data:{
					action:$('input.action',$(f)).val(),
					fname:$('input#fname').val(),
					lname:$('input#lname').val(),
					cname:$('input#cname').val(),
					email:$('input#email').val(),
					js:$('input#js').val()
				},
				success:function(json) {
					var m = '';
					if (json.success) {
						m = '<p><strong>Thank You!</strong> '+json.success+'</p>';
					} else {
						m = '<p><strong>An error has occurred.</strong> '+json.error+'</p>';
					}
					formReveal($(f),m);
				},
				error:function(x,t,e) {
					formReveal($(f),'<p><strong>An error has occurred.</strong> Please try again later.</p>');
				}
			});
			return false;
		},
		invalidHandler: function(f,v) {
			$('input:text:not(.invalid)',v).each(function() {
				$(this).not('.n-req').val($(this).siblings('span.title').text());
			});
		},
		errorPlacement: function(err,el) {
			var fn = $(el).siblings('span.title').text();
			$(el).siblings('span.error').remove();
			if(!$(el).siblings('span.error').length) {
				$(el).parent().append('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
			}
		}
	}
	$('form.em-sub').validate(formOpt);
	$('form.em-sub').prepend('<input type="hidden" name="js" id="js" value="1">');
	
	
	
});



function formShade(el) {
	$(el).prepend('<div class="shade"><img src="/cr/images/icon_spinner.gif" alt="" style="display:none;"></div>');
}
function formReveal(el,str) {
	$(el)
		.fadeOut(800,function() {
			$(this).html(str);
		})
		.fadeIn(800);
}

//get cookie
function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}