// (c)2008 Daniel M. Gattermann

var initRan = false;
var COOKIE_NAME = 'iCountrCookie';
var COOKIE_ELEM_SEP = '++--++';
var counters = new Array();
var counterID = 0;


function init() {
	var loaded = loadCountersCookie();
	if (!loaded) {
		var c = new Counter('New Counter', 0);
		this.counters.push(c);
	}
	refreshMain();
	this.initRan = true;
	
	document.getElementById('blogContactLink').setAttribute("target", "_blank");
}



// class

function Counter(name, value) {
	this.n = name;
	this.i = value;
	this.d = counterID++;
	
	this.getId = function() {
		return this.d;
	};
	
	this.getValue = function() {
		return this.i;
	};
	
	this.setValue = function(i) {
		this.i = i;
	}
	
	this.getName = function() {
		return this.n;
	}
	
	this.setName = function(name) {
		this.n = name;
	}


	this.incr = function() {
		this.i++;
	}
	
	this.decr = function() {
		this.i--;
	}
	
	this.getCookieString = function() {
		return this.getName() + '(' + this.getValue() + ')';
	}

	this.getDisplayHtml = function() {
		var html = document.createElement("div");
		html.setAttribute('class', 'line');
		html.setAttribute('onclick', 'javascript:incrCounter(' + this.getId() + ');');
		html.innerHTML = 	'	<span class="text" >' + this.getName() + '</span>'
						+	'	<span class="value">' + this.getValue() + '</span>';
		return html;
	}
	
	this.getEditHtml = function() {
		var html = document.createElement("div");
		html.setAttribute('id', 'edit-' + this.getId());
		html.setAttribute('class', 'line');
		html.innerHTML =  '<span class="text"><input type="text" id="name-' + this.getId() + '" value="' + this.getName() + '" /></span>'
						+ '<span class="value">'
				+ '<span class="decrCounterButtonEdit" onclick="javascript:decrCounterEdit(' + this.getId() + ');">&ndash;</span>'
						+ '<input type="text" id="value-' + this.getId() + '" value="' + this.getValue() + '" name="zip"/>'
				+ '<span class="incrCounterButtonEdit" onclick="javascript:incrCounterEdit(' + this.getId() + ');">+</span>'
						+ '</span>'
						+ '<span class="removeCounterButton" onclick="javascript:removeCounter(' + this.getId() + ');">&times;</span>';
		return html;
	}
	
}



// counter functions

function getCounter(id) {
	for (var i = 0; i < this.counters.length; i++) {
		var curr = this.counters[i];
		if (curr.getId() == id) {
			return curr;
		}
	}
}

function incrCounter(id) {
	getCounter(id).incr();
	refreshMain();
	saveCountersCookie();
}

function incrCounterEdit(id) {
	document.getElementById('value-' + id).value++;
}

function decrCounterEdit(id) {
	document.getElementById('value-' + id).value--;
}


function updateCountersFromEditInput() {
	var newArr = new Array();
	for (var i = 0; i < this.counters.length; i++) {
		var curr = this.counters[i];
		
		if (!document.getElementById('value-' + curr.getId()).value.match(/^-?\d+$/)) {
			return false;
		}
		
		var newC = new Counter( document.getElementById('name-' + curr.getId()).value , document.getElementById('value-' + curr.getId()).value );
		newArr.push(newC);
	}
	this.counters = newArr;
	return true;
}

function addCounter() {
	var newC = new Counter('New Counter', 0);
	document.getElementById('editContainer').appendChild(newC.getEditHtml());
	this.counters.push(newC);
}

function removeCounter(id) {
	if (confirm('Remove counter "' + getCounter(id).getName() + '"?')) {
		document.getElementById('editContainer').removeChild(document.getElementById('edit-' + id));
		var newArr = new Array();
		for (var i = 0; i < this.counters.length; i++) {
			var curr = this.counters[i];
			if (curr.getId() != id) {
				newArr.push(curr);
			}
		}
		this.counters = newArr;
	}
}



// saving and loading

function saveCountersCookie() {
	var cookieString = '';
	for (var i = 0; i < this.counters.length; i++) {
		cookieString += this.counters[i].getCookieString() + this.COOKIE_ELEM_SEP;
	}
	this.setCookie(this.COOKIE_NAME, cookieString);
}

function loadCountersCookie() {
	var cookie = this.getCookie(this.COOKIE_NAME);
	if (cookie) {
		var stringArray = cookie.split(this.COOKIE_ELEM_SEP);
		this.counters = new Array();
		for (var i = 0; i < stringArray.length; i++) {
			var res = stringArray[i].match(/^(.*)\((.*)\).*$/);
			if (res) {
				var c = new Counter(res[1], res[2] * 1);
				this.counters.push(c);
			}
		}
		return true;
	} else {
		return false;
	}
}



// GUI functions

function showEdit() {
	if (!this.initRan) return false;
	refreshEdit();
	document.getElementById('main').style.display = 'none';
	document.getElementById('edit').style.display = 'block';
}

function editDone() {
	var updateOk = updateCountersFromEditInput();
	if (updateOk) {
		saveCountersCookie();
		refreshMain();
		document.getElementById('edit').style.display = 'none';			
		document.getElementById('main').style.display = 'block';				
	} else {
		alert('Please check your input.\nOnly positive and negative numbers are allowed in the value field.');
	}
}		

function editCancel() {
	if (confirm('Back without saving any changes?')) {
		refreshMain();
		document.getElementById('edit').style.display = 'none';			
		document.getElementById('main').style.display = 'block';
	}
}		


function refreshMain() {
	document.getElementById('mainContainer').innerHTML = '';
	for (var i = 0; i < this.counters.length; i++) {
		document.getElementById('mainContainer').appendChild(this.counters[i].getDisplayHtml());			
	}
}

function refreshEdit() {
	document.getElementById('editContainer').innerHTML = '';
	for (var i = 0; i < this.counters.length; i++) {
		document.getElementById('editContainer').appendChild(this.counters[i].getEditHtml());			
	}
}


// cookie tools

function setCookie(name, value) {
	var expires = new Date( (new Date()).getTime() + (365 * 24 * 60 * 60 * 1000) );
	document.cookie = name + '=' + value + '; expires=' + expires.toGMTString() + '; path=/';
}

function getCookie(name) {
	var results = document.cookie.match ( '(^|;) ?' + name + '=([^;]*)(;|$)' );
	return results ? unescape(results[2]) : null;
}

function eraseCookie(name) {
	document.cookie = name + '=; expires=-1; path=/';
}
