/**
 * @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: '...' });

	/**
	 * Language Dropdown Generator
	 * @version: 0.2
	 * @param: fx (transition effect: fade, slide or toggle)
	 * @param: element (replacing anchor element - span required)
	 * @param: elementClass (replacing anchor element class for grabbing)
	 * @param: originClass (additional Class to mainnav)
	 * @param: activeClass (mainnav active element class)
	 * @param: conainerClass (wrapping element class)
	 */
	$.fn.droplet = function (options) {
		var options = $.extend({
			fx: 'fade',
			element: '<div class="droplet-item"><span></span></div>',
			elementClass: 'droplet-item',
			originClass: 'droplet-nav',
			activeClass: 'active',
			containerClass: 'droplet-container'
		}, options);

		// prepare container
		var container = $(this);
			container.hide()
				.addClass(options.originClass)
				.wrap('<div class="' + options.containerClass + '"></div>')
				.before($(options.element))
				.parent().find('.' + options.elementClass + ' span').html($(this).find('li.' + options.activeClass).text());

		// add events
		container.parent().bind('mouseenter mouseleave', function (e) {
			e.preventDefault();
			if(options.fx == 'toggle') container.toggle();
			if(options.fx == 'slide') {
				if(e.type == 'mouseenter') container.stop().slideDown();
				if(e.type == 'mouseleave') container.stop().css('height', 'auto').slideUp();
			}
			if(options.fx == 'fade') {
				if(e.type == 'mouseenter') container.stop().fadeIn();
				if(e.type == 'mouseleave') container.stop().css('opacity', 1).fadeOut();
			}
		}).end()
		// remove selected item
		.find('li').each(function () {
			if($(this).hasClass('active')) $(this).remove();
		});
	};
	$('#langnav').droplet({fx: 'fade'});
});
//(function ($) { when js on top })(jQuery);
