var cElm;
window.onresize = function() {
	handleResize(null);	
};

function getIeVersion() {
	if (AJS.isIe() ) {
		var ua = navigator.userAgent;
	    var MSIEOffset = ua.indexOf("MSIE ");
	    var v = parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
	    return v;
	}
	return 10;
}

function init(elm,gal) {
	if (gal == 0) {
		AJS.setOpacity(AJS.$('outerwrapper'),0);
		AJS.fx.fadeIn(AJS.$('outerwrapper'));
	}	
	var tp = AJS.absolutePosition(AJS.$('news_content')).y;
	var he = (AJS.$('content_inner').scrollHeight > 405) ? AJS.$('content_inner').scrollHeight : 340;
	var bt = (getIeVersion() == 6) ? 20 : 10;
	var scrollBar = new CustomScrollBar(AJS.$('news_content'),170,he,bt,tp);
	
	var topNavElms = AJS.$bytc('a','top_nav');
	for(var i=0; i< topNavElms.length;i++) {
		if(topNavElms[i].className.match('top_active') === null) {
			AJS.AEV(topNavElms[i],'mouseover',topNavHover);
			AJS.AEV(topNavElms[i],'mouseout',topNavHover);
		}
	}
	var subNavElms = AJS.$bytc('a','sub_nav');
	for(i=0; i< subNavElms.length;i++) {
		AJS.AEV(subNavElms[i],'mouseover',subNavHover);
		AJS.AEV(subNavElms[i],'mouseout',subNavHover);
	}
	
	cElm = AJS.$('sub_'+elm);
	setActive(cElm);
	
	if (getIeVersion() == 6) {
		AJS.$('background_wrapper').style.height = document.body.scrollHeight+'px';
	}
	handleResize(null);	
}

var bg;
var im;
function handleResize(evt) {
	if (getIeVersion() <= 6) {
		return;
	}
	if(!bg) {
		bg = AJS.$('background_wrapper');
	}
	if (!im) {
		im = AJS.$('background_image');
	}
	if(bg==null || im==null){
		return; 
	}
	var winW = window.innerWidth  || document.documentElement.clientWidth;
	var winH = window.innerHeight || document.documentElement.clientHeight;
	var res = new Array(winW,calcImg(winW,orgW,orgH),calcImg(winH,orgH,orgW),winH);
	var key = (winW > winH) ? ((res[1] > winH)? 0: 2) :((res[2] > winW) ? 2:0);
	im.width = res[key];
	im.height = res[key+1];
	bg.style.width = winW+'px';
	bg.style.height = winH+'px';
	var d = AJS.getRequest("http://www.eisblume-worb.ch/setImageSession.php?width="+res[key]+"&height="+res[key+1]+'&wheight='+winH+'&wwidth='+winW);
	d.sendReq()
}

function calcImg(arg,arg2,arg3) {
	return Math.round(arg3 * (1 / arg2 * arg));
}

function scrollbarWidth() {
		var width; 
		if(AJS.isIe()) {
			width = document.body.offsetWidth-document.body.clientWidth;
		}
		else {
			document.body.style.overflow = 'hidden';
			width = document.body.clientWidth;
			document.body.style.overflow = 'scroll';
			width -= document.body.clientWidth;	
		}
		document.body.style.overflow = '';
		return 0;
}

var scroller;
var innerContent;
var fx;
var cW = 0;
var cM = 0;
var t;

function scrollHere(elm) {
	if (!elm) {
		elm = cElm;
	}
	if (!elmS) {
		elmS = AJS.$('navigation_sub_scroller');
	}
	if (!innerContent) {
		elmC = AJS.$('content_inner');
	}
	var nW = elm.scrollWidth;
	var nM = AJS.absolutePosition(elm).x - AJS.absolutePosition(elmC).x - 8;
	if (fx) {
		fx.clearTimer();
		cW = fx.now.width;
		cM = fx.now.marginLeft;
		fx = null;
	}
	fx = new AJS.fx.Styles(elmS).custom({ 'marginLeft': [cM, nM],'width': [cW, nW]});
	fx.options.duration = 400;
	fx.options.onComplete = function() {
		cW = fx.now.width; 
		cM = fx.now.marginLeft;
	};
}

function setActive(elm) {
	elmS = AJS.$('navigation_sub_scroller');
	cM = AJS.absolutePosition(elm).x - AJS.absolutePosition( AJS.$('content_inner')).x - 8;
	cW = elm.scrollWidth;
	if (elmS) {
		elmS.style.marginLeft = cM+'px';
		elmS.style.width = cW+'px';
	}
}

var intVal;
var fx1;
function topNavHover(event) {
	var nId = (AJS.isIe()) ? event.srcElement : event.target;
	if(fx1 && fx1.elm == nId) {
				fx1.stop();
				fx1 = null;
		}
	if (event.type == 'mouseover') {
		fx1 = new colorFader(nId,'FFFDF6','CECDC9',1,20);
	}
	else {
		fx1 = new colorFader(nId,'CECDC9','FFFDF6',1,20);
	}
}

function subNavHover(event) {
	if (event.type == 'mouseover') {
		if (t !== null) {
			clearTimeout(t);
		}
		var rElm = (AJS.isIe()) ? event.srcElement : event.target;
		scrollHere(rElm);
	}
	else {
		t = window.setTimeout("scrollHere("+null+")", 500);
	}
}

function goToGallery() {
	var fx = AJS.fx.fadeOut(AJS.$('outerwrapper'));
	fx.options.duration = 1000;
	fx.options.onComplete = function() {
		document.location = 'http://www.eisblume-worb.ch/gallery/';
	}
}
