//	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);
        	}
        });
    });
});