$(function() {
	if ($('#main').hasClass('home')) {
		setupHomeRotations();
	} else if ($('#main').hasClass('products')) {
		setupProducts();
	} else if ($('#main').hasClass('prod')) {
		setupSingleProduct();
	} else if ($('#main').hasClass('mfm')) {
		setupMfmLive();
	}
	$('#tabs a').click(function() {
		var tab = $(this).parent();
		if (!tab.hasClass('current')) {
			tab.siblings('.current').each(function() {
				$('#' + this.id + 'Body').fadeOut(250);
				$(this).removeClass('current');
			});
			$('#' + tab.attr('id') + 'Body').fadeIn(250);
			tab.addClass('current');
		}
		return false;
	});
	$('#tabs li:not(.current)').each(function() {$('#' + this.id + 'Body').hide();});
	if (!ie6) {
		setTimeout(function() {
			$.preloadCssImages();
		}, 1000);
	}
	$('#infoForm').submit(function() {
		if ($.trim($('#formName').val()) == '') {
			alert('Please enter a contact name.');
			$('#formName').focus();
			return false;
		}
		if ($.trim($('#formCompany').val()) == '') {
			alert('Please enter your company name.');
			$('#formCompany').focus();
			return false;
		}
		if ($.trim($('#formEmail').val()) == '') {
			alert('Please enter you email.');
			$('#formEmail').focus();
			return false;
		}
		if ($.trim($('#formPhone').val()) == '') {
			alert('Please enter your phone number.');
			$('#formPhone').focus();
			return false;
		}
		return true;
	});
	$('#shade, #popup').hide();
	$('#sulfateBug').click(function() {
		$('#popup div.body').load(baseDir + '/ingredient-features/', showPopup);
		return false;
	});
	$('#privacyLink').click(function() {
		$('#popup div.body').load(baseDir + '/privacy/', showPopup);
		return false;
	});
	$('#legalLink').click(function() {
		$('#popup div.body').load(baseDir + '/legal/', function() {
			showPopup();
			$('#legalPrivacy').click(function() {
				if (ie) {
					$('#popup').hide();
				} else {
					$('#popup').fadeOut(500);
				}
				$('#privacyLink').trigger('click');
				return false;
			});
		});
		return false;
	});
	$('#popup div.top').click(function() {
		if (ie) {
			$('#shade, #popup').hide();
		} else {
			$('#shade, #popup').fadeOut(500);
		}
		return false;
	});
	if ($('#sulfate1').size() > 0) {
		$('#sulfate2').hide().data('showing', false);
		setInterval(function() {
			if ($('#sulfate2').data('showing')) {
				$('#sulfate2').fadeOut(500).data('showing', false);
				$('#sulfate1').fadeIn(500);
			} else {
				$('#sulfate2').fadeIn(500).data('showing', true);
				$('#sulfate1').fadeOut(500);
			}
		}, 4000);
	}
	$(window).resize(resizeLayout);
	resizeLayout();
});

function setupHomeRotations() {
	var imgNo = Math.floor(Math.random() * 8) + 1;
	$('#header').css('background-image', "url('" + tmpltDir + "/images/header" + imgNo + ".jpg')");
	$('#title2, #title3, #bug2, #bug3').hide().html('');
	$('#title1, #bug1').addClass('showing').html('');
	setInterval(swapHomeTitles, 5000);
	setInterval(swapHomeBugs, 10000);
}

function swapHomeTitles() {
	var curr = $('#homeTitles div.showing');
	var id = parseInt(curr.attr('id').substr(5)) + 1;
	if (id > 3) id = 1;
	if (ie) {
		curr.removeClass('showing').hide();
		$('#title' + id).show().addClass('showing');
	} else {
		curr.removeClass('showing').fadeOut(500);
		$('#title' + id).fadeIn(750).addClass('showing');
	}
}

function swapHomeBugs() {
	var curr = $('#homeBugs a.showing');
	var id = parseInt(curr.attr('id').substr(3)) + 1;
	if (id > 3) id = 1;
	curr.removeClass('showing').fadeOut(500);
	$('#bug' + id).fadeIn(750).addClass('showing');
}

function setupProducts() {
	if (ie) {
		$('a', '#productsMain').each(function() {
			var $this = $(this);
			$this.css('background-position', '-' + $this.css('width') + ' 0');
			if (this.id == 'productsDig') {
				$this.data('background-image', $this.css('background-image'));
				$this.css('background-image', 'none');
			}
		});
		$('div.quickInfo', '#productsMain').css('opacity', 0);
		$('#productsMainGrey').hide();
	} else {
		$('div, a', '#productsMain').css('opacity', 0);
	}
	$('#productsMain').bind('greyout', function() {
		if (ie) {
			$('#productsMainGrey').show();
		} else {
			$('#productsMainGrey').stop().animate({'opacity': 1}, 500);
		}
	}).bind('showcolor', function() {
		if (ie) {
			$('#productsMainGrey').hide();
		} else {
			$('#productsMainGrey').stop().animate({'opacity': 0}, 500);
		}
	});
	if (ie) {
		$('#productsMain').mouseenter(function() {$(this).trigger('greyout');})
		.mouseleave(function() {$(this).trigger('showcolor');});
	}

	$('a', '#productsMain').mouseenter(function() {
		var $this = $(this);
		var $info = $('#' + this.id + 'Info');
		if (ie) {
			if (this.id == 'productsDig') {
				$this.css('background-image', $this.data('background-image'));
			}
			$this.css('background-position', '0 0');
		} else {
			$('#productsMain').trigger('greyout');
			$this.stop().animate({'opacity': 1}, 250);
		}
		if ($this.hasClass('leftInfo')) {
			$info.stop().animate({'background-position': '0 0', 'opacity': 1}, 500);
		} else if ($this.hasClass('rightInfo')) {
			$info.stop().animate({'background-position': '0 0', 'opacity': 1}, 500);
		} else if ($this.hasClass('leftUpInfo')) {
			$info.stop().animate({'top': $info.data('origTop') - 30, 'left': $info.data('origLeft') - 80, 'opacity': 1}, 500);
		} else if ($this.hasClass('rightUpInfo')) {
			$info.stop().animate({'top': $info.data('origTop') - 40, 'left': $info.data('origLeft') + 60, 'opacity': 1}, 500);
		}
	}).mouseleave(function() {
		var $this = $(this);
		var $info = $('#' + this.id + 'Info');
		if (!ie) {
			$this.stop().animate({'opacity': 0}, 500);
			$('#productsMain').trigger('showcolor');
		}
		if ($this.hasClass('leftInfo')) {
			$info.stop().animate({'background-position': $info.css('width') + ' 0', 'opacity': 0}, 250, function() {
				if (ie) {
					$this.css('background-position', '-' + $this.css('width') + ' 0');
				}
			});
		} else if ($this.hasClass('rightInfo')) {
			$info.stop().animate({'background-position': '-' + $info.css('width') + ' 0', 'opacity': 0}, 250, function() {
				if (ie) {
					$this.css('background-position', '-' + $this.css('width') + ' 0');
				}
			});
		} else if ($this.hasClass('leftUpInfo')) {
			$info.stop().animate({'top': $info.data('origTop'), 'left': $info.data('origLeft'), 'opacity': 0}, 250, function() {
				if (ie) {
					if ($this.attr('id') == 'productsDig') {
						$this.css('background-image', 'none');
					}
					$this.css('background-position', '-' + $this.css('width') + ' 0');
				}
			});
		} else if ($this.hasClass('rightUpInfo')) {
			$info.stop().animate({'top': $info.data('origTop'), 'left': $info.data('origLeft'), 'opacity': 0}, 250, function() {
				if (ie) {
					$this.css('background-position', '-' + $this.css('width') + ' 0');
				}
			});
		}
	});

	$('a.leftUpInfo, a.rightUpInfo', '#productsMain').each(function() {
		var $info = $('#' + this.id + 'Info');
		$info.data({'origTop': $info.position()['top'], 'origLeft': $info.position()['left']});
	});
}

function setupSingleProduct() {
	$('a', '#productsTable').mouseenter(function() {
		$(this).stop().animate({'opacity': 1}, 350);
	}).mouseleave(function() {
		if (!$(this).hasClass('current')) {
			$(this).stop().animate({'opacity': 0.5}, 350);
		}
	});
}

function setupMfmLive() {
	$('#mfmTitle2').hide();
	$('#mfmTitle1').data('showing', 'true');
	setInterval(function() {
		$('div.mfmTitle').each(function() {
			var $this = $(this);
			if ($this.data('showing')) {
				$this.fadeOut(300).data('showing', false);
			} else {
				$this.fadeIn(300).data('showing', true);
			}
		});
	}, 5000);
}

function resizeLayout() {
	if (ie7) {
		return;
	}
	var minHeight = 898;
	if ($('#main').hasClass('mfm')) {
		minHeight = 1025
	} else if ($('#main').hasClass('products')) {
		minHeight = 993;
	}
	if ($(window).height() > minHeight) {
		$('#content').css('height', $(window).height() - 235);
	} else {
		$('#content').css({height: 'auto'});
	}
}
function showPopup() {
	$('#shade').css('height', $('#main').height());
	if (ie) {
		$('#shade, #popup').show();
	} else {
		$('#shade, #popup').fadeIn(500);
	}
}


(function(c){function g(a){a=a.replace(/left|top/g,"0px");a=a.replace(/right|bottom/g,"100%");a=a.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");a=a.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);return[parseFloat(a[1],10),a[2],parseFloat(a[3],10),a[4]]}if(!document.defaultView||!document.defaultView.getComputedStyle){var d=jQuery.curCSS;jQuery.curCSS=function(a,b,e){if(b==="background-position")b="backgroundPosition";if(b!=="backgroundPosition"||!a.currentStyle||a.currentStyle[b])return d.apply(this,
arguments);var f=a.style;if(!e&&f&&f[b])return f[b];return d(a,"backgroundPositionX",e)+" "+d(a,"backgroundPositionY",e)}}var h=c.fn.animate;c.fn.animate=function(a){if("background-position"in a){a.backgroundPosition=a["background-position"];delete a["background-position"]}if("backgroundPosition"in a)a.backgroundPosition="("+a.backgroundPosition;return h.apply(this,arguments)};c.fx.step.backgroundPosition=function(a){if(!a.bgPosReady){var b=c.curCSS(a.elem,"backgroundPosition");b||(b="0px 0px");b=
g(b);a.start=[b[0],b[2]];b=g(a.options.curAnim.backgroundPosition);a.end=[b[0],b[2]];a.unit=[b[1],b[3]];a.bgPosReady=true}b=[];b[0]=(a.end[0]-a.start[0])*a.pos+a.start[0]+a.unit[0];b[1]=(a.end[1]-a.start[1])*a.pos+a.start[1]+a.unit[1];a.elem.style.backgroundPosition=b[0]+" "+b[1]}})(jQuery);
