
// remap jQuery to $
(function($){

	$.fn.equalizeHeights = function() {
		var opt = {};
		var tallestHeight, tallest;

		// extend our default options with anything passed in by user
		$.extend(opt, $.fn.equalizeHeights.defaults, arguments[0] || {});

		this.each(function(){

			// our extended element
			var el = $(this);

			if(!tallestHeight) {
				tallestHeight = el.outerHeight(opt.includeMargin);
				tallest = el;
			} else{ 
				if((el.outerHeight(opt.includeMargin) > tallestHeight)) {
					tallestHeight = el.outerHeight(opt.includeMargin);
					tallest = el;
				}

			}

		});

		return this.each(function(){
			var height = $(this).height();
			var outerHeight = $(this).outerHeight(opt.includeMargin);

			if(tallest.get()[0] != this) {
				var finalHeight = height + (tallestHeight - outerHeight) + opt.offset;
				if(!opt.setChildren) {
						$(this).height(finalHeight);

				} else {
					var delta = finalHeight - $(this).outerHeight(opt.includeMargin);
					var child = $(this).find(opt.target);
					child.height(child.height() + delta + opt.offset);
				}
			}
		});

	};
	
	// Default options for the plugin.
	$.fn.equalizeHeights.defaults = {
		includeMargin: false,
		setChildren: false,
		target: ">:last-child",
		offset: 0
	};

	$.humjay = $.humjay || {};
	$.humjay.squaredi = {
		overlay: false,
		conf: {
			icon: false
		}
	};

	function Squaredi(el, conf) {
		el = $(el);

		if(conf.icon) {
			el.append('<span class="icon"/>');
		}

		el.click(function(e){
			if(!$.humjay.squaredi.overlay) {
				$("<div/>").attr("id", "humjay-overlay").text("Click Anywhere to Close").prepend('<div class="magnified-content"/>').appendTo(document.body);
				$.humjay.squaredi.overlay = $("#humjay-overlay").overlay({
					mask: {
						color: '#000000',
						loadSpeed: 200,
						opacity: 0.9
					},
					top: '30%'
				}).data("overlay");
			}
			$('#humjay-overlay').children('.magnified-content').text((conf.attr) ? el.attr(conf.attr) : el.text());
			$.humjay.squaredi.overlay.load();
			e.preventDefault();
		});

	}

	// jQuery plugin implementation
	$.fn.squaredi = function(conf) { 
		this.each(function(i) {		

			var config = $.extend({}, $.humjay.squaredi.conf, conf); 
			if($(this).hasClass('icon')) config.icon = true;
			var el = new Squaredi($(this), config);
			$(this).data("squaredi", el);	
		});

		return this;

	};



			/*
			 * @fileoverview JQuery plugin to add HTML5 placeholder
			 * attribute to all browsers
			 *
			 * @author humjay jason@humjay.com
			 * @version 0.1
			 */


				// This create an observer pattern with forms that allow our new fields to listen to submits.
				function Broadcaster(el, evts) {
					var subscribers = [];
					var self = this;

					$(el).bind(evts, function(e){
						self.notify(e.type)
					});

					this.notify = function(evt) {
						$.each(subscribers, function(i, el){
							el.notifiedOf(evt);
						});
						return this;
					};

					this.addSubscriber = function(sub) {
						var exists = false;
						$.each(subscribers, function(i, el){
							if(sub === el) exists = true;
						});

						if(!exists) subscribers.push(sub);
					}
				}

				$.humjay = $.humjay || {};
				$.humjay.placeholder = {
					conf: {
						placeholderAttribute: 'placeholder'
					}
				}
				function Placeholder(el, conf) {
					el = $(el);

					// check if this is a correct element
					if($.inArray(el.attr('tagName').toUpperCase(), "INPUT TEXTAREA".split(' ')) === -1) { return false; }

					// grab what we need and store in private properties
					var self	= this;
					var placeholder = el.attr($.humjay.placeholder.conf.placeholderAttribute);

					// populate the placeholder text
					el.val(placeholder);

					// assign handlers
					el
					.focus(function(){
						if(el.val() === placeholder) el.val("");
					})
					.blur(function(e){
						if(el.val() === "") el.val(placeholder);
					});


					this.clearIfDefault = function() {
						if(el.val() === placeholder) el.val("");
					};

					this.notifiedOf = function(evt) {
						if(evt === "submit") this.clearIfDefault();
					}

				};

				$.fn.broadcaster = function(evts) {
					this.each(function(i) {
						var el = new Broadcaster($(this), evts);
						$(this).data("broadcaster", el);
					});
				}

				// jQuery plugin implementation
				$.fn.placeholder = function(conf) { 

					if(!('placeholder' in document.createElement('input'))) {

						conf = $.extend({}, $.humjay.placeholder.conf, conf); 

						this.each(function() {		
							if($(this).attr('placeholder')) {

								var el = new Placeholder($(this), conf);
								$(this).data("placeholder", el);

								var owner = $(this).parents("form").last()

								if(!owner.data("broadcaster")) owner.broadcaster("submit");

								owner.data("broadcaster").addSubscriber(el);
							}

						});
					}

					return this;

				};





 



})(window.jQuery);



// 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) );
  }
};



// catch all document.write() calls
(function(doc){
  var write = doc.write;
  doc.write = function(q){ 
    log('document.write(): ',arguments); 
    if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
  };
})(document);



