$(document).ready( function() {
	$.ajaxSetup({
		cache: false
	});
	
	$('a[href=#]').attr('href','javascript:void(0);');
	
	$('.navButton').live( 'mouseover', function() {
		var oldSrc = $(this).attr('src').split('.');
		$(this).attr( 'src', oldSrc[0] + '_hover.' + oldSrc[1] );
	});
	
	$('.navButton').live( 'mouseout', function() {
		var oldSrc = $(this).attr('src');
		$(this).attr( 'src', oldSrc.replace('_hover','') );
	});
	
	if ( document.getElementById('showcase') != null ) {
		$('#showcase').cycle({ 
			fx: 'fade'
		});
	}
	
	if ( document.getElementById('contactTrigger') != null ) {
		$('#contactTrigger').live('click', function() {
			$.facybox(function() {
				$.get('/forms/contact.asp', function(data) { $.facybox(data) })
			});
			
		});
		$('#cancelContactTrigger').live('click', function() {
			$(document).trigger('close.facybox');
		});
		$('#closeTrigger').live('click', function() {
			$(document).trigger('close.facybox');
		});
		$('#sendContactTrigger').live('click', function() {
			$('#validationMessage').empty().hide();
			
			var errors = [];
			
			var contactName = $('#contactName').val();
			var contactEmail = $('#contactEmail').val();
			var contactMessage = $('#contactMessage').val();
			
			if ( contactName == '' ) { errors.push('Your name is required'); }
			if ( contactEmail == '' ) { errors.push('Your email address is required'); }
			if ( contactMessage == '' ) { errors.push('Your message to us is required'); }
			
			if ( errors.length > 0 ) {
				var errorMessage = '<p>The following errors must be corrected before your message can be sent:</p>';
				errorMessage += '<ol>';
				for ( var i=0, j=errors.length; i<j; i++ ) {
					errorMessage += '<li>' + errors[i] + '</li>';
				}
				errorMessage += '</ol>';
				$('#validationMessage').append(errorMessage).show();
			} else {
				var contactData = $('#contactForm').serialize();
				$('#esceeFormContent').empty().html('<img src="/images/facybox/loading.gif" alt="sending..." />');
				$.post('/forms/contact.asp', contactData, function(data) {
					$('#esceeFormContent').html(data);
				});
			}
		});
	}
	
	if ( document.getElementById('applyTrigger') != null ) {
		$('#applyTrigger').live('click', function() {
			$.facybox(function() {
				$.get('/forms/careers.asp', function(data) { $.facybox(data) })
			});
			
		});
		$('#cancelApplyTrigger').live('click', function() {
			$(document).trigger('close.facybox');
		});
		$('#closeTrigger').live('click', function() {
			$(document).trigger('close.facybox');
		});
		
		$('#doApplyTrigger').live('click', function() {
			$('#validationMessage').empty().hide();
			
			var errors = [];
			
			var contactName = $('#contactName').val();
			var contactEmail = $('#contactEmail').val();
			var contactPhone = $('#contactPhone').val();
			var coverLetter = $('#coverLetter').val();
			
			if ( contactName == '' ) { errors.push('Your name is required'); }
			if ( contactEmail == '' ) { errors.push('Your email address is required'); }
			
			if ( errors.length > 0 ) {
				var errorMessage = '<p>The following errors must be corrected before your resume can be sent:</p>';
				errorMessage += '<ol>';
				for ( var i=0, j=errors.length; i<j; i++ ) {
					errorMessage += '<li>' + errors[i] + '</li>';
				}
				errorMessage += '</ol>';
				$('#validationMessage').append(errorMessage).show();
			} else {
				$('#partOne').hide();
				$('#partTwo').show();
				new AjaxUpload('upload_button', {
					action: '/forms/careers_submit.asp',
					data: {
						contactName : contactName,
						contactEmail : contactEmail,
						contactPhone : contactPhone,
						coverLetter : coverLetter
					},
					onSubmit: function(file, extension) {
						// allow only 1 upload
						this.disable();
						// allow only pictures
						if (! (extension && /^(txt|rtf|doc|pdf)$/.test(extension))) {
							// extension is not allowed
							var errorMessage = '<p>Only .txt, .rtf, .doc, or .pdf files may be uploaded</p>';
							$('#validationMessage').append(errorMessage).show();
							// reenable
							this.enable();
							// cancel upload
							return false;
						}
					},
					
					onComplete: function(file, response) {
						this.enable();
						var upload_response = eval( '(' + response + ')' ); // SO FUCKING UNSAFE -- should be parsed
						if (upload_response.success == "false" ) {
							$('#esceeFormContent').empty().html('<div id="errorMessage"><p>We\'re sorry, but your resume could not be sent.</p><p>Please feel free to call us directly at (480) 505-0919 or email us at <a href="mailto:escee@propertypartner.com">escee@propertypartner.com</a></p></div><br /><p><a href="javascript:void(0);" name="closeTrigger" id="closeTrigger">Close</a></p>');
						} else {
							$('#esceeFormContent').empty().html('<div id="successMessage"><p>Your resume was sent successfully.</p><p>You can expect a response from us if we feel that you may be a good fit for us.</p></div><br /><p><a href="javascript:void(0);" name="closeTrigger" id="closeTrigger">Close</a></p>');
						}
					}
				});
			}
		});
	}
	
	if ( document.getElementById('listingShowcase') != null ) {
		var currentPosition = 0;
		var leftBound = 0;
		var rightBound;
		var numListings = listings.length;
		if ( numListings % 3 == 0 ) {
			rightBound = (numListings / 3) - 1;
		} else {
			rightBound = ((numListings + (3 - (numListings % 3))) / 3) - 1;
		}
		$('#scrollRightTrigger').click(function(event) {
			currentPosition += 1;
			if ( currentPosition > rightBound ) { currentPosition = leftBound; }
			currentMargin = ( -1 * currentPosition * ( 223 * 3 ) );
			
			$('#listings')
			.animate(
				{ marginLeft: currentMargin }, {
				duration: 'slow',
				easing: 'easeInOutSine'
				});
		});
		$('#scrollLeftTrigger').click(function(event) {
			currentPosition -= 1;
			if ( currentPosition < leftBound ) { currentPosition = rightBound; }
			currentMargin = (  -1 * currentPosition * ( 223 * 3 ) );
			
			$('#listings')
			.animate(
				{ marginLeft: currentMargin }, {
				duration: 'slow',
				easing: 'easeInOutSine'
				});
		});
		
		$('.listing').live('click', function() {
			var currPropertyId = $(this).attr('rel');
			for ( var i=0, j=listings.length; i<j; i++ ) {
				if ( listings[i].id == currPropertyId ) {
					// set showcase info
					$('#listingShowcase').css('background', 'url('+listings[i].image+')');
					$('#listingTitle').text(listings[i].title);
					$('#listingPrice').text(listings[i].price);
					$('#listingAddress').text(listings[i].address);
					$('#flyerTrigger').attr('rel', listings[i].flyer);
					$('#contactBrokerTrigger').attr('rel', listings[i].title);
					break;
				}
			}
		});
		
		$('#flyerTrigger').live('click', function() {
			var currentFlyer = $(this).attr('rel');
			window.open(currentFlyer);
		});
		
		$('#contactBrokerTrigger').live('click', function() {
			var currPropertyName = $(this).attr('rel');
			$.facybox(function() {
				$.get('/forms/contactBroker.asp?propertyName='+currPropertyName, function(data) { $.facybox(data) })
			});
			
		});
		$('.contactBrokerTrigger').live('click', function() {
			var currPropertyName = $(this).attr('rel');
			$.facybox(function() {
				$.get('/forms/contactBroker.asp?propertyName='+currPropertyName, function(data) { $.facybox(data) })
			});
			
		});
		$('#cancelContactBrokerTrigger').live('click', function() {
			$(document).trigger('close.facybox');
		});
		$('#closeTrigger').live('click', function() {
			$(document).trigger('close.facybox');
		});
		$('#sendContactBrokerTrigger').live('click', function() {
			$('#validationMessage').empty().hide();
			
			var errors = [];
			
			var contactName = $('#contactName').val();
			var contactEmail = $('#contactEmail').val();
			var contactMessage = $('#contactMessage').val();
			
			if ( contactName == '' ) { errors.push('Your name is required'); }
			if ( contactEmail == '' ) { errors.push('Your email address is required'); }
			if ( contactMessage == '' ) { errors.push('Your message to us is required'); }
			
			if ( errors.length > 0 ) {
				var errorMessage = '<p>The following errors must be corrected before your message can be sent:</p>';
				errorMessage += '<ol>';
				for ( var i=0, j=errors.length; i<j; i++ ) {
					errorMessage += '<li>' + errors[i] + '</li>';
				}
				errorMessage += '</ol>';
				$('#validationMessage').append(errorMessage).show();
			} else {
				var contactData = $('#contactForm').serialize();
				$('#esceeFormContent').empty().html('<img src="/images/facybox/loading.gif" alt="sending..." />');
				$.post('/forms/contactBroker.asp', contactData, function(data) {
					$('#esceeFormContent').html(data);
				});
			}
		});
		
		$('.sendContactBrokerTrigger').live('click', function() {
			$('#validationMessage').empty().hide();
			
			var errors = [];
			
			var contactName = $('#contactName').val();
			var contactEmail = $('#contactEmail').val();
			var contactMessage = $('#contactMessage').val();
			
			if ( contactName == '' ) { errors.push('Your name is required'); }
			if ( contactEmail == '' ) { errors.push('Your email address is required'); }
			if ( contactMessage == '' ) { errors.push('Your message to us is required'); }
			
			if ( errors.length > 0 ) {
				var errorMessage = '<p>The following errors must be corrected before your message can be sent:</p>';
				errorMessage += '<ol>';
				for ( var i=0, j=errors.length; i<j; i++ ) {
					errorMessage += '<li>' + errors[i] + '</li>';
				}
				errorMessage += '</ol>';
				$('#validationMessage').append(errorMessage).show();
			} else {
				var contactData = $('#contactForm').serialize();
				$('#esceeFormContent').empty().html('<img src="/images/facybox/loading.gif" alt="sending..." />');
				$.post('/forms/contactBroker.asp', contactData, function(data) {
					$('#esceeFormContent').html(data);
				});
			}
		});
		
		$('#listTrigger').live('click', function() {
			$(this).hide();
			$('#listingShowcase').hide();
			$('#listingsControl').hide();
			$('#hideListTrigger').show();
			$('#listingsListView').show();
		});
		$('#hideListTrigger').live('click', function() {
			$(this).hide();
			$('#listingsListView').hide();
			$('#listTrigger').show();
			$('#listingShowcase').show();
			$('#listingsControl').show();
		});
		
		$('.listingImageList').live('click', function() {
			var largeImage = $(this).attr('rel');
			$.facybox({ image: largeImage });
			
		});
 	}
});
