$(document).ready(function() {
	$('.profile img').each(function() {
		var image = new Image();
		image.src = $(this).attr('src');
	});
	
  // Disabling SIFR for now
	//genSIFR();
	
	$('.profile + div').css('margin-left', function() {
		return $(this).siblings('.profile').width();
	});
	
	if($.browser.msie) {
		$('table tr:odd').css('background-color', '#f8fbfb');
		$('table tr:even').css('background-color', '#e8f4f2');
	}
	
	if(!($.browser.msie && $.browser.version < 7.0)) {
		$('.form form .details').hide();
		$('.form form .chk input[type="checkbox"]').click(function() {
			var parent = $(this).parent();
			var cls = $(this).attr('id').replace('serv-chk-', '.');
			var details = $(parent).siblings(cls);
			
			if($(this).is(':checked')) {
				$(details).show();
				$(parent).find('.nobold').hide();
			} else {
				$(details).hide();
				$(parent).find('.nobold').show();
			}
		});
	}
});

$(window).load(function() {
	if($.browser.webkit || ($.browser.msie)) {
		setTimeout("setHeights()", 50); //window.onload needs to fire after the flash is loaded.
	} else {
		setHeights();
	}
});

function genSIFR() {
	$('.content #content h2').sIFR(calibri_bold).css({fontSize: '32.5px'}); //IE.
	$('.content #content #third-nav-wrapper + h3').css('max-width', function() {
		var width = $(this).parent().width();
		var wrap = $(this).siblings('#third-nav-wrapper').outerWidth(true);
		return width - wrap;
	});
	if(!($.browser.msie && $.browser.version < 7.0))
		$('.content #content h3').css('letter-spacing', '1px').sIFR(helvetica, "", {offsetTop: 2});
	$('.home h3').sIFR(calibri_bold, "", {tuneHeight: -5}).css('font-size', '18.8px');
	$('.home #snippets .date').css('line-height', function(index, value) {
		$(this).siblings('h3').css('line-height');
	}).sIFR(calibri, "", {tuneHeight: -2}).css('font-size', '12px');
	$('.content #content div.highlight h4').sIFR(calibri, "", {tuneHeight: -9});
	$('.content #content h4').sIFR(helvetica_light, "", {tuneHeight: -8});
	$('.content #content h5').sIFR(calibri_bold, "", {tuneHeight: -8});
	$('.content #sidebar h3').sIFR(helvetica_bold, "", {offsetTop: -2, tuneHeight: -8});
	$('.content #sidebar h4').sIFR(helvetica_light, "", {tuneHeight: -7});
	$('.content #sidebar h2').css({fontSize: '1.39em', letterSpacing: '-1px'}).sIFR(baskerville, "", {offsetTop: 4}); /**/
}


function setHeights() {
	$.fn.maxHeight = function() {
		//TODO: Work out how to pass functions into functions.
		var value = $(this).siblings('#content').height();
		var attr;
		if ($.browser.msie && $.browser.version < 7.0) {
			attr = 'height';
		} else {
			var attr = 'max-height';
		}
		$(this).css(attr, value);
		return this;
	}
	
	/** sIFR is screwing up margins. Add any sIFRised elements here **/
	$('#words').equalHeights(true);
	$('.home #main #blog').maxHeight();
	$('.content #main').equalHeights(true);
	$('.content #content #content-inner').height(function(){
		return $(this).parent().height() - ($(this).innerHeight() - $(this).height());
	});
	if($.browser.msie && $.browser.version < 7.0) {
		$('.dotted-border-top :first-child').css('padding-top', '0.5em');
		$('.dotted-border-top :first-child object').css('margin-top', '0.5em');
	}
}

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).outerHeight(true) > currentTallest) { currentTallest = $(this).outerHeight(true); }
		});
		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 ($.browser.msie && $.browser.version == 6.0) {
			$(this).children().each(function(i) {
				$(this).css('height', currentTallest - $(this).outerHeight(true) + $(this).height());
			});
		}
		$(this).children().each(function(i) {
			$(this).css('min-height', currentTallest - $(this).outerHeight(true) + $(this).height());
		});
	});
	return this;
};

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' && $.browser.msie && (parseFloat($('body').css('font-size')) / getWindowWidth()).toFixed(1) > 0.0) {
		var calcFontSize = function(){		
			return (parseFloat($('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;
};
