$(document).ready(function() {
	
	var $fpp;

	$('a[rel=lightbox]').lightBox({
		imageLoading: 'http://uptonsquare.co.uk/wp-content/themes/UptonSquare/images/lightbox/loading.gif',
		imageBtnClose: 'http://uptonsquare.co.uk/wp-content/themes/UptonSquare/images/lightbox/close.gif',
		imageBtnPrev: 'http://uptonsquare.co.uk/wp-content/themes/UptonSquare/images/lightbox/prev.gif',
		imageBtnNext: 'http://uptonsquare.co.uk/wp-content/themes/UptonSquare/images/lightbox/next.gif',
		imageBlank: 'http://uptonsquare.co.uk/wp-content/themes/UptonSquare/images/lightbox/blank.gif'
	}); 

	// Property pages menu item add span. Adds a span aroung the text that follows the ~ in the page name.
  	$('.sub-menu a').each(function(){
		var cont = $(this).html();
		var sub = cont.split("~");
		if(sub[1]) {
			var res = $.trim(sub[0]) + "<span>" + $.trim(sub[1]) + "</span>";
			$(this).empty().append(res);
		}
	})
	
	// Metric / Imperial measurments toggle
	$('#convertTo').click(function(event){
		var $target = $(event.target);
		$('dd.metric').toggle();
		$('dd.imperial').toggle();
		$target.is('span') ? $target.parent().children().toggle() : $target.children().toggle();
	});

	
	// Prevent default action
	// $('#menu-main > li > a').click(function(event) {
	// 			event.preventDefault();
	// 	});
	
	// Show sub menu of hovered item
	$('#menu-main > li').hoverIntent(function() {
			var $target = $(this);
			// $fpp = $target.text().toLowerCase();
			// 			$fpp = $fpp.replace(/&/g, "and");
			// 			$fpp = $fpp.replace(/ /g, "-");
			$target.find("ul").length ? $target.find("ul").toggle('fast') : null;
		}, function(){
			var $target = $(this);
			$target.find("ul").toggle('fast')
		});
		
	$('#menu-main > li > ul > li > a').click(function(){
		$(this).parent().parent().parent().unbind('mouseenter mouseleave');
	});
		
	 // Hide all sub menues apart from those of Property Types
	 $('#menu-main > li > ul').each(function(){
	 	if($(this).parent().find('a').html() != "Property Types"){
	 		$(this).css('display', 'none');
	 	} else {
	 		$(this).parent().unbind('mouseenter mouseleave');
	 	}
	 });
		
	// show sub menu if current page is sub menu item
	$('#menu-main > li > ul > li > a[href*='+$curPage+']').each(function(){
			$(this).parent().parent().css('display', 'block');
			$(this).parent().parent().parent().unbind('mouseenter mouseleave');
	});
	
	

});
