function enhancePage() {
	//is called on every page; stuff can be executed based on the id of the body element
	var bodyid = document.body.id;
	if (bodyid == null || bodyid == "") return false;
	
	switch (bodyid) {
		case("screen_twitter_login"):
		case("screen_delicious_login"):
		case("screen_instapaper_login"):
		case("screen_search"):
		case("screen_twitter_login_blocked"):
			setFieldFocus();
			break;
		case("screen_tweets"):
			break;
	}
	enhanceStatus();
}

function enhanceStatus() {
	var statusfield = document.getElementById("status");
	if (statusfield) {
		if (statusfield.value) {
			statusfield.focus();
		}
		var u = null;
		if (typeof(username) != "undefined") {
			u = username;
		}
		counter.init(u);
	}
}

function setFieldFocus(formId, fieldName, dontSelect) {
	//puts the focus on text or password input field with name fieldName in form with id formId and selects the text within it;
	//when no fieldName is specified, the first empty field in form with formId is used;
	//when also formId isn't specified it takes the first empty field of the first form on the page
	//when dontSelect is true, the text within the focussed field isn't selected
	var selectedField, firstField;
	var f = document.getElementById(formId);
	if (!f) f = document.forms[0];
	if (f) {
		for (var i=0; i<f.length; i++) {
			var thisField = f.elements[i];
			if (thisField.type == "text" || thisField.type == "password") {
				if (!firstField) {firstField = thisField;}//keep this in case there's no empty fields
				if (fieldName != null) {
					if (thisField.name == fieldName) selectedField = thisField;
				} else {
					//check if the field is empty
					if (thisField.value == "") {
						selectedField = thisField;
						break;
					}
				}
			}
		}
	}
	if (!selectedField) {selectedField = firstField;}
	if (selectedField) {
		selectedField.focus();
		if (!dontSelect) selectedField.select();
	}
}

//-- Start counter --
	var counter = {
		counter: document.getElementById("counter"),
		status: document.getElementById("status"),
		okColor: "#cccccc",//has to be the same value as #counter color
		warningColor: "#cc4444",
		badColor: "#cc0000",
		okBgColor: "#ffffff",//has to be the same value as #status background-color
		badBgColor: "#ffe9e5",
		okRGB: null,
		warningRGB: null,
		maxChars: 140,
		warningZone: 20,//num of chars where color starts to change
		username: null,
		
		init: function(username) {
			if (!this.isSymbianWebkit()) {
				//only initialize the counter when this isn't webkit on a symbian
				//webkit on symbian only updates onblur, so this counter doesn't work properly (tried to work around that by using a timeout for setting blur() and focus(), but that didn't work out)
				this.username = username;
				this.okRGB = this.hexToRgb(this.okColor);
				this.warningRGB = this.hexToRgb(this.warningColor);
				//this.update();
				//this.status.focus();//remove
				this.status.onkeydown = this.status.onkeyup = function() {
					counter.update();
				}
			}
		},
		
		update: function() {
			var numChars = this.status.value.length;
			var charsLeft = this.maxChars-numChars;
			var color = this.okColor;
			var bgColor = this.okBgColor;
			if (charsLeft < this.warningZone) {
				if (charsLeft >= 0) {
					color = this.getScaleColor((this.warningZone-charsLeft)/this.warningZone);
				} else {
					color = this.badColor;
					bgColor = this.badBgColor;
				}
			}
			var rtsafe = "";
			if (this.username !== null) {
				rtsafe = (charsLeft < (this.username.length+5)) ? "not ":"";
				rtsafe = '<span id="rtsafe">'+rtsafe+'RT-safe</span>';
			}
			this.counter.style.color = color;
			this.status.style.backgroundColor = bgColor;
			this.counter.innerHTML = rtsafe+charsLeft;
		},
		
		hexToRgb: function(hex) {
			//returns array[r,g,b] for hex value
			//param hex: color value in format #ffcc00
			if (hex.indexOf("#") != 0 || hex.length != 7) return null;
			var r = parseInt ( hex.substring (1,3),16);
			var g = parseInt ( hex.substring (3,5),16);
			var b = parseInt ( hex.substring (5,7),16);
			return (new Array(r,g,b));
		},
		
		getScaleColor: function(scale) {
			var color;
			if (scale > 0) {
				var red = this.getScaleValue(this.okRGB[0], this.warningRGB[0], scale);
				var green = this.getScaleValue(this.okRGB[1], this.warningRGB[1], scale);
				var blue = this.getScaleValue(this.okRGB[2], this.warningRGB[2], scale);
				color = "rgb("+red+", "+green+", "+blue+")";
			} else {
				color = this.badColor;
			}
			return(color);
		},
		
		getScaleValue: function(min, max, scale) {
			min = parseFloat(min);
			max = parseFloat(max);
			return (Math.floor(min + scale*(max-min)));
		},
		
		isSymbianWebkit: function() {
			//checks if the browser is webkit on a symbian
			//found at http://www.hand-interactive.com/resources/detect-mobile-javascript.htm
			var deviceS60 = "series60";
			var deviceSymbian = "symbian";
			var engineWebKit = "webkit";
			var uagent = navigator.userAgent.toLowerCase();
			
			if (uagent.search(engineWebKit) > -1) {
			if ((uagent.search(deviceS60) > -1 || 
				uagent.search(deviceSymbian) > -1)) {
					return true;
				} else {
					return false;
				}
			}
			return false;
		},
	}
	//counter.init("juaron");
//-- Einde counter --

//-- stuff to execute on every page
enhancePage();
