google.load("jquery","1.5");
google.load("jqueryui","1.8");
google.load("swfobject", "2.2");

// global config
var CONFIG = {
	googleLoad_MAPS: function(fn){
		if (_.isFunction(fn)) {
			google.load("maps", "3.x", {
				other_params: 'sensor=false',
				"callback": fn
			});
		}		
	},
	pathToGebeco: '/javascripts/smsG/',
	pathToTigges: '/javascripts/smsT/',
	pathToGroups: '/javascripts/smsS/',
	pathToPartner: '/javascripts/smsP/',
	configFile: 'config.js'
};

google.setOnLoadCallback(function(){
	// loaded data, global
	if (typeof(window.DATA) == 'undefined'){
		window.DATA = {};
	}
	$.extend(window.DATA, {hotels:{},meta:null,guides:{}});
	
	//lazy loader
	;(function($){$.xLazyLoader=function(method,options){if(typeof method=='object'){options=method;method='init';};new xLazyLoader()[method](options);};$.xLazyLoader.defaults={js:[],css:[],img:[],name:null,timeout:20000,success:function(){},error:function(){},complete:function(){},each:function(){}};var head=document.getElementsByTagName("head")[0];function xLazyLoader()
	{var self=this,s,loaded=[],errors=[],tTimeout,cssTimeout,toLoad,files=[];this.init=function(options)
	{if(!options)return;s=$.extend({},$.xLazyLoader.defaults,options);toLoad={js:s.js,css:s.css,img:s.img};$.each(toLoad,function(type,f){if(typeof f=='string')
	f=f.split(',');files=files.concat(f);});if(!files.length){dispatchCallbacks('error');return;};if(s.timeout){tTimeout=setTimeout(function(){var handled=loaded.concat(errors);$.each(files,function(i,file){$.inArray(file,handled)==-1&&errors.push(file);});dispatchCallbacks('error');},s.timeout);};$.each(toLoad,function(type,urls){if($.isArray(urls))
	$.each(urls,function(i,url){load(type,url);});else if(typeof urls=='string')
	load(type,urls);});};this.js=function(src,callback,name)
	{var $script=$('script[src*="'+src+'"]');if($script.length){$script.attr('pending')?$script.bind('scriptload',callback):callback();return;};var s=document.createElement('script');s.setAttribute("type","text/javascript");s.setAttribute("src",src);s.setAttribute('id',name);s.setAttribute('pending',1);s.onerror=addError;$(s).bind('scriptload',function(){$(this).removeAttr('pending');callback();setTimeout(function(){$(s).unbind('scriptload');},10);});var done=false;s.onload=s.onreadystatechange=function(){if(!done&&(!this.readyState||/loaded|complete/.test(this.readyState))){done=true;s.onload=s.onreadystatechange=null;$(s).trigger('scriptload');};};head.appendChild(s);};this.css=function(href,callback,name)
	{if($('link[href*="'+href+'"]').length){callback();return;};var link=$('<link rel="stylesheet" type="text/css" media="all" href="'+href+'" id="'+name+'"></link>')[0];if($.browser.msie){link.onreadystatechange=function(){/loaded|complete/.test(link.readyState)&&callback();};}else if($.browser.opera){link.onload=callback;}else{var hostname=location.hostname.replace('www.',''),hrefHostname=/http:/.test(href)?/^(\w+:)?\/\/([^\/?#]+)/.exec(href)[2]:hostname;hostname!=hrefHostname&&$.browser.mozilla?callback():(function(){try{link.sheet.cssRules;}catch(e){cssTimeout=setTimeout(arguments.callee,20);return;};callback();})();};head.appendChild(link);};this.img=function(src,callback)
	{var img=new Image();img.onload=callback;img.onerror=addError;img.src=src;};this.disable=function(name)
	{$('#lazy-loaded-'+name,head).attr('disabled','disabled');};this.enable=function(name)
	{$('#lazy-loaded-'+name,head).removeAttr('disabled');};this.destroy=function(name)
	{$('#lazy-loaded-'+name,head).remove();};function load(type,url){self[type](url,function(status){status=='error'?errors.push(url):loaded.push(url)&&s.each(url);checkProgress();},'lazy-loaded-'+(s.name?s.name:new Date().getTime()));};function dispatchCallbacks(status){s.complete(status,loaded,errors);s[status](status=='error'?errors:loaded);clearTimeout(tTimeout);clearTimeout(cssTimeout);};function checkProgress(){if(loaded.length==files.length)dispatchCallbacks('success')
	else if(loaded.length+errors.length==files.length)dispatchCallbacks('error');};function addError(){errors.push(this.src);checkProgress();};};})(jQuery);
	
	// additional jQuery-functions BEGIN	
	// get url-params
	jQuery.urlParam = function(name){
		var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
		if (results != null) return results[1];
		return 0;
	};
	// checks, if an element exists, before starting to work on it
	jQuery.fn.doOnce = function(func){
		this.length && func.apply(this);
		return this;
	};
	// shorts an string by length-param and appends "..."
	shortText = function(str,l){
		if (str.length <= l) return str;
		return str.substring(0,(l-3)) + '...';
	};
	// formats prices
	formatPrice = function(bp){
		if (bp === null) return '---&nbsp;&nbsp;';
		bp = bp.toString();
		l = bp.length;
		if (bp.indexOf('.') !== -1) {l = bp.indexOf('.');}
		if (bp.indexOf(',') !== -1) {l = bp.indexOf(',');}
		if (l > 3)  {bp = bp.substring(0,l-3) + '.' + bp.substring(l-3,l);}
		return bp;
	};
	// sorting
	by = function (name, minor){
		return function (o, p){
			var a, b;
			if (o && p && typeof o === 'object' && typeof p === 'object'){
				a = o[name];
				b = p[name];
				if (a === b){					
					return typeof minor === 'function' ? minor(o, p) : 0;
				}
				if (typeof a === typeof b){
					return a < b ? -1 : 1;
				}
				return typeof a < typeof b ? -1 : 1;
			} else {
				throw {name: 'Error',message: 'Expected an object when sorting by ' + name};
			}
		};
	};
	
	
	jQuery(document).ready(function(){
		CONFIG.isTigges = jQuery('#content.tigges').length > 0;
		CONFIG.isGroups = jQuery('#content.specialgroups').length > 0;
		CONFIG.isPartner = jQuery('#content.partner').length > 0;
		//CONFIG.isCompany = jQuery('#content.company').length > 0;	// is also gebeco
		CONFIG.isGebeco = (!(CONFIG.isTigges || CONFIG.isGroups || CONFIG.isPartner));
		
		var configToLoad;
		var controllerToLoad;
		
		if (CONFIG.isGebeco) {
			configToLoad = CONFIG.pathToGebeco;
			controllerToLoad = CONFIG.pathToGebeco;
		} 
		if (CONFIG.isTigges) {
			configToLoad = CONFIG.pathToTigges;
			controllerToLoad = CONFIG.pathToTigges;
		} 
		if (CONFIG.isGroups) {
			configToLoad = CONFIG.pathToGroups;
			controllerToLoad = CONFIG.pathToGebeco;
		} 
		if (CONFIG.isPartner) {
			configToLoad = CONFIG.pathToPartner;
			controllerToLoad = CONFIG.pathToGebeco;
		}
		configToLoad += CONFIG.configFile;
		controllerToLoad += 'controller.js';
		
		// TODO: redefine loading-chain, put all actions in one callback
		jQuery.xLazyLoader({
			js: [
				'/javascripts/smsShared/libs.js'
			],
			success: function(){
				jQuery.getJSON( // get META-Data first, because it's needed at many points
					'/trips/meta.json', 
					function(data){
						DATA.meta = data;
						jQuery.xLazyLoader({
							js: [
								configToLoad,
								'/javascripts/smsShared/common.js',
								controllerToLoad
							],
							success: function(){
								if (_.isFunction(doAllTheNeedfulThings)) {
									doAllTheNeedfulThings();
								}
							}
						});
					}
				);
			}
		});		
	});
	
});
