					
					var _timer = null;
					
					
					function saveSearchToDatabase(){
						_timer = null;
						if ($('#tenantSearch').val().length > 0){					
							var count = $('#tenantListing li:not(".refresh, .alphaHeader, .alphaSub, .sorry")').size();
							$.post("/assets/library/search.php", { 'searchedFor': $('#tenantSearch').val(), 'count': count },function(){
							});
						}
					}
					
					
						function getFlashMovie(movieName) {
						  var isIE = navigator.appName.indexOf("Microsoft") != -1;
						  return (isIE) ? window[movieName] : document[movieName];
						}
												
						function callToActionscript(id,level,action){				
								//alert('asking for '+id+' on '+action);
						    getFlashMovie("storeLocator").JSfunction(id,level,action);
						}
						
						function storeClicked(storeId){
					
							if (tenantData != undefined){
								var url = '/shop/'+tenantData[storeId];
								window.location=url;
							} else {					
								if ($('li#tenant_'+storeId).size() > 0){
									var url = $('li#tenant_'+storeId).children('a').attr('href');
									window.location=url;
								}
							}
						}
						

						function storeHovering(storeId){
							$('#tenantListing li > a').removeClass('hovering');
							
							if ($('#tenant_'+storeId).size() != 0){
								$('#tenantListing').scrollTo($('#tenant_'+storeId).prevAll('li:first'));
								$('#tenant_'+storeId).children('a').addClass('hovering');
							}
						}
				
				
						function returnMatches(findText){
						
							$.ajax({
								url: '/assets/library/search.php',
								type: "POST",
								data: "findText="+findText,
								dataType: "json",
								success: function(matches){

									printMatches(matches);
								
								}, error: function(data){
								
								alert('A problem occurred while searching the database');
								
								}
							});
						}
						
						function printMatches(matches){
							
							$('#alphaScroll').find('a').css('visibility','hidden');
							
							callToActionscript('null','null','clear');
							
							$('#tenantListing').html('<li class="refresh" style="background:#dbe3e9; color:#88a6bb; font-size:10px; text-transform:uppercase; margin:0;">&larr; Display all shops</li>');
							
							if (matches.stores != undefined){
								$('#tenantListing').append('<li class="alphaSub">Shops</li>');

								for (x in matches.stores){
		
									if (x != 'totalCount'){
									
										$('#tenantListing').append('<li class="level_'+matches.stores[x].level+'" id="tenant_'+matches.stores[x].id+'"><a href="/shop/'+matches.stores[x].url+'">'+matches.stores[x].name+'</a></li>');
										
										//alert(matches.stores.totalCount);
										
										//alert(JSON.stringify(matches.stores));
										
										if (matches.stores.totalCount == 1){
											
											var level = matches.stores[x].level;
											if (level == 'LL') {level = 0;}
											
											if (!isNaN(matches.stores[x].id) && matches.stores[x].id != 0 && !isNaN(level)){
												
												//alert('name '+matches[x].id+' id '+matches[x].id+' level '+level);
												callToActionscript(matches.stores[x].id,level,'over');
												storeHovering(matches.stores[x].id);
											
											} else {
												
												getFlashMovie("storeLocator").JSdialog('<b>'+matches.stores[x].name+'</b>'+"\n is located on "+level,'over');
												storeHovering(matches.stores[x].id);
											}
										}
									} //if this is not totalcount
								} //end foreach
							}


							if (matches.categories != undefined){
							
								$('#tenantListing').append('<li class="alphaSub">Categories</li>');

								for (x in matches.categories){
									if (x != 'totalCount'){
										$('#tenantListing').append('<li class="alphaHeader">'+x+'</li>');
										for (j in matches.categories[x]){
											$('#tenantListing').append('<li class="level_'+matches.categories[x][j].level+'" id="tenant_'+matches.categories[x][j].id+'"><a href="/shop/'+matches.categories[x][j].url+'">'+matches.categories[x][j].name+'</a></li>');
										}
									} //if this is not totalcount
								} //end foreach
							}



							if (matches.offering != undefined){
								$('#tenantListing').append('<li class="alphaSub">Stores that offer:</li>');
								for (x in matches.offering){
									if (x != 'totalCount'){
										$('#tenantListing').append('<li class="alphaHeader">'+x+'</li>');
										for (j in matches.offering[x]){
											$('#tenantListing').append('<li class="level_'+matches.offering[x][j].level+'" id="tenant_'+matches.offering[x][j].id+'"><a href="/shop/'+matches.offering[x][j].url+'">'+matches.offering[x][j].name+'</a></li>');
										}
									} //if this is not totalcount
								} //end foreach
							}

							
							
							if (matches.messages != undefined){
								
		
								$('#tenantListing').append('<li class="alphaSub">Messages:</li>');
								for (x in matches.messages){
									
									if (x != 'totalCount'){
										
										var extra = message = '';
										
										if (matches.messages[x].message !== null && matches.messages[x].message !== ''){
											message = matches.messages[x].message+'<br /><br />';
										}
									
										if (matches.messages[x].uri !== null && matches.messages[x].uri !== ''){			
											extra = '<small>More info can be found here:</small><br /><a href="'+matches.messages[x].uri+'">'+matches.messages[x].uri+'</a>';
										}
										
										
										$('#tenantListing').append('<li style="text-align:center; font-size:14px; padding-top:15px;">'+message+extra+'</li>');
									
									} //if this is not totalcount
								} //end foreach
							}
						
							
							
						if (matches.stores == undefined && matches.offering == undefined && matches.categories == undefined && matches.messages == undefined){
							
							$('#tenantListing').append('<li class="sorry"><br />No results were found.<br /><br /><small>Please bear with us as we gather more insight<br />into what <em>you</em> - our visitor, is looking for.<br /><br />A more intuitive search will be available soon. </small></li>');
						
						}
							$('#discoveryBox img.pulsate').remove();
						}
						
						
						function searchTenants(){
							
							$('#tenantSearch').css('color','#000');

							if ($('#tenantSearch').val() !== $('#tenantSearch').attr('title') && $('#tenantSearch').val() !== '' && $('#tenantSearch').val().length > 0){
								
								if ($('#discoveryBox').find('img.pulsate').size() == 0){
									$('#discoveryBox').append('<img class="pulsate" src="/assets/img/pulsate.gif" style="position:absolute; top:8px; left:11px;">');
								}
								
								var enteredText = $('#tenantSearch').val().toLowerCase();
								returnMatches(enteredText);
								
								if (_timer != null){
      						window.clearTimeout(_timer);
      					}
								_timer = window.setTimeout("saveSearchToDatabase()", 550);
								
								
							} else {
								callToActionscript('null','null','clear');
								refreshListing();							
							}
							
						}
							
							
						
						function refreshListing(){	
							$('#tenantSearch').val('').select().focus();
							$('#tenantListing').html(refreshedListing);
							
							if ($('#hideAlphabet').size() == 0){
								$('#alphaScroll').find('a').css('visibility','visible');
							}
							
						}
						
						
						var refreshedListing = '';
						
						function storeRefreshListing(){
							refreshedListing = $('#tenantListing').html();
						}
				
						
						
						$(document).ready(function(){
				
							storeRefreshListing();
							
							$('#tenantListing li.refresh').livequery('click',refreshListing);
							
							if ($('#tenantSearch').val() == ''){
								$('#tenantSearch').val($('#tenantSearch').attr('title')).css('color','#999');	
							} else {
								searchTenants();
								$('#discoveryBox img.pulsate').remove();
							}
							
							
							$('#tenantSearch').focus(function(e){
							
								if ($(this).val() == $(this).attr('title')){
									$(this).val('');
								}
							});

							$('#tenantSearch').blur(function(e){
								if ($(this).val() == ''){
									$(this).val($(this).attr('title')).css('color','#999');
								}
							});
							
				
							//$('#tenantSearch').keyup(searchTenants);
							
							$('#tenantSearch').keyup(function(e){
								$(this).css('color','#000');
								if (e.which == 13){
									$('#discoveryBox button').trigger('click');
								}		
								if ($(this).val() == ''){refreshListing();}			
							});
						
							
							$('#discoveryBox button').click(searchTenants);
							
							var version = deconcept.SWFObjectUtil.getPlayerVersion();
							
							if (version['major'] > 10 || (version['major'] == 10 && version['minor'] >= 0 && version['rev'] >= 2)){ //flahs 10
								
								var so = new SWFObject("/assets/flash/Flash900.swf", "storeLocator", "330", "434", "10", "#eff4f7");
								so.write("flashcontent");
				
							} else if (version['major'] <= 8) { //flash 8 and below
								
								var so = new SWFObject("/assets/flash/Flash900_9.swf", "storeLocator", "330", "434", "9", "#eff4f7");
								so.write("flashcontent");
								so.useExpressInstall('/assets/flash/expressinstall.swf');

							} else { //flash 9 to 10.0.1 
							
								var so = new SWFObject("/assets/flash/Flash900_9.swf", "storeLocator", "330", "434", "9", "#eff4f7");
								so.write("flashcontent");
							}
							
							$('#tenantListing li:not(".refresh, .alphaHeader, .alphaSub")').livequery(function(x){
								$(this).hover(function(e){
									
									var id = $(this).attr('id').substr(7) - 0;
									var level = $(this).attr('class').substr(6);
									if (level == 'LL') {level = 0;}
									
									if (!isNaN(id) && id != 0 && !isNaN(level)){
										
										callToActionscript(id,level,'over');
										
									} else {
										getFlashMovie("storeLocator").JSdialog('<b>'+$(this).find('a').text()+'</b>'+"\n is located on "+level,'over');
									}
									
								},function(e){
								
									var id = $(this).attr('id').substr(7) - 0;
									var level = $(this).attr('class').substr(6);
									if (level == 'LL') {level = 0;}
									if (!isNaN(id) && id != 0 && !isNaN(level)){
										callToActionscript(id,level,'out');
										
									} else {
										getFlashMovie("storeLocator").JSdialog('','out');
									}
								
								});							
							});
						});
