window.addEvent("domready", function() {
	overrideA();
	placeHeads();

	if(Browser.Engine.name == "trident") {
		eHeader = $('flash_header').getElement('object');
	} else {
		eHeader = $('flash_header').getElement('embed');
	}

	current_uri = new URI(location.href);

	if(location.hash.substr(1) != "") {
		hash_uri = location.hash.substr(1)
	}
});

window.addEvent('hashchange', function() {
	hash_uri = location.hash.substr(1)
	if(hash_uri != "") {
		eOpenLink(hash_uri);
	}

});

function setCurrentFlashSide() {

	try {

		if(hash_uri != null && hash_uri != "") {
			eOpenLink(hash_uri);
			eHeader.eOpenLink(current_id);
		} else{
			if(current_uri.get('directory')) {
				current_id = current_uri.get('directory');
				current_id = current_id.substr(1);
			}

			if(current_id == 0) {
				current_id = "buero/neues/";
			}

			eHeader.eOpenLink(current_id);
		}

	} catch(e) {
		//setTimeout("setCurrentFlashSide()", 100);
	}
}

eHeader = null;
last_link_request = null;
current_id = 0;
current_uri = null;
hash_uri = null;

// Main Opener/Link loader (called by Flash Header and Internal Call)
function eOpenLink(id) {

	if(id == current_id) {
		return;
	}

	location.href = 'http://' + location.host + location.pathname + '#' + id;

	try {
		pageTracker._trackPageview('http://' + location.host + '/' + id);
	} catch(e) {
	}
	
	var req = new Request.HTML({
		url: 'http://' + location.host + '/' + id,
		evalScripts: true,
		evalResponse: true,
		method: 'get',
		data: {type: '110124'},
		onRequest: function() {

			if(last_link_request != null) {
				last_link_request.cancel();
				last_link_request = null;
			}

			last_link_request = this;
		},
		onComplete: function() {

			var html = this.response.html;
			var content = $('content_wrap');
			content.style.height = content.getHeight() + "px";

			var fade = new Fx.Tween(content,{link: 'chain'});
			fade.addEvent("complete", function() {

				this.removeEvents("complete");
				if(Browser.Engine.name != "trident") {
					var content = $('content_wrap');

					var oldHeight = content.getHeight();
					content.set("html", this.setHtml);
					content.style.height = "";
					var newHeight = content.getHeight();
					content.style.height = oldHeight + "px";

					this.start("height", oldHeight, newHeight);
				} else {

					var content = $('content_wrap');
					content.set("html", this.setHtml);
					content.style.height = "100%";
				}

				placeHeads();
				overrideA();
				replace_texts();

				this.start("opacity", 0, 1);
			});

			fade.setHtml = html;
			fade.start("opacity", 1, 0);
			overrideA();
		}
	}
	);

	req.send();
	current_id = id;
}

// Internal Call, toggles Flash Header, calls Main Opener
function openLink(id) {
	new Fx.Scroll(document.body).toTop();

	eOpenLink(id);
	eHeader.eOpenLink(id);
}

// Override all "a" Tags with Internal Call
function overrideA() {
	$$('a').each(function(a) {
		var link = a.get('href');
		var target = a.get('target');
		if(0>link.indexOf("mailto:") && "_blank" != target) {
			a.removeEvents('click');
			a.addEvents({
			'click': function() {
				openLink(link)
				return false;
			}
			});
		}
	});
}

// Place Heads of Articles
function placeHeads() {

	var heads = $$('.box_top_wrap');

	var head = null;
	var img = null;
	var base = null;
	var left = "";

	for(var i=0; i < heads.length; i++) {

		base = heads[i];
		head = heads[i].getElement('div');

		img = heads[i].getElementsByTagName("img");
		img = $(img[0]);

		if(head && img) {

			head.style.position = "";
			head.style.top = "";

			var top = img.getHeight();
			top = top - head.getHeight()

			if(img.getHeight() > head.getHeight()) {
				base.style.height = img.getHeight() + "px";

				left = head.getLeft() - img.getLeft();

				head.style.position = "absolute";
				head.style.bottom = 0;
				head.style.left = left + "px";

			} else {
				base.style.height = head.getHeight() + "px";
			}

			if(false && img.getHeight() < head.getHeight()) {
				head.style.position = "relative";
				head.style.top = top + "px";
			}
		}
	}
}
