// Set up jQuery actions
$(document).ready(function() {
   	/*$('#frontpage *').tooltip({
		extraClass: "canz"
	});*/
	
	// Validate and send emails
	$("#emailButton").submit(function() {
		//var ad_type = $("form").attr("class");
		var id = $("#id").val();
		var user_id = $("#user_id").val();
		var sender_email = $("#email").val();
		var sender_name = $("#name").val();
		var sender_message = $("#message").val();
		
		if(validateEmailForm()){
			$.ajax({
			   type: "POST",
			   url: $(this).attr("action"),
			   data: 'id=' + id + '&user_id=' + user_id + '&email=' + sender_email + '&name=' + sender_name + '&message=' + sender_message,
			   success: function(msg){
			     $("#email-form").html(msg);
			   }
			 });
		}
		
		return false
	});
	
	// Handle AJAX expiry requests
	$(".editExpiryDate").click(function() {
		$.ajax({
		  	type: "GET",
		  	url: $(this).attr("href"),
		  	dataType: "html",
		  	success: function(msg){
		  		$("#expiry-status").html(msg);
		  	}
		});
		return false;
	});
	
	// Handle AJAX bookmark requests
	$("#bookmark_save").click(function() {
		$.ajax({
		  	type: "GET",
		  	url: $(this).attr("href"),
		  	dataType: "html",
		  	success: function(msg){
		  		$(".bookmarks").html(msg);
		  	}
		});
		return false;
	});
	
	// Handle alerts
	$("#alert_add").click(function(){
		
		$("#alert_box").append($("#alert_extra_row").html());
		$("#alert_submit_box").show();
		
		return false;
		
	});
	
	$("a.alert_del").click(function(){
		
		var $rowid = $(this).attr('id').split('-');
		
		$("#alert-" + $rowid[1]).html('');
		$("#alert_submit_box").show();
		$("#alert_message").show();
		
		return false;
		
	})
	
});

// Generic AJAX GET request
function doGET(linkobj, updateobj){
	$.ajax({
	  	type: "GET",
	  	url: $(linkobj).attr("href"),
	  	dataType: "html",
	  	success: function(msg){
	  		$("#"+updateobj).html(msg);
	  	}
	});
	return false;
}

function jumpToFlat(id){
	window.location = '/flats/view/' + id;
}

function jumpToFlatmate(id){
	window.location = '/flatmates/view/' + id;
}

function jumpToChurch(id){
	window.location = '/churches/view/' + id;
}

function jumpToUser(id){
	window.location = '/users/view/' + id;
}

function toggleEmailForm(){
	/*var formstatus = document.getElementById('email-form').style.display;
	if(formstatus == 'none'){
		document.getElementById('email-form').style.display = 'block';
	}else{
		document.getElementById('email-form').style.display = 'none';
	}*/
	$("#email-form").slideToggle("normal");
}

function validateEmailForm(){
	var sender_email = document.getElementById('email');
	var sender_name = document.getElementById('name');
	var sender_message = document.getElementById('message');
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if(sender_name.value == ''){
		sender_name.style.backgroundColor = '#8EA0AA';
		return false;
	}else if(filter.test(sender_email.value) == false){
		sender_email.style.backgroundColor = '#8EA0AA';
		return false; 
	}else if(sender_message.value == ''){
		sender_message.style.backgroundColor = '#8EA0AA';
		return false;
	}else{
		return true;
	}
	
}

function setAccountType(type){
	switch(type){
		case 'flat':
		document.getElementById('flat').style.display = 'block';
		document.getElementById('flatmate').style.display = 'none';
		break;
		
		case 'flatmate':
		document.getElementById('flatmate').style.display = 'block';
		document.getElementById('flat').style.display = 'none';
		break;
		
	}
}

function showFlatPhotos(id){
	window.open('/flats/photos/' + id, 'photos', 'height=600,width=700,location=no,menubar=no,toolbar=no,scrollbars=yes');
}

function findCoords(type){
	var leftPosition = (screen.width) ? (screen.width-500)/2 : 0;
	var topPosition = (screen.height) ? (screen.height-500)/2 : 0;

	window.open('/' + type + '/googlemaps/', 'maps', 'width=520,height=500,location=no,toolbar=0,status=0,menubar=0,scrollbars=1,top='+topPosition+',left='+leftPosition);
}

function removeMap(type){
	
	if(type == 'church'){
		document.getElementById('ChurchLatitude').value = 0;
		document.getElementById('ChurchLongitude').value = 0;
		document.getElementById('ChurchZoom').value = 0;
	}else{
		document.getElementById('FlatLatitude').value = 0;
		document.getElementById('FlatLongitude').value = 0;
		document.getElementById('FlatZoom').value = 0;
	}
}

function searchMap(){
	var leftPosition = (screen.width) ? (screen.width-500)/2 : 0;
	var topPosition = (screen.height) ? (screen.height-500)/2 : 0;
	window.name = "main";
	window.open('/flats/googlemapsearch/', 'maps', 'width=540,height=540,location=no,toolbar=0,status=0,menubar=0,scrollbars=1,top='+topPosition+',left='+leftPosition);
}
