

if (!console) {
	var console = {};
	console.log = function() {
	}
	console.dir = function() {
	}
	console.group = function() {
	}
}

var sys = Object.extend({},{
	reg: []
});

var IE = {
	detect: function() {
		var ua = navigator.userAgent;
		var MSIEOffset = ua.indexOf("MSIE ");
		if (MSIEOffset == -1) {
			this.version = 0;
		} else {
			this.version = parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
		}
	},
	version: 0
}
IE.detect();

Event.observe(window, 'load', init);

function init() {

	/**
	* 
	*/
	instanceSitemap = new Sitemap();
	
	/**
	 * init Foldables
	 */
	initFoldables();
	
	/** beautify Headlines
	 * 
	 */
	beautifyBoxHeadline();
	

	
	/**
	 * 
	 */
	if (Prototype.Browser.IE && IE.version < 7) {
		// png fix
		$$('img').each(function(n) {
			imgFixPng(n);
		});
		
		// enable ul.mainnavi li:hover ul.subnavi { visibility: visible; }
		fixNaviIElt7();
	}
}

function beautifyBoxHeadline() {
	
	var dynamic_headlines = $$('h1.dynamic, h2.dynamic, h3.dynamic, h1.dynamicText, h2.dynamicText, h3.dynamicText ');
	
	dynamic_headlines.each(function(hn){
		
		var imagetype = 'gif';
		if (hn.hasClassName('png'))
			imagetype = 'png';
		if (hn.hasClassName('jpg'))
			imagetype = 'jpg';
		
		var text = hn.firstChild.nodeValue;
		hn.update('<img src="/src/dynamic/headline/regular/' + encodeURIComponent(text) + '.' + imagetype + '" alt="'+text+'" height="16" />');
	});
	
}

function fixNaviIElt7() {
	var list = $$('div#navigation ul.navimain > li');
	
	list.each(function(li) {
		Event.observe(li, 'mouseover', function(evt) {
			this.addClassName('hover');
		}.bind(li));
		Event.observe(li, 'mouseout', function(evt) {
			this.removeClassName('hover');
		}.bind(li));
	});
}

function initFoldables() {
	//alert("initFoldables once");
	
	var foldables = $$('ul.foldable');
	
	foldables.each(function(n) {
		var xyz = new Foldable(n);
		//console.log("new Foldable instance liElems.length = " + xyz.liElems.length);
		sys.reg.push(xyz);
	});
	
}

function imgFixPng(img) {
	var imgName = img.src.toUpperCase();
	if (imgName.substring(imgName.length-3, imgName.length) == "PNG" || imgName.indexOf('/IMG/DYNAMIC/') > -1)
	{

		var imgID = (img.id) ? "id='" + img.id + "' " : "";
		var imgClass = (img.className) ? "class='" + img.className + "' " : "";
		var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
		var imgStyle = "display:inline-block;" + img.style.cssText;
		if (img.align == "left") imgStyle = "float:left;" + imgStyle;
		if (img.align == "right") imgStyle = "float:right;" + imgStyle;
		if (img.parentElement.href) {
			imgStyle = "cursor:hand;" + imgStyle;
		}	
		var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='no-scale');\"></span>";
			img.outerHTML = strNewHTML;
			//i = i-1;
	}
}


var Sitemap = Class.create({
	
	divTop: null,
	divBottom: null,
	sitemapLink: null,
	sitemapImage: null,
	
	liElems: [],
	liActiveIndex: null,
	
	timeout: null,
	timeoutInterval: 1000,
	
	initialize: function() {
		
		/**
		 * bindings
		 */
		this.onLinkOver = this.onLinkOver.bindAsEventListener(this);
		this.onLinkOut = this.onLinkOut.bindAsEventListener(this);
		
		/**
		 * init sitemapLink
		 */
		if (!this.divTop) this.createTop();
		if (!this.divBottom) this.createBottom();
		
		this.sitemapLink = $$('li#metaSitemap a')[0];
		this.sitemapLink.absolutize();
		this.sitemapLink.style.zIndex = 100;
		
		this.sitemapImage = this.sitemapLink.select('img')[0]; 
		
		Event.observe(this.sitemapLink, 'mouseover', this.onLinkOver);
		Event.observe(this.sitemapLink, 'mouseout', this.onLinkOut);
		
		/**
		 * optimize?
		 */
		Event.observe(this.divTop, 'mouseover', this.onLinkOut);
		Event.observe(this.divBottom, 'mouseover', this.hide.bind(this));
		
		Event.observe($('header'), 'mouseover', function() {
			this.resetTimeout();
		}.bind(this));
		
		/**
		 * init liElems
		 */
		this.liElems = $$('ul.navimain > li');
		
		this.liElems.each(function(n, index){
			if (n.hasClassName('active')) {
				this.liActiveIndex = index;
			}
		}.bind(this));
	},
	onLinkOver: function() {
		this.show();
	},
	onLinkOut: function() {
		if (this.timeout) clearTimeout(this.timeout);
		this.timeout = setTimeout(this.hide.bind(this), this.timeoutInterval);
	},
	resetTimeout: function() {
		clearInterval(this.timeout);
	},
	show: function() {
		this.sitemapImage.src = '/img/tmp/meta.sitemap.on.png';
		this.divTop.style.display = 'block';
		this.divBottom.style.display = 'block';
		this.showSubs();
	},
	hide: function() {
		this.sitemapImage.src = '/img/tmp/meta.sitemap.off.png';
		this.divTop.style.display = 'none';
		this.divBottom.style.display = 'none';
		this.hideSubs();
	},
	showSubs: function() {
		this.liElems.each(function(n){
			n.addClassName('active');
		}.bind(this));
	},
	hideSubs: function() {
		this.liElems.each(function(n, index){
			if (index != this.liActiveIndex) n.removeClassName('active');
		}.bind(this));
	},
	createTop: function() {
		this.divTop = Builder.node('div', { className: 'sitemapTop' });
		document.body.appendChild(this.divTop); 
	},
	createBottom: function() {
		//console.dir(document.viewport);
		//console.dir(document.viewport.getDimensions());
		this.divBottom = Builder.node('div', { className: 'sitemapBottom' });
		document.body.appendChild(this.divBottom); 
	}
});

var Foldable = Class.create({
	
	rand: null,
	ul: null,
	liElems: [],
	divElems: [],
	index: 0,
	
	initialize: function(p_ul) {
		
		this.liElems = [];
		this.divElems = [];
		
		this.rand = Math.random();
		
		// init
		this.onClick = this.onClick.bindAsEventListener(this);
		
		// main
		this.ul = p_ul;
		//this.ul.style.border = '1px solid red';
		
		this.ul.select('li').each(function(elem) {
			this.initLi(elem);
		}.bind(this));
	},
	
	initLi: function (li) {
		var index = this.liElems.length;
		this.liElems.push(li);
		var div = li.select('div.content')[0];
		this.divElems.push(div);
		if (li.hasClassName('active')) {
			this.index = index;
		}
		else {
			div.hide();
		}
		var a = li.select('a.title')[0];
		a.name = index;
		Event.observe(a, 'click', this.onClick);
		
	},
	onClick: function(evt) {
		evt.stop();
		evt.target.blur();
		var index = evt.target.name;
		this.showIndex(index);
	},
	showIndex: function(index) {
		// if already open, close
		if (this.index == index) {
			this.disableIndex(index);
			this.index = null;
		}
		// else close old one
		else {
			if (this.index != null) {
				this.disableIndex(this.index);
			}
			this.enableIndex(index);
			this.index = index;
		}
	},
	enableIndex: function(index) {
		//if (this.liElems[index].hasClassName('inactive')) {
			this.liElems[index].removeClassName('inactive');
		//}
		this.liElems[index].addClassName('active');
		Effect.BlindDown(this.divElems[index], {
			duration: 0.5
		});
	},
	disableIndex: function(index) {
		this.liElems[index].removeClassName('active');
		var refd = this.liElems[index];
		Effect.BlindUp(this.divElems[index], {
			duration: 0.5,
			onComplete: function() {
				refd.addClassName('inactive');
			}.bind(this)
		});
	}
	
});
