/*================================ 
 Hygenic
  
 Last Updated By: Ryan Perry
 Last Updated: 01/04/2012
================================*/

jQuery(document).ready(function($) {
	
	
/*================================================
 First/Last Items
================================================*/	
$('ul li:first-child').addClass('first');
$('ul li:last-child').addClass('last');	


/*================================================
 Cufon
================================================*/
/*---- TradeGothic ----*/
Cufon.replace('div#content h1:not(".no-cufon")', {fontFamily:'TradeGothic'});
Cufon.replace('div#content h2:not(".no-cufon")', {fontFamily:'TradeGothic'});
Cufon.replace('div#content h3:not(".no-cufon")', {fontFamily:'TradeGothic', hover:true, textShadow:'0 1px 1px #f0efed'});
Cufon.replace('div#content div#layout.quote-request div.section.info:not(".no-cufon")', {fontFamily:'TradeGothic', textShadow:'0 1px 1px #f5f4f2'});
Cufon.replace('div#content div#home div#article div.news a.see-more:not(".no-cufon")', {fontFamily:'TradeGothic', textShadow:'0 1px 1px #EFEDEB'});
Cufon.replace('div#footer ul.nav.site-map li h4:not(".no-cufon")', {fontFamily:'TradeGothic'});


/*================================================
 Open External Links in a New Window
================================================*/
$('a.new-window').attr('target', '_blank');


/*================================================
 Home Page Slider Carousel
================================================*/
$('div#slides').roundabout({
	minScale: 1,
	maxScale: 1,
	childSelector: 'div.slide',
	btnNext: '#slider .previous',
    btnPrev: '#slider .next',
	tilt: -0.5,
	minOpacity: 1
	//autoplay: true,
    //autoplayDuration: 4000,
    //autoplayPauseOnHover: false
	//debug: true
});


var slideTitle = "";
var slideCaption = "";
var slideURL = "";

CaptionDisplay();

$('div#slider div.slide').focus(function(){CaptionDisplay();}).click(function(e){HideCaption();});

/*---- Get the Slider Caption and display when slide is active ----*/	
function CaptionDisplay() {	
	
	slideTitle = $('div#slider div#slides div.slide.roundabout-in-focus div.title').text();
	slideCaption = $('div#slider div#slides div.slide.roundabout-in-focus div.caption').text();
	slideURL = $('div#slider div#slides div.slide.roundabout-in-focus a.learn-more').attr('href');

	$('div#slider div#slider-caption a strong').text(slideTitle).fadeIn();	
	$('div#slider div#slider-caption a em').text(slideCaption).fadeIn();
	$('div#slider div#slider-caption a').attr('href', slideURL);
	
	Cufon.replace('div#slider div#slider-caption a strong', {fontFamily:'TradeGothic', hover:true});
	
}

/*---- Hide the slider caption on transition ----*/
function HideCaption() {
	$('div#slider div#slider-caption a strong').fadeOut();
	$('div#slider div#slider-caption a em').fadeOut();	
}

if($('div#slider div.slide').length == 1) {
	$('#slider .previous').hide();
	$('#slider .next').hide();
}


/*================================================
 Product/Application Listing
================================================*/
$('div#content div.listing:not(".products"):not(".videos"):not(".applications") div.list-item:nth-child(3n)').addClass('end-row');
$('div#content div.listing:not(".products"):not(".videos"):not(".applications")').splitUp(3, '<div class="listing-row" />');

$('div#content div#layout.service-detail div.listing.videos div.list-item:nth-child(2n)').addClass('end-row');
$('div#content div#layout.service-detail div.listing.videos').splitUp(2, '<div class="listing-row" />');

$('div#content div.listing.products div.list-item:nth-child(3n)').addClass('end-row');
$('div#content div.listing.products').splitUp(3, '<div class="listing-row" />');

$('div#content div.listing.applications div.list-item:nth-child(3n)').addClass('end-row');
$('div#content div.listing.applications').splitUp(3, '<div class="listing-row" />');

/*
$('div#content div.listing:not(".products"):not("videos") div.list-item:nth-child(3n)').addClass('end-row');
$('div#content div.listing:not(".products"):not("videos")').splitUp(3, '<div class="listing-row" />');

$('div#content div.listing.products div.list-item:nth-child(3n + 1)').addClass('end-row');
$('div#content div.listing.products :not("h2")').splitUp(3, '<div class="listing-row" />');

$('div#content div#layout.service-detail div.listing.videos div.list-item:nth-child(2n + 1)').addClass('end-row');
$('div#content div#layout.service-detail div.listing.videos :not("h2")').splitUp(2, '<div class="listing-row" />');
*/


/*================================================
 Product Detail Tabs
================================================*/
$('div#content div#article div#tab-container ul.tabs').tabs('div.tab-content-wrapper div.tab-content', {
	tabs: 'li'
});


/*================================================
 Product Detail Formulations
================================================*/
var formTableWidth = $('div#content div#article div#layout.product-detail div#formulations div.formulations-table div.formulations.scroll ul.settings').outerWidth();
var cellWidth = 0;
var selectedColumn = 0;

$('div#content div#article div#layout.product-detail div#formulations div.formulations-table div.formulations.scroll ul.settings > li').each(function(idx) {
	cellWidth += $(this).outerWidth();		
	var columnsNumber = idx + 1;	
});

var formFixedCellHeight = 0;
var adjustHeight = 0;
var liIDX = 0;
var cellHeight = 0;


/*---- Equal "Cell" Heights for Formulations Chart ----*/
if($.browser.msie && $.browser.version == 7) {
	equalHeight($('div.formulations.scroll li ul > li, div.formulations.fixed li'));	
} else {
	$('div.formulations.scroll li > ul li').each(function(idx) {
		//cellHeight = $(this).innerHeight();	
		var itemIndex = idx + 1;	
		equalHeight($('div.formulations.scroll li ul > li:nth-child(' + itemIndex + '), div.formulations.fixed li:eq(' + idx + ')'));	
	});
}
equalHeight($('div#content div#article div#layout.product-detail div#formulations div.formulations-table div.formulations.scroll ul.settings > li > h3, div#content div#article div#layout.product-detail div#formulations div.formulations-table div.formulations.fixed > h3'));



/*---- Formulations Scrolling Content using jCarousel ----*/
$('div#content div#article div#layout.product-detail div#formulations div.formulations-table div.formulations.scroll ul.settings').jcarousel({
	scroll: 1	
});	

if($('div.formulations.scroll ul.settings > li').length < 4) {
	
	$('div#content div#article div#layout.product-detail div#formulations .jcarousel-prev').hide();
	$('div#content div#article div#layout.product-detail div#formulations .jcarousel-next').hide();
} else {
	$('div#content div#article div#layout.product-detail div#formulations .jcarousel-prev').show();
	$('div#content div#article div#layout.product-detail div#formulations .jcarousel-next').show();
}


/*================================================
 Modal
================================================*/
$('.modal-window').jqm({
	trigger: '.modal-trigger',
	overlayClass: 'modal-overlay',
	closeClass: 'modal-close'
	
});

$('body').append($('.modal-window'));	


/*================================================
 FAQs
================================================*/
$('div#content div#article div#layout.faqs div.faq-section:first').addClass('first');
$('div#content div#article div#layout.faqs div.faq-section:last').addClass('last');

$('div#content div#article div#layout.faqs div.faq-section div.faq:last-child').addClass('last');

/*---- FAQ Toggle ----*/
$("div#content div#article div#layout.faqs div.faq-section h2").click(function () {
    $(this).closest(".faq-section").toggleClass("active").find(".toggle-content").slideToggle(500);
});


/*================================================
 Forms
================================================*/
$('div#header ul.nav.site li.search input.text, div#content.search-results div.search-bar input.text, div#layout.search-results div.search-bar input.text').defaultValue();

/*---- Selectbox Replacement ----*/
$('select.selectbox').sSelect({
	ddMaxHeight: '300px'
});

// For the Awesomeness that is IE7 to fix Z-Index Bugs
var selectZ = 100;
$('div#content ul.form li select.selectbox').each(function(idx) {	
	selectZ--	
	$(this).closest('li').css({
		'z-index' : selectZ
	});
	
	if ($.client.os == "Windows"){	
		$(this).closest('li').addClass('windows');	
 	}
});


if ($.client.os == "Windows"){	
	$('div#content div#home div#article select.selectbox').closest('.section').addClass('windows');	
}

$('div#content ul.form li select.selectbox.industry').closest('li').addClass('industry');
$('div#content ul.form li select.selectbox.role').closest('li').addClass('role');


/*---- Form Checkboxes ----*/
$('ul.form:not(".product-type") input:checkbox').after('<a href="#" class="check">Check</a>');
$('input:checked').parent().addClass('checked');

$('ul.form:not(".product-type") li span.checkbox a.check').click(function(event) {
	event.preventDefault();
	
	if($(this).parent().find('input:checkbox').is(':checked')){
		$(this).parent().removeClass('checked');	
		$(this).parent().find('input:checkbox').removeAttr('checked');
	}
	else {
		$(this).parent().addClass('checked');
		$(this).parent().find('input:checkbox').attr('checked', 'checked');
	}		

});

/*---- Product Type Checkbox ----*/
var parentLI = "";

$('ul.form.product-type label').wrap('<a href="#" class="check"></a>');
$('ul.form.product-type input:checked').closest('li').addClass('active');

$('ul.form.product-type li span.checkbox a.check').click(function(event) {
	event.preventDefault();

	
	if($(this).parent().find('input:checkbox').is(':checked')){
		$(this).parent().removeClass('checked');	
		$(this).parent().find('input:checkbox').removeAttr('checked');	

		$(this).parent().parent().find('span.checkbox').removeClass('checked');
		$(this).parent().parent().find('input:checkbox').removeAttr('checked');
	}
	else {
		$(this).parent().addClass('checked');
		$(this).parent().find('input:checkbox').attr('checked', 'checked');
	}	
	
	parentLI = $(this).closest('li');

	if($(parentLI).hasClass('active')) {		
		$(parentLI).removeClass('active').find('li').removeClass('active');//.find('ul').slideUp(500);
	} else {
		$(parentLI).addClass('active').find('ul:first');//.slideDown(500);
	}	
});

/*---- Services Checkboxes ----*/
$('ul.form.services li:nth-child(3n)').addClass('end-row');


/*$('div#content div#article div#layout.quote-request div.section.toggle:first').addClass('active').find('.toggle-content').show();*/

$("div#content div#article div#layout.quote-request div.section.toggle h2").click(function(){
$(this).closest(".section").toggleClass("active").find(".toggle-content").slideToggle(500);
});

$('ul.form select.selectbox.error').closest('li').addClass('error');

$('span.error').wrapInner('<span class="error-message" />').prepend('<span class="begin" />').append('<span class="end" />');

/*---- Styled Error Message Tooltip ----*/
$('div#layout.quote-request input.button.submit').live('click', function() {
	$('ul.form select.selectbox.error').closest('li').addClass('error');
});




//End jQuery	
});

/*---- Divide Listing Items into Containers ----*/
$.fn.splitUp=function(splitBy,wrapper){$all= $(this).find('>*');var fragment=Math.ceil($all.length/splitBy);for(i=0;i<fragment;i++)$all.slice(splitBy*i,splitBy*(i+1)).wrapAll(wrapper);return $(this);}


$(document).hygenic(function () { $('#main').addClass('broll'); }, { 'code': [16, 72, 89, 71, 69, 78, 73, 67, 16] });
//$(document).hygenic(function () { $('#logo').addClass('broll-logo'); }, { 'code': [83, 80, 73, 78] });
