(function(){
	
	EYE.uploadPhotos = function(){
		var maxInputs = 8,
			addField = function() {
				if (maxInputs > 0) {
					changeInputs(-1);
  					tmpid = Math.ceil(Math.random()*10000);
					$('#propertyPhotos').append('<p><label for="">Photo: </label><input class="textField" name="photo['+tmpid+']" type="file" />&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" class="removePhotoField">delete</a></p>');
				}
				return false;
			},
			click = function(ev) {
				var trgtEl = $(ev.target).closest('a');
				if (trgtEl.size() > 0) {
					if (trgtEl.is('.removePhotoField')) {
						changeInputs(1);
						trgtEl.parent().remove();
						return false;
					} else if (trgtEl.is('.removeTmpImage')) {
				  		img = trgtEl.attr('rel');
				  		imgId = trgtEl.attr('id');
				  		$.ajax({
							type: "POST",
							url: "/ajaxserver.php",
							data: $.param({
								plugin: 'deleteTempImage',
								imgId: imgId,
								img: img
							}),
							success: function(){
								trgtEl.parent().remove();
								changeInputs(1);
							}
						 });
						 return false;
					} else if ('a.removeImage') {
						img = trgtEl.attr('rel');
						imgId = trgtEl.attr('id');
						$.ajax({
						type: "POST",
						url: "/ajaxserver.php",
						data: $.param({
								plugin: 'deleteImage',
								imgId: imgId,
								img: img
							}),
						success: function(){
							trgtEl.parent().remove();
							changeInputs(1);
						}
						});
						 return false;
					}
				}
			},
			changeInputs = function (dir) {
				maxInputs = Math.max(0,Math.min(8, maxInputs + dir));
				$('#addPhotoField').css('display', maxInputs == 0 ? 'none' : '');
			};
		return {
			init: function() {
				maxInputs -= $('#propertyPhotos input').size() + $('#uploadedPhotos img').size();
				$('#addPhotoField').bind('click', addField);
				$('#propertyPhotos, #uploadedPhotos').bind('click', click);
			}
		};
	}();
	EYE.register(EYE.uploadPhotos.init, 'init');
})();
jQuery(function($) {
  // provinces 
  provincesVal = $('select#provinces').val();
  if(provincesVal == 'other'){
  	$('#other_province').show();
  }else{
  	$('#other_province').hide();
  }
  
  $('select#provinces').bind(
  	'change',
  	function(){
  		provinceId = $(this).val();
  		if(provinceId == 'other'){
  			$('#other_province').show();
  			$('select#communities').html('<option value="">Choose one option</option><option value="other">other...</option>');
  		}else{
  			$('select#communities').attr('disabled', true);
  			$('#other_province').hide();
	  		$.ajax({
			   type: "POST",
			   url: "/ajaxserver.php",
			   data: "plugin=getCommunities&province="+provinceId,
			   success: function(msg){
			   		$('select#communities').html($('communities', msg).text());
			   		$('select#zones').html('<option value="">Choose one option</option>');
			   		$('select#communities').attr('disabled', false);
			   }
			});
  		}
  	}
  );
  
  communitiesVal = $('select#communities').val();
  if(communitiesVal == 'other'){
  	$('#other_community').show();
  }else{
  	$('#other_community').hide();
  }
  
  $('select#communities').bind(
  	'change',
  	function(){
  		communityId = $(this).val();
  		if(communityId == 'other'){
  			$('#other_community').show();
  			$('select#zones').html('<option value="">Choose one option</option><option value="other">other...</option>');
  		}else{
  			$('select#zones').attr('disabled', true);
  			$('#other_community').hide();
	  		$.ajax({
			   type: "POST",
			   url: "/ajaxserver.php",
			   data: "plugin=getZones&community="+communityId,
			   success: function(msg){
			   		$('select#zones').html($('zones', msg).text());
			   		$('select#zones').attr('disabled', false);
			   }
			});
  		}
  	}
  );
  
  zonesVal = $('select#zones').val();
  if(zonesVal == 'other'){
  	$('#other_zone').show();
  }else{
  	$('#other_zone').hide();
  }
  
  $('select#zones').bind(
  	'change',
  	function(){
  		zoneId = $(this).val();
  		if(zoneId == 'other'){
  			$('#other_zone').show();
  		}else{
  			$('#other_zone').hide();
  		}
  	}
  );
  
  // tell a friend
  $('#menuTellafriend').bind(
		'click',
		function() {
			tellForm = $('#tellafriend_form');
			if (tellForm.css('display') == 'none') {
				tellForm.show().animate({top:1}, 300);
			} else {
				tellForm.animate({top:-400}, 300, function(){$(this).hide()});
			}
			this.blur();
			return false;
		}
	);
	$('#tell_cancel').bind(
		'click',
		function() {
			tellForm = $('#tellafriend_form');
			if (tellForm.css('display') == 'block') {
				tellForm.animate({top:-400}, 300, function(){$(this).hide()});
			}
			this.blur();
			return false;
		}
	);
	$('#tell_send').bind(
		'click',
		tellAFriend
	);
	
	$("a[rel^='gallery']").fancybox();
  
});

var tellAFriend = function()
{
	$('#tell_send, #tell_cancel').attr('disabled', true);
	$('#tell_ajax_indicator').css('display', 'inline');
	
	$.ajax(
		{
			type			: 'POST',
			url				: '/ajaxserver.php',
			data			: 'plugin=tellAFriend&email=' + $('#tell_email').val() + '&name=' + $('#tell_name').val() + '&friend_email=' + $('#tell_friends_email').val() + '&friend_name=' + $('#tell_friends_name').val()  + '&message=' + $('#tell_message').val()  + '&url=' + window.location.href,
			complete		: function()
			{
				$('#tell_send, #tell_cancel').attr('disabled', false);
				$('#tell_ajax_indicator').hide();
			},
			
			success			: function(xml)
			{
				errors = $('errorResponse', xml);
				okies = $('ok', xml);
				
				errors.each(
					function()
					{
						alert(this.firstChild.nodeValue);
					}
				);
								
				if (errors.size() == 0) {
					var message = $('message', xml)[0].firstChild.nodeValue;
					alert(message);
					$('#tell_cancel').trigger('click');
				}
			}
		}
	);
	this.blur();
	return false;
}
