$(document).ready(function(){
	/**
	 * REGISTER FORM - CHANGE PRICES FOR BUY/RENT
	 */	
	register_update_prices();
	
	
	/**
	 * ANIMATED LINKS
	 */
	//animated_links();
	
	// check that an animiated page is open
	// if it is then animated to the clicked page
	/*
	$('#navigation a').click(function(){
		var href = $(this).attr('href');
		// check login/register page is displayed
		if($("#shadowBoxWrapper").length && (href != '/more/contact-us' || href != '/register')) {
			// get page contents
			$.ajax({
				type:"GET",
				url:href,
				data:"ajax=1",
				success:function(msg) {
					//build html object
					var tmp = $(msg);
					// get contents
					var html = tmp.find('#content').html();
					// change the document title
					document.title = tmp.find('#content h1').html();
					// slide in current
					$('#content').slideToggle("slow",function(){
						// update html
						$('#content').html( html );
						// slide out
						$('#content').slideToggle("slow");
						// re-assign all click handlers
						ajax_reassign();
					});
				}
			});
		return false;
		}
	});
	*/

	/**
	 * GOLD NAV BAR HIGHLIGHTING
	 */
	$("ul#nav_gold li").hover(function() {

		if(this.id != "nav_gold_current" && $(this).children("a")[0]) {
			$("#nav_gold_current").removeClass("selected");
		}

	}, function() {

		if(this.id != "nav_gold_current" && $(this).children("a")[0]) {
			$("#nav_gold_current").addClass("selected");
		}

	});
	
	/**
	 * NAVIGATION
	 */
	$("ul#navigation li").hoverIntent(
		function(){
			var li = $(this);
			li.addClass('hover');
			li.find("ul").slideToggle("slow")
		},
		function(){
			var li = $(this);
			$(this).find('ul').slideToggle("slow",function(){
				li.removeClass('hover');
			});
		}
	);
	
	
	/**
	 * AJAX FORMS
	 */
	if( $('.form_ajax').length &&  $('.form_ajax .ajax_send').length) {
		$('.form_ajax .ajax_send').bind("click",function(){
			var form = $(this).parents('form');
			var action = form.attr('action');
			var data = form.serialize();
			
			$('#ajax_loader').show();

			$.ajax({
				type:"POST",
				url:action,
				data:"ajax=1&"+data,
				success:function(msg) {
				
					// redirect to a page if set & successful
					if($("#redirect").length && msg.indexOf('flash_good') != -1) {
						window.location = $("#redirect").val();
					}
				
					$('#ajax_loader').hide();
					$('#ajax_message').html(msg).fadeIn();

					// remove form input values
					if( msg.indexOf("flash_good") != -1) {
						form.find(":input:visible").val('');
					}
				}
			});
			
		return false;
		});
	}
	
	
	/**
	 * SEARCH ITEM
	 */
	if( $('#search_item_links').length ) {
		// add click handler to nav links
		$('#search_item_links a').click(function(){
			// get the href
			var href = $(this).attr('href');
			// get the container to fade in
			var container = $(href);
			// get the class name
			var classname = container.attr('class');

			$("#ajax_message").html('').hide();
			
			// remove active class from nav
			$('#search_item_links a').removeClass('active');
			$(this).addClass('active');
			
			// check the container is not already active
			if( classname.indexOf('active') == -1 ) {
				// hide the currently active container
				$('.content .active').fadeOut("normal", function(){
					// show the clicked container
					container.addClass('active').fadeIn("normal",function(){
						
						// load google map after animation
						if(href=='#map') {
							//$("#google_map").html("<img src='/imgs/icons/ajax-loader.gif' alt='Loading' />");
							if( googlemap_postcode != '' ) {
								search_postcode( googlemap_postcode );
							}
						}
					});
				}).removeClass('active');
			}
			
		return false;
		});
	}
	

	// these links appear on summary page and custom animate to specific tabs
	$('.prop_details').click(function(){
		var rel = $(this).attr('rel');
		var container = $("#"+rel);

		$('#search_item_links a').removeClass('active');
		// hide the currently active container
		$('.content .active').fadeOut("normal", function(){
			// show the clicked container
			container.fadeIn().addClass('active');
		}).removeClass('active');

		// highlight the link
		jQuery.each($('#search_item_links a'),function(){
			href = $(this).attr("href");
			if( href.indexOf(rel) != -1 ) {
				$(this).addClass('active');
			}
		});
		
	return false;
	});
	
	
	// image slider
	if( $('#pictures').length ) {
		var images = $('#pictures .image_mover img');
		var displaying = 1;
		
		// hide buttons by default
		$('#pictures .up').hide();
		$('#pictures .down').hide();
		
		// show down button if more than 3 images
		if(images.length > 3) {
			$('#pictures .down').show();
		}
		
		// deal with up/down animation when clicked
		$('#pictures .image_slide').click(function(){
			// get move direction
			var direction = ($(this).attr('rel')=='up') ? '+' : '-' ;
			// get the 1st image index
			displaying = (direction=='-') ? displaying+1 : displaying-1 ;
			// move the slider
			$('#pictures .image_mover').animate({marginTop:direction+"=118px"});
			
			// show/hide up button
			if( displaying > 1 ) {
				$('#pictures .up').show();
			} else if(displaying == 1) {
				$('#pictures .up').hide();
			}
			
			// show/hide down button
			if( images.length-displaying <= 2  ) {
				$('#pictures .down').hide();
			} else {
				$('#pictures .down').show();
			}
			
		return false;
		});
	}
	
	// enlarge big images
	if( $('#pictures').length ) {
		$("#pictures .big_image").click(function(){
			var href = $(this).attr('href');
			
			// pre-load big image and insert onto page
			var image = $("<img>").attr('src',href).load(function(){
				$("#pictures .main").append( $(this) );
				$("#pictures .main img:first").fadeOut().remove();
			});
			
		return false;
		});
	}
	
	/**
	 * ROTATE HEADER IMAGES
	 */
	$('#header_image').innerfade({
		speed: 'slow', 
		timeout: 4000, 
		type: 'sequence', 
		containerheight: '254px'
	});
});


function ajax_forms() {
	/**
	 * AJAX FORMS
	 */
	if( $('.form_ajax').length &&  $('.form_ajax .ajax_send').length) {
		$('.form_ajax .ajax_send').bind("click",function(){
			var form = $(this).parents('form');
			var action = form.attr('action');
			var data = form.serialize();
			
			$('#ajax_loader').show();

			$.ajax({
				type:"POST",
				url:action,
				data:"ajax=1&"+data,
				success:function(msg) {
					// redirect to a page if set & successful
					if($("#redirect").length && msg.indexOf('flash_good') != -1) {
						window.location = $("#redirect").val();
					}
					
					$('#ajax_loader').hide();
					$('#ajax_message').html(msg).fadeIn();
					
					// remove form input values
					if( msg.indexOf("flash_good") != -1) {
						form.find(":input:visible").val('');
					}
				}
			});
			
		return false;
		});
	}	
}

function animated_links() {
	if(!$("#shadowBoxWrapper").length) {
	$('.animated_link').click(function(){
		var href = $(this).attr('href');
		
		// if the content div doesn't exist
		if( $('#content').length == 0 ) {
			window.location = href;
		}
		
		// check page isn't already open
		if(!$("#shadowBoxWrapper").length) {
			// get contact page contents
			$.ajax({
				type:"GET",
				url:href,
				data:"ajax=1",
				success:function(msg) {
					//build html object
					var tmp = $(msg);
					// change the document title
					document.title = tmp.find('#content h1').html();
					// get contents
					var html = tmp.find('#content').html();
					// slide in current
					$('#content').slideToggle("slow",function(){
						// update html
						$('#content').html( html );
						// slide out
						$('#content').slideToggle("slow");
						// re-assign all click handlers
						ajax_reassign();
					});
				}
			});
		}
		
	return false;
	});
	}
}


/**
 * REGISTER FORM - CHANGE PRICES FOR BUY/RENT
 */
function register_update_prices() {
	if( $('#form_register').length ) {
		$("#i_am_looking_to").change(function(){
			var selected = $(this).val();
			var action = $(this).parents("form").attr('action');
			
			$('.update_price').html('<option>Loading</option>');
			
			$.ajax({
				type:"POST",
				url:action,
				data:"ajax=1&action=change_prices&selected="+selected,
				success:function(msg) {
					$('.update_price').html(msg);
				}
			});
		});
	}
}


/**
 * Start the postcode search
 * @param string pc
 */
function search_postcode(pc) {
	do_search( pc, initialize );
}

/**
 * Search for the Lat/Long for a given postcode
 * @param string postcode
 * @param function callbackFunction
 */
function do_search(postcode,callbackFunction) {
	localSearch.setSearchCompleteCallback(null, function() {
		if(localSearch.results[0]) {
			var Lat = localSearch.results[0].lat;
			var Lng = localSearch.results[0].lng;
			callbackFunction(Lat,Lng);
			//console.log(Lat+":"+Lng);
		} else {
			document.getElementById("google_map").innerHTML = '<p>A problem occured finding the Postcode, </p>';
		}
	});
	localSearch.execute(postcode + ", UK");
}

/**
 * Create the Google Map
 * @param int latitude
 * @param int longitude
 */
function initialize(latitude, longitude) {
	//var latlng = new google.maps.LatLng(53.360656, -2.581135);
	var latlng = new google.maps.LatLng(latitude, longitude);
	var myOptions = {
		zoom: 15,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("google_map"), myOptions);
	
	// create marker
	var marker = new google.maps.Marker({
		position: latlng,
		map: map
	});
}


/**
 * Re-assign all click handlers after an ajax event
 */
function ajax_reassign() {
	// re-assign form logic
	ajax_forms();
	// re-assign update prices
	register_update_prices();
}
