function baswl_debug(msg) {
	window.status = msg ? 'BAS_WL: ' + msg : '';
}

BAS_WL = {
	script_resources: new Array(
//		'scriptaculous/prototype.js',
//		'scriptaculous/scriptaculous.js',
//		'scriptaculous/scriptaculous.js?load=effects',
		'xhr.js',
		'utils.js',
		'date.php',
		'slider.js',
		'calendar.js',
		'iata_search.js',
		'map/map_functions.js',
		'flashmap/flashfuncs.js',
		'flashmap/swfobject.js',
//		'flashmap/swfmacmousewheel.js',
		'searchprovider.js',
		'searchprovider/flug.js',
		'searchprovider/reisen.js',
		'searchprovider/hotels.js',
		'searchprovider/mietwagen.js',
		'searchprovider/preisarchiv.js',
		'searchprovider/preisatlas.js'
	),

	std_style_resources: new Array(
		'stdlayout.css',
		'stdcolors.css'
	),

	style_resources: new Array(
		'slider.css'
	),

	xhr_gw_3lc_fluege:    '?feed=3lc_fluege&',
	xhr_gw_3lc_hotels:    '?feed=3lc_hotels&',
	xhr_gw_3lc_mietwagen: '?feed=3lc_mietwagen&',
	xhr_gw_xml_transfer:  '?feed=xml_transfer&',
	xhr_gw_tpl_transfer:  '?feed=template&',

	image_server:         'http://wl.bookandsmile.de/img/',

	form_container_id:    'baswl_forms',
	result_container_id:  'baswl_results'
};

BAS_WL._find_location = function() {
	var scripts = document.getElementsByTagName('script');
	for( var i = 0; i < scripts.length; i++ )
		if( scripts[i].src && scripts[i].src.indexOf('/whitelabel.js') > -1 )
		{
			BAS_WL._parse_query( scripts[i].src );

			/^https?:\/\/([^\/]+)(.*?\/)js\/whitelabel\.js/i.exec( scripts[i].src );

			var hp = RegExp.$1.split(':');
			this.location = {
				hostname: hp[0],
				port:     hp[1],
				path:     RegExp.$2
			};

			return;
		}

	throw('BAS_WL._find_location(): Can not find whitelabel location!');
};

BAS_WL._load_script = function(sURL,fOnLoad) {
	function scriptOnload() {
		this.onreadystatechange = null;
		this.onload = null;
		window.setTimeout(fOnLoad,20);
	};

	var loadScriptHandler = function() {
		var rs = this.readyState;
		if (rs == 'loaded' || rs == 'complete') {
			scriptOnload();
		}
	};

	var oS = document.createElement('script');
	oS.type = 'text/javascript';

	if (fOnLoad) {
		oS.onreadystatechange = loadScriptHandler;
		oS.onload = scriptOnload;
	};

	oS.src = sURL;
	document.getElementsByTagName('head')[0].appendChild(oS);
};

BAS_WL._parse_query = function(query) {
	if( query.length > 1 ) {
		query = query.substring(query.indexOf("?") + 1);
		var pairs = query.split(/[;&]/);
		this.query = this.query || window.BAS_WL_Config || { };
		for( var i = 0; i < pairs.length; i++ )
			if( pairs[i].length > 2 ) {
				var key = pairs[i].substr(0, pairs[i].indexOf("="));
				var val = pairs[i].substr(pairs[i].indexOf("=")+1);

				// special cases
				if( key == 'no_scriptaculous' || key == 'enable_flug' ||
				    key == 'enable_hotels' || key == 'enable_reisen' ||
					key == 'enable_mietwagen' || key == 'sliders' ||
					key == 'compact' || key == 'partner_id' )
				{
					val = this.parsebool(val);
				}

				this.query[key] = val;
			}
	}
};

BAS_WL.selectSearch = function(mode) {
	if( typeof window.BAS_WL[mode+'SearchProvider'] == 'function' ) {
		this.search = new window.BAS_WL[mode+'SearchProvider']();
		return this.search;
	}
	else {
		throw('BAS_WL.selectSearch(): No search provider for "' + mode + '" found');
	}
};

BAS_WL.parsebool = function(what) {
	if( what == 'yes' || what == 'true' || what == 'on' || what == '1' ) {
		return true;
	}
	return false;
};

BAS_WL.isIE = window.ActiveXObject ? true : false;
BAS_WL.initialized = false;

/*
 * Main initialisation function. Called by load() after fetching all external scripts.
 */
BAS_WL.init = function() {

	// This prevents strange recursion on Opera
	if( ! BAS_WL.initialized )
		BAS_WL.initialized = true;
	else
		return;


	if( ! this.query.relay ) this.query.relay = '/relay.php';

	var url_pid =  (this.query.partner_id ? 'partner_id=' + this.query.partner_id + '&' : '');

	this.xhr_gw_3lc_fluege    = this.query.relay + this.xhr_gw_3lc_fluege + url_pid;
	this.xhr_gw_3lc_hotels    = this.query.relay + this.xhr_gw_3lc_hotels + url_pid;
	this.xhr_gw_3lc_mietwagen = this.query.relay + this.xhr_gw_3lc_mietwagen + url_pid;
	this.xhr_gw_xml_transfer  = this.query.relay + this.xhr_gw_xml_transfer + url_pid;
	this.xhr_gw_tpl_transfer  = this.query.relay + this.xhr_gw_tpl_transfer + url_pid;

	this.useSlider = this.query.sliders;

	var self = this;

	var cID = self.query.container || 'baswl_container';
	var cDiv = document.getElementById(cID);
	if( ! cDiv ) {
		throw('BAS_WL.init(): Unable to find target element "' + cID + '"');
		return;
	}

	var cDiv2 = document.createElement("div");
	cDiv2.id = "baswl_global";
	cDiv2.setAttribute("id","baswl_global");
	cDiv.appendChild(cDiv2);
	cDiv = cDiv2;

	cDiv.appendChild(document.createElement('div')); cDiv.lastChild.id = 'baswl_forms';
	cDiv.appendChild(document.createElement('div')); cDiv.lastChild.id = 'baswl_regler';

	cDiv.appendChild(document.createElement('iframe')); cDiv.lastChild.id = 'baswl_loader_iframe';
	cDiv.appendChild(document.createElement('div')); cDiv.lastChild.id = 'baswl_loader';

	cDiv.appendChild(document.createElement('div')); cDiv.lastChild.id = 'baswl_result';


	var container = document.getElementById(self.form_container_id);
	if( container ) {
		if( self.query.enable_flug || self.query.enable_hotels || self.query.enable_mietwagen ||  self.query.enable_preisarchiv || self.query.enable_preisatlas )
		{
			self.tabs = {};

			var tabs = document.createElement('div');
				tabs.id = 'baswl_form_menu';
				tabs.appendChild(document.createElement('ul'));

			// Create tabs
			function mkTab(name, title)
			{
				if( self.query['enable_' + name] )
				{
					var is_default = (
						(!self.query.default_mode && !tabs.firstChild.firstChild) ||
						(self.query.default_mode == name)
					);

					tabs.firstChild.appendChild(document.createElement('li'));
					tabs.firstChild.lastChild.appendChild(document.createElement('a'));

					self.tabs[name] = tabs.firstChild.lastChild.firstChild;
					self.tabs[name].onfocus = function() { this.blur() };

					self.tabs[name].id        = 'baswl_form_tab_' + name;
					self.tabs[name].href      = '#';
					self.tabs[name].className = is_default ? 'active' : 'inactive';
					self.tabs[name].onclick   = function() { BAS_WL.utils.switchtab(name) };
					self.tabs[name].innerHTML = title;

					if( is_default )
						self.default_tab = name;
				}
			}

			mkTab('flug',        'Fl&uuml;ge' );
			mkTab('reisen',      'Reisen'     );
			mkTab('hotels',      'Hotels'     );
			mkTab('mietwagen',   'Mietwagen'  );
			mkTab('preisarchiv', 'Preisarchiv');
			mkTab('preisatlas',  'Preisatlas' );

			tabs.firstChild.appendChild(document.createElement('li'));
			tabs.firstChild.lastChild.className = 'clear';

			container.appendChild(tabs);
			container.appendChild(document.createElement('div'));

			self.forms    = container.lastChild;
			self.forms.id = 'baswl_form_area';

			var xhr = new BAS_WL.XHR();
			var forms = [ 'flug', 'reisen', 'hotels', 'mietwagen', 'preisarchiv', 'preisatlas' ];
			var what  = 0;

			function mkForms()
			{
				var form = forms[what++];
				var me   = arguments.callee;

				if( form )
				{
					if( ! self.query['enable_' + form] )
					{
						me();
					}
					else
					{
						xhr.get( self.xhr_gw_tpl_transfer + 'tpl=' + form, null,
							function(x) {
								baswl_debug('Loading template ... (' + form + ')');

								var div = document.createElement('div');
									div.innerHTML = x.responseText;

								self.forms.appendChild(div);

								var node = div.firstChild.firstChild;
								while( node && node.nodeType != 1 )
									node = node.nextSibling;

								// Double switchtab prevents IE render quirks
								BAS_WL.utils.switchtab(form);
								BAS_WL.selectSearch(form).initForm(node);
								BAS_WL.utils.switchtab(form);

								me();
							} );
					}
				}
				else
				{
					BAS_WL.utils.switchtab(self.default_tab);
					baswl_debug('');

					/* on load callback */
					if (BAS_WL_Config.onload)
						BAS_WL_Config.onload(self);
				}
			}

		}

		BAS_WL.utils = new BAS_WL.Utils();
		mkForms();

		/* Hack fuer wunderweib.de */
		if( window.location.hostname.indexOf("wunderweib.de") > -1 )
		{
			var x = container;

			for( var i = 0; i < 5; i++ )
				x = x.parentNode;

			x.className = x.className ? ( x.className + ' baswl_no_overflow_hidden' ) : 'baswl_no_overflow_hidden';

			var s = document.createElement('link');
			s.rel = "stylesheet";
			s.type = "text/css";
			s.href = "http://wl.bookandsmile.de/css/wunderweib.de.css";

			document.getElementsByTagName("head")[0].appendChild(s);
		}
	}
	else {
		throw('BAS_WL.init(): Can not find target element "' +
			self.form_container_id + '"');
	}
};


/*
 * BAS_WL loader, fetches external scripts synchronously and calls init()
 */
BAS_WL.load = function() {
	try {
		BAS_WL._find_location();
	}
	catch(ex) {
		alert(ex.message);
	}

	var head = document.getElementsByTagName('head')[0];
	var self = this;

	var what = 0;
	var query = this.query;
	var scripts = this.script_resources;
	var baseurl = 'http://' + this.location.hostname +
		( this.location.port ? ':' + this.location.port : '' ) +
		this.location.path + 'js/'
	;

	function onscriptload() {
		if( scripts[what] )
		{
			var script = scripts[what++];
			baswl_debug('Loading JS ... (' + script + ')');

			var skip = (
				( script.indexOf("scriptaculous")                 > -1 && query.no_scriptaculous     ) ||
				( script.indexOf("searchprovider/flug.js")        > -1 && ! query.enable_flug        ) ||
				( script.indexOf("searchprovider/reisen.js")      > -1 && ! query.enable_reisen      ) ||
				( script.indexOf("searchprovider/hotels.js")      > -1 && ! query.enable_hotels      ) ||
				( script.indexOf("searchprovider/preisarchiv.js") > -1 && ! query.enable_preisarchiv ) ||
				( script.indexOf("searchprovider/preisatlas.js")  > -1 && ! query.enable_preisatlas  )
			);

			if( skip )
				arguments.callee();
			else
				self._load_script(baseurl + script, arguments.callee);
		}
		else
		{
			baswl_debug('Init ...');
			BAS_WL.init();
		}
	}

	onscriptload();


	if( ! this.query.no_standard_styles )
	{
		for( var i = 0; i < this.std_style_resources.length; i++ )
		{
			var link = document.createElement('link');
				link.setAttribute('rel',  'stylesheet');
				link.setAttribute('type', 'text/css');
				link.setAttribute('href', 'http://' + this.location.hostname +
					( this.location.port ? ':' + this.location.port : '' ) +
					this.location.path + 'css/' + this.std_style_resources[i] );

			baswl_debug('Loading CSS ... (' + this.std_style_resources[i] + ' left)');
			head.appendChild( link );
		}
	}

	for( var i = 0; i < this.style_resources.length; i++ )
	{
		var link = document.createElement('link');
			link.setAttribute('rel',  'stylesheet');
			link.setAttribute('type', 'text/css');
			link.setAttribute('href', 'http://' + this.location.hostname +
				( this.location.port ? ':' + this.location.port : '' ) +
				this.location.path + 'css/' + this.style_resources[i] );

		baswl_debug('Loading CSS ... (' + this.style_resources[i] + ' left)');
		head.appendChild( link );
	}
};


// Load & init BAS_WL
BAS_WL.load();

