$(function(){
	initButtons();
	initBackLinks();
	initTooltips();
	initTicker(1800);
});

var garamond = { src: 'fileadmin/images/garamond.swf' };
sIFR.ua.supported = sIFR.ua.supported && (!sIFR.ua.ie || sIFR.ua.ieVersion > 6);
sIFR.activate(garamond);		
sIFR.replace(garamond, {
	selector:'#col3>#col3_content > .csc-header > h1.csc-firstHeader',
	css: '.sIFR-root { background-color: #fcfdfd; color: #002F60; text-transform:uppercase }',
	wmode:'transparent'
});

function initTicker(mseconds){
	$('.ticker').each(function(){
		var elements = $(this).find('.links>*');
		if (elements.length <= 1) return;
	    var activeElements = elements.filter(':visible');
		var nextActiveElement;
	    if (activeElements.length != 1 ||
	        activeElements.next().length == 0) {
	        nextActiveElement = elements.filter(':first-child');
	    } else {
	        nextActiveElement = $(activeElements).next();
	    }
	    $(activeElements).hide();
	    $(nextActiveElement).fadeIn(300);
	});
    window.setTimeout('initTicker('+mseconds+')', mseconds);
}

function initTooltips(){
	var xOffset = 20;
	var yOffset = 20;
	$('.infobox img').hover( function(e){
		$(".infobox-open").remove();
		$("body").append($(this).nextAll('.more').clone().addClass('infobox-open'));
		$(".infobox-open")
			.css("top",($(e.currentTarget).offset().top - xOffset) + "px")
			.css("left",($(e.currentTarget).offset().left + yOffset) + "px")
			.css("position","absolute")
			.fadeIn("fast");
			initCorners();
    },
	function(){
		$(".infobox-open").remove();
		$('ignore').remove();
    });	
}

function initBackLinks() {
	$("a[href*='/back/']").each(function(){
		var o = $(this);
		var href = o.attr('href');
		var re = /(back\/[\d]+)(\/.*)/;
		if (re.test(href)) {
			$(this).click(function(){
				if (location.hash) {
					var hash = location.hash.replace('#','');
					var newHref = href.replace(re, '$1,'+hash+'$2');
					o.attr('href',newHref);
				}
				//console.log('insertBackHash: '+href + ' => ' + newHref);
				//return false;
			});
		}
	});
}

function initButtons() {
	var ovBtn;
	var clBtn;
	
	var update = function(){
		$('.gui-btn-hover').removeClass('gui-btn-hover');
		if (ovBtn != clBtn) $(ovBtn).addClass('gui-btn-hover');

		$('.gui-btn-click').removeClass('gui-btn-click');
		$(clBtn).addClass('gui-btn-click');
	};

	var onMouseOver = function(e) {
		ovBtn = e.currentTarget;
		update();
	};

	var onMouseOut = function(e) {
		ovBtn = null;
		clBtn = null;
		update();
	};

	var onMouseDown = function(e){
		clBtn = e.currentTarget;
		clBtn.blur();
		update();
	};

	var onMouseUp = function(e){
		clBtn = null;
		update();
	};

	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		$('.gui-btn').mouseenter(onMouseOver);
		$('.gui-btn').mouseleave(onMouseOut);
	}

	$('.gui-btn').mousedown(onMouseDown);
	$('.gui-btn').mouseup(onMouseUp);

}

