
$(function(){
	
	$("a.popup").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true
	});
	
	$("a.mapup").fancybox({
		'width'				: '75%',
		'height'			: '75%',
        'autoScale'     	: false,
        'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'speedIn'			:	800, 
		'speedOut'			:	800,
		'type'				: 'iframe'
	});
	
	
	$('.image_cycle').cycle();

	$('#servicePanel').cycle(
	{
		speed: 1000, 
		timeout: 6500
	});	
	
	$('#sayings').cycle(
	{
		speed: 1000, 
		timeout: 12000
	});	
	
	$('.addPlaceholder').addPlaceholder(
	{
		'class': 'addPlaceholder'	
	});
	
	$('#searchDefineForm select').change(function() {
	    $(this).closest("form").submit();
	});

	
	if ( $('#debug').size() == 1 ) {
		var max_height = 0;
		$('#sayings div').each(function(){
			if ( $(this).attr('cycleH') && $(this).attr('cycleH') > max_height ) {
				max_height = $(this).attr('cycleH');
			}
		});
		//account for the column headers...
		max_height = max_height + 100;
		if ( $('#content').height() < max_height ) $('#content').height(max_height);
	}
	
		
	
	if ($('#loginBandWrap').size() == 1 ) {
		$('#login_email').focus(function(){
			if ( $(this).val() == 'Enter email address' ) {
				$(this).val('');
			}
		});
		$('#login_email').blur(function(){
			if ( $(this).val() == '' ) 
				$(this).val('Enter email address');
		});
		$('#login_password_text').focus(function(){
			
			if ( $('#login_password_text').data('do_not_trigger_focus') ) {
				$('#login_password_text').data('do_not_trigger_focus',false);
				return;
			}
			
			$('#login_password_text').hide();
			$('#login_password').val('');
			$('#login_password').show('');
			$('#login_password').focus();
			
		});
		$('#login_password').blur(function(){
			if ( $('#login_password').val() == '' ) {
				$('#login_password_text').val('Enter password');
				$('#login_password_text').show();		
				$('#login_password_text').data('do_not_trigger_focus',true);		
				$('#login_password_text').focus();
				$('#login_password').hide('');
			}
							
		});
	}
	
	
	$('a.readmore').click(function () { 
		var read_more = '<span class="strong">Read More</span>';
		var read_less = '<span class="strong">Read Less</span>';
		var current = $(this).html();
		$(this).prev().slideToggle('slow'); 
		if (current == read_more) {
			$(this).html(read_less);
		}
		if (current == read_less) {
			$(this).html(read_more);
		}
		 
		return true;
	});
	
	
	
	
	function setup_pop_map() {
		if( $('#pop_map_canvas').size() != 1 ) return;
				
				var properties = $('body').data('properties');
				var lat = String(properties[0]['latitude']);
				var lng = String(properties[0]['longitude']);
				var myOptions = {
					zoom: 16,
					center: new google.maps.LatLng(lat, lng),
					mapTypeId: google.maps.MapTypeId.ROADMAP
					};
				map = new google.maps.Map(document.getElementById("pop_map_canvas"), myOptions);				
				
							
				for(var i=0; i<properties.length; i++ ) {
					marker = add_pointer(map, properties[i]);
				}
				
			
			$('pop_map_canvas').hide();

		
	}
	
	
	
	
	
	function setup_large_map() {
		if( $('#large_map_canvas').size() != 1 ) return;
				
		
		geocoder = new google.maps.Geocoder();
		geocoder.geocode( { 'address': 'TQ2 5QB'}, 
		function(results, status) {			
			if (status == google.maps.GeocoderStatus.OK) {	
				var myOptions = {
					zoom: 12,
					center: results[0].geometry.location,
					mapTypeId: google.maps.MapTypeId.ROADMAP
					};
				map = new google.maps.Map(document.getElementById("large_map_canvas"), myOptions);				
				
				map_center = results[0].geometry.location;
				
				var markers = [];
				var properties = $('body').data('properties');
				for(var i=0; i<properties.length; i++ ) {
					marker = add_pointer(map, properties[i]);
					markers.push(marker);
				}
				
				var mcOptions = {gridSize: 25, maxZoom: 13};
				var markerCluster = new MarkerClusterer(map,markers,mcOptions);
														
			} else {
				alert("Geocode was not successful for the following reason: " + status);
			}
			
			$('large_map_canvas').hide();
		});
		
	}
	function add_pointer(map, property) {			
		
		var location = new google.maps.LatLng(property.latitude, property.longitude);
		
		var marker_image = new google.maps.MarkerImage('images/layout/pins/pin_key_'+property.key+'.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 #" + property.color + "; background-color: #FFFFFF; padding: 8px;";
        
       
        var innerHTML = '<img border="0" width="150" height="100" style="float: left; margin-right: 30px;" src="' + property.image + '" />';
        innerHTML += '<div style="float: left;">';
        innerHTML += '<h2 style="margin-top: 0px;"><a href="detail.php?id=' + property.id + '">' + property.street + ', ' + property.town + '</a></h2>';
        innerHTML += '<p style="font-size: 110%; margin: 2px 0 4px;">' + property.type + '</p>';
        innerHTML += '<p style="font-size: 110%; margin: 2px 0 4px;">&pound;' + property.price + '</p>';
        innerHTML += '<p class="niceLink" style="margin-top: 0;"><a style="margin-top: 5px;" href="detail.php?p=' + property.id + '">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(-240, 0)
                ,zIndex: null
                ,boxStyle: { 
                  width: "480px",
                  height: '320px'
                 }
                ,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);
		});
        
        return marker;

	}
	
	setup_large_map();
	setup_pop_map();
	
});

