/**
 * Zentropy javascript core
 *
 * - Provides frequently used extensions to base javascript objects
 * - jQuery browser detection tweak
 * - Define functions used in events
 */

// Add String.trim() method
String.prototype.trim = function(){
	return this.replace(/\s+$/, '').replace(/^\s+/, '');
}

// Add Array.indexOf() method
if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function (obj, fromIndex) {
    if (fromIndex == null) {
      fromIndex = 0;
    } else if (fromIndex < 0) {
      fromIndex = Math.max(0, this.length + fromIndex);
    }
    for (var i = fromIndex, j = this.length; i < j; i++) {
      if (this[i] === obj){
        return i;
      }
    }
    return -1;
  };
}

// jQuery Browser Detect Tweak For IE7
jQuery.browser.version = jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 && window["XMLHttpRequest"] ? "7.0" : jQuery.browser.version;

// Console.log wrapper to avoid errors when firebug is not present
// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function() {
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if (this.console) {
    console.log(Array.prototype.slice.call(arguments));
  }
};

// init object
var Zentropy = Zentropy || {};

/**
 * Image handling functions
 */
Zentropy.image = { _cache : [] };

// preload images
Zentropy.image.preload = function() {
  for (var i = arguments.length; i--;) {
    var cacheImage = document.createElement('img');
    cacheImage.src = arguments[i];
    Zentropy.image._cache.push(cacheImage);
  }
}
;
function checkNarrow(){
	var size = {
		sevenfive: 750,
		eight: 800,
		nine: 900
	};
	jQuery.each(size, function(cls, size) {
		if (jQuery(window).width() <= size) {
			jQuery('html').addClass(cls);
		} else {
			jQuery('html').removeClass(cls);
		}
	});
}

jQuery(window).resize(checkNarrow);
jQuery(window).ready(checkNarrow);
;
jQuery(document).ready(function() {
	var $ = jQuery;
	var nav = $('#main-menu, .menu');
	var menuItems = $('li', nav);
	menuItems.each(function() {
		var item = $(this);
		$('a', this).click(function(obj){
			menuItems.each(function(){
				$(this).removeClass('active').removeClass('active-trail');
				$('a', this).removeClass('active');
			});
			item.addClass('active').addClass('active-trail');
			$(this).addClass('active');
		});
	});
});

;
(function($) {
	$.fn.toEm = function(settings){
		settings = jQuery.extend({
			scope: 'body'
		}, settings);
		var that = parseFloat(this[0],10);
		var teststr = '&nbsp;';
		for (var i=1; i<=99; i++) {
			teststr += '<br/>&nbsp;';
		}
		scopeTest = jQuery('<div style="display: none; font-size: 1em; margin: 0; padding:0; height: auto; line-height: 1; border:0;">' + teststr + '</div>').appendTo(settings.scope),
		scopeVal = scopeTest.height() / 100.0;
		scopeTest.remove();
		return (that / scopeVal).toFixed(8) + 'em';
	};

	$.fn.equalHeights = function(container) {
		tallest = 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		
		tallest = $(tallest).toEm({'scope' : container});	

		return this.each(function() {
			$(this).css("min-height", tallest).css("overflow","visible");
		});
	}

	$('.cols').each(function() {
		$('.fourcoll', this).equalHeights(this);
	});
})(jQuery);;

