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();
			tfl.showDialog('terms');
		});
		$('#reqpass').click(function (e) {
			e.preventDefault();
			tfl.showDialog('pass');
		});
	});
	
	tfl.signin = function() {
		// Google analytics count as user.
		var _gaq = _gaq || [];
		_gaq.push(['_trackPageview', '/fleet-login/']);
		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) {
				tfl.showError(result.reason);
			},
			onsuccess: function(result) { }
		})) {
			$("#OnTrackLoginBox").html("<b>Signing in with cookie...</b>");
		}
	}
	
	tfl.showlogin = function() {
		tfl.openLogin();
		$("#tf-other-pages").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();
		$("#tf-other-pages").show();
		$("#dialog").show();
		switch(dialog) {
			case 'terms':
				$("#tf-other-pages").load(tfl.htmldir+"/terms.html");
				$("#OnTrackLoginBox").width("550px");
				$("#tf-other-pages").width("550px");
				break;
			case 'pass':
				$("#tf-other-pages").load(tfl.htmldir+"/reqpass.html");
				break;
		}
	}
	
	tfl.hideDialog = function() {
		$("#dialog").hide();
		$("#btnsignin").focus();
	}
}
var tfl = new TelogisFleetLogin();