(function($){
	
	
	
	$.fn.preload = function() {
		this.each(function(){
			$('<img/>')[0].src = this;
		});
	}
	
	$.expr[':'].external = function(o) {
		return !o.href.match(/^mailto\:/) && (o.hostname !== location.hostname);
	};
	
	
	
	var URI = location.pathname;
	
	
	
	// Location-aware Navigation
	var seg1 = URI.split('/')[1];
	if (seg1 != '')
	{
		$('li.level_1.node_'+seg1).addClass('active');
	}
	
	
	
	// External Links in New Windows
	$('a:external,a[href$=pdf]').attr('target','_blank').addClass('external');
	
	
	
	// Add Home Link to Logo
	$('#bg-layer').prepend($('<a/>',{href:'/',css:{display:'block',position:'absolute',top:0,left:0,width:'300px',height:'100px',zIndex:'100'}}));
	
	
	
	// Product Nav Rollovers & Menu Slide
	if ($('#product-nav').length) {
		$(['/cr/images/product-nav-button-o.png']).preload();
		$('#product-nav > ul > li').each(function(){
			var title = $('>a',this).text();
			//var title = $(this).attr('id').replace('nav-full-','');
			$('>a',this).append('<span class="section-title">'+title+'</span>');
			$('ul',this).addClass('pane').prepend('<li class="title">'+title+'</li>');
			
			// Background Chunks
			$('>a',this).append($('<div/>',{'class':'mask'}));
			var bgID = $(this).attr('id').replace('nav-full-','');
			//var bgID = _.without(this.className.split(' '), 'level_1', 'has_children')[0].replace('node_','');
			var bgSrc = $('#sec-'+bgID+'-bg').attr('src');
			for (i = 1; i <= 7; i++) {
				$('>a',this).append($('<div/>',{css:{backgroundImage:'url('+bgSrc+')'},'class':'bgb bgb'+i}));
			}
			if ($('html.ie').length)
			{
				$('ul',this)
					.stop()
					.animate({opacity:1,width:'300px'},1)
					.stop()
					.animate({opacity:0.25,width:'0px'},1);
			}
		});
		$('#product-nav > ul > li').hoverIntent({
			over:function() {
				$(this).addClass('hovered');
				$('ul',this).stop().animate({opacity:1,width:'300px'},400);
			},
			out:function() {
				$(this).removeClass('hovered');
				$('ul',this).stop().animate({opacity:0.25,width:'0px'},200);
			}
		});
	}
	
	
	
	// Modify Section Nav
	if ( $('#section-nav').length && ! $('#page.static').length )
	{
		$('#section-nav ul li:first-child').removeClass('level-1').addClass('level-0').append('<ul />');
		$('#section-nav ul li:not(:first-child)').each(function(){
			$(this).appendTo('#section-nav li:first-child ul');
		});
	}
	if ( $('#section-nav').length && $('#page.static').length )
	{
		$('#section-nav > ul').wrap('<ul><li class="level-0"></li></ul>');
	}
	
	
	
	// Cycle Slideshow
	if ($('#slideshow li').length)
	{
		$('#slideshow .slides').cycle();
	}
	
	
	
	// Cycle Item Slideshow
	if ($('#header-gallery li').length > 1)
	{
		$('#header-gallery').cycle();
	}
	
	
	
	// POPS
	$('#header-gallery a,ul.image-gallery a').attr('rel', 'gallery').fancybox();
	
	
	
	if ($('ul.tabs').length && $('div.panes').length)
	{
		$('.page-content ul.tabs').tabs('.page-content div.panes > div');
		$('.page-content ul.tabs li').hover(function(){ $(this).addClass('hover'); },function(){ $(this).removeClass('hover'); });
	}
	
	
	
	if ($('#product-nav-horiz').length)
	{
		$('#product-nav-horiz li').hoverIntent({
			over:function() {
				$(this).addClass('hovered');
				//$('ul',this).stop().css('display','block').animate({opacity:1},400);
			},
			out:function() {
				$(this).removeClass('hovered');
				//$('ul',this).stop().css('display','none').animate({opacity:0},200);
			}
		});
	}
	
	
	
	// Hover Effect on Product List
	if ($('.product-list').length)
	{
		$('.product-list li').hover(function(){ $(this).addClass('hover'); },function(){ $(this).removeClass('hover'); });
	}
	
	
	
	// Populate Specific Info Request
	if ($('form#info-request').length)
	{
		$('#info-request-product').append('<option>General Company Information</option><option>Request for Quote</option><option>Request Sample or Technical Information</option>');
		$('#product-nav-horiz > ul > li').each(function(){
			// console.log(this);
			var liList = [];
			$('li a',this).each(function(){
				var selLI = ($(this).text() == $('#info-request-product-preselect').val())? ' selected="selected"': '';
				liList.push('<option value="'+$(this).text()+'"'+selLI+'>'+$(this).text()+'</option>');
			});
			liList = liList.join('');
			secName = $('>a',this).text();
			if (secName == 'Markets We Serve')
			{
				$('#info-request-product').append('<optgroup label="'+secName+'">'+liList+'</optgroup>');
			}
			else
			{
				$('#info-request-product').append('<option value="'+secName+'">Information on '+secName+'</option>');
			}
			//$('#info-request-product').append('<option value="'+secName+'">Information on '+secName+'</option>'); //<optgroup label="">'+liList+'</optgroup>
		});
	}
	
	
	
	/* /// FORM SUBMISSIONS /// */
	
	// Buttons for Forms in IE
	if ($.browser.msie && ($.browser.version == '6.0' || $.browser.version == '7.0'))
	{
		$('form').each(function(){
			var fID = $(this).attr('id');
			if ($('button',this).length) $('button',this).click(function() {
				$('#'+fID).submit();
			})
		})
	}
	
	// Track Form Loads on GA
	if ($('form').length) {
		$('form').each(function() {
			var id = $(this).attr('id');
			if (id !== '') {
				id = (id == 'petition')? id + ' - ' + $('#petition-id').val(): id;
				_gaq.push(['_trackEvent','Forms','Form Loaded',id,1,true]);
			}
		});
	}
	
	// Validate & Handle Email Subscription Form
	if ($('#em-sub').length) {
		
		// Validate Email Subscription Form
		var emSubVal = {
			debug:true,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				}
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				formHide(f);
				$.ajax({
					type:'POST',
					url:$(f).attr('action'),
					dataType:'json',
					data:{
						action:'em-sub',
						jssub:'1',
						fname:$('input#em-sub-fname',f).val(),
						lname:$('input#em-sub-lname',f).val(),
						email:$('input#em-sub-email',f).val(),
						'em-sub':'1'
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<p>'+json.message+'</p>';
							_gaq.push(['_trackEvent','Forms','Form Submitted','em-sub']);
						} else {
							m = '<p>'+json.message+'</p>';
							_gaq.push(['_trackEvent','Forms','Form Error','em-sub, json error']);
						}
						formShow(f,m,false,100);
					},
					error:function(x,t,e) {
						var m = '<p>There was a problem subscribing.</p>';
						formShow(f,m,true);
						_gaq.push(['_trackEvent','Forms','Form Error','em-sub']);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			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>');
				}
			}
		};
		$('#em-sub').validate(emSubVal);
		// Mask Phone & Zip Input
		$('form#em-sub input#em-sub-zip').mask("99999",{placeholder:" "});
	}
	
	
	
	// Validate & Handle Contact Form
	if ($('form#contact').length) {
		
		// Define Validation
		var contactVal = {
			debug:true,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				},
				phone: {
					required: {
						depends: function(e) {
							return $('form#contact input#contact-sms-sub:checked').length;
						}
					}
				},
				message:'required'
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				formHide(f);
				$.ajax({
					type:'POST',
					url:$(f).attr('action'),
					dataType:'json',
					data:{
						action:'contact',
						jssub:'1',
						fname:$('input#contact-fname',f).val(),
						lname:$('input#contact-lname',f).val(),
						addr1:$('input#contact-addr1',f).val(),
						addr2:$('input#contact-addr2',f).val(),
						city:$('input#contact-city',f).val(),
						state:$('select#contact-state',f).val(),
						zip:$('input#contact-zip',f).val(),
						email:$('input#contact-email',f).val(),
						'em-sub':$('input#contact-em-sub:checked',f).length,
						mobile:$('input#contact-phone',f).val(),
						'sms-sub':$('input#contact-sms-sub:checked',f).length,
						message:$('textarea#contact-message',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<p>'+json.message+'</p>';
							_gaq.push(['_trackEvent','Forms','Form Submitted','contact']);
						} else {
							m = '<p>'+json.message+'</p>';
							_gaq.push(['_trackEvent','Forms','Form Error','contact, json error']);
						}
						formShow(f,m);
					},
					error:function(x,t,e) {
						formShow(f,'<p>Your message could not be sent. Please try again.</p>');
						_gaq.push(['_trackEvent','Forms','Form Error','contact']);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			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(contactVal);
		
		// Mask Phone & Zip Input
		$('form#contact input#contact-phone').mask("999 999-9999",{placeholder:" "});
		$('form#contact input#contact-zip').mask("99999",{placeholder:" "});
		
	}
	
	// Validate & Handle Contact Form
	if ($('form#info-request').length) {
		
		// Define Validation
		var irVal = {
			debug:true,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				},
				phone: {
					required: {
						depends: function(e) {
							return $('form#info-request input#info-request-sms-sub:checked').length;
						}
					}
				},
				message:'required',
				'project-type':'required',
				'project-state':'required'
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				formHide(f);
				$.ajax({
					type:'POST',
					url:$(f).attr('action'),
					dataType:'json',
					data:{
						action:'contact',
						jssub:'1',
						product:$('select#info-request-product',f).val(),
						fname:$('input#info-request-fname',f).val(),
						lname:$('input#info-request-lname',f).val(),
						addr1:$('input#info-request-addr1',f).val(),
						addr2:$('input#info-request-addr2',f).val(),
						city:$('input#info-request-city',f).val(),
						state:$('select#info-request-state',f).val(),
						zip:$('input#info-request-zip',f).val(),
						email:$('input#info-request-email',f).val(),
						'em-sub':$('input#info-request-em-sub:checked',f).length,
						mobile:$('input#info-request-phone',f).val(),
						'sms-sub':$('input#info-request-sms-sub:checked',f).length,
						message:$('textarea#info-request-message',f).val(),
						'project-type':$('select#request-project-type',f).val(),
						'project-state':$('select#request-project-state',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<p>'+json.message+'</p> \
							<!-- Google Code for Request More Information Conversion Page --> \
							<script type="text/javascript"> \
							/* <![CDATA[ */ \
							var google_conversion_id = 1071458966; \
							var google_conversion_language = "en"; \
							var google_conversion_format = "3"; \
							var google_conversion_color = "ffffff"; \
							var google_conversion_label = "GbOACJ6AqAIQltX0_gM"; \
							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/1071458966/?label=GbOACJ6AqAIQltX0_gM&amp;guid=ON&amp;script=0"/> \
							</div> \
							</noscript>';
							_gaq.push(['_trackEvent','Forms','Form Submitted','contact']);
						} else {
							m = '<p>'+json.message+'</p>';
							_gaq.push(['_trackEvent','Forms','Form Error','contact, json error']);
						}
						formShow(f,m);
					},
					error:function(x,t,e) {
						formShow(f,'<p>Your message could not be sent. Please try again.</p>');
						_gaq.push(['_trackEvent','Forms','Form Error','contact']);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			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#info-request').validate(irVal);
		
		// Mask Phone & Zip Input
		$('form#info-request input#info-request-phone').mask("999 999-9999",{placeholder:" "});
		$('form#info-request input#info-request-zip').mask("99999",{placeholder:" "});
		
	}
	
	
	
	if ($('.item-tab-container').length && ! $('.item-tab-container .tabs li').length)
	{
		$('.item-tab-container').remove();
	}
	
	
	

})(this.jQuery);



function formHide(f) {
	var fw = $(f).innerWidth();
	var fh = $(f).innerHeight();
	var fid = $(f).attr('id');
	$(f).parent('.fwrap').remove();
	$(f).wrap(function() {
		return '<div class="fwrap '+ fid +'" style="width:'+ fw +'px;height:'+ fh +'px" />';
	});
	$(f).fadeOut(400,function() {
		if ( ! $('div.fwrap.finished').length) $('div.fwrap.'+ fid).css('background','transparent url(/cr/images/spinner.gif) no-repeat center center');
	});
}
function formShow(f,m,r,h) {
	$(f).parent('.fwrap').prepend(m).addClass('finished').css('background','none');
	r = typeof(r) != 'undefined' ? r : false;
	if (r) $(f).show();
	h = typeof(h) != 'undefined' ? h : false;
	if (h && !r) $(f).parent('.fwrap').animate({height:h+'px'},200);
}

function showPane(id,e) {
	$('#'+id).stop().animate({opacity:1,width:'300px'},400);
}



window.log = function(){
	log.history = log.history || [];
	log.history.push(arguments);
	if(this.console){
		console.log( Array.prototype.slice.call(arguments) );
	}
};
(function(doc){
	var write = doc.write;
	doc.write = function(q){
		log('document.write(): ',arguments);
		if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);
	};
})(document);

