/**
 * @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");
			};

			// scroll functionality
			$('#mainnav li a').click(function() {
				if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
					&& location.hostname == this.hostname) {

					var $target = $(this.hash);
					$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
					if ($target.length) {
						var targetOffset = $target.offset().top;
						$('html,body').animate({scrollTop: targetOffset - ($('.mainnavholder').height()-1)}, 900);
						return false;
					}
				}
			});

			// calc mainnav length
			var mainnavWidth = 0;
			$('ul#mainnav').find('li').each(function () {
				mainnavWidth += $(this).outerWidth(true);
			});
			$('ul#mainnav').css('width', (mainnavWidth+25))
		}
	};
	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: '...' });
});
//(function ($) { when js on top })(jQuery);
