$(document).ready(function() {
	
	setTimeout(function(){ fade_straplines(); }, 3000);
	if ($('.home_gallery_grey_outer').size() > 0) {
		setTimeout(function(){ fade_home_gallery(); }, 3000);	
	}
	
	rhs_margin_top();
	home_gallery();
	if ($('#keywords').length != 0) {
		auto_suggest();
	}
	if ($.cookie('json_done') != 'done') {
		
		$.ajax({ url: "ajax/auto_complete_json.php"});
		$.cookie('json_done','done');
	}
	//search_settings();
	$('a.lightbox').lightBox({maxHeight: 600,maxWidth: 800});  
	
	$('iframe').each(function() {
			var url = $(this).attr("src");
			$(this).attr("src",url+"&wmode=opaque");
		});
		
	var scroll_pos = $(window).scrollLeft()*-1;
  	$('#fixed_header').css('left', scroll_pos+'px');	
  	$('#rhs-box-fixed').css('left', scroll_pos+'px');
		
	$(window).scroll(function() {
  			var scroll_pos = $(window).scrollLeft()*-1;
  			$('#fixed_header').css('left', scroll_pos+'px');
  			$('#rhs-box-fixed').css('left', scroll_pos+'px');
  			//alert(scroll_pos);
	});
	
});

$(window).load(function () {
 page_length_adjust();
});


function page_length_adjust() {
	var window_height = $(window).height();
	var main_height = $('#main').height();
	
	var footer_height = $('#footer').height();
	var total_height = main_height+footer_height+92+32;
	//alert(total_height);
	if (total_height < window_height) {
			//alert('hello');
			var new_main_height = window_height-(footer_height+92+32);
			$('#main').height(new_main_height);
			//alert(new_main_height);
	}
	
	
}



function search_settings() {
	    
		if 	($('#keywords').val() == 'Post Code,Street Name or Area Name') {
			
			$('#keywords').val('');
		}
		if ($('#lifestyle_open_input').val() == 'open' || $('#region_open_input').val() == 'open') {
			make_extra_inputs();
		}
		
		
		var form_string = $('#quick_search_index_php_RPW_quick_search_obj').serialize();
		//$.cookie('search_settings',form_string);
		$.cookie('search_settings',form_string, { path: '/' });
		$('#quick_search_index_php_RPW_quick_search_obj').submit();
}

function handleNoFlash(errorCode) {
    if (errorCode == FLASH_UNAVAILABLE) {
      alert("Error: Flash doesn't appear to be supported by your browser");
      return;
    }
  }  




function validate_pw_form(thisform)
{
	with (thisform)
	  {
		  if (password.value != password_conf.value) { 
			  alert("Passwords fields don't match");
			  return false; 
		  }
		  if (password.value.length < 6) {
			  alert("Passwords should be at least 6 characters");
			return false; 
		  }
	  }
}

function my_account_login(thisform) {
	$.cookie('signed_in', 'true');
}

function my_account_logout() {
	$.cookie('signed_in', null);
	
}


function auto_suggest() {
	//$(function() {
		
		//var postcodes = $.getJSON("ajax/search_full_data.json");
		//postcodes = $.parseJSON(postcodes);

		var projects = [{"value":"AL10","category":"Areas"},{"value":"AL2","category":"Areas"},{"value":"AL3","category":"Areas"},{"value":"BA3","category":"Areas"},{"value":"BS16","category":"Areas"},{"value":"BS26","category":"Areas"},{"value":"BS32","category":"Areas"},{"value":"BS35","category":"Areas"},{"value":"BS39","category":"Areas"},{"value":"BS6","category":"Areas"},{"value":"BS8","category":"Areas"},{"value":"EST","category":"Areas"},{"value":"GL13","category":"Areas"},{"value":"GL55","category":"Areas"},{"value":"LU6","category":"Areas"},{"value":"LU7","category":"Areas"},{"value":"MK15","category":"Areas"},{"value":"MK16","category":"Areas"},{"value":"MK18","category":"Areas"},{"value":"MK40","category":"Areas"},{"value":"MK44","category":"Areas"},{"value":"MK45","category":"Areas"},{"value":"NN13","category":"Areas"},{"value":"NR12","category":"Areas"},{"value":"OX1","category":"Areas"},{"value":"OX10","category":"Areas"},{"value":"OX13","category":"Areas"},{"value":"OX14","category":"Areas"},{"value":"OX16","category":"Areas"},{"value":"OX17","category":"Areas"},{"value":"OX2","category":"Areas"},{"value":"OX3","category":"Areas"},{"value":"OX44","category":"Areas"},{"value":"OX5","category":"Areas"},{"value":"OX6","category":"Areas"},{"value":"OX7","category":"Areas"},{"value":"OX8","category":"Areas"},{"value":"OX9","category":"Areas"},{"value":"PL13","category":"Areas"},{"value":"PL14","category":"Areas"},{"value":"PO18","category":"Areas"},{"value":"RG3","category":"Areas"},{"value":"RH12","category":"Areas"},{"value":"RH13","category":"Areas"},{"value":"RH14","category":"Areas"},{"value":"SG18","category":"Areas"},{"value":"SG19","category":"Areas"},{"value":"SK4","category":"Areas"},{"value":"SW14","category":"Areas"},{"value":"SW3","category":"Areas"},{"value":"SW5","category":"Areas"},{"value":"TQ1","category":"Areas"},{"value":"TR14","category":"Areas"},{"value":"TW92EN","category":"Areas"},{"value":"W8","category":"Areas"},{"value":"WD17","category":"Areas"},{"value":"WD23","category":"Areas"},{"value":"WD3","category":"Areas"},{"value":"WD4","category":"Areas"},{"value":"WD5","category":"Areas"},{"value":"WR11","category":"Areas"}];
		
		
		
		//$( "#keywords" ).autocomplete({ source: "ajax/autocomplete.php", delay: 0 });
		$.widget( "custom.catcomplete", $.ui.autocomplete, {
			_renderMenu: function( ul, items ) {
				var self = this,
					currentCategory = "";
				$.each( items, function( index, item ) {
					if ( item.category != currentCategory ) {
						ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
						currentCategory = item.category;
					}
					self._renderItem( ul, item );
				});
			}
		});
		
		

		$( "#keywords" ).catcomplete({
			minLength: 1,
			delay: 0,
			//source: projects
			source: "ajax/filter_feed.php"
		});
		
	//});
		
	
}




var pause_gallery = false;


function home_gallery() {
	$('#gallery_left').live('mouseover mouseout', function(event) {
		  if (event.type == 'mouseover') {
			  $(this).css('margin-top','-60px');
			  $(this).addClass('pointer');
			  pause_gallery = true;
		  } else {
			  $(this).removeClass('pointer');
			  $(this).css('margin-top','0px');
			  pause_gallery = false;
		  }
		});
	
	$('#gallery_left').live('click', function(event) {
		
		var number_of_home_gallery = $('.home_gallery_grey_outer').size();
		var previous_home_gallery = this_home_gallery-1;
		if (previous_home_gallery < 1) {
			previous_home_gallery = number_of_home_gallery;
		}
		$('#home_gallery_'+this_home_gallery).hide();
		$('#home_gallery_'+previous_home_gallery).show();
		this_home_gallery = previous_home_gallery;
		});
	
	$('#gallery_right').live('click', function(event) {
		
		var number_of_home_gallery = $('.home_gallery_grey_outer').size();
		next_home_gallery = this_home_gallery+1;
		if (next_home_gallery > number_of_home_gallery) {
			next_home_gallery = 1;
		}	
		$('#home_gallery_'+this_home_gallery).hide();
		$('#home_gallery_'+next_home_gallery).show();
		this_home_gallery = next_home_gallery;
		});
	
	
	
	$('#gallery_right').live('mouseover mouseout', function(event) {
		  if (event.type == 'mouseover') {
			  $(this).css('margin-top','-60px');
			  $(this).addClass('pointer');
			  pause_gallery = true;
		  } else {
			  $(this).removeClass('pointer');
			  $(this).css('margin-top','0px');
			  pause_gallery = false;
		  }
		});
	
}




var this_strap_no = 1;
var next_strap_number = 0;

function fade_straplines() {
	//alert('hello');
	var number_of_straps = $('.strap_text').size();
	next_strap_number = this_strap_no+1;
		if (next_strap_number > number_of_straps) {
			next_strap_number = 1;
		}	
		
	$('#strap_text_'+this_strap_no).fadeOut(2000);
	$('#strap_text_'+next_strap_number).fadeIn(2000);
	//alert(number_of_straps);
	this_strap_no = next_strap_number;
	setTimeout(function(){ fade_straplines(); }, 5000);
}


var this_home_gallery = 1;
var next_home_gallery = 0;

function fade_home_gallery() {
	//alert('hello');
	if (!pause_gallery) {
		var number_of_home_gallery = $('.home_gallery_grey_outer').size();
		next_home_gallery = this_home_gallery+1;
			if (next_home_gallery > number_of_home_gallery) {
				next_home_gallery = 1;
			}	
			
		$('#home_gallery_'+this_home_gallery).fadeOut(2000);
		$('#home_gallery_'+next_home_gallery).fadeIn(2000);
		//alert(number_of_straps);
		this_home_gallery = next_home_gallery;
	}
		
	
	
	setTimeout(function(){ fade_home_gallery(); }, 5000);
}




function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
};





function rhs_margin_top() {
	var fixed_height = $('#rhs-box-fixed').height()-135;
	$('#rhs-box-moving').css('margin-top',fixed_height+'px');
	$('#rhs-box-moving').show();
	
	
	
	
}

/*
 * 
		
		
		$( "#keywords" ).catcomplete({
			minLength: 0,
			delay: 0,
			//source: "ajax/autocomplete.php"
			//source: "ajax/search_full_data.json",
			source: function( request, response ) {
				$.ajax({
					url: "ajax/search_full_data.json",
					dataType: "jsonp",
					data: {
						//featureClass: "P",
						//style: "full",
						maxRows: 12,
						value_startsWith: request.term
					}
				/*
				,
					success: function( data ) {
						response( $.map( data.geonames, function( item ) {
							return {
								label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
								value: item.name
							}
						}));
					}
				*/	
				//});
		//	}
			
			//,
			
			
			
			
			//dataType: "jsonp",
			//data: { maxRows: 12, name_startsWith: request.term }
		//});
		/*
		.data( "catcomplete" )._renderItem = function( ul, item ) {
			return $( "<li></li>" )
				.data( "item.catcomplete", item )
				.append( "<a>" + item.value + "<br>" + item.name + "</a>" )
				.appendTo( ul );
		};
		
		*/



