/**
 * 
 * @param {String} searchForInputId
 * @param {String} actionButtonId
 * @param {String} hideContentId
 */
function MapSwitcher(searchForInputId, actionButtonId, hideContentId) {
	// get existing elements from document...
	this._searchForInput = document.getElementById(searchForInputId);
	this._actionButton = document.getElementById(actionButtonId);
	this._hideContent = document.getElementById(hideContentId);
	
	// create map iframe...
	this._mapFrame = document.createElement('iframe');
	this._mapFrame.id = 'mapFrame';
	this._mapFrame.className = MapSwitcher.HIDDEN_CLASS;
	
	// add map iframe to the document...
	this._hideContent.parentNode.insertBefore(this._mapFrame, this._hideContent);
	
	// attach to click of action button...
	this._attachToClick(this._actionButton);
}

MapSwitcher.HIDDEN_CLASS = 'hidden';
MapSwitcher.MAP_URL = 'http://www.punchpubs.co.uk/punch/punchpubs/scripts/bizmap_ioko.asp?location={0}';

MapSwitcher.prototype = {
	/**
	 * shows the map
	 */
	execute: function() {
		this._mapFrame.src = this._getUrl(this._searchForInput.value);
		this._toggleMapVisible();
		return false;
	},
	
	_cancelAction: function(e) {
		if (e.preventDefault) {
			e.preventDefault();
		} else {
			e.returnValue = false;
		}
	},
	
	/**
	 * browser independent way of attaching to click
	 * @param {Object} btn
	 */
	_attachToClick: function(btn) {
		// create callback function...
		var self = this;
		var callback = function(e) { self.execute(); self._cancelAction(e); };
		
		// attach to event in the correct way...
		if (btn.addEventListener) {
			btn.addEventListener('click', callback, false);
		} else if ( btn.attachEvent ) {
			btn.attachEvent('onclick', callback);
		} else {
			btn.onclick = callback;
		}
	},
	
	/**
	 * gets url for map frame for the given location
	 * @param {String} location
	 * @return {String} url for map frame to show correct map
	 */
	_getUrl: function(location) {
		var url = new String( MapSwitcher.MAP_URL );
		var cleanLocation = encodeURIComponent(location);
		return url.replace('{0}', cleanLocation);
	},
	
	/**
	 * whether map frame is visible
	 * @return {Boolean}
	 */
	_mapIsVisible: function() {
		this._mapFrame.className != MapSwitcher.HIDDEN_CLASS;
	},
	
	/**
	 * whether content is visible
	 * @return {Boolean}
	 */
	_contentIsVisible: function() {
		this._hideContent.className != MapSwitcher.HIDDEN_CLASS;
	},
	
	/**
	 * shows map frame and hides content
	 */
	_toggleMapVisible: function() {
		if (!this._mapIsVisible()) {
			this._hideElement(this._hideContent);
			this._showElement(this._mapFrame);
		}
	},
	
	/**
	 * hides map frame and shows content
	 */
	_toggleContentVisible: function() {
		if (!this._contentIsVisible()) {
			this._hideElement(this._mapFrame);
			this._showElement(this._hideContent);
		}
	},
	
	/**
	 * Hides given element 
	 * @param {Object} el
	 */
	_hideElement: function(el) {
		el.cacheClass = el.className;
		el.className = MapSwitcher.HIDDEN_CLASS;
	},
	
	/**
	 * Shows given element previously hidden by _hideElement...
	 * @param {Object} el
	 */
	_showElement: function(el) {
		var newClass = (el.cacheClass) ? (el.cacheClass) : ('');
		el.className = newClass;
	}
}