function TelogisFleetLogin() {
	var tfl = this;
	tfl.loginshown = false;
	tfl.htmldir = "/assets/fleet-login";
	tfl.isMSIE = false;
	tfl.isFirefox = false;
	tfl.version = 0;
	tfl.isIncompatibleBrowser = true;
	
	$(document).ready(function() {
		if (top.location != window.location) top.location = window.location;
		tfl.hidelogin();
		$('#loginLink').click(function (e) {
			e.preventDefault();
			tfl.togglelogin();
		});
		$('#termsLink').click(function (e) {
			e.preventDefault();
			$("#tf-other-pages").height("370px");
			$("#OnTrackLoginBox").width("550px");
			$("#tf-other-pages").width("530px");
			$("#tf-other-pages").load("/assets/fleet-login/terms.html");
			$("#tf-login-form").hide();
			$("#tf-other-pages").show();
		});
		$('#forgotpassword').click(function (e) {
			e.preventDefault();
			tfl.showDialog('pass');
		});
	});
	
	tfl.signin = function() {
	    // The user's ip address is set by PHP from within header.php
		if( user_ip_address != "" ) {
			// Clicky analytics count as Fleet user.
			$.ajax({
				url: "/wp-content/themes/telogis/clicky-log-fleetuser.php?ip_address="+user_ip_address,
				type: "get",			
				cache: false
			});
		}
		Auth.signin($("#username").val(), $("#password").val(), $('#autologin').is(":checked"), 'ontrack/default', {
			onerror: function(result) {
				tfl.showError(result.reason);
			},
			onsuccess: function(result) {
				$("#tf-other-pages").html("<b>Signing in...</b>");
				$("#tf-login-form").hide();
				$("#tf-other-pages").show();
			}
		});
	}
	
	tfl.openLogin = function (){
		// Detect IE 6.0 +
		if (navigator.userAgent.indexOf("MSIE") != -1) {
			var temp = navigator.userAgent.split("MSIE");
			tfl.version = parseFloat(temp[1]);
			if (tfl.version >= 6) {
				isMSIE = true;
				tfl.isIncompatibleBrowser = false;
			}
		} else if (navigator.userAgent.indexOf("Firefox/") != -1) {
			var temp = navigator.userAgent.split("Firefox/");
			tfl.version = parseFloat(temp[1]);
			if (tfl.version >= 3) {
				isFirefox = true;
				tfl.isIncompatibleBrowser = false;
			}
		}
		// Show beta text
		if (tfl.isIncompatibleBrowser) {
			$("#OTL-Beta").text("Beta");
		}
		// Sign in automatically if possible
		if (Auth.signinWithCookie({
			onerror: function(result) {
                                $("#OnTrackLoginBox").html(tfl.tempLogin);
				tfl.showError(result.reason);
			},
			onsuccess: function(result) {
                                $("#OnTrackLoginBox").html("<b>Signing in with cookie...</b>");
                        }
		})) {
                       tfl.tempLogin = $("#OnTrackLoginBox").html();
                       $("#OnTrackLoginBox").html("<b>Signing in with cookie...</b>");
		}
	}
	
	tfl.showlogin = function() {
		tfl.openLogin();
		$("#tf-other-pages").hide();
		$("#reqpass").hide();
		$("#reqpass-done").hide();
		$("#tf-login-form").show();
		$("#OnTrackLoginContainer").css("background", "url(/wp-content/themes/telogis/images/template/login-box-corner.png) no-repeat scroll left bottom");
		$("#OnTrackLoginBox").show();
		$("#OnTrackLoginBox").width("220px");
		$("#OnTrackLoginBox").height("auto");
		$("#username").focus();
		tfl.loginshown = true;
	}
	
	tfl.hidelogin = function() {
		$("#OnTrackLoginContainer").css("background", "none");
		$("#OnTrackLoginBox").hide();
		tfl.loginshown = false;
	}
	
	tfl.togglelogin = function() {
		(tfl.loginshown) ? tfl.hidelogin() : tfl.showlogin();
	}
	
	tfl.showError = function (error) {
		$('#otl-error').html(error);
	}

	tfl.showMessage = function (msg) {
		$('#otl-message').html(msg);
	}
	
	tfl.showDialog = function(dialog) {
		$("#tf-login-form").hide();
		switch(dialog) {
			case 'terms':
				$("#tf-other-pages").load(tfl.htmldir+"/terms.html?x=1");
				$("#OnTrackLoginBox").width("550px");
				$("#tf-other-pages").width("530px");
				$("#tf-other-pages").css("max-height","350px");
				$("#tf-other-pages").show();
				break;
			case 'pass':
				$("#reqpass").show();
				$("#usernameOrEmail").focus();
				break;
		}
	}
}
function requestPassword() {
	var usernameOrEmail = document.getElementById("usernameOrEmail").value;
	if (usernameOrEmail == "") return 0;
	Auth.retrievePassword(usernameOrEmail, {
		onerror: function(result) {
			alert(result.reason);
		},

		onsuccess: function(result) {
			$("#reqpass").hide();
			$("#reqpass-done").show();
		}
	});
}
var tfl = new TelogisFleetLogin();
