Epson.WhereToBuy = function( element )
{
	var _self = this;
	var _element = element;
	var _search_field = null;
	var _form = null;
	var _records = 0;

	var _map = null;
	var _router = null;
	var _point = null;
	var _zoom_widget = null;
	var _view_widget = null;
	var _overview_widget = null;

	var _product_url = '';
	var _prod_id = '';
	var _error_url = '';
	var _product_name = '';
	var _mm_url = 'http://www.multimap.com/maps/'
	var _print_url = '/jsps/storeLocator/pageMap.jsp';
	var _email = location.protocol+'//'+ location.host + '/jsps/storeLocator/emailPage.jsp';
	var _short_url = 'http://www.multimap.com/mod/common/short_url.htm?long_url=';
	var _directions_url = 'http://www.multimap.com/directions/';
	var _data_source = '';
	var _filters = [];
	var _filter_logic = 'AND';
	var _email_url = '';
	var _country_code = 'GB';
	var _location = '';
	var _location_title = '';
	var _sku = false;
	var _width_radius = true;
	var _curr_postcode = '';
	var _marker_details = null;

	var Consts =
	{
		max_pages:5,
		results_per_page:10,
		max_distance:100
	}


	function setup()
	{
		// pull out mm data
		var data = {  };
		jQuery.each( _element.className.split( ' ' ), function()
		{
			var match = this.match( /^wtb-([^-]+)-(.+)$/ );

			if ( match )
			{
				if ( data[ match[1] ] )
				{
					if ( data[ match[1] ] instanceof Array ) data[ match[1] ].push( unescape( match[2] ) );
					data[ match[1] ] = [ data[ match[1] ], unescape( match[2] ) ];
				}
				else data[ match[1] ] = unescape( match[2] );
			}
		} );
		jQuery.each( data, function( key )
		{
			if (( this instanceof Array ) || (key == 'filters')) eval( '_'+key+'=this' );
			else eval( '_'+key+'="'+this+'"' );

			if ( key == 'country_code') _country_code = this;

		} );

		// set up markers
		if ( MMFactory ) Markers.setup();

		// set up filters
		if ( MMFactory ) Filters.setup();

		// bind event to link
		if ( _element.nodeName == 'A' )
		{
			if ( _location ) jQuery( _element ).bind( 'click', UI.openMap );
			else jQuery( _element ).bind( 'click', UI.open );
		}
		else
		{
			_search_field = _element;
			_form = jQuery( _search_field ).parents( 'form' );
			_form.bind( 'submit', Search.search );

			// pre-popuplate the search field if a value was stored
			var saved_query = Epson.Cookies.read( 'postcode' );
			if ( saved_query && jQuery( _search_field ).val() == '' ) jQuery( _search_field ).val( saved_query );

			Validation.setup();
		}
	}

	var Map =
	{
		callback:null,

		load: function( callback )
		{
			Map.callback = callback;

			setTimeout( Map.pollLoad, 200 );

			jQuery( 'head' ).append( '<script src="http://clients.multimap.com/API/maps/1.2/epsoneuro_api12?callback=void" type="text/javascript"></script>' );
			jQuery( 'head' ).append( '<script src="/content/where_to_buy/store_mapping.json" type="text/javascript"></script>' );
		},

		pollLoad: function()
		{
			if ( !MMFactory ) setTimeout( Map.pollLoad, 200 );
			else if ( Map.callback )
			{
				// set up markers
				Markers.setup();

				// set up filters
				if ( MMFactory ) Filters.setup();

				Map.callback();
			}
		}
	}

	var Markers = 
	{
		icon:null,
		point:null,
		data:[],

		setup: function()
		{
			Markers.icon = new MMIcon( Epson.Path + 'images/ico_MMMarker_blue.png' );
			Markers.icon.iconSize = new MMDimensions( 25, 32 );
			Markers.icon.iconAnchor = new MMPoint( 12, 37 );
			Markers.icon.infoBoxAnchor   = new MMPoint( 12, -5 );
		},

		create: function( record )
		{
			if ( record.partner_status && Epson.StoreMapping.partner_statuses[ record.partner_status.toString().toLowerCase() ] )
			{
				var marker_icon = new MMIcon( Epson.StoreMapping.partner_statuses[ record.partner_status.toString().toLowerCase() ] );
				marker_icon.iconSize = new MMDimensions( 25, 32 );
				marker_icon.iconAnchor = new MMPoint( 12, 37 );
				marker_icon.infoBoxAnchor   = new MMPoint( 12, -5 );
			}
			else var marker_icon = Markers.icon;
			var marker = _map.createMarker( record.point, { label:record.name, icon:marker_icon, 'text':record.counter } );
			marker.setInfoBoxContent( '<div id="WTBMMInfo"><h4>' + record.store_name + '</h4><div>' + record.street + '</div><div>' + record.street2 + '</div><div>' + record.street3 + '</div><div id="PostCode">' + record.pc + '</div><div>' + record.telephone + '</div><div class="directions"><a href="#">' + Epson.Localisation.WhereToBuy.TXT003 + '</a><label>' + Epson.Localisation.WhereToBuy.TXT004 + ':</label> <input type="text" id="f_directions_to" /> <a href="#" class="route">' + Epson.Localisation.WhereToBuy.TXT005 + '</a></div><div class="buttons"><a href="#" class="buttonSmall print"><span>' + Epson.Localisation.WhereToBuy.TXT006 + '</span></a> ' + ( _email_url ? '<a href="#" class="buttonSmall email"><span>' + Epson.Localisation.WhereToBuy.TXT007 + '</span> ' : '' ) + '</a></div></div>' );

			marker.recordid = record.counter;
			marker.coords = record.point;
			
			marker.store_name = record.store_name;
			marker.street = record.street;
			marker.street2 = record.street2;
			marker.street3 = record.street3;
			marker.pc = record.pc;
			marker.telephone = record.telephone;
			marker.fax = record.fax;
			marker.website = record.website;
			marker.monday = record.monday;
			marker.tuesday = record.tuesday;
			marker.wednesday = record.wednesday;
			marker.thursday = record.thursday;
			marker.friday = record.friday;
			marker.saturday = record.saturday;
			marker.sunday = record.sunday;
      marker.id = record.id;
			
			Markers.data.push( marker );
			
			
			
		},

		open: function( fn, data, marker )
		{
			setTimeout( function()
			{
				
				_marker_details = marker;
				
				// set up events
				jQuery( '#WTBMMap .MMInfoBox .directions a:first' ).bind( 'click', Directions.open );
				jQuery( '#WTBMMap .MMInfoBox .directions input' ).bind( 'keydown', Directions.find );
				jQuery( '#WTBMMap .MMInfoBox .route' ).bind( 'click', Directions.find );
				jQuery( '#WTBMMap .MMInfoBox .directions' )[0].scrollTop = 0;
				jQuery( '#WTBMMap .MMInfoBox .print' ).bind( 'click', Print.load );
				jQuery( '#WTBMMap .MMInfoBox .email' ).bind( 'click', Email.load );

				_curr_postcode = jQuery( '#WTBMMap .MMInfoBox #PostCode' ).text();

				// save store location
				Markers.point = marker.coords;

				// remove all overlays
				if ( Directions.route ) for ( var x = 0; x < Directions.route.polyLine.length; x++ ) Directions.route.polyLine[x].remove();
			}, 100 );
		}
	}

	var Location =
	{
		load: function()
		{
			var map_block = jQuery( '<div id="WTBMMap" class="wide"></div>' );

			// reload popup with map
			Epson.UI.Popup.clear();
			Epson.UI.Popup.populate( map_block );
			Epson.UI.Popup.dimension();
			Epson.UI.Popup.Loader.display();

			// detect if MultiMap has been loaded in
			if ( !MMFactory ) return Map.load( Location.load );

			// set up geo-search criteria
			var address = new MMAddress();
			address.qs = _location;
			address.country_code = _country_code;

			// perform geo-search
			var geocoder = new MMGeocoder( function( result )
			{
				_point =  result[0].coords;

				Epson.UI.Popup.Loader.hide();

				// SET UP MULTIMAP API
				// set up map widgets
				_zoom_widget = new MMPanZoomWidget( new MMBox () ); 
				_zoom_widget.setContainer( jQuery( '#WTBMMap' )[0] );
	
				_view_widget = new MMMapTypeWidget ( undefined, new MMBox() );
				_view_widget.setContainer ( jQuery( '#WTBMMap' )[0] );
	
				_overview_widget = new MMOverviewWidget ( undefined, undefined, new MMBox () );
				_overview_widget.setContainer( jQuery( '#WTBMMap' )[0] );
	
				// set up map
				_map = MMFactory.createViewer( jQuery( '#WTBMMap' )[0] , MM_WORLD_MAP );
				_map.setMappingAPI( 'multimap' );
				_map.goToPosition( new MMLocation ( _point ) );
				_map.addWidget ( _zoom_widget );
	
				// zoom to bounds
		//		_map.goToPosition( ( _map.getAutoScaleLocation( bounds ) ) );
			} );
			geocoder.geocode( address );
		}
	}

	var Search =
	{
		data:null,
		page:0,
		query:'',

		search: function(e)
		{
			var field = jQuery( '#WTBSearchField' ).length ? jQuery( '#WTBSearchField' ) : ( _search_field ? jQuery( _search_field ) : null );
			var value = field ? field.val() : ( Search.query ? Search.query : '' );

			if ( e ) e.preventDefault();

			if ( Validation.validator.validateForm() )
			{
				// display loader
				UI.Loader.display();

				// detect if MultiMap has been loaded in
				if ( !MMFactory ) return Map.load( Search.search );

				// reset progrssive filtering
				_sku = true;
				_width_radius = true;

				Search.query = value;

				// reset pagination page
				Pagination.page = 0;

				// set up geo-search criteria
				var address = new MMAddress();
				address.qs = Search.query;
				address.country_code = _country_code;

				// perform geo-search
				var geocoder = new MMGeocoder( Search.handleGeoResult );
				geocoder.geocode( address );
			}
		},

		geoSearch: function()
		{
			// display loader
			UI.Loader.display();

			// use geo-search result to match store locations
			var search = new MMSearch();
			search.data_source = _data_source;
			search.count = Consts.results_per_page;
			search.point = _point;
			if ( _width_radius ) search.max_distance = Consts.max_distance;
			search.radius_units = 'miles';
			search.order_by_fields = 'distance';
			search.order_by_order = 'asc';
			search.filters = Filters.data;
			search.logic = _filter_logic;
			search.start_index = ( Pagination.page * Consts.results_per_page ) + 1;

			// perform multimap search based around geo point
			var request = new MMSearchRequester( Search.handleSearchResult );
			request.search( search );
		},

		handleGeoResult: function( data, error )
		{
			if ( arguments[0] && arguments[0].length == 1 )
			{
				var result = arguments[0][0];
	
				// save geo-search result centre-point
				_point = result.coords;
				
				// search based on predefined point
				Search.geoSearch();
			}
			else if ( arguments[0] && arguments[0].length > 1 )
			{
				// if popup doesn't exist, load it
				if ( !jQuery( '#Popup' ).length ) UI.open();

				// if map doesn't exist, load if
				if ( !jQuery( '#WTBMMap' ).length )
				{
					var result_block = jQuery( '<div class="supportList genericRoundedCorners"><div class="top"><div class="tl"></div><div class="tr"></div><div class="t"></div></div><div class="content"><ul id="WTBResults" class="listOfCentres "></ul><div class="mapNavigation"><div id="WTBPagination" class="pagination"><a href="#" class="buttonPrevious">&nbsp;<span>' + Epson.Localisation.WhereToBuy.TXT010 + '</span></a></li><span class="numbering"></span><a href="#" class="buttonNext">&nbsp;<span>' + Epson.Localisation.WhereToBuy.TXT011 + '</span></a></div></div></div><div class="bottom"><div class="bl"></div><div class="br"></div><div class="b"></div></div></div>' );
					var map_block = jQuery( '<div class="mapSection "><div class="findStore"><p>' + Epson.Localisation.WhereToBuy.TXT001 + '</p><form id="WTBSearch" action="#" method="GET"><label for="WTBSearchField" class="fl_info">' + Epson.Localisation.WhereToBuy.TXT008 + '</label><input id="WTBSearchField" class="ft_text" type="text" name="" value="' + Search.query + '"/><button type="submit"><strong class="buttonBlue"><span>' + Epson.Localisation.WhereToBuy.TXT009 + '</span></strong></button><p class="postcodeExample">' + Epson.Localisation.WhereToBuy.TXT002 + '</p></form></div><div id="WTBMMap"></div></div>' );

					
	
					// reload popup with map
					Epson.UI.Popup.clear();
					Epson.UI.Popup.populate( result_block );
					Epson.UI.Popup.populate( map_block );
					Epson.UI.Popup.dimension();
					if ( !_search_field ) Epson.UI.Popup.addButton( '<<', UI.open );
	
					// bind search event
					jQuery( '#WTBSearch' ).bind( 'submit', Search.search );

					// set up validation
					Validation.setup();

					// set up form elements
					map_block.find( 'label.fl_info' ).each( Epson.Forms.InfoLabels.setup );

					// bind pagination events
					jQuery( '#WTBPagination a' ).bind( 'click', Pagination.paginate );

					// SET UP MULTIMAP API
					// set up map widgets
					_zoom_widget = new MMPanZoomWidget( new MMBox () ); 
					_zoom_widget.setContainer( jQuery( '#WTBMMap' )[0] );
		
					_view_widget = new MMMapTypeWidget ( undefined, new MMBox() );
					_view_widget.setContainer ( jQuery( '#WTBMMap' )[0] );
		
					_overview_widget = new MMOverviewWidget ( undefined, undefined, new MMBox () );
					_overview_widget.setContainer( jQuery( '#WTBMMap' )[0] );

					// set up map
					_map = MMFactory.createViewer( jQuery( '#WTBMMap' )[0] , MM_WORLD_MAP );
					_map.setMappingAPI( 'multimap' );
					_map.goToPosition( new MMLocation ( new MMAddress ( { country_code:_country_code } ), 7 ) );
					_map.addWidget ( _zoom_widget );

					// add event listeners
					_map.addEventHandler( 'openInfoBox', Markers.open );
				}

				// clear overlays
				_map.removeAllOverlays();
	
				// reset data
				Search.data = [];
	
				// reset markers
				Markers.data = [];
	
				// clear results
				jQuery( '#WTBResults li' ).remove();

				// iterate through possible locations and populate result pane
				jQuery.each( arguments[0], function(i)
				{
					var addresses = this.address.display_name.split( ',' );
					var results = jQuery( '<li><a href="#" class="marker blueMarker"><span>' + ( i + 1 ) + '</span></a><div class="centreDetails"><h4><a href="#">' + addresses[0] + '</a></h4></div></li>' );

					addresses.shift();
					jQuery.each( addresses, function()
					{
						results.find( '.centreDetails' ).append( jQuery( '<div>' + this + '</div>' ) );
					} );

					// bind jump to event to links
					var coords = this.coords;
					results.find( 'a' ).bind( 'click', function(e) { _point = coords; Search.geoSearch(); e.preventDefault(); } );
	
					// add into list
					jQuery( '#WTBResults' ).append( results );
				} );

				// hide pagination
				UI.ResultsPane.hidePagination();

				// remove loader
				UI.Loader.hide();
			}
			else
			{
				// if popup doesn't exist, load it
				if ( !jQuery( '#Popup' ).length ) UI.open();

				Epson.UI.Popup.load( _error_url, function()
				{
					// bind search event
					jQuery( '#WTBSearch' ).bind( 'submit', Search.search );
					jQuery( '#WTBSearchField' ).val( Search.query );

					// set up validation
					Validation.setup();

					// set up form elements
					jQuery( '#Popup label.fl_info' ).each( Epson.Forms.InfoLabels.setup );

					Epson.UI.Popup.dimension();
				} );

				Epson.UI.Popup.addButton( '<<', UI.open );

				// remove loader
				UI.Loader.hide();
			}
		},

		handleSearchResult: function()
		{
			var data = arguments[0][0];
			var bounds = new MMBounds();

			if ( data.total_record_count > 0 )
			{
				// save the query in a cookie
				Epson.Cookies.write( 'postcode', Search.query );

				// save record count for pagination
				_records = data.total_record_count;

				// if popup doesn't exist, load it
				if ( !jQuery( '#Popup' ).length ) UI.open();

				// if map doesn't exist, load if
				if ( !jQuery( '#WTBMMap' ).length )
				{
					var result_block = jQuery( '<div class="supportList genericRoundedCorners"><div class="top"><div class="tl"></div><div class="tr"></div><div class="t"></div></div><div class="content"><ul id="WTBResults" class="listOfCentres "></ul><div class="mapNavigation"><div id="WTBPagination" class="pagination"><a href="#" class="buttonPrevious">&nbsp;<span>' + Epson.Localisation.WhereToBuy.TXT010 + '</span></a></li><span class="numbering"></span><a href="#" class="buttonNext">&nbsp;<span>' + Epson.Localisation.WhereToBuy.TXT011 + '</span></a></div></div></div><div class="bottom"><div class="bl"></div><div class="br"></div><div class="b"></div></div></div>' );
					var map_block = jQuery( '<div class="mapSection "><div class="findStore"><p>' + Epson.Localisation.WhereToBuy.TXT001 + '</p><form id="WTBSearch" action="#" method="GET"><label for="WTBSearchField" class="fl_info">' + Epson.Localisation.WhereToBuy.TXT008 + '</label><input id="WTBSearchField" class="ft_text" type="text" name="" value="' + Search.query + '"/><button type="submit"><strong class="buttonBlue"><span>' + Epson.Localisation.WhereToBuy.TXT009 + '</span></strong></button><p class="postcodeExample">' + Epson.Localisation.WhereToBuy.TXT002 + '</p></form></div><div id="WTBMMap"></div></div>' );
	
					// reload popup with map
					Epson.UI.Popup.clear();
					Epson.UI.Popup.populate( result_block );
					Epson.UI.Popup.populate( map_block );
					Epson.UI.Popup.dimension();
					if ( !_search_field ) Epson.UI.Popup.addButton( '<<', UI.open );

					// bind search event
					jQuery( '#WTBSearch' ).bind( 'submit', Search.search );

					// set up validation
					Validation.setup();

					// set up form elements
					map_block.find( 'label.fl_info' ).each( Epson.Forms.InfoLabels.setup );

					// bind pagination events
					jQuery( '#WTBPagination a' ).bind( 'click', Pagination.paginate );

					// SET UP MULTIMAP API
					// set up map widgets
					_zoom_widget = new MMPanZoomWidget( new MMBox () ); 
					_zoom_widget.setContainer( jQuery( '#WTBMMap' )[0] );
		
					_view_widget = new MMMapTypeWidget ( undefined, new MMBox() );
					_view_widget.setContainer ( jQuery( '#WTBMMap' )[0] );

					_overview_widget = new MMOverviewWidget ( undefined, undefined, new MMBox () );
					_overview_widget.setContainer( jQuery( '#WTBMMap' )[0] );

					// set up map
					_map = MMFactory.createViewer( jQuery( '#WTBMMap' )[0] , MM_WORLD_MAP );
					_map.setMappingAPI( 'multimap' );
					_map.goToPosition( new MMLocation ( _point ) );
					_map.addWidget ( _zoom_widget );

					// add event listeners
					_map.addEventHandler( 'openInfoBox', Markers.open );
				}
	
				// clear overlays
				_map.removeAllOverlays();
	
				// reset data
				Search.data = [];
	
				// reset markers
				Markers.data = [];
	
				// clear results
				jQuery( '#WTBResults li' ).remove();
	
				// iterate through resultset
				if ( data.totalRecordCount > 0 && data.records && data.records.length > 0 )
				{
					jQuery.each( data.records, function()
					{
						// save data
						Search.data.push( this );

						// create a marker on the map for each record
						Markers.create( this );
	
						// expand bounds of multimap to encompass all points
						bounds.extend( this.point );
					} );
				}
	
				// zoom to bounds
				_map.goToPosition( ( _map.getAutoScaleLocation( bounds ) ) );
	
				// display results in pane
				UI.ResultsPane.populate();
				UI.ResultsPane.updatePagination();
	
				// remove loader
				UI.Loader.hide();
			}
			else if ( _width_radius )
			{
				_width_radius = false;
			
				Search.geoSearch();
			}
			else if ( _sku )
			{
				// remove sky from filters
				Filters.resetWithoutSKU();

				// redo search without sku as a filter
				Search.geoSearch();
			}
			else
			{
				// if popup doesn't exist, load it
				if ( !jQuery( '#Popup' ).length ) UI.open();

				Epson.UI.Popup.load( _error_url, function()
				{
					// bind search event
					jQuery( '#WTBSearch' ).bind( 'submit', Search.search );
					jQuery( '#WTBSearchField' ).val( Search.query );

					// set up validation
					Validation.setup();

					// set up form elements
					jQuery( '#Popup label.fl_info' ).each( Epson.Forms.InfoLabels.setup );

					Epson.UI.Popup.dimension();
				} );

				Epson.UI.Popup.addButton( '<<', UI.open );

				// remove loader
				UI.Loader.hide();
			}
		}
	}

	var Filters =
	{
		data:[],

		setup: function()
		{
			jQuery( _filters ).each( function()
			{
				var filter = this.match( /^([^\+]+)/ );
				var operator = this.match( /\+([^\+]+)\+/ );
				var value = this.match( /([^\+]+)$/ );

				if ( filter && operator && value )
				{
					// Epson SKU logic
					if ( filter[1] == 'sku' ) _sku = true;

					Filters.data.push( new MMSearchFilter( filter[1], operator[1], value[1] ) );
				}
			} );
		},

		resetWithoutSKU: function()
		{
			_sku = false;
			Filters.data = [];

			jQuery( _filters ).each( function()
			{
				var filter = this.match( /^([^\+]+)/ );
				var operator = this.match( /\+([^\+]+)\+/ );
				var value = this.match( /([^\+]+)$/ );

				// Epson SKU logic
				if ( filter && filter[1] != 'sku' && operator && value ) Filters.data.push( new MMSearchFilter( filter[1], operator[1], value[1] ) );
			} );
		}
	}

	var Pagination =
	{
		page:0,

		paginate: function(e)
		{
			if ( this.className.match( /next/i ) ) Pagination.page++;
			else if ( this.className.match( /previous/i ) ) Pagination.page--;

			Search.geoSearch();

			e.preventDefault();
		}
	}

	var Directions = 
	{
		
		route:null,

		open: function(e)
		{
			//jQuery( '#WTBMMap .MMInfoBox .directions' ).animate( { scrollTop:17 }, 300, 'swing' ).find( 'input' ).focus().val( Search.query );
			
			jQuery( '#WTBMMap .MMInfoBox .directions a' ).hide();
			jQuery( '#WTBMMap .MMInfoBox .directions label, #WTBMMap .MMInfoBox .directions input, #WTBMMap .MMInfoBox .directions a.route' ).show();
			jQuery( '#WTBMMap .MMInfoBox .directions' ).find( 'input' ).focus().val( Search.query );


			_router = MMFactory.createRouteRequester( Directions.load, _map );

			e.preventDefault();
		},
		
		find: function(e)
		{
			if ( ( e.keyCode && e.keyCode == 13 ) || !e.keyCode )
			{
				var _directions_from = escape(jQuery( '#WTBMMap .MMInfoBox .directions input' ).val());
				
				var _directions_to;
				if (_curr_postcode != '') _directions_to = escape(_curr_postcode)
				else _directions_to = escape(Search.query);
				
				var url = _directions_url + "?" + 'qs_1=' + _directions_from + '&countryCode_1=' +  _country_code + '&qs_2=' + _directions_to + '&displayName_2=' + _directions_to + '&countryCode_2=' + _country_code + '&mode=driving&optimizeFor=time&mapData=917';
				
				//open in full screen
				var w, h;
				if (jQuery.browser.msie) { w = screen.availWidth; h = screen.availHeight; }
				else { w = window.innerWidth; h = window.innerHeight; }
				
				if ( _directions_from != _directions_to ) window.open( url, 'map', 'width=' + w +', height=' + h + ', left=0, top=0, menubar=yes, resizable=yes, toolbar=yes, statusbar=yes, scrollbars=yes' );
								
				e.preventDefault();
			}
		}		
	}

	var Print =
	{
		load: function(e)
		{
			
			var curr_marker = [];
			curr_marker.push(_marker_details.store_name);
			curr_marker.push(_marker_details.street);
			curr_marker.push(_marker_details.street1);
			curr_marker.push(_marker_details.street2);
			curr_marker.push(_marker_details.street3);
			curr_marker.push(_marker_details.pc);
			curr_marker.push(_marker_details.telephone);
			curr_marker.push(_marker_details.fax);
			curr_marker.push(_marker_details.website);
			curr_marker.push(_marker_details.monday);
			curr_marker.push(_marker_details.tuesday);
			curr_marker.push(_marker_details.wednesday);
			curr_marker.push(_marker_details.thursday);
			curr_marker.push(_marker_details.friday);
			curr_marker.push(_marker_details.saturday);
			curr_marker.push(_marker_details.sunday);

			Epson.Cookies.write('Store',curr_marker);

			
			var url = _print_url + '?' + '&loc_lat=' + Markers.point.lat + '&loc_lon=' + Markers.point.lon + '&zoomFactor=' + ( parseInt( _map.getZoomFactor() )  ) ;
			/*
			var url = _print_url + '?' + 'qs=' + Search.query + '&countryCode=' + _country_code + '&lat=' + Markers.point.lat + '&lon=' + Markers.point.lon + '&zoomFactor=' + ( parseInt( _map.getZoomFactor() ) + 3 ) + '&loc_lat=' + Markers.point.lat + '&loc_lon=' + Markers.point.lon + '&mapType=4&dataPreferences=null';

			*/

			var w, h;
			if (jQuery.browser.msie) { w = screen.availWidth; h = screen.availHeight; }
			else { w = window.innerWidth; h = window.innerHeight; }
			window.open( url, 'map', 'width=' + w +', height=' + h + ', left=0, top=0, menubar=yes, resizable=yes, toolbar=yes, statusbar=yes, scrollbars=yes' );
			

			e.preventDefault();
		}
	}

	var Email =
	{
		load: function(e)
		{
		  var i = _marker_details;
			// set map url
			var url = ( _email_url.match( /\?/ ) ? '&' : '?' ) + 'link=' + escape( _email + '?' + '&id=' + _marker_details.id + '&zoomFactor=' + ( parseInt( _map.getZoomFactor() ) ) + '&prodid=' + _prod_id ) ;
			//var url = _email + '?' + '&id=' + Markers.point.id + '&zoomFactor=' + ( parseInt( _map.getZoomFactor() )  ) ;

			Epson.UI.Popup.load( _email_url + url , Email.setup );

			e.preventDefault();
		},

		setup: function()
		{
			Epson.Email.setup();

			Epson.UI.Popup.element.find( '.headerContent .backButton a' ).unbind( 'click' ).bind( 'click', function() { jQuery( this ).unbind( 'click' ).bind( 'click', UI.open ); Search.search() } );

			Epson.UI.Popup.dimension( Epson.UI.Popup.undimension );
		}
	}

	var Validation =
	{
		validator:null,

		setup: function()
		{
			var id = _search_field ? _search_field.id : 'WTBSearchField';
			Validation.validator = new jQuery.validation( { form:_form, auto:false, event:'change', error:Epson.Forms.displayError } );
			Validation.validator.validate( {id:id, type:'is_string', error_message:Epson.Localisation.WhereToBuy.TXT012, required:true } );
		}
	}

	var UI =
	{
		open: function(e)
		{
			// display popup
			Epson.UI.Popup.open( _product_name );

			// remove back button if there was one
			Epson.UI.Popup.removeButton();

			// call ajax to load in content
			if ( !_search_field ) Epson.UI.Popup.load( _product_url, UI.setup );

			if (e) e.preventDefault();
		},

		openMap: function(e)
		{
			// display popup
			Epson.UI.Popup.open( _location_title );

			Location.load();

			if (e) e.preventDefault();
		},

		setup: function()
		{
			Epson.UI.Popup.dimension();

			// bind search event to field search
			jQuery( '#WTBSearch' ).bind( 'submit', Search.search );

			// pre-popuplate the search field if a value was stored
			var saved_query = Epson.Cookies.read( 'postcode' );
			if ( saved_query && jQuery( '#WTBSearchField' ).val() == '' ) jQuery( '#WTBSearchField' ).val( saved_query );

			// set up validation
			Validation.setup();

			// set up form elements
			jQuery( '#Popup label.fl_info' ).each( Epson.Forms.InfoLabels.setup );
		},

		ResultsPane:
		{
			populate: function()
			{
				jQuery.each( Search.data, function(i)
				{
					/*var special_store = this.store_type && Epson.StoreMapping.store_types[ this.store_type.toString().toLowerCase() ] ? '<img src="' + Epson.StoreMapping.store_types[ this.store_type.toString().toLowerCase() ] + '" />' : '';*/
					var special_store = this.image ? '<img src="' + Epson.StoreMapping.image.path.toString().toLowerCase() + this.image + '" />' : '';
					var marker_style = this.partner_status && Epson.StoreMapping.partner_statuses[ this.partner_status.toString().toLowerCase() ] ? 'background-image:url(' + Epson.StoreMapping.partner_statuses[ this.partner_status.toString().toLowerCase() ] + ')' : '';
					var results = jQuery( '<li><a href="#" class="marker blueMarker" style="' + marker_style + '"><span>' + this.counter + '</span></a><div class="centreDetails">' + special_store + '<h4><a href="#">' + this.store_name + '</a></h4><div>' + this.street + '</div><div>' + this.street2 + '</div><div>' + this.town + '</div><div>' + this.pc + '</div></div></li>' );

					// bind jump to event to links
					results.find( 'a' ).bind( 'click', function(e) { Markers.data[i].openInfoBox(); e.preventDefault() } );
	
					// add into list
					jQuery( '#WTBResults' ).append( results );
				} );
			},

			updatePagination: function()
			{
				jQuery( '#WTBPagination' ).css( { display:'' } );

				// hide buttons if necessary
				var visibility = ( Pagination.page == 0 ) ? { visibility:'hidden' } : { visibility:'visible' };
				jQuery( '#WTBPagination a.buttonPrevious' ).css( visibility );
				var visibility = ( Pagination.page >= Consts.max_pages || Search.data.length < Consts.results_per_page || ( ( Pagination.page + 1 ) * Consts.results_per_page ) >= _records ) ? { visibility:'hidden' } : { visibility:'visible' };
				jQuery( '#WTBPagination a.buttonNext' ).css( visibility );

				// update text
				jQuery( '#WTBPagination span' ).text( ( ( Pagination.page * Consts.results_per_page ) + 1 ) + ' - ' + ( ( Pagination.page * Consts.results_per_page ) + Search.data.length ) );
			},

			hidePagination: function()
			{
				jQuery( '#WTBPagination' ).css( { display:'none' } );
			}
		},

		Loader:
		{
			display: function()
			{
				if ( !jQuery( '#WTBLoader' ).length )
				{
					var field = jQuery( '#WTBSearchField' ).length ? jQuery( '#WTBSearchField' ) : ( _search_field ? jQuery( _search_field ) : null );

					if ( field )
					{
						var width = field.width();
						var offset = field.offset();

						jQuery( '#Container' ).append( jQuery( '<div id="WTBLoader" class="loader"></div>' ).css( { position:'absolute', top:offset.top+6+'px', left:offset.left+width-10+'px' } ) );
					}
				}
			},
	
			hide: function()
			{
				jQuery( '#Container .loader' ).remove();
			}
		}
	}

	setup();
}
