//	Listing Page Javascript	//
$(document).ready(function() {
	/*
	var map_center = new google.maps.LatLng($.gmap.lat, $.gmap.long);
	var myOptions = {
	  zoom: 10,
	  center: map_center,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById('mapTest'), myOptions);
	*/
	//handles thumbnail image clicking
	$('img.thumb').click(function() {
		$('#photoBox').children('img:first').attr('src', $(this).attr('src'));
		img_seq_num = $(this).attr('alt').substr(-1, 1);
		$('#currentPhoto').text(img_seq_num);
		
		//update "on" thumbnail
		$('a.thumb').removeClass('on');
		$(this).parent().addClass('on');
	});
	
	//handles clicking photo next button
	$('#btnPhotoNext').click(function() {
		images = $('img.thumb');
		curr = $('#currentPhoto').html();
		next = (curr == images.length) ? 1 : parseInt(curr) + 1;

		//change the picture
		$('#photoBox').children('img:first').attr('src', images.eq(next - 1).attr('src'));
		$('#currentPhoto').text(next);
		
		//update "on" thumbnail
		$('a.thumb').removeClass('on');
		images.eq(next - 1).parent().addClass('on');
	});
	
	//handles submission of large email owner form
	$('#largeEmailSubmit').click(function(e) {
        e.preventDefault();
		//disable the submit button to avoid spamming
        //and change the button text to Sending...
        $('#largeEmailSubmit').attr({'disabled' : 'true', 'value' : 'Sending...' });
        action = $.cake.http_root+'subscriptions/contact_owner';

        $.ajax({
        	type: "POST",
        	url: action,
        	data: $("#largeContactForm").serialize(),
        	dataType: 'text',
        	success: function(html_result, status) {
        		$('#contactOwnerFeedback').html(html_result);
        		var match = /id="contactOwnerError"/i.test(html_result);
    			if(match) {
    				//if the response is an error, show the error message
    				$('#largeEmailSubmit').removeAttr('disabled');
    				$('#largeEmailSubmit').attr('value', 'Submit');
    			} else {
    				$('#largeEmailSubmit').remove();
    			}
        	},
        	error: function(obj, status) {
        		alert('Error: '+status);
        	}
        });
    });
	
	//handles submission of small email-owner form
	$('#smallEmailSubmit').click(function(e) {
        e.preventDefault();
		//disable the submit button to avoid spamming
        //and change the button text to Sending...
        $('#smallEmailSubmit').attr({'disabled' : 'true', 'value' : 'Sending...' });
        action = $.cake.http_root+'subscriptions/contact_owner';

        $.ajax({
        	type: "POST",
        	url: action,
        	data: $("#smallContactForm").serialize(),
        	dataType: 'text',
        	success: function(html_result, status) {
        		$('#moreInfoFeedback').html(html_result);
        		var match = /id="contactOwnerError"/i.test(html_result);
    			if(match) {
    				//if the response is an error, show the error message
    				$('#smallEmailSubmit').removeAttr('disabled');
    				$('#smallEmailSubmit').attr('value', 'Submit');
    			} else {
    				$('#smallEmailSubmit').remove();
    			}
        	},
        	error: function(obj, status) {
        		alert('Error: '+status);
        	}
        });
    });
	
	//handles submission of the subcription request
	$('#subscribeSubmit').click(function(e) {
        e.preventDefault();
		//disable the submit button to avoid spamming
        //and change the button text to Sending...
        $('#subscribeSubmit').attr({'disabled' : 'true', 'value' : 'Sending...' });
        action = $.cake.http_root+'subscriptions/subscribe';

        $.ajax({
        	type: "POST",
        	url: action,
        	data: $("#SubscriptionSubscribeForm").serialize(),
        	dataType: 'text',
        	success: function(html_result, status) {
        		$('#subscriptionResult').html(html_result);
        		var match = /id="contactOwnerError"/i.test(html_result);
    			if(match) {
    				//if the response is an error, show the error message
    				$('#subscribeSubmit').removeAttr('disabled');
    				$('#subscribeSubmit').attr('value', 'Submit');
    			} else {
    				$('#subscribeSubmit').remove();
    			}
        	},
        	error: function(obj, status) {
        		alert('Error: '+status);
        	}
        });
    });
	
	//handles submission of small email-owner form
	$('#modalEmailSubmit').click(function(e) {
        e.preventDefault();
		//disable the submit button to avoid spamming and change the button text to Sending...
        $('#modalEmailSubmit').attr({'disabled' : 'true', 'value' : 'Sending...' });
        action = $.cake.http_root+'subscriptions/contact_friend';

        $.ajax({
        	type: "POST",
        	url: action,
        	data: $("#modalContactForm").serialize(),
        	dataType: 'text',
        	success: function(html_result, status) {
        		$('#contactFriendFeedback').html(html_result);
        		var match = /id="contactOwnerError"/i.test(html_result);
    			if(match) {
    				//if the response is an error, show the error message
    				$('#modalEmailSubmit').removeAttr('disabled');
    				$('#modalEmailSubmit').attr('value', 'Submit');
    			} else {
					$('#modalEmailSubmit').removeAttr('disabled');
					$('#modalEmailSubmit').attr('value', 'Send');
    				//$('#modalEmailSubmit').remove();
    			}
        	},
        	error: function(obj, status) {
        		alert('Error: '+status);
        	}
        });
    });
	
	/**
	 * Function to set the marker on the map after a successful geocode
	 */
    var markersArr = [];
    function setMarker(map, marker_latLng, viewport) {
    	while(markersArr[0]) {
    		markersArr.pop().setMap(null);
    	}
    	marker = new google.maps.Marker({'map':map, 'position':marker_latLng});
    	markersArr.push(marker);
    	map.panTo(marker_latLng);
    	if(viewport)
    		map.fitBounds(viewport);    	
    }	
	
	//setup the global variables
	var latlng = new google.maps.LatLng($.gmap.lat,$.gmap.long);
    var myOptions = {
      zoom: 14,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
    };
    var map = new google.maps.Map(document.getElementById("mapBorder"), myOptions);
    setMarker(map, latlng);

    //event handler to resize the map when the tab is viewed
    $("a.butMap").click(function() {
    	google.maps.event.trigger(map, 'resize');
    	map.setCenter(latlng);
    });
});
