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

	BASE.mainnav = {
		init: function () {
			this.container = $('#mainnav');
			this.items = this.container.find('> li a');
			//this.items.bind('mouseenter', $.proxy(BASE.mainnav, 'toggle'));
			this.calcWidth();
			this.elements = this.container.find('> li');
			this.elements.each($.proxy(BASE.mainnav, 'process'));
			this.animation = false;
			
		},
		process: function (index, item) {
			var item = $(item);
			var list = item.find('ul');
			
			if(item.hasClass('active')) {
			
				list.css({
					'width': 0,
					'height': 25,
					'padding': 0,
					'overflow': 'hidden',
					'display': 'block'
				});
				// setup animation
				list.css({'padding': '9px 5px 0 5px', 'display': 'block'}).css('display', 'none');

				list.animate({
					'width': (list.data('width')+10)
				}, 1000, 'linear', $.proxy(BASE.mainnav, 'toggleAnimation')).css('display', 'none');
			}
		},
		calcWidth: function () {
			this.container.find('ul').each(function (index, item) {
				var width = $(item).css('display', 'block').css('width', 'auto').width();
				$(item).data('width', width).css('display', 'none');
			});
		},
		toggle: function (obj) {
			var item = $(obj.currentTarget).parent().find('ul');
			
			// temporary stop
			if(this.animation) return false;

			// check lenght and display settings
			if(!item.length || item.css('display') == 'block') return false;
			
			// reset all
			var items = this.items.parent().find('ul');
				items.css({
					'width': 0,
					'height': 25,
					'padding': 0,
					'overflow': 'hidden',
					'display': 'none'
				});

			// setup animation
			item.css({'padding': '9px 5px 0 5px', 'display': 'block'}).css('display', 'none');
			item.animate({
				'width': item.data('width')+10,
				'display': 'toggle'
			}, 400, 'linear', $.proxy(BASE.mainnav, 'toggleAnimation')).css('display', 'none');
			console.log('animated');

			// toggle animation state
			this.toggleAnimation();
		},
		toggleAnimation: function () {
			this.animation = (this.animation == false) ? true : false;
		}
	};
	BASE.mainnav.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.6
	 * @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 options = $.extend({
			label: true,
			strip: '',
			add: ''
		}, options);

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

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

		return this.each(function () {
			// 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', function (e) {
				if($(this).attr('value') == label) hide($(this), e, label)
			})
			$(this).bind('blur', function (e) {
				($(this).attr('value') == label) ? hide($(this), e, label) : show($(this), e, label);
			})
		})
		
	};
	
	$('#newsletter_email').fieldLabel({ strip: ': ' });
	//$('.fieldLabel').fieldLabel({ strip: ': ', add: '...' });
	
	$.easing.easeOutQuart = function (x, t, b, c, d) {
	    return -c * ((t=t/d-1)*t*t*t - 1) + b;
	};
	
	$.fn.duplicate = function(count, cEvents) {
		var tmp = [];
		for (var i=0; i<count; i++){
			$.merge(tmp, this.clone(cEvents).get());
		}
		return this.pushStack(tmp);
	};
	
});
//(function ($) { when js on top })(jQuery);
