$(document).ready(function(){
	var zIndexNumber = 10000;
	$('div').not('div.noZ').each(function(){
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
	$('[id^="aExpand_"]').click(function(){
		var divToExpand = $(this).attr('id').replace('aExpand_', '#divExpand_');
		$(divToExpand).toggleClass('expanded');
		return false;
	});
	$("#aSubmitEmailFriend").click(function(){
		EmailFriend($(this));
		return false;
	});
	$('#aEyebrowShow_Su.anon').click(function(){
		var e = $(this);
		if (e.hasClass('active')) {
			var shownDiv = $('div.show:visible');
			$('#divShowEyebrow:visible').slideUp(300, function(){
				shownDiv.hide();
				e.removeClass('active');
			});
		}
		else {
			$('a.eyebrow.active').removeClass('active');
			$('div.show:visible').hide();
			$('#divSu').show();
			$('#divShowEyebrow:hidden').slideDown(300);
			e.addClass('active');
		}
		return false;
	});
	$('#aEyebrowShow_Si').click(function(){
		var e = $(this);
		if (e.hasClass('anon')) {
			if (e.hasClass('active')) {
				var shownDiv = $('div.show:visible');
				$('#divShowEyebrow:visible').slideUp(300, function(){
					shownDiv.hide();
					e.removeClass('active');
				});
			}
			else {
				$('a.eyebrow.active').removeClass('active');
				$('div.show:visible').hide();
				$('#divSi').show();
				$('#divShowEyebrow:hidden').slideDown(300);
				e.addClass('active');
			}
		}
		else {
			SubmitLogout();
		}
		return false;
	});
	$('#aSwitchTo_Si').click(function(){
		$('a.eyebrow.active').removeClass('active');
		$('div.show:visible').hide();
		$('#divSi').show();
		$('#aEyebrowShow_Si').addClass('active');
		return false;
	});
	$('#aSwitchTo_Su').click(function(){
		$('a.eyebrow.active').removeClass('active');
		$('div.show:visible').hide();
		$('#divSu').show();
		$('#aEyebrowShow_Su').addClass('active');
		return false;
	});
	$('a.forgotPw').click(function(){
		$('div.show:visible').hide();
		$('#divForgotPw').show();
		return false;
	});
	$('a.forgotUn').click(function(){
		$('div.show:visible').hide();
		$('#divForgotUn').show();
		return false;
	});
	$('#aEyebrowShow_Share').click(function(){
		var e = $(this);
		if (e.hasClass('active')) {
			var shownDiv = $('div.show:visible');
			$('#divShowEyebrow:visible').slideUp(300, function(){
				shownDiv.hide();
				$('#divEf').hide();
				e.removeClass('active');
			});
		}
		else {
			$('a.eyebrow.active').removeClass('active');
			$('div.show:visible').hide();
			$('#divShare').show();
			$('#divShowEyebrow:hidden').slideDown(300);
			e.addClass('active');
		}
		return false;
	});
	$('#aEyebrowShow_Ef').click(function(){
		$('div.show:visible').hide();
		$('#divEf').show();
		$('#divShowEyebrow:hidden').slideDown(300);
		return false;
	});
	$('a.cancel.eyebrowShow').click(function(){
		var shownDiv = $('div.show:visible');
		$('#divShowEyebrow:visible').slideUp(300, function(){
			shownDiv.hide();
			$('a.eyebrow.active').removeClass('active');
		});
		return false;
	});
	$('#aChkRm').click(function(){
		$(this).toggleClass('checked');
		return false;
	}).focus(function(){
		$(this).addClass('focus');
	}).blur(function(){
		$(this).removeClass('focus');
	});
	$('#aSubmitSi').click(function(){
		SubmitLogin();
		return false;
	});
	$('#aSubmitSu').click(function(){
		SubmitRegistration();
		return false;
	});
	$('#aSubmitEf').click(function(){
		SubmitEmailFriend($(this));
		return false;
	});
	$('#aSubmitForgotPw').click(function(){
		SubmitRecoverPw();
		return false;
	});
	$('#aSubmitForgotUn').click(function(){
		SubmitRecoverUn();
		return false;
	});
	$('#aRequestCall').click(function(){
		var widgetId = '46b64d15697d1a41c18accab8e4833444edd4686';
		var gvNumber = $('#txtGvNumber').val();
		var gvName = 'Orlareal';
		click2call(widgetId, gvNumber, gvName);
	});
});
function SubmitEmailFriend(e){
	$("#spaEfError").hide();
	var sendData = new Object();
	sendData.__type = "ContactSendFields:#WCF";
	sendData.Url = e.attr('href');
	sendData.FirstName = $("#txtEfYour_Fname").val();
	sendData.LastName = $("#txtEfYour_Lname").val();
	sendData.Email = $("#txtEfYour_Email").val();
	sendData.FriendFirstName = $("#txtEfFriend_Fname").val();
	sendData.FriendLastName = $("#txtEfFriend_Lname").val();
	sendData.FriendEmail = $("#txtEfFriend_Email").val();
	sendData.Comments = $("#txtEfY_Message").val();
	var data = new Object();
	data.sendData = sendData;
	var sdata = JSON.stringify(data);
	$.ajax({
		url: "/SiteService.svc/EmailFriend",
		type: "POST",
		data: sdata,
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		error: function(xhr, desc, exception){
			alert(xhr.responseText);
		},
		success: function(obj){
			obj = obj.d;
			if (obj.Status.toLowerCase() == "success") {
				ClearEmailFriend();
				$('#spaEfError').html(obj.Message).addClass('success').show();
				var shownDiv = $('div.show:visible');
				setTimeout(function(){
					$('#divShowEyebrow:visible').slideUp(300, function(){
						$('#spaEfError').hide().html('').removeClass('success');
						shownDiv.hide();
						$('a.eyebrow.active').removeClass('active');
					});
				}, 4000);
			}
			else {
				$('#spaEfError').addClass('alert').html(obj.Message).show();
				setTimeout(function(){
					$('#spaEfError').slideUp(300, function(){
						$(this).html('').removeClass('alert');
					});
				}, 3000);
			}
		}
	});
}
function SubmitLogin(){
	$('#spaSiError').hide();
	$('#divSubmit').show();
	var login = new Object();
	login.__type = "Login:#WCF";
	login.UserName = $("#txtSiUserName").val();
	login.PW = $("#txtSiPw").val();
	if ($('#aChkRm').hasClass('checked')) {
		login.RM = '1';
	}
	var data = new Object();
	data.li = login;
	var sdata = JSON.stringify(data);
	$.ajax({
		url: "/SiteService.svc/Login",
		type: "POST",
		data: sdata,
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		error: function(xhr, desc, exception){
			alert(XMLHttpRequest.toString());
			$('#divSubmit').hide();
		},
		success: function(obj){
			obj = obj.d;
			var output = obj.Message;
			if (obj.Status.toLowerCase() == "success") {
				$('#aEyebrowShow_Si').html('sign out').removeClass('anon');
				$('#aEyebrowShow_Su').html('my orlareal').removeClass('anon');
				$('#aMyOrlareal').removeClass('anon');
				$('#divEyeLidBottom').html(obj.Message);
				var shownDiv = $('div.show:visible');
				$('#divShowEyebrow:visible').slideUp(300, function(){
					shownDiv.hide();
					$('a.eyebrow.active').removeClass('active');
					ClearLogin();
				});
			}
			else {
				$('#spaSiError').addClass('alert').html(obj.Message).show();
				setTimeout(function(){
					$('#spaSiError').slideUp(300, function(){
						$(this).html('').removeClass('alert');
					});
				}, 6000);
			}
		}
	});
}
function SubmitRegistration(){
	$('#spaSuError').hide();
	var reg = new Object();
	reg.__type = "Registration:#WCF";
	reg.UserType = $("#selUserType").val();
	reg.FirstName = $("#txtSuFName").val();
	reg.LastName = $("#txtSuLName").val();
	reg.Phone = $("#txtSuPhone").val();
	reg.Email = $("#txtSuEmail").val();
	reg.UserName = $("#txtSuUserName").val();
	reg.PW = $("#txtSuPw").val();
	reg.ConfirmPW = $("#txtSuPw2").val();
	var data = new Object();
	data.reg = reg;
	var sdata = JSON.stringify(data);
	$.ajax({
		url: "/SiteService.svc/Register",
		type: "POST",
		data: sdata,
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		error: function(xhr, desc, exception){
			alert(XMLHttpRequest.toString());
		},
		success: function(obj){
			obj = obj.d;
			var output = obj.Message;
			if (obj.Status.toLowerCase() == "success") {
				$('#aEyebrowShow_Si').html('sign out').removeClass('anon');
				$('#aEyebrowShow_Su').html('my orlareal').removeClass('anon');
				$('#aMyOrlareal').removeClass('anon');
				$('#divEyeLidBottom').html(obj.Message);
				var shownDiv = $('div.show:visible');
				$('#divShowEyebrow:visible').slideUp(300, function(){
					shownDiv.hide();
					$('a.eyebrow.active').removeClass('active');
					ClearRegistration();
				});
			}
			else {
				$('#spaSuError').addClass('alert').html(obj.Message).show();
				setTimeout(function(){
					$('#spaSuError').slideUp(300, function(){
						$(this).html('').removeClass('alert');
					});
				}, 6000);
			}
		}
	});
}
function ClearRegistration(){
	$("#selUserType").val('select');
	$("#txtSuFName").val('');
	$("#txtSuLName").val('');
	$("#txtSuPhone").val('');
	$("#txtSuEmail").val('');
	$("#txtSuUserName").val('');
	$("#txtSuPw").val('');
	$("#txtSuPw2").val('');
}
function ClearLogin(){
	$("#txtSiUserName").val('');
	$("#txtSiPw").val('');
	$('#aChkRm.checked').removeClass('checked');
}
function ClearEmailFriend(){
	$("#txtEfYour_Fname").val('');
	$("#txtEfYour_Lname").val('');
	$("#txtEfYour_Email").val('');
	$("#txtEfFriend_Fname").val('');
	$("#txtEfFriend_Lname").val('');
	$("#txtEfFriend_Email").val('');
	$("#txtEfY_Message").val('I found this property, and thought you might like to see it.');
}
function SubmitLogout(){
    $.ajax({
        url: "/SiteService.svc/Logout",
        type: "POST",
        data: "{}",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        error: function(xhr, desc, exception){
            alert(xhr.responseText);
        },
        success: function(obj){
            obj = obj.d;
			if (obj.Status.toLowerCase() == "success") {
				$('#aEyebrowShow_Si').html('sign in').addClass('anon');
				$('#aEyebrowShow_Su').html('Join<span>(free!)</span>').addClass('anon');	
				$('#aMyOrlareal').addClass('anon');
				$('#divEyeLidBottom').html(obj.Message);		
			}
			else {
				alert(obj.Message);
			}
        }
    });
}
function SubmitRecoverPw(){
	var userName = $('#txtForgotPwUsername').val();
	$.ajax({
		url: "/SiteService.svc/RecoverPw",
		type: "POST",
		data: "{\"userName\":\"" + userName + "\"}",
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		error: function(xhr, desc, exception){
			alert(xhr.responseText);
		},
		success: function(obj){
			obj = obj.d;
			if (obj.Status.toLowerCase() == "success") {
				$('#spaForgotPwError').addClass('success').html(obj.Message).show();
				setTimeout(function(){
					$('#spaForgotPwError').slideUp(300, function(){
						$(this).html('').removeClass('success');
						$('div.show:visible').hide();
						$('#divSi').show();
					});
				}, 9000);
			}
			else {
				$('#spaForgotPwError').addClass('alert').html(obj.Message).show();
				setTimeout(function(){
					$('#spaForgotPwError').slideUp(300, function(){
						$(this).html('').removeClass('alert');
					});
				}, 9000);
			}
		}
	});
}
function SubmitRecoverUn(){
	var email = $('#txtForgotUnEmail').val();
	$.ajax({
		url: "/SiteService.svc/RecoverUn",
		type: "POST",
		data: "{\"email\":\"" + email + "\"}",
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		error: function(xhr, desc, exception){
			alert(xhr.responseText);
		},
		success: function(obj){
			obj = obj.d;
			if (obj.Status.toLowerCase() == "success") {
				$('#spaForgotUnError').addClass('success').html(obj.Message).show();
				setTimeout(function(){
					$('#spaForgotUnError').slideUp(300, function(){
						$(this).html('').removeClass('success');
						$('div.show:visible').hide();
						$('#divSi').show();
					});
				}, 9000);
			}
			else {
				$('#spaForgotUnError').addClass('alert').html(obj.Message).show();
				setTimeout(function(){
					$('#spaForgotUnError').slideUp(300, function(){
						$(this).html('').removeClass('alert');
					});
				}, 9000);
			}
		}
	});
}
function fullScreen() {
    this.moveTo(0, 0);
    this.resizeTo(screen.availWidth, screen.availHeight)
}
function click2call(button_id, cid_number, cid_name){
	var iframe = document.createElement('iframe');
	iframe.style.width = 0;
	iframe.style.height = 0;
	iframe.style.border = 0;
	document.body.appendChild(iframe);
	
	var doc = null;
	if (iframe.contentDocument) 
		doc = iframe.contentDocument;
	else 
		if (iframe.contentWindow) 
			doc = iframe.contentWindow.document;
		else 
			if (iframe.document) 
				doc = iframe.document;
	if (doc == null) 
		throw "Document not initialized";
	
	doc.open();
	doc.write('');
	doc.close();
	
	var form = doc.createElement("form");
	form.method = "post";
	form.action = "https://clients4.google.com/voice/embed/webButtonConnect";
	
	input = doc.createElement("input");
	input.type = "text";
	input.name = "buttonId";
	input.value = "" + button_id;
	form.appendChild(input);
	
	input = doc.createElement("input");
	input.type = "text";
	input.name = "callerNumber";
	input.value = "" + cid_number;
	form.appendChild(input);
	
	input = doc.createElement("input");
	input.type = "text";
	input.name = "name";
	input.value = "Testing";
	form.appendChild(input);
	
	input = doc.createElement("input");
	input.type = "text";
	input.name = "showCallerNumber";
	input.value = "1";
	form.appendChild(input);
	
	doc.body.appendChild(form);
	
	form.submit();
}
