if (typeof jQuery != 'undefined') {
  $(function() {
  	$('#options .thumbs > :last-child, .multi:last-child, .resource.profiles .section:last').addClass('last');
  	if(jQuery().accordion) {
	  	$('#options').accordion({
	  		collapsible: true,
	  		active: false,
	  		clearStyle: true,
	  		autoHeight: false
	  	});
	  	$('#options > div').accordion({
	  		header: 'h3',
	  		collapsible: true,
	  		active: false,
	  		clearStyle: true,
	  		autoHeight: false
	  	});
	  	$('#materials').accordion({
	  		//event: 'mouseover',
	  		header: 'h2',
	  		autoHeight: false
	  	});
	  	// fix IE 7 clear issues in accordion
	  	if(!(jQuery.support.htmlSerialize)) {
	  		$('.list1 + .thumbs, .thumbs.clear').before('<div style="clear:both;"></div>');
	  	}
  	}
  	if(jQuery().colorbox) {
  		$(".details a[rel='detail']").colorbox();
  	}
  	
  	//open external links in a new window
  	$("a[href^=http]").each(
			function() {
				if(this.href.indexOf(location.hostname) == -1)
					$(this).attr('target','_blank');
		});
		$('a[rel*=external]').click(function() {
			window.open(this.href,'external');
			return false;
		});
		
		//open image bank link on PDP in new window
		$('.product_display .details a[href*=image-bank]').click(function() {
					window.open(this.href,'external');
					return false;
		});
    		
  	// begin overlay stuff
		// begin generic overlay function
		function overlay(trigger,target) {
	  	$(trigger).click(function() {
	  		var src = $(this).attr('href');
	  		$(target).load(src+' .container',function() {
	  			$(this).append('<a class="close">&times;</a>');
			  	$('html, #options h2, #options h3, #options h4, .overlay .close').click(function() {
			  		$(target).fadeOut(function() {
			  			$(this).children('.container').remove();
			  		});
			  	});
					$('a[rel*=external]').click(function() {
						window.open(this.href,'external','status=0,location=0,toolbar=0,scrollbars=1,resizable=1,directories=0,width=900');
						return false;
					});
			  	$('.overlay').click(function(event) {event.stopPropagation();});
	  		}).fadeIn();
	  		return false;
	  	});
		}
		// end generic overlay function
		// add overlay holders
  	$('#options, .resource #main').append('<div class="overlay" style="display: none;"></div>');
  	//$('#options a[rel~=ultrafoam]:first-child').before('<div class="overlay ultrafoam" style="display: none;"></div>');
  	//$('.ultrafoam').parent('.thumbs').addClass('ultrafoam_container');
		// invoke overlays
  	overlay('#options a[rel=detail]','#options > .overlay');
  	//overlay('#options a[rel~=ultrafoam]','#options .thumbs .overlay');
  	overlay('.resource .thumbs a[rel~=detail]','.resource .overlay');
  	// end overlay stuff
  	if(jQuery().tooltip) {
  		$('a[rel~=detail][title]').tooltip({
  			track: true,
  			showURL: false,
  			showBody: ' - '
  		});
  	}
  	// sample request form functionality (to influence layout with CSS)
  	if(jQuery().makeacolumnlists) {
  		$('#materials .multi ul').makeacolumnlists({cols: 4});
  		$('#literature .multi ul').makeacolumnlists({cols: 2});
  	}
  	$('#materials > div > div').each(function() {
  		if($(this).find('li').length < 5 || $(this).find('h4').length == 0) {
  			$(this).prev().insertBefore($(this).children(':first'));
  			$(this).addClass('minor');
  		}
  	});
  	$('#literature > div > div').each(function() {
  		if($(this).find('h4').length == 0) {
  			$(this).prev().insertBefore($(this).children(':first'));
  		}
  	});
  	// material request form “select all” functionality
  	$('.request #materials h3, .request #literature h2').append('<label><input type="checkbox" /> select all</label>');
  	$('.request #materials h3, .request #literature #other h2 label').remove();
  	$('.request #materials h3 input').change(function() {
  		if($(this).is(':checked')) {
  			$(this).closest('h3').next().find('input[type=checkbox]').attr('checked',true);
  		}
  		else {
  			$(this).closest('h3').next().find('input[type=checkbox]').attr('checked',false);
  		}
  	});
  	$('.request #materials h3 + * input').change(function() {
  		if(!($(this).is(':checked'))) {
  			$(this).closest('.request #materials h3 + *').prev().find('input[type=checkbox]').attr('checked',false);
  		}
  	});
  	$('.request #literature h2 input').change(function() {
  		if($(this).is(':checked')) {
  			$(this).closest('h2').next().find('input[type=checkbox]').attr('checked',true);
  		}
  		else {
  			$(this).closest('h2').next().find('input[type=checkbox]').attr('checked',false);
  		}
  	});
  	$('.request #literature div h2 input').change(function() {
  		if($(this).is(':checked')) {
  			$(this).closest('h2').nextAll().find('input[type=checkbox]').attr('checked',true);
  		}
  		else {
  			$(this).closest('h2').nextAll().find('input[type=checkbox]').attr('checked',false);
  		}
  	});
  	$('.request #literature h2 ~ * input').change(function() {
  		if(!($(this).is(':checked'))) {
  			$(this).closest('.request #literature h2 ~ *').siblings('h2').find('input[type=checkbox]').attr('checked',false);
  		}
  	});
  	// request samples link functionality
  	$('a[rel="request"]').click( function() {
        window.open($(this).attr('href'),'request','menubar=0,location=0,toolbar=0,directories=0,scrollbars=1,width=1000');
        return false;
    });
    // rep locator functionaliy
    $('#rep_locator').empty().append('<div id="area_select"></div><div id="rep_container"></div>');
    $('#area_select').load('rep_list.html #state_list', function() {
    	$(this).prepend('<span class="selected">Select your area</span>');
			$(document).click(function() {
				$('#area_select #state_list').fadeOut();
			});
			$('#area_select').click(function(e) {
				if($('#area_select #state_list').is(':hidden')) {
					$('#area_select #state_list').fadeIn();
			   	e.stopPropagation();
				}
				else {$('#area_select #state_list').fadeOut();}
			});
			$('#state_list li a').click(function() {
				$('#area_select .selected').contents().replaceWith($(this).text());
				$('#area_select #state_list').fadeOut();
				var elID = $(this).attr('href');
				// here comes the IE7 workaround for the attribute value normalization issue
				if(!(jQuery.support.hrefNormalized)) {
					var elID = $(this).attr('href').substring($(this).attr('href').indexOf('#'));
				}
				// end IE7 workaround
				$('#rep_container').load('rep_list.html '+elID, function() {
					$(this).addClass('active');
					$(this).children('div:not(.multiple)').find('h2').remove();
				});
				return false;
			});
    });
    // end $('#area_select').load() function
    // end rep locator functionality
    // begin landing page text fading functionality
		$('.landing.top').closest('html').css('overflow-x','hidden');
		$('.landing.top #main h1').hide();
		$('.landing.top #main').append('<img class="text1" /><img class="text2" />');
			//- root_dir (root directory) is defined in header.php
		if(window.root_dir === undefined) {root_dir = 0;} // just checking if it has actually been defined
		$('.landing.tables .text1').attr('src',root_dir+'/img/text_landing_tables-1.png');
		$('.landing.tables .text2').attr('src',root_dir+'/img/text_landing_tables-2.png');
		$('.landing.banquettes .text1').attr('src',root_dir+'/img/text_landing_banquettes-1.png');
		$('.landing.banquettes .text2').attr('src',root_dir+'/img/text_landing_banquettes-2.png');
		$('.landing.products .text1').attr('src',root_dir+'/img/text_landing_products-1.png');
		$('.landing.products .text2').attr('src',root_dir+'/img/text_landing_products-2.png');
		$('.landing.outdoor .text1').attr('src',root_dir+'/img/text_landing_outdoor-1.png');
		$('.landing.outdoor .text2').attr('src',root_dir+'/img/text_landing_outdoor-2.png');
		$('.landing.resource .text1').attr('src',root_dir+'/img/text_landing_resources-1.png');
		$('.landing.resource .text2').attr('src',root_dir+'/img/text_landing_resources-2.png');
		$('.landing.top .text2').hide().fadeIn(1500);
    // end landing page text fading functionality
  	var items_per_row	= 5;
  	if($('body').is('.banquettes')) {
  		var items_per_row = 3;
  	}
		var li_count = $('ul.overview > li').length;
		var final_row_count	= li_count % items_per_row;
		final_row_count = (final_row_count == 0) ? items_per_row : final_row_count;
		$('ul.overview > li').eq(-final_row_count).addClass('last_row_item');
		if (final_row_count > 1){
			$('ul.overview > li:last-child').addClass('last_row_item').prevUntil('.last_row_item').addClass('last_row_item');
		}
    
    //open all PDF links in a new window
    $('a[href$=pdf]').click(function() {
							window.open(this.href,'external');
							return false;
		});
    $('#newsletter').appendTo('#container');
    // additional functions should be added here
  });
}
