//------  include.js -------
// Copyright © 2010 by LeKiosque SAS — All Right Reserved
// By SDL on 2010-08-12 9:36
// JQuery extension : Javascript includer feature.

var _includedScripts = [
"js/aes.js",
"js/build.js",
"js/jquery.extensions.js",
"js/jquery.cookie.js",
//"js/stacktrace-min-0.3.js",
//"js/localstorage.js",
//"js/setup.js",
"js/sha1.js",
"js/api.js",
"js/authentication.js",
"js/social.js",
///"js/modal.js",
"js/cart.js",
"js/json.js",
"js/urls.js"
];

jQuery.extend({
	_includeCache: new Array(),

	// CAUTION: this relies on the fact, that jQuery detects when the page is finished loading,
	//                 so don't use this inside jQuery(document).ready( function($){} )
	// TODO: check, if this is called within jQuery(document).ready() and throw some error
	include: function(){
		var current= jQuery._includeCache.length;
		// just copy the filenames to the include cache for later usage
		for( var i = 0; i < arguments.length; i++ ) {
			// check for file duplicates!
			var inCache = false;
			for (source in jQuery._includeCache) {
				if (jQuery._includeCache[source]["filename"].indexOf(arguments[i])>=0) {
					// already in the cache, don't add it twice
					inCache = true;
					//alert(arguments[i]+" is already in the cache, ignoring it");
				}
			}
			if (!inCache) {
				// TODO: resolve relative filenames in multilevel includes
				jQuery._includeCache[current++]={filename:arguments[i], ready:false, loading:false}
			}
		}
	},

	// we need to overwrite the jQuery.ready function that gets called periodically
	// to reliably load all our included javascripts sequencially
	_super_ready: jQuery.ready,
	ready: function(){
		for(var i=0; i < jQuery._includeCache.length;i++)
		{
			var include = jQuery._includeCache[i];
			// if we encounter an unloaded javascript, start the ajax call
			if(!include.ready && !include.loading)
			{	//			lekiosque/kiosk/js
				//			lekiosque/Web/js
				var url = null;
					
				if (/^https?:\/\//.test(include.filename)
				||  /^https?:\/\/(.*?\.)lekiosque\.fr/.test($(location).attr("href"))
				||  /lekiosque\/Web/.test($(location).attr("href"))
				||  /^https?:\/\/presse-numerique\.boulanger\.fr/.test($(location).attr("href"))
				)
					url = include.filename;
				else
				    url = include.filename;
					//url = "../Web/"+include.filename;
						
				$.ajax({
					url:url, dataType:'script',
					async:false, cache: true,
					success:function(){include.ready=true},
					// TODO: how about some error-messages??
					error:function(){
						alert("Erreur de chargement du script "+url+" — contactez lekiosque.fr svp"); 
						include.ready=true; 
						include.loading=false;
					}
				});
				include.loading=true;
			}
			// if we encounter a loading javascript, just return from this function
			// on the next ready() call we will check this again
			else if(!include.ready && include.loading) return
		}
		// we should have included everything when we have come to this line,
		// so now delegate to the original jQuery ready check
		jQuery._super_ready(arguments);
	}
});

function includeScripts () {
	for (source in _includedScripts) {
		$.include(_includedScripts[source]);
	}
}


includeScripts();

