/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 
 * javascript method: "pxToEm"
 * by:
   Scott Jehl (scott@filamentgroup.com) 
   Maggie Wachs (maggie@filamentgroup.com)
   http://www.filamentgroup.com
 *
 * Copyright (c) 2008 Filament Group
 * Dual licensed under the MIT (filamentgroup.com/examples/mit-license.txt) and GPL (filamentgroup.com/examples/gpl-license.txt) licenses.
--------------------------------------------------------------------*/

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().css({'min-height': 0}); 
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(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 ($.browser.msie) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};

$.fn.equalHeightsAlt = function(px) {
	$(this).each(function(){
		var currentTallestAlt = 0;
		$(this).children().children().children(".boxTopLeft").css({'min-height': 0}); 
		$(this).children().children().children(".boxTopLeft").each(function(i){
			if ($(this).height() > currentTallestAlt) { currentTallestAlt = $(this).height(); }
		});
		if (!px || !Number.prototype.pxToEm) currentTallestAlt = currentTallestAlt.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie) { $(this).children().children().children(".boxTopLeft").css({'height': currentTallestAlt}); }
		$(this).children().children().children(".boxTopLeft").css({'min-height': currentTallestAlt}); 
	});
	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;
};

$.fn.roundThemCorners = function() {
	$(this).each(function(){
		var whatLivedHere = $(this).html();
		$(this).replaceWith("<div class=\"boxWrapper\"><div class=\"boxTop clearfix\"><div class=\"boxTopLeft\">" + whatLivedHere + "</div><div class=\"boxTopRight\"></div></div><div class=\"boxBottom clearfix\"><div class=\"boxBottomLeft\"></div><div class=\"boxBottomRight\"></div></div></div>");
	});
};

$.fn.roundImage = function() {
	$(this).each(function() {
		var imageSrc = $(this).attr("src");
		var imageAlt = $(this).attr("alt");
		var imageHigh = $(this).height();
		var imageWide = $(this).width();
		$(this).replaceWith("<div class=\"roundImage\"><img src=\"" + imageSrc + "\" alt=\"" + imageAlt + "\" /><div class=\"top clearfix\"><div class=\"tl\"></div><div class=\"tr\"></div></div><div class=\"bottom clearfix\"><div class=\"bl\"></div><div class=\"br\"></div></div></div>");
		$("div.roundImage > div > div").css({"height" : imageHigh + "px", "width" : imageWide + "px"});
	});
}

$.fn.bigRoundImage = function() {
	$(this).each(function() {
		var bigImageSrc = $(this).attr("src");
		var bigImageAlt = $(this).attr("alt");
		var bigImageHigh = $(this).height();
		var bigImageWide = $(this).width();
		$(this).replaceWith("<div class=\"bigRoundImage\"><img src=\"" + bigImageSrc + "\" alt=\"" + bigImageAlt + "\" /><div class=\"top clearfix\"><div class=\"tl\"></div><div class=\"tr\"></div></div><div class=\"bottom clearfix\"><div class=\"bl\"></div><div class=\"br\"></div></div></div>");
		$("div.bigRoundImage > div > div").css({"height" : bigImageHigh + "px", "width" : bigImageWide + "px"});
	});
}

$.fn.smallRoundImage = function() {
	$(this).each(function() {
		var smallImageSrc = $(this).attr("src");
		var smallImageAlt = $(this).attr("alt");
		var smallImageHigh = $(this).height();
		var smallImageWide = $(this).width();
		$(this).replaceWith("<div class=\"smallRoundImage\"><img src=\"" + smallImageSrc + "\" alt=\"" + smallImageAlt + "\" /><div class=\"top clearfix\"><div class=\"tl\"></div><div class=\"tr\"></div></div><div class=\"bottom clearfix\"><div class=\"bl\"></div><div class=\"br\"></div></div></div>");
		$("div.smallRoundImage > div > div").css({"height" : smallImageHigh + "px", "width" : smallImageWide + "px"});
	});
}

$.fn.myHeights = function() {
	$(this).each(function() {
		var boxLeftHigh = $(this).children(".boxTopLeft").height();
		var boxRightHigh = $(this).children(".boxTopRight").height();
		if (boxLeftHigh >= boxRightHigh) {
			$(this).children(".boxTopRight").height(boxLeftHigh);
		} else {
			$(this).children(".boxTopLeft").height(boxRightHigh);
		}
	});
}

// Fire up jQuery
$(document).ready(function(){

	// Set up utility nav
	$("#header li:last-child").addClass("last");
	$("#footer li:last-child").addClass("last");
	$("#footer li:eq(2)").addClass("last");
	$("#footer li:eq(3)").addClass("clear");
	
	// Set up the rounded boxes
	$(window).bind("load", function() {
		$(".box").roundThemCorners();
		$(".boxTopLeft").children("p:last-child").addClass("last");
	});
	
	// Fire roundImage Functions
	$(window).bind("load", function() {
		$(".roundImage").roundImage();
		$(".bigRoundImage").bigRoundImage();
		$(".smallRoundImage").smallRoundImage();
	});
	
	//Fire last classes
	$(".rightBox div").children(":last").addClass("last");
	
	//Fire Equal Height Function
	$(window).bind("load", function() {
		$(".boxes").equalHeightsAlt();
		$(".boxTop").myHeights();
	});
	
	//Open external links in a new window (yuck, oh well)
	$("a[href*='http://']:not([href*='"+location.hostname+"'])").attr("target","_blank");
	
	$("#quotes").cycle({
		speed: 1000,
		timeout: 8000  
	});
	
	$("#photos:first-child").addClass("first");
	
	$(window).load(function(){
		$("#photos").cycle({
			delay: -4000,
			speed: 1000,
			timeout: 8000
		});
	});
	
});