



$(function()
{



	// property exhibition london banner - fade away after 10 seconds

	setTimeout("$('#pel').fadeOut()", 10000);




	
	if ( $('.magnify').size() > 0 ) {
		$('.magnify').lightBox();
		$('.slideshow').cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			pause: true
		});
		

		$('#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 = $('#location_link').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.ROADMAP
						};
		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;
				
		
		geocoder = new google.maps.Geocoder();
		geocoder.geocode( { 'address': 'TQ1 2JG'}, 
		function(results, status) {			
			if (status == google.maps.GeocoderStatus.OK) {
				
				var myOptions = {
					zoom: 14,
					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 properties = $('body').data('properties');
				for(var i=0; i<properties.length; i++ ) {
					add_pointer(map, properties[i]);
				}
								
							
								
			} 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('http://www.johncouch.co.uk/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 #7385A0; background-color: #FFFFFF; padding: 8px;";
        
       
        var innerHTML = '<a href="detail.php?id=' + property.id + '"><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.php?id=' + property.id + '">' + 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;"><img style="margin: 0 5px 0 0; float: left;" src="/img/layout/arrow1.png" /> <a style="margin-top: 5px;" href="detail.php?id=' + 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(-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;
		});	
	
/*
	// set up a reusable dialog...
	$('<div id="dialog"></div>').dialog(
	{
		autoOpen: false,
		modal: true, 
		height: 200,
		width: 500, 
		title: 'Information'
	});
	
	
	
	
	
	$('#madeToMeasure, #wallStickers').click(function(){
									   
		window.location.href = $('a',this).attr('href');
									   
	});
	
	
	
	// product order form
	$('#productOrderItemForm').submit(function()
	{
		$('input.qty', $(this)).each(function()
		{
			if ( $(this).val() > 0 )
			{
				$('#productOrderItemForm').submit();
			}
		});
		alert('<p><strong>Error</strong><br/>Please enter a quantity.</p>', 500, 200);
		return false;
	});
	
	// product position select dropdown
	// show input box if they choose 'other' from the select
	$('#productPositionSelect').change(function()
	{
		if ( $(this).val() == 'other' )
		{
			$(this).after('<input style="margin:0px 3px 0 0;" type="text" id="position_other" name="product[data][position]" value="" />');
		}
		else
		{
			$('#position_other').remove();
		}
	});
	
	
	
	
	
	
	$('#productTabs a').click(function(){
	
		$('#productTabs li').removeClass('current');
		$(this).closest('li').addClass('current');
	
		var id = $(this).attr('id').replace('link_','');
	
		$('div.information').hide();
		$('#'+id).show();
	
		return false;
	});

	
	
	
	
	// product detail - click thumb, change src of larger one
	$('#mainProductThumbs img').click(function()
	{
		var l = $(this).attr('src').replace(/_sqr/, '_l');
		$('#mainProductImage img').attr('src', l);
		
		var xl = $(this).attr('src').replace(/_sqr/, '_xl');
		$('#mainProductImage_LinkClickForLarger').attr('href', xl);
		
		
	});

	
	
	
	// product detail page - faq
	$('dl.toggleMyBits dt').toggle(
	function()
	{
		$(this).next().show();
	}, 
	function()
	{
		$(this).next().hide();
	}
	);

	
	
	// assign lightbox to product image.
	if ( $('.lightboxMe').length )
	{
		$('.lightboxMe').lightBox();
	}
	
	
	// hover product main image, shows link to see larger one...
	$('#mainProductImageWrap')
	.mouseover(function()
	{
		$('#mainProductImage_LinkClickForLarger').stop().show();
	})
	.mouseout(function()
	{
		$('#mainProductImage_LinkClickForLarger').stop().hide();
	});

	
	
	
	
	
	// alt table rows - basket
	$('table.stripes > tbody > tr:even').addClass('even');
	
	
	
	
	// checkout / admin create order 
	$('.checkoutBlockHead').click(function()
	{
		$('.checkoutBlock').slideUp(300);
		$( '#' + $(this).attr('id').replace(/head/, 'checkout') ).slideDown(400);
	});
	
	$('input').bind('keyup click', function()
	{
		checkRequiredFields();
	});
	
	checkRequiredFields();
	
	
	
	
	
	
	
	// order display screen - image size toggle
	$('#toggleImageSizesLink').toggle(
	function()
	{
		$('td.imageCell').width('90px');
		$('td.imageCell img').width('90px');
	}, 
	function()
	{
		$('td.imageCell').width('45px');
		$('td.imageCell img').width('45px');
	});

	*/

});

