doNothing = function(){}

handleOrientationChange = function(){
	hideAddressBar();
}

hideAddressBar = function(){
	setTimeout(
		function(){window.scrollTo(0, 1)},
		500
	);
}


LL_Logger = {
	loggingPane: null,
	llog: function(){
		var logStr = '';
		for (var i = 0; i < arguments[0].length; ++i){
			var arg = arguments[0][i];
			logStr += arg + ' ';
		}
		var item = DIV(
			{
				'style':
				{
					'border-bottom': '1px solid #e1e1e1',
					'padding': '3px 0'
				}
			},
			repr(logStr)
		);
		var self = this;
		if (!$('logging_pane')) self.makeLoggingPane();
		setTimeout(
			function(){
				$('logging_pane').appendChild(item);
			},
			500
		);
	},
	makeLoggingPane: function(){
		if (!this.loggingPane){
			var b = getElementsByTagAndClassName('body', null)[0];
			var content = DIV({'style': {'border': '1px solid #999'}});
			var inner = DIV(
				{
					'style': {
						'background-color': '#e1e1e1',
						'padding': '2px 10px',
						'border-bottom': '1px solid #999',
						'color': '#356798',
						'font-weight': 'bold',
						'font-size': '15px'
					}
				},
				"LIVINGLOGIC LOG"
			);
			var scroller = DIV(
				{
					'style': {
						'background-color': '#fff',
						'padding': '10px',
						'height': '180px',
						'overflow': 'auto'
					},
					'id': 'logging_pane'
				}
			);
			content.appendChild(inner);
			content.appendChild(scroller);
			setTimeout(function(){b.appendChild(content)}, 500);
			this.loggingPane = content;
		}
	}
}
llog = function(){
	LL_Logger.llog(arguments);
}


STATIC_TEST = false;
DOMAIN = location.hostname;
LIVE_URL = 'http://' + DOMAIN + '/data/';

// >> LL_CookieTool Version: rel-1-0-0
	LL_CookieTool = {
		setCookies: function(cObjs){
			for (var i = 0; i < arguments.length; ++i){
				var c = arguments[i];
				c.setCookie();
			}
		},
		getCookie: function(name){
			var cstr = document.cookie;
			if (cstr.length > 0){
				cookies = cstr.split('; ');
				for (var i = 0; i < cookies.length; ++i){
					var cook = cookies[i];
					if (cook.substring(0, cook.lastIndexOf('=')) == name){
						return cook.substring(cook.lastIndexOf('=') + 1 , cook.length);
					}
				}
			}
			return null;
		},
		eraseCookie: function(name, domain, path){
			var c = new CookieData(name, null, domain, path, null, null);
			c.eraseCookie();
		}
	}
	
	CookieData = function(name, value, domain, path, expires, secure){
		this.name = name;
		this.value = value;
		this.domain = domain;
		this.path = path;
		this.expires = expires;
		this.secure = secure;
	}
	
	CookieData.prototype.setCookie = function(){
		var cook = this.name + '=' + unescape(this.value);
		cook += this.domain ? '; domain=' + this.domain : '';
		cook += this.expires ? '; expires=' + this.expires : '';
		cook += this.path ? '; path=' + this.path : '/';
		cook += this.secure ? '; secure' : '';
		document.cookie = cook;
	}
	
	CookieData.prototype.eraseCookie = function(){
		var cook = this.name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT';
		cook += this.domain ? '; domain=' + this.domain : '';
		cook += this.path ? '; path=' + this.path : '/';
		document.cookie = cook;
	}
// <<


LL_iPhoneSimpleNavigator = {
	nodes: [],
	activeNode: null,
	search: false,
	currentDetailId: null,
	appendNode: function(Node){
		if(Node instanceof NavNode){
			if (Node.name != 'suche'){
				connect(Node.node, 'onclick', bind('handleNavigationNode', this, Node));
			}else{
				connect(Node.node, 'onclick', bind('handleSearchNode', this, Node));
				connect('searchResetButton', 'onclick', bind('handleSearchInfoReset', this));
			}
			this.nodes.push(Node);
		}
	},
	handleNavigationNode: function(Node, e){
		hideElement('detailContainer');
		showElement('container');
		var i, n;
		for (i = 0; i < this.nodes.length; ++i){
			n = this.nodes[i];
			if (n.name != 'suche'){
				if (n !== Node){
					n.makePassive();
				}else{
					n.makeActive();
					this.activeNode = n;
					n.makeAction();
				}
			}
		}
	},
	handleSearchNode: function(Node, e){
		if (this.search){
			Node.makePassive();
			return false;
		}
		Node.makeActive();
		return true;
	},
	handleSearchInfoReset: function(e){
		$('searchField').value = '';
		hideElement('searchInfoOuter');
		this.handleNavigationNode(this.activeNode);
	},
	handlePagingAction: function(page){
		var Node = this.activeNode;
		Node.currentPage = page ? page : 0;
		Node.makeAction(page);
		return false;
	},
	handleBackButton: function(){
		hideElement('detailContainer');
		showElement('container');
		this.currentDetailId = null;
	},
	resetPage: function(){
		$('searchField').value = '';
		hideElement('searchInfoOuter');
		this.makeNodeActiveByName(this.nodes[0].name);
	},
	checkMoreInfoFormEmail: function(){
		var error = false;
		var email, pAt, pAt2, pDot;
		email = $('moreInfoFormField').value;
		pAt = email.indexOf('@');
		pAt2 = email.lastIndexOf('@');
		pDot = email.lastIndexOf('.');
		if (pAt == -1 || pAt2 == -1 || pAt != pAt2) error = true;
		if (pDot == -1 || pDot < pAt) error = true;
		if (!error){
			hideElement('formError');
			LL_CookieTool.setCookies(new CookieData('InfoFormMailAddress', email, DOMAIN, '/', 'Sun, 01-Jan-12 00:00:01 GMT', false));
			this.handleMoreInfoFormSubmit(email);
		}else{
			showElement('formError');
		}
	},
	handleMoreInfoFormSubmit: function(email){
		var url, def;
		email = encodeURI(email);
		url = LIVE_URL + this.activeNode.name + '/' + this.currentDetailId + '/?email=' + email;
		def = doSimpleXMLHttpRequest(url);
		def.addCallbacks(bind('submitMoreInfoForm', this), bind('submitMoreInfoFormError', this));
		return false;
	},
	submitMoreInfoForm: function(def){
		hideElement('formContent');
		hideElement('errorContent');
		showElement('successContent');
	},
	submitMoreInfoFormError: function(def){
		hideElement('formContent');
		hideElement('successContent');
		showElement('errorContent');
	},
	getNodeByName: function(name){
		var i, n;
		for (i = 0; i < this.nodes.length; ++i){
			n = this.nodes[i];
			if (n.name == name) return n;
		}
		return null;
	},
	makeNodeActiveByName: function(name){
		var i, n;
		for (i = 0; i < this.nodes.length; ++i){
			n = this.nodes[i];
			if (n.name == name){
				this.activeNode = n;
				n.makeActive();
				n.makeAction();
			}else{
				n.makePassive();
			}
		}
	},
	showDetail: function(id){
		var container = $('container');
		var detailContainer = $('detailContainer');
		this.addTargetPadding();
		this.addLoadingRotor();
		hideElement(container);
		showElement(detailContainer);
		detailContainer.innerHTML = '';
		this.insertDetailContent(detailContainer, id);
	},
	showMoreInfoEmailPanel: function(){
		hideElement('successContent');
		hideElement('errorContent');
		showElement('formContent');
		appear('moreInfoFormOuter', {'duration': 0.5});
		var emailCookieValue = LL_CookieTool.getCookie('InfoFormMailAddress');
		if (emailCookieValue) $('moreInfoFormField').value = emailCookieValue;
	},
	insertDetailContent: function(target, id){
		url = LIVE_URL + this.activeNode.name + '/' + id;
		this.currentDetailId = id;
		if (STATIC_TEST) url = 'companyDetailTest.text';
		var def = doSimpleXMLHttpRequest(url);
		def.addCallbacks(bind('insertContent', this, target), bind('insertContentError', this));
	},
	insertContent: function(target, def){
		target.innerHTML = def.responseText;
		this.removeTargetPadding();
		this.removeLoadingRotor();
		hideAddressBar();
	},
	insertContentError: function(def){
		logFatal('Fehler beim uebertragen des Inhalts:', def.message);
	},
	disableSearch: function(){
		var Node = this.getNodeByName('suche');
		if (Node) Node.disable();
	},
	enableSearch: function(){
		var Node = this.getNodeByName('suche');
		if (Node) Node.enable();
	},
	addTargetPadding: function(){
		setStyle($('detailContainer'), {'padding-bottom': '380px'});
	},
	removeTargetPadding: function(){
		updateNodeAttributes($('detailContainer'), {'style': ''});
	},
	addLoadingRotor: function(){
		setNodeAttribute($('detailContainer'), 'class', 'rotorOn');
	},
	removeLoadingRotor: function(){
		setNodeAttribute($('detailContainer'), 'class', 'rotorOff');
	}
}

NavNode = function(args){
	this.node = null;
	this.id = null;
	this.name = null;
	this.category = 'AbstractNode';
	this.classNamePass = null;
	this.classNameAct = null;
	this.target = 'container';
	this.url = 'listTest.text';
	this.currentPage = null;
	this.startWithSearch = false;
	if (args && typeof args == 'object'){
		if (args.startWithSearch) this.startWithSearch = true;
	}
}

NavNode.prototype.updatePageTitle = function(){
	var t, newTitle;
	t = $('pageTitle');
	if (t.innerHTML.lastIndexOf(':') > -1){
		newTitle = t.innerHTML.substring(0, t.innerHTML.lastIndexOf(':')) + ': ' + this.category;
	}else{
		newTitle = t.innerHTML + ': ' + this.category;
	}
	//document.title = newTitle;
}

NavNode.prototype.makeActive = function(){
	setNodeAttribute(this.node, 'class', this.classNameAct);
	LL_iPhoneSimpleNavigator.enableSearch();
	this.updatePageTitle();
}

NavNode.prototype.makePassive = function(){
	setNodeAttribute(this.node, 'class', this.classNamePass);
	this.currentPage = null;
}

NavNode.prototype.makeAction = function(page){
	var iPSimNav = LL_iPhoneSimpleNavigator;
	if (this.startWithSearch && ! (page > -1)){
		this.removeLoadingRotor();
		$(this.target).innerHTML = '';
		$('searchField').value = '';
		hideElement('searchInfoOuter');
		$('category').innerHTML = '<b>' + iPSimNav.activeNode.category + '</b>';
		showElement('categoryDesc');
		if (!iPSimNav.search) iPSimNav.handleSearchNode(iPSimNav.getNodeByName('suche'));
	}else{
		if (iPSimNav.search && ! (page > -1)){
			iPSimNav.handleSearchNode(iPSimNav.getNodeByName('suche'));
		}
		$('searchField').value = '';
		hideElement('searchInfoOuter');
		hideElement('categoryDesc');
		var t, sf, url, sep, def, name;
		var cgiArgs = {};
		t = $(this.target);
		t.innerHTML = '';
		sf = $('searchField');
		if (this.currentPage || iPSimNav.activeNode.name == this.name){
			if (this.currentPage) cgiArgs.page = this.currentPage;
			if (sf.value != '') cgiArgs.phrase = sf.value;
		}else{
			sf.value = '';
			hideElement('searchInfoOuter');
		}
		this.addTargetPadding();
		this.addLoadingRotor();
		sep = queryString(cgiArgs) ? '?' : '';
		url = this.url + sep + queryString(cgiArgs);
		def = doSimpleXMLHttpRequest(url);
		def.addCallbacks(bind('insertContent', this, t), bind('insertContentError', this));
	}
}

NavNode.prototype.addTargetPadding = function(){
	setStyle(this.target, {'padding-bottom': '1000px'});
}

NavNode.prototype.removeTargetPadding = function(){
	updateNodeAttributes(this.target, {'style': ''});
}
	
NavNode.prototype.addLoadingRotor = function(){
	setNodeAttribute($('container'), 'class', 'rotorOn');
}
NavNode.prototype.removeLoadingRotor = function(){
	setNodeAttribute($('container'), 'class', 'rotorOff');
}

NavNode.prototype.insertContent = function(target, def){
	target.innerHTML = def.responseText;
	this.removeTargetPadding();
	this.removeLoadingRotor();
	hideAddressBar();
}

NavNode.prototype.insertContentError = function(def){
	logFatal('Fehler beim uebertragen des Inhalts:', def.message);
}


NewsNode = function(args){
	this.constructor(args);
	this.node = $('button_news');
	this.id = 'button_news';
	this.name = 'news';
	this.category = 'News';
	this.classNamePass = 'button_news_pass button';
	this.classNameAct = 'button_news_act button';
	if (!STATIC_TEST) this.url = LIVE_URL + this.name + '/';

}
NewsNode.prototype = new NavNode();


ArtikelNode = function(args){
	this.constructor(args);
	this.node = $('button_artikel');
	this.id = 'button_artikel';
	this.name = 'artikel';
	this.category = 'Artikel';
	this.classNamePass = 'button_artikel_pass button';
	this.classNameAct = 'button_artikel_act button';
	if (!STATIC_TEST) this.url = LIVE_URL + this.name + '/';
}
ArtikelNode.prototype = new NavNode();


ProdukteNode = function(args){
	this.constructor(args);
	this.node = $('button_produkte');
	this.id = 'button_produkte';
	this.name = 'produkte';
	this.category = 'Produkte';
	this.classNamePass = 'button_produkte_pass button';
	this.classNameAct = 'button_produkte_act button';
	this.url = 'listTest_var2.text';
	if (!STATIC_TEST) this.url = LIVE_URL + this.name + '/';
}
ProdukteNode.prototype = new NavNode();


FirmenNode = function(args){
	this.constructor(args);
	this.node = $('button_firmen');
	this.id = 'button_firmen';
	this.name = 'firmen';
	this.category = 'Firmen';
	this.classNamePass = 'button_firmen_pass button';
	this.classNameAct = 'button_firmen_act button';
	this.url = 'listTest_var2.text';
	if (!STATIC_TEST) this.url = LIVE_URL + this.name + '/';
}
FirmenNode.prototype = new NavNode();


SucheNode = function(args){
	this.constructor(args);
	this.node = $('button_suche');
	this.id = 'button_suche';
	this.name = 'suche';
	this.category = 'Suche';
	this.classNamePass = 'button_suche_pass button';
	this.classNameAct = 'button_suche_act button';
	this.classNameDisable = 'button_suche_dis button';
	this.searchSlideStat = 0;
	this.disabled = false;
	connect('searchButton', 'onclick', bind('makeAction', this));
}
SucheNode.prototype = new NavNode();

SucheNode.prototype.makeActive = function(){
	if (this.disabled == false){
		setNodeAttribute(this.node, 'class', this.classNameAct);
		slideDown('searchArea', {'duration': 0.3});
		this.searchSlideStat = 1;
		LL_iPhoneSimpleNavigator.search = true;
	}
}

SucheNode.prototype.makePassive = function(){
	if (this.disabled == false){
		setNodeAttribute(this.node, 'class', this.classNamePass);
		if (this.searchSlideStat > 0) slideUp('searchArea', {'duration': 0.5});
		this.searchSlideStat = 0;
		LL_iPhoneSimpleNavigator.search = false;
	}
}

SucheNode.prototype.disable = function(){
	if (this.searchSlideStat == 1) this.makePassive();
	LL_iPhoneSimpleNavigator.search = false;
	setNodeAttribute(this.node, 'class', this.classNameDisable);
	this.disabled = true;
}

SucheNode.prototype.enable = function(){
	var className = LL_iPhoneSimpleNavigator.search == false ? this.classNamePass : this.classNameAct;
	setNodeAttribute(this.node, 'class', className);
	this.disabled = false;
}

SucheNode.prototype.makeAction = function(Node, e){
	if (!this.disabled){
		var t, si, sf, sep, url;
		var cgiArgs = {};
		hideElement('detailContainer');
		showElement('container');
		this.makePassive();
		url = LL_iPhoneSimpleNavigator.activeNode.url;
		LL_iPhoneSimpleNavigator.search = false;
		LL_iPhoneSimpleNavigator.activeNode.currentPage = null;
		LL_iPhoneSimpleNavigator.activeNode.currentDetailId = null;
		si = $('searchInfoText');
		sf = $('searchField');
		if (sf.value != ''){
			si.innerHTML = sf.value;
			slideDown('searchInfoOuter', {'duration': 0.5});
			t = $(this.target);
			this.addTargetPadding();
			this.addLoadingRotor();
			if (sf.value != '') cgiArgs.phrase = encodeURI(sf.value);
			sep = queryString(cgiArgs) ? '?' : ''
			url = url + sep + queryString(cgiArgs);
			def = doSimpleXMLHttpRequest(url);
			def.addCallbacks(bind('insertContent', this, t), bind('insertContentError', this));
		}
	}
}


StartNode = function(args){
	this.constructor(args);
	this.node = $('button_start');
	this.id = 'button_start';
	this.name = 'start';
	this.category = 'Startseite';
	this.classNamePass = 'button_start';
	this.classNameAct = 'button_start';
}
StartNode.prototype = new NavNode();

StartNode.prototype.makeActive = function(){
	LL_iPhoneSimpleNavigator.disableSearch();
	this.updatePageTitle();
}

StartNode.prototype.makePassive = function(){
	this.currentPage = null;
}

StartNode.prototype.makeAction = function(){
	LL_iPhoneSimpleNavigator.resetPage();
}


ImpressumNode = function(args){
	this.constructor(args);
	this.node = $('button_impressum');
	this.id = 'button_impressum';
	this.name = 'impressum';
	this.category = 'Impressum';
	this.classNamePass = 'button_impressum';
	this.classNameAct = 'button_impressum';
	this.url = 'impressum.html';
}
ImpressumNode.prototype = new NavNode();

ImpressumNode.prototype.makeActive = function(){
	LL_iPhoneSimpleNavigator.disableSearch();
	this.updatePageTitle();
}

ImpressumNode.prototype.makePassive = function(){
	//setNodeAttribute(this.node, 'class', this.classNamePass);
	this.currentPage = null;
}

ImpressumNode.prototype.makeAction = function(){
	var t, sf, url, sep, def, name;
	var cgiArgs = {};
	t = $(this.target);
	t.innerHTML = '';
	sf = $('searchField');
	if (this.currentPage || LL_iPhoneSimpleNavigator.activeNode.name == this.name){
		if (this.currentPage) cgiArgs.page = this.currentPage;
		if (sf.value != '') cgiArgs.phrase = sf.value;
	}else{
		sf.value = '';
		hideElement('searchInfoOuter');
	}
	this.addTargetPadding();
	this.addLoadingRotor();
	sep = queryString(cgiArgs) ? '?' : '';
	url = this.url + sep + queryString(cgiArgs);
	def = doSimpleXMLHttpRequest(url);
	def.addCallbacks(bind('insertContent', this, t), bind('insertContentError', this));
}


DatenschutzNode = function(args){
	this.constructor(args);
	this.node = $('button_datenschutz');
	this.id = 'button_datenschutz';
	this.name = 'datenschutz';
	this.category = 'Datenschutz';
	this.classNamePass = 'button_datenschutz';
	this.classNameAct = 'button_datenschutz';
	this.url = 'datenschutz.html';
}
DatenschutzNode.prototype = new NavNode();

DatenschutzNode.prototype.makeActive = function(){
	LL_iPhoneSimpleNavigator.disableSearch();
	this.updatePageTitle();
}

DatenschutzNode.prototype.makePassive = function(){
	//setNodeAttribute(this.node, 'class', this.classNamePass);
	this.currentPage = null;
}

DatenschutzNode.prototype.makeAction = function(){
	var t, sf, url, sep, def, name;
	var cgiArgs = {};
	t = $(this.target);
	t.innerHTML = '';
	sf = $('searchField');
	if (this.currentPage || LL_iPhoneSimpleNavigator.activeNode.name == this.name){
		if (this.currentPage) cgiArgs.page = this.currentPage;
		if (sf.value != '') cgiArgs.phrase = sf.value;
	}else{
		sf.value = '';
		hideElement('searchInfoOuter');
	}
	this.addTargetPadding();
	this.addLoadingRotor();
	sep = queryString(cgiArgs) ? '?' : '';
	url = this.url + sep + queryString(cgiArgs);
	def = doSimpleXMLHttpRequest(url);
	def.addCallbacks(bind('insertContent', this, t), bind('insertContentError', this));
}

