



$(function()
{
	
	$('.image_cycle').cycle();
	
	$('.message-box').fadeOut(5000);
	
	if ( $('.home-tabs').size() == 1 ) {
		$('.home-tabs').find('a').click(function(){
			var el_id = $(this).attr('href').substr(1, $(this).attr('href').length);
			$('#'+el_id).parent().children('div').not('#'+el_id).hide();
			$('#'+el_id).show();
			$(this).parent().parent().find('.active').removeClass('active');
			$(this).parent().addClass('active');
			return false;
		});
	}
	
	
	//Send to a friend relies on JS so we only show it if JS is running...
	$('.sendToAFriend').show();
	
	$('#send_to_friend_dialog').dialog({
		autoOpen: false,
		height: 'auto',
		width: 'auto',
		modal: true,
		title: 'Send To A Friend',
		resizable: false,
		dialogClass: 'dialog-send-to-friend'
	});
	

	$('#form_send_to_friend').submit(function(){

		var your_name 		= $('#your_name').val();
		var friends_name	= $('#friends_name').val();
		var friends_email	= $('#friends_email').val();
			
		var errors = false;
		if ( your_name == '' ) {
			errors = true;
			$('#your_name').css('border-color','#990000');
		}
		else {
			$('#your_name').css('border-color','#C2CBD5');
		}
		if ( friends_name == '' ) {
			errors = true;
			$('#friends_name').css('border-color','#990000');
		}
		else {
			$('#friends_name').css('border-color','#C2CBD5');
		}
		if ( friends_email == '' ) {
			errors = true;
			$('#friends_email').css('border-color','#990000');
		}
		else {
			$('#friends_email').css('border-color','#C2CBD5');
		}
		
		if ( errors === true ) {
			return false;
		}
		else {
			return true;
		}
	});
	
	$('.sendToAFriend').click(function(){
		$('#send_to_friend_dialog').dialog('open');
		
		return false;
	});

	
	$('#testis').cycle(
	{
		speed: 1000, 
		timeout: 10000
	});
	
	if ( $('#moreInfoTabs').size() == 1 ) {
		
		$('#moreInfoTabs>div').not('.active').hide();
		
		$('#moreInfoTabs>ul>li>a').click(function(){
			var tab_id = $(this).attr('href').substring(1);
			$(this).parent().siblings('.active').removeClass('active');
			$(this).parent().addClass('active');
			
			$('#moreInfoTabs>div.active').hide();
			$('#'+tab_id).show().addClass('active');
			
			
			return false;
		});
	}
	
	$('#heard_about').change(function(){
		if ( $(this).val() == 'other' ) {
			$('#div_heard_about_other').show('slow');
		} else {
			$('#div_heard_about_other').hide('slow');
		}
		
	});
	
	if ( $('#frm_worth').size() > 0 ) { 
		$('#appointment_date').focus(function(){
			if($(this).val()=='dd/mm/yyyy') $(this).val('');
		});	
		$('#appointment_date').blur(function(){
			if($(this).val()=='') $(this).val('dd/mm/yyyy');
		});
		
		$('.datepicker').datepicker({
			minDate: new Date(),
			dateFormat: 'dd/mm/yy'
		});
		
	}
	
	if ( $('.homeSearch').size() > 0 ) {
		$('.homeSearch').find('input:radio').change(function(){
			
			if($(this).val() == 'buy' ) {
				$('.property_type_buy').show();
				$('#under_offer_buy').attr('checked','checked');
				$('#under_offer_rent').removeAttr('checked');
				$('.property_type_rent').hide();
			} else {
				$('.property_type_rent').show();
				$('#under_offer_rent').attr('checked','checked');
				$('#under_offer_buy').removeAttr('checked');
				$('.property_type_buy').hide();
			}
		});
		
		$('#frmSearchPaging > select').change(function(){
			$(this).parent().submit();
		});
	}
	
	if ( $('.refineSearch').size() > 0 ) {
		$('.refineSearch').find('.type_change').change(function(){
			if($(this).val() == 'buy' ) {
				$('#under_offer_label').text('Include Under Offer, Sold STC');
			} else {
				$('#under_offer_label').text('Include Let Agreed');
			}
		});
		
		$('#frmSearchPaging > select').change(function(){
			$(this).parent().submit();
		});
	}
	
	if ( $('#frmSavedSearch').size() > 0 ) {
		
		$('#frmSavedSearch').find('input:radio').change(function(){
			if($(this).val() == 'buy' ) {
				$('.property_type_buy').show();
				$('.property_type_rent').hide();
			} else {
				$('.property_type_rent').show();
				$('.property_type_buy').hide();
			}
		});
		
	}
	
	if ( $('#frmSavedSearch_edit').size() > 0 ) {
		
		$('#frmSavedSearch_edit').find('input:radio').change(function(){
			if($(this).val() == 'buy' ) {
				$('.property_type_buy_edit').show();
				$('.property_type_rent_edit').hide();
			} else {
				$('.property_type_rent_edit').show();
				$('.property_type_buy_edit').hide();
			}
		});
		
		$('#deleteSearchButton').click(function(){
			if ( $(this).val() == 'Confirm Delete?' ) {
				document.location.href = '/property-alerts?action=delete&search_id=' + $('#search_id').val();
				return false;
			}
			
			$(this).val('Confirm Delete?');
			$(this).after('<input style="float: left; margin-right: 10px; " class="submit" id="deleteSearchCancel" type="button" value="Cancel" />');
			
			$('#deleteSearchCancel').click(function(){
				$('#deleteSearchButton').val('Delete Search');
				$('#deleteSearchCancel').remove();		
				
				return false;		
			});
			
			return false;
		});
		
	}
	
	
	if ( $('.magnify').size() > 0 ) {
		$('.magnify').lightBox();
		$('.slideshow').cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			pause: true, 
			timeout: 5000, 
		    next:   '#next', 
		    prev:   '#prev'
		});
		$('.slideshow').cycle('pause');
		

		$('#thumbs a').click(function(){
			var index = parseInt($(this).attr('rel'),10) - 1;
			$('.slideshow').cycle(index);
		});
	}
	
	if ( $('.image_cycle').size() > 0 ) {
	$('.image_cycle').cycle();
	}

	 
	
	function setup_map(){
		if ( $('#map_canvas').size() != 1 ) return;
		var address = $('#map_canvas').attr('rel');	
		if ( address == '' ) return;
		
		address = address.split(',');
		var location = new google.maps.LatLng(address[0], address[1]);
		
		var myOptions = {
						zoom: 14,
						center: location,
						mapTypeId: google.maps.MapTypeId.SATELLITE
						};
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);				
					
		map_center = location;
		marker = new google.maps.Marker({
							map: map, 
							position: location
						});
		

	}
	
	setup_map();
	
	function setup_large_map() {
		if( $('#large_map_canvas').size() != 1 ) return;

		map_center = new google.maps.LatLng(50.072786,-5.264168); 
						
		var myOptions = {
			zoom: 11,
			center: map_center,
			mapTypeId: google.maps.MapTypeId.ROADMAP
			};
		map = new google.maps.Map(document.getElementById("large_map_canvas"), myOptions);		
				
		
		
		
		var properties = $('body').data('properties');
		var local_areas= $('body').data('local_areas');
		var search_url = $('body').data('search_url');	
				
		if ( properties !== null && properties !== undefined )
		for(var i=0; i<properties.length; i++ ) {
			add_pointer(map, properties[i],search_url+'&amp;type=map');
		}	
		
		if ( local_areas !== null && local_areas !== undefined ) {
			for(var i=0; i<local_areas.length; i++ ) {
				add_area_pointer(map, local_areas[i]);
			}	
		}
		
	
		
	}
	function add_pointer(map, property, search_url) {		
		
		
		
		
		var location = new google.maps.LatLng(property.latitude, property.longitude);
		
		var marker_image = new google.maps.MarkerImage('/img/layout/pin.png');				
				
		var marker = new google.maps.Marker({
					map: map, 
					position: location,
					icon: marker_image
				});	
				
									
		var boxText = document.createElement("div");				
        boxText.style.cssText = "border: 5px solid #0B3056; background-color: #FFFFFF; padding: 8px;";
        
       
        var innerHTML = '<a href="detail-' + property.id + search_url + '"><img border="0" style="float: left; margin-right: 30px;" src="' + property.image + '" /></a>';
        innerHTML += '<div style="float: left;">';
        innerHTML += '<h2 style="margin-top: 10px;"><a href="/detail-' + property.id + search_url + '">' + property.name + '</a></h2>';
        innerHTML += '<p style="font-size: 110%; margin-top: 2px;">&pound;' + property.price + ' ' + property.tenure + '</p>';
        innerHTML += '<p style="margin-top: 0;"> <a style="margin-top: 5px;" href="/detail-' + property.id + search_url + '">Full Property Details</a></p>';
        innerHTML += '</div>';
        innerHTML += '<br clear="all" style="font-height: 0.1px; line-height: 0.1px;" />';

        $(boxText).html(innerHTML);
        
        var myOptions = {
                 content: boxText
                ,disableAutoPan: false
                ,maxWidth: 0
                ,pixelOffset: new google.maps.Size(-140, 0)
                ,zIndex: null
                ,boxStyle: { 
                  width: "600px",
                  height: '460px'
                 }
                ,closeBoxMargin: "10px"
                ,infoBoxClearance: new google.maps.Size(1, 1)
                ,isHidden: false
                ,pane: "floatPane"
                ,enableEventPropagation: false
        }; 
        var ib = new InfoBox(myOptions);
        
        google.maps.event.addListener(marker, "click", function (e) {
        	if ( $('body').data('openib') !== undefined ) {
        		$('body').data('openib').close();
        	}
        	$('body').data('openib',ib);
			ib.open(map, this);
		});

	}
	
	function add_area_pointer(map, local_area) {		
		
		
		
		
		var location = new google.maps.LatLng(local_area.latitude, local_area.longitude);
		
		var marker_image = new google.maps.MarkerImage('/img/layout/pin.png');				
				
		var marker = new google.maps.Marker({
					map: map, 
					position: location,
					icon: marker_image
				});	
				
									
		var boxText = document.createElement("div");				
        boxText.style.cssText = "border: 5px solid #0B3056; background-color: #FFFFFF; padding: 8px;";
        
       
        var innerHTML = '<img border="0" style="float: left; margin: 0 30px 20px 0;" src="' + local_area.image + '" />';

        innerHTML += '<h2 style="margin-top: 10px;">' + local_area.title + '</h2>';
        innerHTML += '<p style="margin-top: 0;">'+local_area.description+'</p>';

        innerHTML += '<br clear="all" style="font-height: 0.1px; line-height: 0.1px;" />';

        $(boxText).html(innerHTML);
        
        var myOptions = {
                 content: boxText
                ,disableAutoPan: false
                ,maxWidth: 0
                ,pixelOffset: new google.maps.Size(-140, 0)
                ,zIndex: null
                ,boxStyle: { 
                  width: "600px",
                  height: '460px'
                 }
                ,closeBoxMargin: "10px"
                ,infoBoxClearance: new google.maps.Size(1, 1)
                ,isHidden: false
                ,pane: "floatPane"
                ,enableEventPropagation: false
        }; 
        var ib = new InfoBox(myOptions);
        
        google.maps.event.addListener(marker, "click", function (e) {
        	if ( $('body').data('openib') !== undefined ) {
        		$('body').data('openib').close();
        	}
        	$('body').data('openib',ib);
			ib.open(map, this);
		});

	}
	
	setup_large_map();
	
	
	
	$('#location_link').click(function(){
			$('#map_canvas').dialog({modal: true,width:600,height:500});
			//The map sometimes breaks - this bit of code fixes it :)
			google.maps.event.trigger(map, 'resize');
			map.setZoom( map.getZoom() );
			map.setCenter(map_center);
			
	
			        
			return false;
		});	
		
		
		//This is used by the custom-search-areas page...
		if ($('.rename_shape').size() > 0 ) {
			$('.rename_shape').click(function(){
				
				$(this).parent('').children('.frm_rename').show('slow');
				
				return false;
			});
		}
	
		//This is used by the custom-search-areas page...
		if ($('.delete_shape').size() > 0 ) {
			$('.delete_shape').click(function(){
				
				if ( $('#dialog').size() != 1 ) {
					$('body').append('<div id="dialog">' +
										'<form action="" method="post">' + 
										'<input type="submit" value="Delete Area" />' +
										'<input type="button" value="Cancel" class="button_cancel" />' +
										'<input type="hidden" name="id" value="" id="delete_id" />' +
										'<input type="hidden" name="action" value="delete_shape" />' +
										'</form>' + 
										'</div>');
					$('#dialog').dialog({
						autoOpen: false,
						height: 'auto',
						width: '300',
						modal: true,
						title: 'Are you sure?',
						resizable: false
					});
					
					$('.button_cancel').click(function(){
						$('#dialog').dialog('close');
						return false;
					});
				}
				
				var delete_id = $(this).parent('').find('.shape_id').val();
				$('#delete_id').val(delete_id);
				
				$('#dialog').dialog('open');
				
				return false;
			});
		}
		
		if ( $('.shortlist').size() > 0 ) {
			$('.shortlist').click(function(){
				
				var url = $(this).attr('href');
				url += '&ajax=true';
				
				var el = this;
				
				$.getJSON(url, function(resp){
					
					if ( resp === undefined ) {
						alert('There was an unexpected error.');
						return;
					}
					
					switch ( resp.action ) {
						case 'success':
							$(el).after('<p style="color:#666; display: inline; font-weight: bold;">Shortlisted</p>')
							$(el).hide();
						break;
						case 'error':
							if ( resp.message === undefined) {
								resp.message = 'An error was returned but no message was given.';
							}
							alert(resp.message)
						break;
						case 'redirect':
							if ( resp.url === undefined) {
								alert('The script tried to redirect but didn\'t send a URL');
								return;							
							}
						
							document.location.href = resp.url;
						break;
						default:
							alert('An unknown action was used.');
					}
				
				
				});
				
				return false;
			});
		}
		
		

});

function show_custom_shape_control(saved) {
	$('#map_wrap').css('position','relative');
	var div_control = '<ul class="map_buttons">' +
						'<li><a href="#" id="control_new_area">New Area</a></li> ' + 
						'<li><a id="control_edit_area" href="#">Edit Area</a></li>';

	div_control	+=	'</ul>';
	$('#displayOptions').append(div_control);
	
	var search_url = $('body').data('search_url');
	
	$('#control_new_area').click(function(){
		if ( saved !== true ) {
			if (! confirm('Discard changes?') ) return false;
		}
		document.location.href = 'listings_custom' + search_url + '&clear=true';		
		return false;
	});

	$('#control_edit_area').click(function(){
		document.location.href = 'listings_custom' + search_url + '&edit=true';		
		return false;
	});

	$('#control_save_area').click(function(){
		return false;
	});		
	
}

function inc_stats(property_id, stat_type ) {
	var url = '/_inc_stats.php?property_id=' + property_id + '&stat_type=' + stat_type;
	$.get(url);
}
