window.onload = function(){
    $('#carousel').Carousel(
		    {
				itemWidth: 220,
				itemHeight: 124,
				itemMinWidth: 100,
				items: 'a',
				reflections: .5,
				rotationSpeed: 13.0
			}
		);
	
	passeHintergrundAn();
	
	if ($('#news').length > 0) {
   startTicker();
}

}

function passeHintergrundAn() {
	var QAProperties = {
		bgColor: 'white',
		bgWidth: 837,
		bgHeight: 628,
		bgPadding: 0,
		bgImage: 'http://anna.mcduke.net/homepage/images/DSCF9955.jpg'
		
		
		
	};
	
	var scaleBg = function () {
		var width, height;
		
		var origWidth = width = QAProperties.bgWidth;
		var origHeight = height = QAProperties.bgHeight;
		
		var windowWidth = $(window).width() - QAProperties.bgPadding;
		var windowHeight = $(window).height() - QAProperties.bgPadding;
		
		/*
		// Mit Seitenverhältnisse
		var aspect = origWidth / origHeight;
		if (height > windowHeight) {
			height = windowHeight;
			width = windowHeight * aspect;
		}
		if (width > windowWidth) {
			width = windowWidth;
			height = windowWidth * (1 / aspect);
		}
		
		
		
		*/
		
		// Ohne Seitenverhältnisse
		width = windowWidth;
		height = windowHeight;
		
		$('#bg').css('top', windowHeight - height + QAProperties.bgPadding / 2)
			.css('left', windowWidth / 2 - width / 2 + QAProperties.bgPadding / 2)
			.attr('width', width)
			.attr('height', height)
			.css('display', 'block');
	};
	
	$('body').prepend($('<img />').attr('src', QAProperties.bgImage)
			.css('position', 'absolute')
			.css('z-index', '-1')
			.css('display', 'none')
			.attr('id', 'bg'));
	scaleBg();
	
	$(window).bind('resize', scaleBg);

}

function startTicker() {
    var schlagzeilen = [
	    ' welcome +++ stop +++ willkommen +++ stop +++ benvenuti +++ stop +++ '
	];
	
	var num = 0;
	var y = 0;
	var index = 0;
	var string = '';
	var news = $('#news');
	var maxwidth = $(window).width() - 40;
	var divwidth = 0;
    var didpad = false;
	var cwidth = 0;
	
	news.empty().append("Lade...");
	var maxheight = news.height();
	
	var fillUp = function () {
	    var tnum = num;
	    
        if (divwidth === 0) {
            news.empty().append(string);
            
    	    while (news.width() < maxwidth && news.height() <= maxheight) {
    	        string = string + schlagzeilen[tnum].charAt(index);
    	        news.empty().append(string);
    	        
    	        index = index + 1;
    	        if (index >= schlagzeilen[tnum].length) {
    	            tnum = (tnum + 1) % schlagzeilen.length;
    	            index = 0;
    	        }
    	    }
    	    
    	    string = string.substr(1, string.length - 2);
    	    news.empty().append(string);
    	    index = index - 1;
    	    
    	    divwidth = news.width();
    	    cwidth = parseInt(divwidth / string.length);
        } else {
            string = string.substr(1, string.length - 1) + schlagzeilen[tnum].charAt(index);
            news.empty();
            
            // Compensate for space at the beginning of text
            // Looks very jumpy on Mozilla
            if (!$.browser.mozilla) {
                var c = string.charAt(0);
                if (c === ' ' && !didpad) {
                    news.css('padding-left', cwidth + 'px');
                    didpad = true;
                } else if (c !== ' ' && didpad) {
                    news.css('padding-left', '0px');
                    didpad = false;
                }
            }
            
            news.append(string);
	        
	        index = index + 1;
	        if (index >= schlagzeilen[tnum].length) {
	            tnum = (tnum + 1) % schlagzeilen.length;
	            index = 0;
	        }
        }
	}
	
	window.setInterval(function () {
        fillUp();
	}, 100);
}
