if (!FSite2)
{
	alert('FSite2.js script is not loaded!');
	throw 'FSite2.js script is not loaded!';
}
if (!SWFObject)
{
	alert('swfobject.js script is not loaded!');
	throw 'swfobject.js script is not loaded!';
}

if (!Tovo)
{
	var Tovo = new Object();
	Tovo.params = new Object();
	Tovo.paramNames = new Object();
	Tovo.paramNames['flight'] = new Array("data_wylotu", "data_przylotu", "miasto_z", "w_kod_z", "miasto_d", "w_kod_d", "ile_dor", "ile_dzi",
		"ile_mlo", "ile_nie", "ile_sen", "ow", "dir", "linie_tanie", "linie_regularne", "afid", "afid_v");
	Tovo.paramNames['hotel'] = new Array();
	Tovo.paramNames['holiday'] = new Array();
	Tovo.sizes = {flight162: 470, flight250: 420, flight500: 231, hotel162: 300, hotel250: 250, hotel500: 180, holiday162: 300, holiday250: 280, holiday500: 180};

	Tovo.processSearcherParams = function()
	{
		var params = FSite2.parseScriptParams();
		if (!params.type)
		{
			alert('"type" parameter not specified!');
			throw '"type" parameter not specified!';
		}
		if (!params.size)
		{
			alert('"size" parameter not specified!');
			throw '"size" parameter not specified!';
		}
		if (!Tovo.sizes[params.type + params.size])
		{
			alert('wrong combination of "type" and "size" parameters!');
			throw 'wrong combination of "type" and "size" parameters!';
		}
		if (!params.domain)
			params.domain = 'www.tovo.pl';
		if (!params.target)
			params.target = params.domain;
		if (!params.atlas)
			params.atlas = '/static/flash/atlas.swf';
		if (!params.searcher)
			params.searcher = '/static/flash/search/' + params.type + params.size + '.swf';
		if (!params.skin)
			params.skin = 'blue';
		if (!params.skinUrl)
			params.skinUrl = '/static/flash/search/%s.swf';
		if (!params.logoUrl)
			params.logoUrl = '/static/flash/search/%s.png';
		if (!params.targetUrl)
			params.targetUrl = '/' + params.type + '/search/flashquery';
		if (!params.atlasXml)
			params.atlasXml = '/%s/atlas/xml/';
		if (!params.cityhelperXml)
			params.cityhelperXml = '/' + params.type + '/atlas/cityhelper/xml/1/nazwa/';
		if (!params.messagesXml)
			params.messagesXml = '/static/flash/search/message.xml';
		if (!params.flashId)
			params.flashId = 'tovo_' + params.type + params.size;
		if (!params.containerId)
			params.containerId = params.flashId + '_container';
		return params;
	}

	Tovo.showSearcher = function(params)
	{
		document.writeln('<div id="' + params.containerId + '" style="position: relative"></div>');
		var so = new SWFObject('http://' + params.domain + params.searcher, params.flashId,
			params.size, Tovo.sizes[params.type + params.size], 9, '#FFFFFF');
		so.addParam('quality', 'high');
		so.addParam('wmode', 'transparent');
		so.addParam('align', 'TL');
		so.addParam('scaleMode', 'noScale');
		so.addParam('AllowScriptAccess', 'always');
		so.addVariable('skinUrl', 'http://' + params.domain + params.skinUrl.replace('%s', params.skin));
		if (params.logo)
			so.addVariable('logoUrl', 'http://' + params.domain + params.logoUrl.replace('%s', params.logo));
		so.addVariable('targetUrl', 'http://' + params.target + params.targetUrl);
		so.addVariable('messagesXml', 'http://' + params.domain + params.messagesXml);
		so.addVariable('cityhelperXml', 'http://' + params.domain + params.cityhelperXml);
		so.addVariable('sourceDomain', window.location.host);
		var paramNames = Tovo.paramNames[params.type];
		if ((typeof paramNames == 'object') && (paramNames.constructor == Array))
		{
			for (var i = 0; i < paramNames.length; i++)
				if (params[paramNames[i]])
					so.addVariable(paramNames[i], params[paramNames[i]]);
		}
		so.write(params.containerId);
	}

	Tovo.extendSearcher = function(typeForm, extendTop, extendRight, extendBottom, extendLeft)
	{
		if (!Tovo.params[typeForm])
		{
			alert('There is no registered searcher with id "' + typeForm + '"');
			return;
		}
		var params = Tovo.params[typeForm];
		var container = document.getElementById(params.containerId);
		var flash = document.getElementById(params.flashId);
		if (!flash._originalWidth)
			flash._originalWidth = flash.clientWidth;
		if (!flash._originalHeight)
			flash._originalHeight = flash.clientHeight;
		var newWidth = flash._originalWidth + extendLeft + extendRight;
		var newHeight = flash._originalHeight + extendTop + extendBottom;
		if ((newWidth != flash._originalWidth) || (newHeight != flash._originalHeight))
			container.style.zIndex = 1000;
		else
			container.style.zIndex = null;
		flash.style.width = newWidth + 'px';
		flash.style.height = newHeight + 'px';
		container.style.width = newWidth + 'px';
		container.style.height = newHeight + 'px';
		container.style.marginLeft = -extendLeft + 'px';
		container.style.marginRight = -extendRight + 'px';
		container.style.marginTop = -extendTop + 'px';
		container.style.marginBottom = -extendBottom + 'px';
	}

	Tovo.showAtlas = function(pole, typeMap, typeForm, colors)
	{
		if (!Tovo.params[typeForm])
		{
			alert('There is no registered searcher with id "' + typeForm + '"');
			return;
		}
		var params = Tovo.params[typeForm];
		window._atlasbg = new FSite2.Layer('tovo_background_layer');
		window._atlasbg.object.style.backgroundColor = '#000000';
		window._atlasbg.object.style.opacity = '0.5';
		window._atlasbg.object.style.filter = 'alpha(opacity=50)';
		window._atlasbg.object.style.zIndex = 10000;
		window._atlasbg.fullScreen(true);
		window._atlas = new FSite2.Layer('tovo_map_layer', false, 'tovo_map_layer');
		window._atlas.object.style.zIndex = 10001;
		var so = new SWFObject('http://' + params.domain + params.atlas, 'tovo_map', 650, 390, 9, '#000000');
		so.addParam("quality", "high");
		so.addParam("wmode", "transparent");
		so.addParam('AllowScriptAccess', 'always');
		if (typeForm)
			so.addVariable("typeForm", typeForm);
		so.addVariable("tabType", typeMap);
		so.addVariable("domena", "http://" + params.domain + params.atlasXml.replace('%s', typeMap));
		so.addVariable("kolor_map1", colors[0]);
		so.addVariable("kolor_map2", colors[1]);
		so.addVariable("kolor_txt1", colors[2]);
		so.addVariable("kolor_txt2", colors[3]);
		so.addVariable("kolor_nav1", colors[4]);
		so.addVariable("kolor_nav2", colors[5]);
		so.addVariable("atlasid", pole);
		so.write('tovo_map_layer');
		window._atlas.center();
	}

	Tovo.setSearcherCity = function (pole, citycode, cityname, countrycode, typeForm)
	{
	//	alert(typeForm);
		var flTag = document.getElementById(typeForm);
	//	alert(flTag);
		flTag.insertCity(pole, citycode, cityname, countrycode);
	//	alert('after');
		close();
	}

	Tovo.hideAtlas = function()
	{
		if (window._atlas && window._atlas.object)
			window._atlas.remove();
		if (window._atlasbg && window._atlasbg.object)
			window._atlasbg.remove();
	}

	insertcity = Tovo.setSearcherCity;
	self.close = Tovo.hideAtlas;
}

var params = Tovo.processSearcherParams();
Tovo.params[params.flashId] = params;
Tovo.showSearcher(params);
