var browser = navigator.userAgent.toLowerCase();
isIE = (browser.indexOf("msie") != -1);



//bei domredy starten
var isInit = false;
window.onDomReady = function(fn){
	if(window.addEventListener){
		window.addEventListener("DOMContentLoaded", fn, false);
	}else if(window.addEvent){
		window.addEvent('domready',fn);
	}else{
		document.onreadystatechange = function(){
			if(document.readyState == "interactive"  || document.readyState == "loaded" || document.readyState == "complete"){
				!isInit ? fn():0;
				isInit = true;				
			}
		}
        }
}
window.onDomReady(jsStart);

function jsStart(){
	smoothScoll();
}

if(window.addEventListener){
	window.addEventListener("load", function() {showNews();}, false);
}else if(window.attachEvent){
	window.attachEvent("onload", function() {showNews();});
}

function showNews(){
	var newstitle = document.getElementById('newstitle').innerHTML;
	if((newsdate > (todaydate - 2592000)) && newstitle.length > 2){
		createCSS("#news", "display:block;");
	}
}

function CloseNews(){
	createCSS("#news", "display:none;");
}

function createCSS(selector, declaration){
        var style_node = document.createElement("style");
        style_node.setAttribute("type", "text/css");
        style_node.setAttribute("media", "screen");
        if (!isIE) style_node.appendChild(document.createTextNode(selector + " {" + declaration + "}"));
        document.getElementsByTagName("head")[0].appendChild(style_node);
        if (isIE && document.styleSheets && document.styleSheets.length > 0) {
		var last_style_node = document.styleSheets[document.styleSheets.length - 1];
		if (typeof(last_style_node.addRule) == "object") last_style_node.addRule(selector, declaration);
        }
}



//percent for each step (lower = slower, higher = faster, default = 18)
var stapSize = 12;

//get all a tags
var aTags = document.getElementsByTagName('a');
//all links activated
var links_actve = true;
//check all links .. if it is an archnor and it have a valid target, it gets the scroll onclick event
function smoothScoll(){
	for(i=0;i<aTags.length;i++){
		if(aTags[i].href.indexOf("#") != -1){
			Anchor_target = aTags[i].href.split('#');
			for(y=0;y<aTags.length;y++){
				if(aTags[y].name && aTags[y].name == Anchor_target[1]){
					aTags[i].onclick = function() {scrolltoTarget(this.href); return false;}
				}else if(document.getElementById(Anchor_target[1])){
					aTags[i].onclick = function() {scrolltoTarget(this.href); return false;}
					y = aTags.length;
				}
			}
		}
	}
}
//find the target, get its position and check the window max scroll height and width
function scrolltoTarget(a_target){
	db = document.body;
	dde = document.documentElement;
	a_target = a_target.split('#');
	var target_object = false;
	for(y=0;y<aTags.length;y++){
		if(aTags[y].name == a_target[1]){
			target_object = aTags[y];
		}
	}
	if(!target_object && document.getElementById(a_target[1])){
		target_object = document.getElementById(a_target[1]);
	}
	if(target_object){
		Tobj = target_object;
		TopPix = 0;
		while(Tobj != null){
			TopPix += Tobj.offsetTop;
			Tobj = Tobj.offsetParent;
		}
		Lobj = target_object;
		LeftPix = 0;
		while(Lobj != null){
			LeftPix += Lobj.offsetLeft;
			Lobj = Lobj.offsetParent;
		}
	}
	winHeight = window.innerHeight  || dde.clientHeight || window.offsetHeight ;
	winWidth = window.innerWidth || dde.clientWidth || window.offsetWidth;
	winScrollHeigth = dde.scrollHeight || db.scrollHeight || window.scrollHeight;
	winScrollWidth = dde.scrollWidth || db.scrollWidth || window.scrollWidth;
	winMaxHeight = winScrollHeigth - winHeight;
	winMaxWidth = winScrollWidth - winWidth;
	if(TopPix > winMaxHeight || a_target[1] == 'bottom'){TopPix = winMaxHeight;}
	if(LeftPix > winMaxWidth || a_target[1] == 'right'){LeftPix = winMaxWidth;}
	if(a_target[1] == 'top'){TopPix = 0;}
	if(a_target[1] == 'left'){LeftPix = 0;}
	if(links_actve){
		links_actve = false;
		startScrollTo(TopPix,LeftPix,stapSize,'y','x');		
	}
}
// starts the scroll move
function startScrollTo(TopPix,LeftPix,steps,lasty,lastx){
	db = document.body;
	dde = document.documentElement;
	SCtop = db.scrollTop || dde.scrollTop || window.pageYOffset || 0;
	SCLeft = db.scrollLeft || dde.scrollLeft|| window.pageXOffset || 0;

	if(lasty == SCtop){TopPix = SCtop;}
	if(lastx == SCLeft){LeftPix = SCLeft;}
	lasty = SCtop;
	lastx = SCLeft;

        if(SCtop<TopPix){
		top_staps = Math.ceil(SCtop+(((TopPix-SCtop)/100)*steps));
	}else if(SCtop>TopPix){
		top_staps = Math.floor(SCtop-(((SCtop-TopPix)/100)*steps));
	}else{
		top_staps = SCtop;
	}
        if(SCLeft<LeftPix){
		left_staps = Math.ceil(SCLeft+(((LeftPix-SCLeft)/100)*steps));
	}else if(SCLeft>LeftPix){
		left_staps = Math.floor(SCLeft-(((SCLeft-LeftPix)/100)*steps));
	}else{
		left_staps = SCLeft;
	}
	if((SCtop != TopPix || SCLeft != LeftPix) && !links_actve){
		window.scrollTo(left_staps,top_staps);
		setTimeout("startScrollTo('"+TopPix+"','"+LeftPix+"','"+steps+"','"+lasty+"','"+lastx+"')",10);
	}else{
		links_actve = true;
	}
}
