﻿(function($j){

	Number.prototype.pxToEm = String.prototype.pxToEm = function(settings){
		//set defaults
		settings = jQuery.extend({
			scope: 'body',
			reverse: false
		}, settings);

		var pxVal = (this == '') ? 0 : parseFloat(this);
		var scopeVal;
		var getWindowWidth = function(){
			var de = document.documentElement;
			return self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
		};	

		/* When a percentage-based font-size is set on the body, IE returns that percent of the window width as the font-size. 
			For example, if the body font-size is 62.5% and the window width is 1000px, IE will return 625px as the font-size. 	
			When this happens, we calculate the correct body font-size (%) and multiply it by 16 (the standard browser font size) 
			to get an accurate em value. */

		if (settings.scope == 'body' && $j.browser.msie && (parseFloat($j('body').css('font-size')) / getWindowWidth()).toFixed(1) > 0.0) {
			var calcFontSize = function(){		
				return (parseFloat($j('body').css('font-size'))/getWindowWidth()).toFixed(3) * 16;
			};
			scopeVal = calcFontSize();
		}
		else { scopeVal = parseFloat(jQuery(settings.scope).css("font-size")); };

		var result = (settings.reverse == true) ? (pxVal * scopeVal).toFixed(2) + 'px' : (pxVal / scopeVal).toFixed(2) + 'em';
		return result;
	};


	$j.fn.equalHeights = function(px) {
		$(this).each(function(){
			var currentTallest = 0;
			$j(this).children().each(function(i){
				if ($j(this).height() > currentTallest) { currentTallest = $j(this).height(); }
			});
			if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
			// for ie6, set height since min-height isn't supported
			if ($j.browser.msie && $j.browser.version == 6.0) { $j(this).children().css({'height': currentTallest}); }
			$j(this).children().css({'min-height': currentTallest}); 
		});
		return this;
	};


	Code.registerNamespace('Website.Pages');

	Website.Pages.mc = {	
		
		onReady: function(){
			var self = this;
			// add event to each tab link that adds class the main promo - css handles the showing and hiding of correct bg and cta link
			$j("ul.promo-tabs li a").each(function(){
				$j(this).click(function(event) {
					var self = this;
					$j(".mainpromo").removeClass().addClass("mainpromo").addClass($j(self).attr("class").slice(0,-2));
					// alert("hello");
					event.preventDefault();
				});
			});
			
			$j("li:first-child").addClass("first");
			$j("li:last-child").addClass("last");

			$j("p:empty").remove();
			
			if($j('body').hasClass("home")){
				$j('ol.MCS2ProductResults').equalHeights(); 
			}
			
		}
		
	};

	$j().ready(function(){
		Website.Pages.mc.onReady();
	});


})(jQuery);