(function($) {
  var cache = [];
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

$(document).ready(function(){
	
	// Property Search
	
	$("#search_type").change(function(){
		if( $(this).val() == "for-sale" ){
			$("#prices_let").hide();
			$("#prices_sale").show();
		}else if( $(this).val() == "to-let" ){
			$("#prices_sale").hide();
			$("#prices_let").show();
		}
	}).change();
	
	// Sort by menu
	
	$("#sortButton, #sortMenu").hover(function(){
		$("#sortMenu").css("height","59px");
	},function(){
		$("#sortMenu").css("height","0");
	});
	
	// Map
	
	$("#buttonMap").click(function(){
		
		var postcode = $(this).attr("data-map");
		
		$.colorbox({
			html: '<div id="property_map"></div>',
			onComplete: function(){
				
				geocoder = new google.maps.Geocoder();
				
				var latlng = new google.maps.LatLng(0,0);
				
				var myOptions = {	
					zoom: 15,
					center: latlng,
					mapTypeId: google.maps.MapTypeId.ROADMAP
				}
				
				map = new google.maps.Map(document.getElementById("property_map"), myOptions);
				
				if(geocoder){
					geocoder.geocode( { 'address': postcode, 'country': 'UK'}, function(results, status) {
						if(status == google.maps.GeocoderStatus.OK) {
							map.setCenter(results[0].geometry.location);
							var marker = new google.maps.Marker({
								map: map, 
								position: results[0].geometry.location
							});
						}else{
							alert( "Unable to locate this property on map." );
						}
					});
				}
			}
		});
		
	});
	
	// Photo enlarge
	
	$("#photo_main, #buttonEPC, #buttonFloorPlan").colorbox({});
	
	// Gallery
	
	var thumbs_animating = false;
	var thumb_offset = 0;
	var photo_count = $("#photo_thumbs li").length;
	
	$("#thumb_next").click(function(){
		
		if( thumbs_animating || $(this).hasClass("disabled") ){ return; }
		thumbs_animating = true;
		$("#thumb_prev").removeClass("disabled");
		
		thumb_offset = thumb_offset + 1;
		$("#photo_thumbs ul").animate({
			left: "-=85px"
		}, 300, "swing", function(){
			if( thumb_offset + 3 == photo_count ){ $("#thumb_next").addClass("disabled"); }
			thumbs_animating = false;
		});
	});
	
	$("#thumb_prev").click(function(){
		
		if( thumbs_animating || $(this).hasClass("disabled") ){ return; }
		thumbs_animating = true;
		$("#thumb_next").removeClass("disabled");
		
		thumb_offset = thumb_offset - 1;
		$("#photo_thumbs ul").animate({
			left: "+=85px"
		}, 300, "swing", function(){
			if( thumb_offset == 0 ){ $("#thumb_prev").addClass("disabled"); }
			thumbs_animating = false;
		});
	}).addClass("disabled");
	
	$("#photo_thumbs a").hover(function(){
		$("#photo_main").attr("href",$(this).attr("href")).css( "background-image", "url('"+$(this).attr("data-photo")+"')" );
		return false;
	});
	
	$("#photo_thumbs a").each(function(){
		$.preLoadImages( $(this).attr("data-photo") );
	});
	
});

