BFSearchWidget = (function(){
	
	var Widget = function() {};

	/**
	 * Place the iframe with absolute positioning
	 * @return void
	 */ 
	Widget.prototype.adjustPosition = function() 
	{
		// get nodes and position
		var container = this.getContainerNode();
		var widget = this.getWidgetNode();
		var position = this.getNodePosition(container);
		
		// adjust position
		widget.style.position = "absolute";
		widget.style.zIndex = 9999;
		widget.style.top = position.top + "px";
		widget.style.left = position.left + "px";
		widget.style.visibility = "visible";
		
		// display the widget (if required)
		if (!window.BF_IFRAME_WIDGET_POSITIONED) 
		{
			var body = document.getElementsByTagName("body")[0];
			body.appendChild(widget);
			window.BF_IFRAME_WIDGET_POSITIONED = true;
		}
	};

	/**
	 * Resize the iframe to match its content
	 * @return void
	 */ 
	Widget.prototype.resize = function(height) 
	{
		this.getWidgetNode().height = (!isNaN(height) && height > 185) ? height : 185;
	};
	
	/**
	 * Return the DOM node representing the container where the widget is placed
	 * @return Object
	 */ 
	Widget.prototype.getContainerNode = function()
	{
		return document.getElementById(this.containerId);
	};
	
	/**
	 * Return the DOM node representing the widget
	 * @return Object
	 */ 
	Widget.prototype.getWidgetNode = function()
	{
		return document.getElementById("vacanze-searchbox-widget");
	};

	/**
	 * Utility method to return the position of a DOM node
	 * @return Object
	 */ 
	Widget.prototype.getNodePosition = function(node) 
	{
		var left = 0;
		var top = 0;
		
		if (node.offsetParent) 
		{
			do {
				left += node.offsetLeft;
				top += node.offsetTop;
			} while (node = node.offsetParent);
		}
		
		return {"left": left, "top": top};
	};
	
	/**
	 * Handle the page resize by repositioning the widget correctly.
	 * @return void
	 */
	Widget.prototype.resizeHandler = function()
	{
		if (window.BFSearchBoxWidgetTimeout) 
		{
			clearTimeout(window.BFSearchBoxWidgetTimeout);
		}
		
		var _self = this;
		
		window.BFSearchBoxWidgetTimeout = setTimeout(function() {
			_self.adjustPosition();
		}, 100);
	};
	
	/**
	 * Add event listeners
	 * @return void
	 */
	Widget.prototype.addListeners = function()
	{
		// inject iframe markup into the container
		this.getContainerNode().innerHTML = this.html.replace(/#BU/, this.baseURL).replace(/#QS/, encodeURI(this.queryString.join("&")));
		
		// set initial position
		this.adjustPosition();
		
		// take a reference to the current instance and the resize handler
		var _self = this;
		var _resize = typeof window.onresize == "function" ? window.onresize : null;
		
		window.onresize = function()
		{
			if (_resize)
			{
				_resize();
			}
			
			_self.resizeHandler();
		};
	};

	/**
	 * Initialize widget by adjusting its position and adding the necessary listeners
	 * @return void
	 */ 
	Widget.prototype.init = function(config) 
	{
		if (!config) 
		{
			config = {};
		}
		
		this.containerId = config.containerId || "search-widget-container";
		this.baseURL = config.baseURL || "http://vacanze.viaggiare.it";
		this.params = {
			businessProfile: config.businessProfile || "VACANZEVIAGGIAREIT",
			subSource: config.subSource || "searchengine",
			proxyPath: config.proxyPath || "bf_iframe_proxy.html",
			durationLabel: config.durationLabel || "Durata",
			periodLabel: config.periodLabel || "Periodo",
			css:  config.css != undefined ? config.css : (this.baseURL + "/vg1/css/vacanze/viaggiare.css"),
			selections: config.selections || "",
			maxRooms: config.maxRooms || 4,  
	        maxAdults: config.maxAdults || 4,
	        maxChildren: config.maxChildren || 3,
	        maxChildrenAge: config.maxChildrenAge || 11,
	        maxDestinationsComboHeight: config.maxDestinationsComboHeight || 135,
	        maxDestinationsComboColumns: config.maxDestinationsComboColumns || 2,
	        maxPeriodsComboHeight: config.maxPeriodsComboHeight || 73,
	        maxAirportsComboHeight: config.maxAirportsComboHeight || 100,
	        bigVersion: config.bigVersion || false,
	        extraParams: config.extraParams || null
		};
		this.html = '<iframe ' +
					'id="vacanze-searchbox-widget" ' +
					'style="visibility:hidden" ' + 
					'src="#BU/vg1/widgets/vacanzeSearchBoxWidget.action?#QS" ' +
					'width="490" height="185" ' +
					'frameBorder="0" scrolling="no" ' + 
					'horizontalscrolling="no" verticalscrolling="no"></iframe>';
		this.queryString = [];
		
		for (var p in this.params) 
		{
			if (p != "extraParams") 
			{
				this.queryString.push(p + "=" + this.params[p]);
			}
		}
		
		if (this.params.extraParams) 
		{
			var extras = [];
			
			for (var x in this.params.extraParams) 
			{
				extras.push(x + "|" + this.params.extraParams[x]);
			}
			
			this.queryString.push("extraParams=" + extras.join(","));
		}
		
		this.addListeners();
	};
	
	// return the configured object
	return new Widget();
	
})();

