/**
 * @author		Angelo Dini
 * @copyright	http://www.divio.ch
 */

// check if console.log is available
if(window['console'] === undefined) window.console = { log: function(){} };

// allow jQuery to chain .log
if('jQuery' in window) jQuery.fn.log = function(msg) { console.log("%s: %o", msg, this); return this; };

// set namespace
var BASE = {};

/**
 * CUSTOM MODULES
 * ##################################################|
 */
jQuery(document).ready(function ($) {
/* private scope - do it the moo way */

	BASE = {
		init: function() {
			// ie6 fixes
			$.fn.fix_pseudos = function(options) {
				this.filter(":first-child").addClass("first-child");
				this.filter(":last-child").addClass("last-child");
			};
		}
	};
	BASE.init();

/**
 * HELPER MODULES
 * ##################################################|
 */
	/**
	 * Target modifier
	 * @version: 0.3
	 * @param: property (target:blank)
	 * @example: <a href="#" rel="target:blank">Lorem Ipsum</a>
	 */
	$.fn.defineTarget = function (options) {
		var options = $.extend({ property: 'rel' }, options);
		return this.each(function () {
			$(this).attr('target', '_' + $(this).attr(options.property).split(':')[1]);
		});
	};
	$('a[rel*="target:"]').defineTarget();
	$('a[class*="target:"]').defineTarget({ property: 'class' });

	/**
	 * E-Mail encrypter
	 * @version: 0.2
	 * @param: autoConvert (converts innerhtml to match the email address)
	 * @example: <a href="#info" rel="divio.ch" class="mailcrypte">E-Mail</a>
	 */
	$.fn.mailcrypter = function (options) {
		var options = $.extend({ autoConvert: true }, options);
		var mailto = 'mailto:' + this.attr('href').replace('#', '') + '@' + this.attr('rel');

		this.attr('href', mailto);
		if(options.autoConvert) this.html(mailto.replace('mailto:', ''));

		// keep chaining
		return this;
	};
	if($('a.mailcrypte').length) $('a.mailcrypte').mailcrypter({ autoConvert: false });

	/**
	 * Pop-Up Generator
	 * @version: 0.2
	 * @param: width (initial width)
	 * @param: height (initial height)
	 * @example: <a href="http://www.google.ch" class="popup">Open Pop-Up</a>
	 */
	$.fn.autopopup = function (options) {
		var options = $.extend({ width: 750, height: 500}, options);
		// set vars
		var url = this.attr('href');
		var size = { 'x': options.width, 'y': options.height };

		// attach event
		return this.bind('click', function (e) {
			e.preventDefault();
			window.open(url, '_blank', 'width=' + size.x + ',height=' + size.y + ',status=yes,scrollbars=yes,resizable=yes');
		});
	};
	$('.popup').autopopup();

	/**
	 * Auto input fill-in
	 * @version: 0.5
	 * @param: label (if true than labeltext on parent else rel attribut on this)
	 * @param: strip (replacement text)
	 * @param: add (add additional information)
	 */
	$.fn.fieldLabel = function (options) {
		//var $this = this;
		var options = $.extend({
			label: true,
			strip: '',
			add: ''
		}, options);

		// store label element and use replacement
		var label = (options.label == true) ? $(this).parent().find('label').text() : label = $(this).attr('rel');
		label = label.replace(options.strip, '');
		label += options.add;

		// initialize
		if(this.attr('value') == '') this.attr('value', label);

		// attach event
		this.bind('click blur', function (e) {
			($(this).attr('value') == label) ? hide($(this), e) : show($(this), e);
		})

		// show functionality
		function show(el, e) {
			if(el.attr('value') != '') return false;
			el.attr('value', label);
			
			log(label);
		};

		// hide functionality
		function hide(el, e) {
			if(e.type == 'blur' && el.attr('value') == label) return false;
			el.attr('value', '');
		};

		// keep chaining
		return this;
	};
	//$('.fieldLabel').fieldLabel({ strip: ': ', add: '...' });
	
	/**
	 * Reverse a jquery collection (last first)
	 * @version: 0.1
	 * @example: $("div").reverse().each(function(){ //reverse order each() });
	 */
	$.fn.reverse = [].reverse;
	
	/**
	 * Computes the maximal height of an array of elements
	 * @version: 0.1
	 * @example: var max = $("#someList li").maxHeight();
	 */
	$.fn.maxHeight = function(){
		var max = 0;
		$(this).each(function(){
			var height = $(this).height();
			max = height>max? height : max;
		});
		return max;
	}
	
	/**
	 * Equal Height Columns
	 * @version: 0.1
	 * @example: $(element).equalHeight();
	 */
	$.fn.equalHeight = function(px) {
		$(this).each(function(){
			var currentTallest = 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 && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
			$(this).children().css({'min-height': currentTallest});
		});
		return this;
	};
	
});
//(function ($) { when js on top })(jQuery);
