

function print_r(theObj)
{
	if(theObj.constructor == Array || theObj.constructor == Object)
	{
		document.write("<ul>")
		
		for(var p in theObj)
		{
			if(theObj[p].constructor == Array || theObj[p].constructor == Object)
			{
				document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
				document.write("<ul>")
				print_r(theObj[p]);
				document.write("</ul>")
			} 
			else 
			{
				document.write("<li>["+p+"] => "+theObj[p]+"</li>");
			}
		}
		document.write("</ul>")
	}
}



function wpfp_do_js( dhis, doAjax ) 
{
    loadingImg = dhis.prev();
    loadingImg.show();
    beforeImg = dhis.prev().prev();
    beforeImg.hide();

    url = document.location.href.split('#')[0];

    params = dhis.attr('href').replace('?', '') + '&ajax=1';

    if (doAjax) 
	{
        jQuery.get(url, params, function(data)
		{
                dhis.parent().html(data);

                if(typeof wpfp_after_ajax == 'function') 
				{
                    wpfp_after_ajax(dhis); // use this like a wp action.
                }
                loadingImg.hide();
            }
        );
    }
}


function preloadImage(imgPath) 
{
	if($('#loader:animated').length != 0) 
	{
		return false;
	} 
	else 
	{
		var img = new Image();

		$(img).load(function() 
		{
			$(this).hide();
			
			$('#featuredimage').removeClass('loading').html(this);

			//$('.fullSizeImage').wrap('<a class="fullImageLink"></a>');
			//$('#loader a').attr('href', imgPath);

			//$(this).css({"height": 'auto', "width": 450});

			//$(this).css({"height": 'auto'});
			
			iHeight = $(this).height();
			
			lHeight = $('#featuredimage').height();
			
			//lHeight = $('#featuredimage img').height(); // how high is the current image?

			if(lHeight == iHeight) 
			{
				$(this).fadeIn();
			} 
			else 
			{
				tempImg = $(this);

				//$('#featuredimage').animate({height: iHeight}, 500).animate({width: 450}, 500, function()
				
				$('#featuredimage').animate({height: iHeight}, 200, function()
				{
					$(tempImg).fadeIn();
				});
			}
		}).error(function() 
		{
			//$('#status').css('display', 'block').html('failed');
		}).attr('src', imgPath)
			
		//.addClass('fullSizeImage');
	}
}



$(function()
{
	
	$('div#featuredproperties div#featured').cycle(
	{
		fx: 'fade',
		timeout:  4000,
		speed:  250,
		pause:  1,
		random: 1
	});


	$('div#nav ul li a')
		.css( {backgroundPosition: "50% 100%"})
		.mouseover(function()
		{
			$(this).find('span').stop().animate({backgroundPosition:"(50% 85%)"}, {duration:200})
		})
		.mouseout(function()
		{
			$(this).find('span').stop().animate({backgroundPosition:"(50% 100%)"}, {duration:200})
		})

	
	/* 
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	~	SEARCH
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	*/

	if($('div#searchform').length)
	{
		var val_pricemin	= $('input[name="price_min"]').val();
		var val_pricemax	= $('input[name="price_max"]').val();
		var thebedval		= $('input#beds').val();
		var thewhereval		= $('input#wheres').val();
		var theavailminval	= $('input#availablemin').val();
		var theavailmaxval	= $('input#availablemax').val();
		

		$('div.choice ul li a').click(function()
		{
			var thisid		= $(this).closest('div.choice').attr('id');
			var thisval		= $(this).attr('rel');
			var thishtml	= $(this).html();

			//alert(thisid);
			
			if($(this).parent().hasClass("active"))
			{
				// nada
				//$(this).parent().removeClass("active");
			}
			else
			{
				$(this).parent().siblings().removeClass("active");
				$(this).parent().addClass("active");
				
				//var theval = $(this).find("span").html();
				//$("input#beds").val(bedval);
				//$("span#textbeds").text(bedval);	
			}

			$('span[rel="' + thisid + '"] b').html(thishtml);

			$('input[rel="' + thisid + '"]').val(thisval);

			return false;
		});	
		

		$("#thepriceslider").slider({
			range: true,
			min: 40,
			max: 160,
			step: 10,
			values: [val_pricemin, val_pricemax],
			slide: function(event, ui)
			{
				var pricemin = ui.values[0];
				var pricemax = ui.values[1];

				$('.ui-slider-handle:first').html('&pound;' + pricemin);
				$('.ui-slider-handle:last').html('&pound;' + pricemax);	

				// actual value for form
				$('input[name="price_min"]').val(ui.values[0]);
				$('input[name="price_max"]').val(ui.values[1]);
				
				// results paragraph
				$('span#ss_pricemin b').html(ui.values[0]);
				$('span#ss_pricemax b').html(ui.values[1]);
			}
		});

		$(".ui-slider-handle:first").addClass('first-child');
		$(".ui-slider-handle:last").addClass('last-child');

		$(".ui-slider-handle:first").html('&pound;' + val_pricemin);
		$(".ui-slider-handle:last").html('&pound;' + val_pricemax);	

		$(".ui-slider-range").append('<span></span>');


	}




	/* 
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	~	SERP
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	*/

	if($('div#mapsidebar').length)
	{
		$('div#mapsidebar ul li a').click(function()
		{
			
			themarker = $(this).attr('rel');

			if($(this).parent().hasClass("active"))
			{
				// already active, do nothing
			}
			else
			{
				$(this).parent().siblings().removeClass("active");
				$(this).parent().addClass("active");
							
				google.maps.event.trigger(markers[themarker], "click");
			}

			return false;
		
		});

	}




	/* 
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	~	MAP FOR PROPERTY PAGE
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	*/

	if($('ul#hotspots').length)
	{
		$('ul#hotspots li a').click(function()
		{
			var searchterm = $(this).attr('rel');

			//alert(searchterm);

			gLocalSearch.setCenterPoint(thell);
			
			//gMap.panTo(gMap.getCenter());

			gLocalSearch.execute(searchterm);


			if($(this).parent().hasClass("selected"))
			{
				// do nothing
				//alert('isnt selected');
			}
			else
			{
				//alert('is selected');
				$(this).parent().siblings().removeClass("selected");
				$(this).parent().addClass("selected");
			}

			return false;

		});
	}
	

	// FAVOURITE POSTS / PROPERTIES

	$('.wpfp-link').live('click', function() 
	{
		dhis = $(this);

		wpfp_do_js(dhis, 1);

		// for favorite post listing page
		if (dhis.hasClass('remove-parent')) 
		{
			dhis.parent("li").fadeOut();
		}

		return false;

	});



	
	$("div#photos ul li a").click(function()
	{
		var imgThmb = $(this).attr("href");	
		//var imgPath = imgThmb.replace(/-\d+x\d+/gi, '');

		//$('.fullSizeImage').fadeOut(500, function(){

		$('div#featuredimage img').fadeOut(200, function()
		{
			$('#featuredimage').addClass('loading');
			
			//alert(imgThmb);

			//preloadImage(imgPath);
		
			preloadImage(imgThmb);

		});

		return false;

	});


	$(".gallery:first a[rel^='prettyPhoto']").prettyPhoto(
	{
		animation_speed:'slow',
		theme:'light_square',
		slideshow:3000, 
		autoplay_slideshow: true, 
		show_title: false,
		deeplinking:false,
		social_tools:false
	});

	//$(".gallery:gt(0) a[rel^='prettyPhoto']").prettyPhoto({animation_speed:'fast',slideshow:10000, hideflash: true});

	$(".gallery:gt(0) a[rel^='prettyPhoto']']").prettyPhoto(
	{
		animation_speed:'slow',
		social_tools:false,
		show_title:false,
		allow_resize:true,
		deeplinking:false,
		overlay_gallery:false,
		keyboard_shortcuts:false
	});

	// LODGE SLIDE
	
	$('.slideshow').cycle({
		fx:     'fade', 
		speed:   1000, 
		speedIn: 600,
		speedOut: 600
	});
	
	
});
