/**
 * @fileOverview Function for the global KfW object
 * @version 0.1
 * @author Michael Schieben <michael@twoantennas.com>
 */

/*jslint nomen: true, debug: true, evil: false, onevar: false, browser: true, plusplus: false */
/*global window: true, KfW: true, jQuery: true */

/**
* KfW is defined at kfw.js and must be included in the <head> element
* KfW must not be undefined at this point of code execution!
*/
if (KfW === undefined) {
	var KfW = {};
	KfW.DEBUG = true;
	KfW.Settings = {};
	KfW.Initializers = [];
}

(function ($) {
	
	/**
	 * jQuery shortcut
	 * @name $
	 * @class 
	 * $ is a shortcut to {@link jQuery}
	 * @see jQuery
	 */
	
	/**
	 * Returns the name of the anchor in href attributes
	 *
	 * <a href="#text"></a>
	 * @example $('a').anchor();
	 * returns -> "text"
	 */
	$.fn.anchor = function () {
		var r = $(this).attr('href');
		if (r)
			return r.split("#").pop();
		else
			return "";
	};
	
	/**
	 * Tests if the current html body has the classname given in sectioname
	 * If sectionname is of type array, the function returns true if the
	 * div#main-wrapper has one of the given classnamens
	 * @param sectionname String | Array classname(s) to test
	 */
	function isSection(sectionname) {
		if (typeof sectionname === "string") {
			return $('#main-wrapper').hasClass(sectionname);
		} else {
			var sectionnames = sectionname;
			for (var i = 0; i < sectionnames.length; i++) {
				sectionname = sectionnames[i];
				if ($('#main-wrapper').hasClass(sectionname)) {
					return true;
				}
			}
		}
		return false;
	}
	KfW.isSection = isSection;
	
	/**
	 * Tests and calls the KfW.Initializers
	 * @see KfW.addInitializer();
	 */
	function initialize() {
		for (var i = 0; i < KfW.Initializers.length; i++) {
			var boot = KfW.Initializers[i];
			var test = boot[1];
			var executeThisInitializer = test;
			if (typeof test === "function") {
				executeThisInitializer = test();
			}
			if (executeThisInitializer) {
				boot[0].call();
			}
		}
	}
	
	KfW.Ids = {};
	/**
	 * Gets an id to numerate DOM-Elements
	 */
	function id(namespace) {
		if (KfW.Ids[namespace] !== undefined) {
			return namespace + '-' + (KfW.Ids[namespace]++);
		} else {
			KfW.Ids[namespace] = 0;
			return KfW.id(namespace);
		}
	}
	KfW.id = id;
	
	/**
	 * Handler for document.ready();
	 */
	function ready() {
		initialize();
	}
	
	$(document).ready(ready);
	
}(jQuery));

// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t = t / d - 1) * t * t * t - 1) + b;
};
