/**
 * @framework	CFF - Classy Frontend Framework
 * @author		Angelo Dini
 * @copyright	http://www.divio.ch under the BSD Licence
 * @requires	Classy, jQuery
 *
 * check if classy.js exists */
 if(window['Class'] === undefined) log('classy.js is required!');

/*##################################################|*/
/* #CUSTOM APP# */
(function ($, Class) {
	/**
	 * Browser helpers
	 * @version: 0.1.1
	 */
	Cl.Browser = Class.$extend({
		initialize: function () {
			// initiate ie6 fixes
			if(!window.XMLHttpRequest) this.ie6();
			if(navigator.userAgent.match(/Mobile/i)) this.mobile();
		},
		ie6: function () {
			// add pseudo fixes
			$.fn.fix_pseudos = function(options) {
				this.filter(":first-child").addClass("first-child");
				this.filter(":last-child").addClass("last-child");
			};
		},
		mobile: function () {
			// pan to the bottom, hides the location bar
			setTimeout(function () {
				window.scrollTo(0, 1);
			}, 100);

			// attach hover events
			$('a').bind('touchstart', function () { $(this).addClass('hover'); });
			$('a').bind('touchend', function () { $(this).removeClass('hover'); });
		}
	});
	// autoinit
	Cl.Browser = new Cl.Browser();
	
	/**
	 * Base initial class
	 */
	Cl.Base = Class.$extend({
		initialize: function () {
			//log('hello world');
		}
	});
	// autoinit
	Cl.Base = new Cl.Base();
// prevent conflicts
})(jQuery, Class);

/**
 * JQUERY CUSTOM PLUGINS
 * ##################################################|
 */
(function ($) {
	/**
	 * Target modifier
	 * @version: 0.3.0
	 * @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 encrypte
	 * @version: 0.3.1
	 * @param: autoConvert (converts innerhtml to match the email address)
	 * @example: <a href="#info" rel="divio.ch" class="mailcrypte">E-Mail</a>
	 */
	$.fn.mailCrypte = function (options) {
		var options = $.extend({
			autoConvert: true
		}, options);

		return this.each(function () {
			var mailto = 'mailto:' + $(this).attr('href').replace('#', '') + '@' + $(this).attr('rel');
			$(this).attr('href', mailto);
			if(options.autoConvert) $(this).html(mailto.replace('mailto:', ''));
		});
	};
	$('a.mailcrypte').mailCrypte({ autoConvert: false });

	/**
	 * Pop-Up Generator
	 * @version: 0.2.1
	 * @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);
		var size = { 'x': options.width, 'y': options.height };
		
		return this.each(function () {
			var url = $(this).attr('href');
			// attach event
			$(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();
	
	/**
	 * Horizontal dropdown menu
	 * @version: 0.2.0
	 * @param: fx (transition effect: fade, slide or toggle)
	 * @example: $(element).dropdown();
	 */
	$.fn.dropDownMenu = function (options) {
		var $this = this;
		var timer;
		var options = $.extend({
			fx: 'toggle',
			delay: 0
		}, options);

		// autohide subtree
		$this.find('ul').hide();

		// mouseenter event
		$this.find('li').bind('mouseenter', function (e) {
			if($(this).find('ul').length) show($(this), $(this).find('ul'));
		});

		// mouseleave event
		$this.find('li').bind('mouseleave', function (e) {
			var el = $(this);
			if(el.find('ul').length) {
				timer = setTimeout(function () {
					hide(el, el.find('ul'));
					el.removeClass('hover');
				}, options.delay);
				el.find('ul').bind('mouseenter', function () {
					clearTimeout(timer);
				});
			}
		});

		// show menu
		function show(parent, child) {
			parent.addClass('hover');
			if(options.fx == 'toggle') child.css('display', 'block').stop().show();
			if(options.fx == 'slide') child.css('height', 'auto').stop().slideDown();
			if(options.fx == 'fade') child.css('opacity', 1).stop().fadeIn();
		}

		//hide menu
		function hide(parent, child) {
			if(options.fx == 'toggle') child.stop().hide();
			if(options.fx == 'slide') child.stop().slideUp();
			if(options.fx == 'fade') child.stop().fadeOut();
		}

		// keep chaining
		return this;
	};
	$('#mainnav').dropDownMenu();

})(jQuery, Class);
