$(document).ready(function(){
	

													 
	 
  // top_nav search drop down
	$("div#search_tab a").click(function(){
		$("div#search_content").slideToggle();
		$("div#search_content p").fadeToggle("normal");
	});
	$("div#search_close a").click(function(){
		$("div#search_content").slideUp();
		$("div#search_content p").fadeOut("normal");
	});
													 
		/*											 
	// original version of search drop down												 
	$('#header_search').hide();
	$('#atoz').hide();
	$('#search_drop').show();
	
	$('#search_drop').click(function(){
		$('#search_drop').hide();
		$('#atoz').show()
		$('#header_search')
		.slideDown();		
		});
	
	
	$('.close_box').click(function(){
																 
		
		$('#header_search').slideUp();
		$('#atoz').hide();
		$('#search_drop').show();
	});
													 
*/											 
/*													 
				$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
  {
    tags: "southampton",
    tagmode: "any",
    format: "json"
  },
  function(data) {
    $.each(data.items, function(i,item){
      $("<img/>").attr("src", item.media.m).appendTo("#town_photo");
      if ( i == 0 ) return false;
    });
  });									 
 */
      
													 
			// Creating custom :external selector
$.expr[':'].external = function(obj){
    return !obj.href.match(/^mailto\:/)
            && (obj.hostname != location.hostname);
};

// Add 'external' CSS class to all external links as a test to prove that they have actually been selected
// $('a:external').addClass('external');


// Hijack click event for external links	
$('a:external').click(function(e) {

  // save link address to variable
	var ext_lnk = $(this).attr('href');
	
	//get user IP
	$.getJSON("http://jsonip.appspot.com?callback=?",function(data){
																														
		var userIP=data.ip
		
		// assemble data o send to tracker.php for writing to the database
		var values=
		{			
			"ip": userIP,
			"link": ext_lnk
		};
		
		// ajax call to write to database
		$.ajax({
			url: "/js/tracker.php",
			dataType: "json",
			data: values
		});

	});	
	
	
});
				


				
				
				
													 
    //hover background for links on user admn pages
		$('#account_table a span').hover(function(){
			$(this).addClass('linkState');
			}, function(){
			$(this).removeClass('linkState');
			});
		
		//hover background for links on corner menu
		$('#corner_menu a').hover(function(){
			$(this).addClass('linkState');
			}, function(){
			$(this).removeClass('linkState');
			});
													 
		// This section positions the logo in the directory pages  
		/*
		 $('.directory_row img').each(function(){
			 $(this).hide();
			 var ph=$(this).parents("div").height();
			 var eh=$(this).height();
			 while(eh=0){
				 eh=$(this).height();
			 };
			
			 $(this).css('top', 10+(ph-eh)/2);
			 $(this).show();
		 });
																					
	 */
	
	
													 
		// javascript disabled message - this code removes the message for users who do have javascript enabled.
		$('#javascript_message').remove();
													 
		// striping for sitemap											 
		$(".stripeMe tr").mouseover(function(){$(this).addClass("over");}).mouseout(function(){$(this).removeClass("over");});
    $(".stripeMe tr:even").addClass("alt");
		
		$(".back_button").click(function(){
				history.back(-1);
				$('this').style.cursor="pointer";
		});
		 
		
		/*
		$('#photo_box').cycle({
			speed: 3000,
			timeout: 6000,
			fit: 0,
			delay:-3000
							  });
		*/
					  
		$('.hideable').hide();
		$('#local_content').show();	
		$('#page_menu > ul > li.inpage a').click(function(e) {
			e.preventDefault();
			$('div.hideable:not(:hidden)').hide();
			
			if($(this).attr('href')=='#local_authorities'){
			    $('#local_authorities').show();
			} else if($(this).attr('href')=='#local_areas'){
			    $('#local_areas').show();
			} else if($(this).attr('href')=='#local_schools'){
			    $('#local_schools').show();
			} else if($(this).attr('href')=='#local_directories'){
			    $('#local_directories').show();
			} else {
				$('#local_content').show();
			}
		});
		
		
		/*				   
		// Hide the blurb in the directory and slide down on hover
		$('#directory_section p.blurb').hide();
		
		$('#directory_section td').hover(function() {
		$(this).find('p.blurb').stop(true,true).slideDown();
		}, function() {
		$(this).find('p.blurb').stop(true,true).slideUp();
		});
		*/
		
		$('#directory_table tr:last td').css('border','none');
	   
	    // Adjust the font size if the Town name is to long for the menu title bar
		if($('#header_location').text().length > 20){
			$('#header_location h1').css({'font-size':'24px','line-height':'24px', 'margin-top':'15px'});
		} else if($('#header_location h1').text().length > 11){
			$('#header_location h1').css({'font-size':'30px','line-height':'30px', 'margin-top':'5px'});
		}else {
			$('#header_location h1').css({'font-size':'48px', 'line-height':'48px'});
		}
		//***********************************************
		
		
		// Toggle the tab background of the menu	
		/*
		$('#nav_menu li a').each(function(){
			
			var title=$(this).attr('title');
		
			
			if(title.search(/_1/)>-1){
			
				var nav_div= $(this).parents('#nav_menu');
			
				if(title == "Home_1"){
					nav_div.css('background-position','0px -50px');
				} else if(title == "Buy-Sell_1"){
					nav_div.css('background-position','0px -100px');
				} else if(title == "Conveyancing_1"){
					nav_div.css('background-position','0px -150px');
				} else if(title == "Mortgages_1"){
					nav_div.css('background-position','0px -200px');
				} else if(title == "Removals_1"){
					nav_div.css('background-position','0px -250px');
				} else if(title == "Storage_1"){
					nav_div.css('background-position','0px -300px');
				} else if(title == "Renting_1"){
					nav_div.css('background-position','0px -350px');
				} else if(title == "Home Improvements_1"){
					nav_div.css('background-position','0px -400px');
				} else {
					nav_div.css('background-position','0px 0px');
				};
			}
		});
		*/
		//*********************************************
		
		
		
		// Toggle the tab background of the menu	
		
		$('#nav_menu li a').each(function(){
			
			var title=$(this).attr('title');
		
			
			if(title.search(/_1/)>-1){
			
				var nav_div= $(this).parents('#nav_menu li');
			    nav_div.addClass('on');
			}
		});
		
		//*********************************************
		
		
		
		
		
	   
	   //Set up the 'Find Town' search box on the home page and header
	    $('.search_box').each(function(){
 
			this.value = $(this).attr('title');
			$(this).addClass('text-label');
		 
			$(this).focus(function(){
				if(this.value == $(this).attr('title')) {
					this.value = '';
					$(this).removeClass('text-label');
				}
			});
		 
			$(this).blur(function(){
				if(this.value == '') {
					this.value = $(this).attr('title');
					$(this).addClass('text-label');
				}
			});
		});		
		
		
			   
		function log( message ) {
			$( "<div/>" ).text( message ).prependTo( "#log" );
			$( "#log" ).attr( "scrollTop", 0 );
		}
		
		
		

		$( ".search_box" ).autocomplete({	
			source: "/search.php",
			minLength: 4,
			
			 search: function(event, ui) {
				 
				 $('<div></div>')
				/*.attr('id', 'spinner')*/
				.hide()
				.appendTo($("#top"))
				.fadeTo('slow', 0.6); 
			},
		  
			

			select: function( event, ui ) {
				
				// The log function is not being used so has been commented out. It has been left for future guidance. 
				/*
				log( ui.item ?
					"Selected: " + ui.item.label :
					"Nothing selected, input was " + this.value);
				*/
				    var town = ui.item.town;		
				
				
				// Put Local town name in header of page. This could be done better with PHP in the future.
				//$('#header_location').text(ui.item.town);	
				
				// Encode names that will appear in the url
				var town_name=urlencode(ui.item.town);
				var place_name=urlencode(ui.item.placename).toLowerCase();
				
				// Move to local.php with get values set for location and classification. These are translated in the .htaccess file.
				window.location = "/houses+for+sale/" + place_name + "-L" + ui.item.id + "/property-S12/P1" ;
					
			},
			open: function() {
				$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
				$('#spinner').fadeOut('slow', function() {
				$(this).remove();
				});
			},
			close: function() {
				$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
			},
		});
		
		// Reduce the font size if the town name is too long to fit in the header
		
						   });


function urlencode(s) {
  s = encodeURIComponent(s);
  return s.replace(/~/g,'%7E').replace(/%20/g,'+');
 }
 

