/* Text Area Zeichen limitieren und Rest ausgeben */
function limitTextAreaChars(textid, infodiv, limit, en){

	/* Belegte Zeichen */
	var text = $('#'+textid).val();	
	var textlength = text.length;

	if(textlength > limit)
	{
		if (en){
			$('#' + infodiv).html('EN: Mehr als ' + limit + ' Zeichen sind nicht möglich!');
		}else{
			$('#' + infodiv).html('Mehr als ' + limit + ' Zeichen sind nicht möglich!');
		}
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}else{
		if (en){
			$('#' + infodiv).html((limit - textlength) + 'EN: Zeichen verbleibend');
		}else{
			$('#' + infodiv).html((limit - textlength) + ' Zeichen verbleibend');
		}
		return true;
	}
}


/* Logos austauschen beim Hover */
$(document).ready(function() {
	$('.greyonhover').hover(function() {
		var srcPath = $(this).attr('src');
		srcPathOver = srcPath.replace("/normal/", "/hover/");
		$(this).attr("src", srcPathOver);
	}, function() {
		var srcPath = $(this).attr('src');
		srcPathOver = srcPath.replace("/hover/", "/normal/");
		$(this).attr("src", srcPathOver);
	});

});


function ankerGet(obj){
	var anker = "";
	
	anker = obj.parent().attr('id');
	if (anker == ""){
		anker = obj.parent().parent().attr('id');
	}
	if (anker == ""){
		anker = obj.parent().parent().parent().attr('id');
	}
	if (anker == ""){
		anker = obj.parent().parent().parent().parent().attr('id');
	}
	if (anker == ""){
		anker = obj.parent().parent().parent().parent().parent().attr('id');
	}
	if (anker == ""){
		anker = obj.parent().parent().parent().parent().parent().parent().attr('id');
	}
	
	if (anker != ""){
		anker = "#" + anker;
	}
	return anker;
}


/* Aktiven Menüpunkt beim Scrollen highlighten */
/* Top Top Lasche anzeigen / verstecken */
var oldMenuItem = null;
var scrollUpDiff = null;
$(document).ready(function() {
	scrollUpDiff = parseInt($('#contentframe').css("padding-top"));
	$('#allwaystotop').hide();
});

$(window).scroll(function() {
	
	scrolltop = $(window).scrollTop() + scrollUpDiff;
	//alert ("scrolltop=" + scrolltop );
	
	if (scrolltop > scrollUpDiff){
		$('#allwaystotop').show();
	}else{
		$('#allwaystotop').hide();
	}
	
	// Alle h2 Überschriften in div durchlaufen
	// Oder Presseelemente
	$('#contentmain div h2, #contentmain div.fce-presseeintrag').each(function(index) {
		var position = $(this).position();
		if(position){
			var top = position.top;
			//var bottom = top + $(this).parent().height();
			//alert ("text=" + $(this).text() + " scrolltop=" + scrolltop + " top=" + top + " bottom=" + bottom);
			var anker = "";
			var href = "";
			var ipos = null;
			var menuItem = null;

			if (top >= scrolltop){
				//anker = "#" + $(this).parent().attr('id');
				anker = ankerGet($(this));
				//anker = "#" + $(this).parent('div.csc-frame, div.csc-default').attr('id');
				//alert("Found @ index=" + index + ': ' + $(this).text() + "  div.top=" + top  + "   scrollTop=" + scrolltop + " anker=" + anker);
				
				// Den Menüpuntk mit dem Ankerlink finden
				if(anker != "#"){
					
					$('#contentleftnav ul.nav1 ul.nav2 a').each(function(indexnav) {
						href = $(this).attr("href");
						ipos = href.indexOf(anker);
						if (ipos > 0){
							menuItem = $(this);
							//alert("Found @ index=" + indexnav + ': ' +menuItem.attr("href") );
							
							// Alte li Klassen current löschen
							if (oldMenuItem){
								oldMenuItem.parent().removeClass('current');
							}
							
							// li class current für gefundendes Element setzen
							menuItem.parent().addClass('current');
							oldMenuItem = menuItem;
							return false;
						}
					});
					return false;
						
				}
			}	

		}
	});

});

$(document).ready(function() {
	menuItem = $('#contentleftnav ul.nav1 ul.nav2 a:first');
	menuItem.parent().addClass('current');
	oldMenuItem = menuItem;
});



/* Anker Navigation: Sorgt dafür dass der Content immer um 230px nach oben geschrollt wird beim Anspringen */
var scrollUpValue = -230;
var scrollUpTimeout = 100;

function addEvent (obj, event, func) {
    if (typeof(obj["on" + event]) == "function") {
        var old_func = obj["on" + event];
        obj["on" + event] = function (e) {
            if (!e)
                e = window.event;
            var return1, return2;
            return1 = old_func(e);
            return2 = func(e);
            if (return1 === false || return2 === false)
                return false;
        };
    } else {
        obj["on" + event] = func;
    }
}
function link_scrollup () {
    //alert("link_scrollup");
    window.setTimeout('window.scrollBy(0, ' + scrollUpValue + ')', scrollUpTimeout); // Der Wert -60 muss an die HÃ¶he des fest positionierten Elements angepasst werden.
}
function init_position_fixed () {
    var i,  hyperlinks, linkcount,  link_uri, document_uri,  hash_position,  pattern;
    if (! window.scrollBy)
        return true;
    if (location.hash)
        window.scrollBy(0, scrollUpValue); // Hier ebenfalls.
    hyperlinks = document.links;
    linkcount = hyperlinks.length;
    document_uri = location.href;
    hash_position = document_uri.indexOf("#");
    if (hash_position > -1) {
        document_uri = document_uri.substring(0, hash_position);
    }
    pattern = new RegExp("file:///");
    document_uri = document_uri.replace(pattern, "file://localhost/");
    for (i = 0; i < linkcount; i++) {
        if (!hyperlinks[i].hash || hyperlinks[i].hash == "#")
            continue;
        link_uri = hyperlinks[i].href;
        hash_position = link_uri.indexOf("#");
        if (hash_position > -1) {
            link_uri = link_uri.substring(0, hash_position);
        }
        if (link_uri.indexOf("file://") == 0) {
            link_uri = link_uri.replace(pattern, "file://localhost/");
        }
        if (link_uri == document_uri) {
            addEvent(hyperlinks[i], "click", link_scrollup);
        }
    }
}

function cancel_event (e) {
	return false;
}
function init_cancel () {
	addEvent(document.links[0], "click", cancel_event);
}
$(document).ready(function() {
	addEvent(window, "load", init_position_fixed);
});

