/*
function m3FormHandler(forms) {
	jQuery.each(forms, function() {
		
		var text_inputs = $(this.form).find(".text_input");
		var handleDefaults = true;
		
		if (this.handleDefaults == false) {
			handleDefaults = false;
		}
		
		if (handleDefaults) {
			$(text_inputs).each(function() {
				if ($(this).val() == $(this).attr("defaultValue")) {
					$(this).addClass("default_value");
				}
				
				$(this).bind("focus blur", handleDefaultValues);
			});
		}
		
		$(this.form).submit(function (e) {
			e.preventDefault();
			
			$(text_inputs).each(function() {
				if ($(this).val() == this.defaultValue) {
					$(this).val("");
				}
				
				if ($(this).hasClass("default_value")) {
					$(this).removeClass("default_value");
				}
			});
		});
		
		if (this.validate_options) {
			$(this.form).validate({
				onfocusout: false,
				rules: this.validate_options.rules,
				messages: this.validate_options.messages,
				invalidHandler: function(form, validator) {
					if (handleDefaults) {
						$(".text_input").unbind("focus blur", handleDefaultValues)
					}
				},
				submitHandler: function(form) {
					m3Log("-- ready to submit");
				}
			});
		}
		
		function handleDefaultValues (e) {
			if (e.type == "focus") {
				if (this.value === this.defaultValue) {
					$(this).selectRange(0, this.value.length);
					//this.value = "";
					$(this).removeClass("default_value");
				}
			}
			if (e.type == "blur") {
				if (this.value == "") {
					this.value = this.defaultValue;
					$(this).addClass("default_value");
				}
				else if (this.value === this.defaultValue) {
					$(this).addClass("default_value");
				}
			}
		}
	});
}
*/

function m3Columnize(uls, num_columns) {
	
	$(uls).each(function(){	
		var num_lis		= $(this).find("li").length;
		var num_rows    = Math.ceil( num_lis / num_columns);			
		var col_tmp     = 1;
		var col_height  = 0;
		var max_height  = 0;
		
		if ( num_lis < num_columns) {
			return;
		}
		/*
		var cols = array();
		cols[0] = 0;
		for( var i = 0; i < num_columns; i++ ) {
			if( i == 0 )
				continue;
			
			cols[i] = num_rows * i + 1;
		}
		*/
		$(this).find("li").each(function(iterator){
			var col_current = Math.ceil((iterator+1)/num_rows);
			
			$(this).addClass("col_" + col_current);
			
			if ((iterator+1) % num_rows == 1) {
				$(this).addClass("first_row");
			}
			
			$(this).addClass("col_" + col_current);	
			
			if (col_current > col_tmp) {
				col_tmp = col_current;
				$(this).css("margin-top", -(col_height));
				
				if (col_height > max_height) {
					max_height = col_height;
				}
				
				col_height = 0;
			}
			
			col_height += $(this).outerHeight();
		});
		
		$(this).css("height", max_height);
	});
}

function m3SmoothScroll(anchor, duration) {

	
	$(anchor).click(function(e) {
		
		e.preventDefault();
		$.scrollTo("#" + this.href.split("#")[1], duration);
	});
}

function m3Log(message) {
	if (window.console && window.console.log) {
		window.console.log(message);
	}
}
