/*! EnhanceJS: a progressive enhancement bootstrejser. Copyright 2012 @scottjehl, Filament Group, Inc. Licensed MIT/GPLv2 */
(function( w, undefined ) {
	
	// Enable JS strict mode
	"use strict";

	var doc = w.document,
		docElem = doc.documentElement,
		head = doc.head || doc.getElementsByTagName( "head" )[ 0 ];
	
	//ejs object for ejs-specific functions
	w.ejs = {};
	
	// hasClass function - check if element has a class
	ejs.hasClass = function( elem, cls ){
		return elem.className.indexOf( cls ) > -1
	}
	
	// Callback for running logic dependent on a property being defined
	// You can use isDefined to run code as soon as the document.body is defined, for example, for body-dependent scripts
	// or, for a script that's loaded asynchronously that depends on other scripts, such as jQuery.
	// First argument is the property that must be defined, second is the callback function
	ejs.onDefine = function( prop, callback ){
		var callbackStack 	= [];
		
		if( callback ){
			callbackStack.push( callback );
		}
		
		function checkRun(){
			if( eval( prop ) ){
				while( callbackStack[0] && typeof( callbackStack[0] ) === "function" ){
					callbackStack.shift().call( w );
				}
			}
			else{
				setTimeout(checkRun, 15); 
			}
		};
		
		checkRun();
	};
	
	// shortcut of isDefine body-specific 
	ejs.bodyReady = function( callback ){
		ejs.onDefine( "document.body", callback );
	};
	
	
	//private style load function
	ejs.loadCSS = function( href, media ){
		var lk = doc.createElement( "link" ),
			links = head.getElementsByTagName( "link" ),
			lastlink = links[ links.length-1 ];
			
		lk.type = "text/css";
		lk.href = href;
		lk.rel = "stylesheet";
			
		if( media ){
			lk.media = media;
		}
		if( lastlink && lastlink.nextSibling ){
			head.insertBefore(lk, lastlink.nextSibling );
		}
		else {
			head.appendChild( lk );
		}
	};
	
	// Private script load function
	ejs.loadJS = function( src ){
		var script = doc.createElement( "script" ),
			fc = head.firstChild;
			script.src = src;

		if( fc ){
			head.insertBefore(script, fc );
		} else {
			head.appendChild( script );
		}
	};	
	
	// Define base directory paths for referencing js, css, img files. Optional.
	ejs.basepath = {
		js	: "",
		css	: ""
	};
	
	// Define arrays to contain JS and CSS files that are available
	ejs.files = {		
		js: {},
		css: {}
	};	
	
	// Define arrays to contain JS and CSS files that will be loaded
	ejs.jsToLoad = [];
	ejs.cssToLoad = [];
	
	// Function for adding files to the queue for loading. 
	// CSS or JS is discovered by file path. 
	// Files should not include base paths, if already defined in ejs.basepath.
	ejs.addFile = function( file ){
		var js = file.indexOf( ".js" ) > -1;
		ejs[ js ? "jsToLoad" : "cssToLoad" ].push( ejs.basepath[ js ? "js" : "css" ] +  file );
	};
	
	// CSS and JS loading functions: load CSS or JS via single ejs.load method
	ejs.load = function ( url ){
		return ( url.indexOf( ".js" ) > -1 ? ejs.loadJS : ejs.loadCSS )( url );
	};
	
	// concatSyntax is a function that decorates a URL in whatever way necessary for a concatenator API. 
	// To configure, just define it as a function with a url argument, and return that url decorated any way your concatenator expects.
	// by default, Enhance uses the  url pattern suggested in the QuickConcat project: https://github.com/filamentgroup/quickconcat
	ejs.concatSyntax = function( url ){
		return url + "=concat";
	};
	
	// Function for triggering the CSS and JS requests
	ejs.enhance = function(){
		if( ejs.jsToLoad.length ){
			ejs.load( ejs.concatSyntax( ejs.jsToLoad.join(",") ) );
		}
		if( ejs.cssToLoad.length ){
			ejs.load( ejs.concatSyntax( ejs.cssToLoad.join(",") )  );
		}
	};

}( this ));
/*
	enhance.config: this example file uses the enhance.js api to:
		 * determine whether a browser is qualified for enhancements
		 * define available CSS and JS assets
		 * test features and device conditions and environment to determine which files to load
		 * load those files via a single concatenated call
*/

(function( win ){

	// re-reference ejs var locally
	var ejs = win.ejs,
		docElem = win.document.documentElement;
	
	// Add your qualifications for major browser experience divisions here.
	// For example, you might choose to only enhance browsers that support document.querySelectorAll (IE8+, etc).
	// Use case will vary, but basic browsers: last stop here!
	if( !( 'querySelectorAll' in win.document && 'addEventListener' in win && Array.prototype.forEach ) ){
		return;
	}
	
	// if you don't use modernizr use the following code:
	// check if browser supports css animation
	/*var animation = false,
	elm = document.getElementById( 'goodie' ),
    animationstring = 'animation',
    keyframeprefix = '',
    domPrefixes = 'Webkit Moz O ms Khtml'.split(' '),
    pfx  = '';
	if( elm.style.animationName !== undefined ) { animation = true; }    
	if( animation === false ) {
	  for( var i = 0; i < domPrefixes.length; i++ ) {
		if( elm.style[ domPrefixes[i] + 'AnimationName' ] !== undefined ) {
		  pfx = domPrefixes[ i ];
		  animationstring = pfx + 'Animation';
		  keyframeprefix = '-' + pfx.toLowerCase() + '-';
		  animation = true;
		  break;
		}
	  }
	}*/
		
	// Add "enhanced" class to HTML element
	//docElem.className += " enhanced";
			
	// Configure css and js paths, if desirable.
	ejs.basepath.js = "/assets/js/";
	ejs.basepath.css = "/assets/css/";
	
	// Define potential JS files for dynamic loading
	ejs.files.js = {
		domlib 		: "_lib/wrap.custom.js",
		//ajaxinc 	: "_lib/ajaxinclude.wrap.js",
		//ecssential 	: "ecssential.min.js",
		webfontldr	:	"plugin/webfontloader.min.js",
		ajaxincjquery 	: "_lib/ajaxinclude.jquery.js",
		//modernizr	: "vendor/modernizr-2.7.2.min.js",
		jquery		: "vendor/jquery-1.11.0.min.js",
		apollo		: "plugin/apollo.min.js",
		smoothscroll	: "plugin/smooth-scroll.js",
		general		: "generalenhancements.js",
		fastclick	: "plugin/fastclick.min.js",
		touch		: "touch.js",
		faq			: "plugin/faq.js",
		widescreen	: "widescreen.js",
		picturefill2 : "plugin/picturefill-2.0.min.js",
		fancybox 	: "plugin/fancybox.min.js",
		lazyloadecho : "plugin/echo.min.js"
	};
	
	// Define potential CSS files for dynamic loading
	ejs.files.css = {
		fancybox	: "fancybox.css"
	};
	
	// Start queueing files for load. 
	// Pass js or css paths one at a time to ejs.addFile 
	
	// Add general js enhancements to all qualified browsers
	ejs.addFile( ejs.files.js.domlib );
	//ejs.addFile( ejs.files.js.ajaxinc );
	//ejs.addFile( ejs.files.js.modernizr );
	//ejs.addFile( ejs.files.js.jquery );
	ejs.addFile( ejs.files.js.apollo );
	ejs.addFile( ejs.files.js.smoothscroll );	
	//ejs.addFile( ejs.files.js.ecssential );
	ejs.addFile( ejs.files.js.webfontldr );
	ejs.addFile( ejs.files.js.general );
	//ejs.addFile( ejs.files.css.aside );
	//ejs.addFile( ejs.files.js.picturefill );
	ejs.addFile( ejs.files.js.picturefill2 );
	ejs.addFile( ejs.files.js.lazyloadecho );
	/*if( window.innerWidth > 450 && animation !== false ) {
		ejs.addFile( ejs.files.css.animate );
		ejs.addFile( ejs.files.js.animate );
	}*/
	
	// if touch events are supported, add touch file
	if( "ontouchend" in win.document ){
		ejs.addFile( ejs.files.js.fastclick );
		ejs.addFile( ejs.files.js.touch );
	}
	
	// if screen is wider than 500px, add fancybox and widescreen files
	if( window.innerWidth > 650/* && window.innerHeight > 400 */){
		ejs.addFile( ejs.files.js.jquery );
		ejs.addFile( ejs.files.js.ajaxincjquery );
		//ejs.addFile( ejs.files.css.fancybox );
		//ejs.addFile( ejs.files.js.fancybox );
		ejs.addFile( ejs.files.js.widescreen );
	}
	
	// add the echo lazyload javascript file if the body has a class of "geschaeftsleitung" 
		
	// Note: since we're using hasClass to check if the body element has a class or not, we need to wrap all remaining logic in a call to ejs.isDefined
	ejs.bodyReady( function(){
		
		if( ejs.hasClass( win.document.body, 'faq' )){
			ejs.addFile( ejs.files.js.faq );
		}
		
		
		// Load the files, enhance page
		ejs.enhance();
		
	});

}( window ));