var Global = {};

function tree() {
	if(System.treeHide == true) return;
	var innerWidth = document.viewport.getWidth();
	var trees = [$('baum1'), $('baum2'), $('baum3')];
	if(1200 <= innerWidth) {
		trees[0].show();
		trees[1].hide();
		trees[2].hide();
	} else if(1000 <= innerWidth) {
		trees[0].hide();
		trees[1].show();
		trees[2].hide();
	} else {
		trees[0].hide();
		trees[1].hide();
		trees[2].show();
	}
}

function init() {
	//make section dragable?
	//no
	/*
	new Draggable($('content'), {
		handle: $('header_title'),
		ghosting: false,
		starteffect: null,
		endeffect: null
	});
	*/
	
	if(typeof winStatic == 'undefined') {		
		//resize tree-navigation
		tree();
		Event.observe(window, 'resize', tree);
	} else {
		var content = $('content');
	
		//to get height we have to show all contentes
		content.show();
		
		//set height
		var section = $$('#sectionInner/div.sectionInner')[0];
		section.setStyle({
			height: (document.viewport.getHeight() - 220) + 'px',
			overflow: 'auto'
		});
        
        if (window.location.href.match(/(?:pageID=3|p=mythos)/))
            System.Night();
            
        if (window.navigator.userAgent.indexOf("Firefox 4"))
            $$(".section .sectionEnd").invoke("setStyle", { height: "24px" }); // due direct-write
		
		/*$A($$('div.menuInner/ul/li/a')).each(function(e) {	
			if(e.hasClassName('noAjax')) return;
			var href = e.getAttribute('href');
			e.onclick = function() {
				System.getContent(this.href);
				this.blur();
				System.Day();
				if(href.indexOf('pageID=3') > -1)
					System.Night();
				return false;
			}
		});*/
	}
}

Event.observe(window, 'load', init);

var System = {
	sendMail: function(form) {
		var ser = $(form).serialize(true);
		
		if(!ser.name || ser.name == '') {
			$('contact_status').hide();
			$('contact_senderror').hide();
			var errorNode = $('contact_error');
			errorNode.show();
			new Effect.Highlight(errorNode, {
				startcolor: '#bf0000',
				endcolor: '#ffffff',
				duration: 0.5
			});
			errorNode.update('Bitte geben Sie einen Namen an');
			return false;
		} else if(!ser.mail || ser.mail == '') {
			$('contact_status').hide();
			$('contact_senderror').hide();
			var errorNode = $('contact_error');
			errorNode.show();
			new Effect.Highlight(errorNode, {
				startcolor: '#bf0000',
				endcolor: '#ffffff',
				duration: 0.5
			});
			errorNode.update('Bitte geben Sie eine E-Mailadresse an');
			return false;
		} else if(!ser.subject || ser.subject == '') {
			$('contact_status').hide();
			$('contact_senderror').hide();
			var errorNode = $('contact_error');
			errorNode.show();
			new Effect.Highlight(errorNode, {
				startcolor: '#bf0000',
				endcolor: '#ffffff',
				duration: 0.5
			});
			errorNode.update('Bitte geben Sie einen Betreff an');
			return false;
		} else if(!ser.message || ser.message == '') {
			$('contact_status').hide();
			$('contact_senderror').hide();
			var errorNode = $('contact_error');
			errorNode.show();
			new Effect.Highlight(errorNode, {
				startcolor: '#bf0000',
				endcolor: '#ffffff',
				duration: 0.5
			});
			errorNode.update('Bitte geben Sie eine Nachricht an');
			return false;
		}
		
		new Ajax.Request('mail.php', {
			method: 'post',
			parameters: Object.extend(ser, {send:1}),
			
			onFailure: function() {
				$('contact_status').hide();
				$('contact_error').hide();
				$('contact_senderror').show();
			},
			
			onException: function(a,e) {
				throw e;
			},
			
			onSuccess: function(r) {
				$('contact_status').hide();
				$('contact_senderror').hide();
				
				if(typeof r.responseXML == 'undefined') {
					$('contact_status').hide();
					$('contact_error').hide();
					$('contact_senderror').show();
					return;
				}
				
				var root = r.responseXML.getElementsByTagName('root').item(0) || false;
				if(root) {
					var error = root.getElementsByTagName('error') || false;
					var ok = root.getElementsByTagName('ok');
					
					if(error.length) {
						var status = $('contact_error');
						status.show();
						new Effect.Highlight(status, {
							startcolor: '#bf0000',
							endcolor: '#ffffff',
							duration: 0.5
						});
						status.update(error.item(0).firstChild.nodeValue);
					} else {
						$('contact_status').show().update('Ihre Email wurde versendet.');
						form.disable();
						$('contact_error').hide();
						$('contact_senderror').hide();
					}
				}
			}
		});
	},
	
	treeHide: false,
	setContent: function(area) {
		Biene.FlyOut();
		$A(['baum1', 'baum2', 'baum3']).each(Element.hide);
		$('content').show();
		this.treeHide = true;
		this.getContent(area.href);
		return false;
	},
	
	getContent: function(url) {
		var section = $('content_inner');
		var header = $('header_title');
		header.update('Lade angeforderte Seite. Bitte warten...');
		section.fade({ to: 0.1, duration: 0.6, afterFinish: function() {
			var blinker = section.update('lade - bitte warten...').blink(100, 500);
			(function() {
				new Ajax.Request(url, {
					method: 'post',
					parameters: 'ajax=1',
					onFailure: function() {
						blinker.stop();
						section.update('Fehler, Seite nicht gefunden');
					},
					onSuccess: function(r) {
						blinker.stop();
						var result = r.responseXML.getElementsByTagName('result').item(0);
						var title = result.getElementsByTagName('title').item(0);
						var content = result.getElementsByTagName('content').item(0);
						header.update(title.firstChild.nodeValue);
						section.update(content.firstChild.nodeValue);
						
						//get new tooltips
						new ToolTipEngine(2);
					},
					onException: function(self, ex) {
						blinker.stop();
						if(ex.message.match(/file_not_found/gi)) {
							section.update('Seite nicht gefunden').setStyle('color:red');
						} else {
							section.update(ex.message);
						}
					}
				});
			}).defer();
			section.appear({ duration: 0.6 });
		}});
	},
	
	night_day: 'day',
	Night: function(e) {
		if(this.night_day == 'night')
			return false;
		
		if(!e) e = $('sonne');
		document.body.style.backgroundColor = 'black';
		document.body.style.backgroundImage = 'url(style/back_top_night.png)';
		e.setAttribute('src', 'style/mond.png');
		e.onclick = function() {
			System.Day(this);
		}
		
		setTimeout(function() { System.Flash(); }, 5000);
		this.night_day = 'night';
	},
	
	Flash: function() {
		if(this.night_day == 'day')
			return false;
			
		var self = this;
		document.body.style.backgroundColor = 'white';
		document.body.style.backgroundImage = 'none';
		setTimeout(function() {
			document.body.style.backgroundColor = 'black';
			document.body.style.backgroundImage = 'url(style/back_top_night.png)';
			setTimeout(function() {
				document.body.style.backgroundColor = 'white';
				document.body.style.backgroundImage = 'none';
				setTimeout(function() {
					document.body.style.backgroundColor = 'black';
					document.body.style.backgroundImage = 'url(style/back_top_night.png)';
				}, 50);
			}, 70);
		}, 30);
	},

	Day: function(e) {
		if(this.night_day == 'day') 
			return false;
		
		if(!e) e = $('sonne');
		document.body.style.backgroundColor = 'white';
		document.body.style.backgroundImage = 'url(style/back.png)';
		e.setAttribute('src', 'style/sonne.gif');
		e.onclick = function() {
			System.Night(this);
		}
		
		this.night_day = 'day';
	}
};

var Bug = {
	interval: null,
	FlyAway: function(img) {
		if(this.interval)
			window.clearInterval(this.interval);
			
		var oldRight = '20px';
		var oldBottom = '40px';
		Biene.FlyIn('Keine Angst, die Libelle kommt wieder :-)');
		this.interval = window.setInterval(function() {
			var right = (parseInt(img.style.right) + 45);
			if(right > (document.viewport.getWidth() + 100)) {
				window.clearInterval(Bug.interval);
				img.style.display = 'none';
				img.style.right = oldRight;
				img.style.bottom = oldBottom;
				window.setTimeout(function() {
					Biene.FlyOut();
					Effect.Appear(img);
				}, 4500);
			} else {
				img.style.right = right + 'px';
				img.style.bottom = (parseInt(img.style.bottom) + 25) + 'px';
			}
		}, 100);
	}
};

var Biene = {
	interval: '',
	hidden: false,
	FlyIn: function(text) {
		if($('content').style.display != 'none') return false;
		if(this.interval != '') clearInterval(this.interval);
		this.interval = '';
		this.setText(text);
		var biene = $('info_biene'), self = this;					
		biene.style.top = '-110px';
		biene.style.display = '';
		this.interval = setInterval(function() {
			var top = parseInt(biene.style.top) + 30;
			if(top >= 20) {
				clearInterval(self.interval);
				self.hidden = false;
			} else biene.style.top = '' + top + 'px';
		}, 100);
	},

	FlyOut: function() {
		if($('content').style.display != 'none') return false;
		if(this.interval != '') clearInterval(this.interval);
		var biene = $('info_biene'), self = this;
		this.interval = setInterval(function() {
			var top = parseInt(biene.style.top) - 30;
			if(top <= -110) {
				biene.style.display = 'none';
				biene.style.top = '-110px';
				clearInterval(self.interval);
				self.hidden = true;
			} else biene.style.top = '' + top + 'px';
		}, 100);
	},

	setText: function(text) {
		$('bs_bg').update(text);
	}
};

if(typeof winStatic == 'undefined') {
	Event.observe(window, 'load', function() {
		$('info_biene').style.top = '10px';
		
		Biene.setText('Herzlich Willkommen in der Holledauer Kräuterwerkstatt!');
		if(document.cookie.indexOf('info_holledau=1') < 0) {
			Biene.setText.delay(2, 'Klicken Sie auf die Äste des Baumes, dort befindet sich die Navigation.');
			document.cookie = 'info_holledau=1';
		}
		Biene.FlyOut.delay(10);
	});
} else {
	Event.observe(window, 'load', function() {
		$('info_biene').style.top = '-110px';
	});
}
