$(document).ready(function(){
	
	$("a.external").attr("target","_blank");
	
	$(".reset").bind("focus",function(){ $(this).val(''); });
  
	if(jQuery.browser.msie){
		/* Apply actions for IE */
		
		
		/* Apply actions for IE 6 */
		var msie_version = parseInt(jQuery.browser.version);
		if(msie_version == 6){
			
		}
	}
	
	$(".search_method_radio").bind("click", function(){
		$id = $(this).attr('id');
		$('.search_box').hide();
		$('#'+$id+'_box').show();
		$('#search_results').toggle();
		$('#search_results_heading').toggle();
	});
	
	// Bio
	$("#edit_profile").click(function(){
		html = $("#profile").html();
		$("#profile").html('<button class="finished_profile">Finished</button><button class="cancel_profile">Cancel</button><textarea id="profile_entry" name="profile" rows="20" cols="45">'+html.replace(/<br>/ig, "\r")+'</textarea><button class="finished_profile">Finished</button><button class="cancel_profile">Cancel</button>');
		
		$(".finished_profile").bind("click", function(){
			var profile = $("#profile_entry").val();
			$(".finish_profile").html("Saving...");
			$.post("/ajax/ajax.sstg", {action: 'editProfile', profile: profile, realtor_id:$("#realtor_id").val()}, function(data){
				$("#profile").html(data);
			});
			$("#edit_profile").show();
		});
		$(".cancel_profile").bind("click", function(){
			var profile = $("#profile_entry").val();
			$.post("/ajax/ajax.sstg", {action: 'getProfile', realtor_id:$("#realtor_id").val()}, function(data){
				$("#profile").html(data);
			});
			$("#edit_profile").show();
		});
		
		$(this).hide();
		
		return false;
	});
	
	$("#edit_image").click(function(){
		$(this).hide();
		$(this).after('<form id="image_upload" method="post" enctype="multipart/form-data" action="/includes/upload_image.sstg"><input id="image_entry" type="file" name="image" /><br /></form><button class="finished_image">Upload</button><button class="cancel_image">Cancel</button><button class="delete_image">Delete Image</button>');
		$(".finished_image").bind("click", function(){
			$("#image_upload").submit();
		});
		$(".cancel_image").bind("click", function(){
			$("#image_upload").remove();
			$("button").remove();
			$("#edit_image").show();
		});
		
		$(".delete_image").bind("click", function(){
			var deleteImage = confirm('Are you sure you want to delete your photo?');
			if(deleteImage){
				$.post("/ajax/ajax.sstg", {action: 'deleteProfileImage', realtor_id:$("#realtor_id").val()}, function(data){
					$("#staff_photo").attr('src','/images/geparnophoto_t.jpg');
				});
				$("#image_upload").remove();
				$("button").remove();
				$("#edit_image").show();
			}
		});
		
		return false;
	});
	
	// Designations
	$("#edit_designations").click(function(){
		$(this).hide();
		$.post("/ajax/ajax.sstg", {action: 'getDesignationsList', realtor_id:$("#realtor_id").val()}, function(data){
			$("#designations").html(data);
		});
		$(this).after('<button class="finished_designations">Save</button><button class="cancel_designations">Cancel</button>');
		
		$(".finished_designations").bind("click", function(){
														   
			$.post("/ajax/ajax.sstg", {'action': 'saveDesignations', 'realtor_id':$("#realtor_id").val(), 'designations[]':$("#designations_list").val()}, function(data){
				$("#designations").html(data);
			});
			$("button").remove();
			$("#edit_designations").show();
		});
		
		$(".cancel_designations").bind("click", function(){
			$.post("/ajax/ajax.sstg", {'action': 'getRealtorDesignations', 'realtor_id':$("#realtor_id").val()}, function(data){
				$("#designations").html(data);
			});
			$("button").remove();
			$("#edit_designations").show();
		});
		return false;
	});
	
	// Specialties
	$("#edit_specialties").click(function(){
		$(this).hide();
		$.post("/ajax/ajax.sstg", {action: 'getSpecialties', realtor_id:$("#realtor_id").val()}, function(data){
			$("#specialties").html(data);
		});
		$(this).after('<button class="finished_specialties">Save</button><button class="cancel_specialties">Cancel</button>');
		
		$(".finished_specialties").bind("click", function(){
														   
			$.post("/ajax/ajax.sstg", {'action': 'saveSpecialties', 'realtor_id':$("#realtor_id").val(), 'specialties':$("#specialties_list").val()}, function(data){
				$("#specialties").html(data);
			});
			$("button").remove();
			$("#edit_specialties").show();
		});
		
		$(".cancel_specialties").bind("click", function(){
			$.post("/ajax/ajax.sstg", {'action': 'getRealtorSpecialties', 'realtor_id':$("#realtor_id").val()}, function(data){
				$("#specialties").html(data);
			});
			$("button").remove();
			$("#edit_specialties").show();
		});
		return false;
	});
	
	
	// Committess
	$("#edit_committees").click(function(){
		$(this).hide();
		$.post("/ajax/ajax.sstg", {action: 'getCommitteesList', realtor_id:$("#realtor_id").val()}, function(data){
			$("#committees").html(data);
		});
		$(this).after('<button class="finished_committees">Save</button><button class="cancel_committees">Cancel</button>');
		
		$(".finished_committees").bind("click", function(){
			$.post("/ajax/ajax.sstg", {action: 'saveCommittees', realtor_id:$("#realtor_id").val(), 'committees[]':$("#committees_list").val()}, function(data){
				$("#committees").html(data);
			});
			$("button").remove();
			$("#edit_committees").show();
		});
		
		$(".cancel_committees").bind("click", function(){
			$.post("/ajax/ajax.sstg", {'action': 'getRealtorCommittees', 'realtor_id':$("#realtor_id").val()}, function(data){
				$("#committees").html(data);
			});
			$("button").remove();
			$("#edit_committees").show();
		});
		return false;
	});
	
	// Facebook
		$("#edit_facebook").click(function(){
		html = $("#facebook").html();
		$("#facebook").html('<input id="facebook_entry" name="facebook" type="input" /><button class="finished_facebook">Finished</button><button class="cancel_facebook">Cancel</button>');
		
		$(".finished_facebook").bind("click", function(){
			var value = $("#facebook_entry").val();
			$(".finished_facebook").html("Saving...");
			$.post("/ajax/ajax.sstg", {action: 'editFacebook', facebook: value, realtor_id:$("#realtor_id").val()}, function(data){
				if(data.length){
					$("#facebook").html('<a href="'+data+'">My Facebook Page</a>');
					$("#facebook_icon").attr("src","/images/edit_16.png");
				} else {
					$("#facebook").html('');
					$("#facebook_icon").attr("src","/images/add.png");
				}
			});
			$("#edit_facebook").show();
		});
		$(".cancel_facebook").bind("click", function(){
			var profile = $("#facebook_entry").val();
			$.post("/ajax/ajax.sstg", {action: 'getFacebook', realtor_id:$("#realtor_id").val()}, function(data){
				if(data.length){
					$("#facebook").html('<a href="'+data+'">My Facebook Page</a>');
					$("#facebook_icon").attr("src","/images/edit_16.png");
				} else {
					$("#facebook").html('');
					$("#facebook_icon").attr("src","/images/add.png");
				}
			});
			$("#edit_facebook").show();
		});
		
		$(this).hide();
		
		return false;
	});

	// Twitter
		$("#edit_twitter").click(function(){
		html = $("#member_twitter").html();
		$("#member_twitter").html('<input id="twitter_entry" name="twitter" type="input" /><button class="finished_twitter">Finished</button><button class="cancel_twitter">Cancel</button>');
		
		$(".finished_twitter").bind("click", function(){
			var value = $("#twitter_entry").val();
			$(".finished_twitter").html("Saving...");
			$.post("/ajax/ajax.sstg", {action: 'editTwitter', twitter: value, realtor_id:$("#realtor_id").val()}, function(data){
				if(data.length){
					$("#member_twitter").html('<a href="'+data+'">My Twitter Page</a>');
					$("#twitter_icon").attr("src","/images/edit_16.png");
				} else {
					$("#member_twitter").html('');
					$("#twitter_icon").attr("src","/images/add.png");
				}
			});
			$("#edit_twitter").show();
		});
		$(".cancel_twitter").bind("click", function(){
			var profile = $("#twitter_entry").val();
			$.post("/ajax/ajax.sstg", {action: 'getTwitter', realtor_id:$("#realtor_id").val()}, function(data){
				if(data.length){																											  
					$("#member_twitter").html('<a href="'+data+'">My Twitter Page</a>');
					$("#twitter_icon").attr("src","/images/edit_16.png");
				} else {
					$("#member_twitter").html('');
					$("#twitter_icon").attr("src","/images/add.png");
				}
			});
			$("#edit_twitter").show();
		});
		
		$(this).hide();
		
		return false;
	});

});
	$(".delete_action").click(function(){
		var del = confirm('Are you sure you want to delete this blog?');
		if(del){
			return true;	
		} else {
			return false;	
		}
	});
	
	$("form").submit(function(){
		error = false;
		$(".required").each(function(){
			if($(this).val().length == 0){
				error = true;
			}
		});
		if(error){
			alert('Please complete all required fields (*)');
			return false;	
		} else {
			return true;	
		}
	});

	function showAnswer(id){
		if('none' == $("#answer_" + id).css("display")){
			$("#answer_" + id).show();
		} else {
			$("#answer_" + id).hide();
		}
	}

