ADMIN_SITE_URL  = SITE_URL + 'administration/';
var property = {
	save: function() {
		if($('#create_property_form').valid() && $('.thumb_file').length > 0) {		
			parseTextAreasForTiny();
			$.ajax({
			   type: "POST",
			   url: ADMIN_SITE_URL + 'properties/ajax_save',
			   data: $('#create_property_form').serialize(),
			   success: function(html){
					if (parseInt(html)) {
						property.redraw();
						jQuery.facebox.close();
						Box.remove();
					} else {
						$('#create_error').html(html);
						$.scrollTo($('#create_error'), 800);
					}
			 }});
		} else {
			if($('.thumb_file').length <= 0) {
				$('.field').append($('<label class="error"></label>').text('Need upload Image'));
				$('.field input').addClass('error');
				$('.field input').focus();
				return;
			}
			$('#create_property_form').submit();
		}
	},
	
	redraw: function() {
		location.reload(true);
	},
	
	showCreate: function() {	
		jQuery.facebox(function() {
			$.ajax({
				   type: "POST",
				   url: SITE_URL + 'listings/ajax_show_create',
				   success: function(html){
						jQuery.facebox(html);
//						setValidate();
				   }
				 });
		});
	},	

	uploadImage: function() {
		show_loading('Uploading image...');
		JsHttpRequest.query(
			  ADMIN_SITE_URL + 'properties/ajax_upload_image', {
				  listing: document.getElementById("photo_to_upload")
			  },
			  function(result, error) {
				  	hide_loading();
				  	if (result.src) {
				  		$('#photos_container').append(result.html);
				  		$('#photo_to_upload').val('');
				  		$('a[rel*=lightbox]').lightBox();
				  	} else {
				  		jQuery.facebox('Some error occured... Try again');
				  	}
			  }
			);
	},
	
	removeImageBd: function(id) {
		if(confirm('Are you sure you want to delete this image?')){
			if($('.thumb_file').length > 1) {
				$.ajax({
					   type: "POST",
					   url: ADMIN_SITE_URL + 'properties/ajax_delete_image',
					   data: 'id=' + id,
					   success: function(html){
							if(parseInt(html)) {
								$('#photo_' + id).remove();
							} else {
								$('.field').append($('<div class="error"></div>').html(html))
								setTimeout(function(){
									$('.field .error').fadeOut("slow");
								}, 3000);
							}
								
					 }});
			} else {
				alert('You can not delete all Images');
			}
		}
	},
	
	removeImage: function(id) {
		if(confirm('Are you sure you want to delete this image?')){
			if($('.thumb_file').length > 1) {
				$('#file_' + id).remove();
				$('#photos_container').append('<input type="hidden" name="remove_image[]" value="' + id + '" />');
			} else {
				alert('You can not delete all Images');
			}
		}
	},
	
	showEdit: function(id) {
		jQuery.facebox(function() {
			$.ajax({
				   type: "POST",
				   url: SITE_URL + 'listings/ajax_show_edit',
				   data: 'id=' + id,
				   success: function(html){
						jQuery.facebox(html);
						$('textarea').each(function() {	
							   var id = '';
							   id = $(this).attr('id');
							   if (id.length) {
								   tinyMCE.execCommand('mceAddControl', false, id);
							   } else {
								   id = 'id_generator_' + ID_GENERATOR++;
								   $(this).attr('id', id);
								   tinyMCE.execCommand('mceAddControl', false, id);
							   }
						});
				 }});
		});
	},

	remove: function(id) {
		if(confirm('Are you sure you want to delete this property?')) { 
			$.ajax({
				   type: "POST",
				   url: SITE_URL + 'listings/ajax_remove_property',
				   data: '&id='+id,
				   success: function(html){
						document.location.href= SITE_URL + 'listings/showownerlistings';
//						property.redraw();
				 }});
		}
	},
	
	calculate_measure: function() {
		$('.measurement').bind('keyup', function() {
			var obj = $(this);
			var number = 0;
			$('.measurement').each(function(){
				if(obj.attr('rel') != $(this).attr('rel')) {
					if(obj.val().length > 0 && parseFloat(obj.val())) {
						$(this).val((parseFloat(obj.val())*parseFloat(property.convert(obj.attr('rel'), $(this).attr('rel')))).toFixed(3));
					} else {
						$(this).val('');
					}
				}
			});
		});
	},
	
	convert : function(sourse, dest) {
		switch(sourse) {
			case 'm_sq':
				switch(dest){
					case 'ft_sq':
						return 10.76391;
					case 'acr':
						return 0.0002471054;
					case 'hec':
						return 0.0001;
				}
				break;
			case 'hec':
				switch(dest){
				case 'ft_sq':
					return 107639.1;
				case 'm_sq':
					return 10000;
				case 'acr':
					return 2.471054;
				}
				break;
			case 'acr':
				switch(dest){
				case 'ft_sq':
					return 43560;
				case 'm_sq':
					return 4046.856;
				case 'hec':
					return 0.4046856;
				}
				break;
			case 'ft_sq':
				switch(dest){
					case 'm_sq':
						return 0.09290304;
					case 'acr':
						return 2.295684*Math.pow(10,-5);
					case 'hec':
						return 9.290304*Math.pow(10, -6);
				}
				break;		
			case 'm':
				switch(dest){
				case 'ft':
					return 3.280839895;				
				}
				break;		
			case 'ft':
				switch(dest){
				case 'm':
					return 0.30479;				
				}
				break;		
		}
	}
		
};

function setValidate() {
	$('#create_property_form').validate({
		rules: {
			company_code: {
				required: true,
				number: true
			},
			listed_name: {
				required: true
			},
			real_name: {
				required: true
			},
			showing_associate_email: {
				required: true,
				email: true
			},
			list_price: {
				number: true				
			},			
			assessment: {
				number: true				
			},			
			fire_district_tax: {
				number: true				
			},			
			real_estate_texes: {
				number: true				
			},			
			foundation_size_length: {
				number: true				
			},			
			foundation_size_width: {
				number: true				
			},			
			fee: {
				number: true				
			},			
			living_room_dimension_length: {
				number: true				
			},			
			living_room_dimension_width: {
				number: true				
			},			
			zip: {
				required: true,
				digits: true
			}
	  	}
	});
}

function toggleForm(object) {
	var object = $('#link_toggle');
	var property = $('#property_fields');
	var options = $('#internet_display_option');
	property.toggle();
	options.toggle();
	display = options.css('display');
	if(display == 'none') {
		object.text(lang.display_options);
	} else {
		object.text('Back to Property Form');
	}
}

function unsetActiveDb(object) {
	object = $(object);
	if(object.attr('checked')) {
		$('#active_db').attr({checked: false});
	}
}

function calculateElevation(obj) {
	obj = $(obj);
	if(obj.attr('id') == 'elev_m') {
		if(obj.val().length > 0  && parseFloat(obj.val())) {			
			$('#elev_ft').val((parseFloat(obj.val())*parseFloat(property.convert('m', 'ft'))).toFixed(3));
		} else {
			$('#elev_ft').val('');
		}
	} else {
		if(obj.val().length > 0  && parseFloat(obj.val())) {
			$('#elev_m').val((parseFloat(obj.val())*parseFloat(property.convert('ft', 'm'))).toFixed(3));
		} else {
			$('#elev_m').val('');
		}
	}
}
function calculateBuild(obj) {
	obj = $(obj);
	if(obj.attr('id') == 'build_sqm') {
		if(obj.val().length > 0 && parseFloat(obj.val())) {			
			$('#build_sqft').val((parseFloat(obj.val())*parseFloat(property.convert('m_sq', 'ft_sq'))).toFixed(3));
		} else {
			$('#build_sqft').val('');
		}
	} else {
		if(obj.val().length > 0  && parseFloat(obj.val())) {
			$('#build_sqm').val((parseFloat(obj.val())*parseFloat(property.convert('ft_sq', 'm_sq'))).toFixed(3));
		} else {
			$('#build_sqm').val('');
		}
	}
}