var isLoaded = false;
var debug = false;

//Konfiguration TopUp
TopUp.host = 'http://www.kiwi-service.de/';
TopUp.images_path = 'fileadmin/template/img/top_up/';
TopUp.players_path = 'fileadmin/template/flash/';

TopUp.addPresets({
  ".csc-textpic-imagewrap a": {
    group: "images",
    layout: "quicklook",
    modal: 0
  }
});


jQuery(function($) {
	center_content();
	
	if($('span.background').is('.flash')) {
		//flash laden - swf
		$('.background').attr('id', 'background')
		.flash({
			height: '100%',
			width: '100%',
			src: $('span.background').text(),
			wmode: 'opaque'
		});
		center_background();
	}
	else if($('span.background').is('.flv')) {
		//flashfilm laden - flv
		$('.background').attr('id', 'background')
		.flash({
			src: 'fileadmin/template/flash/kiwi_player.swf',
			height: '100%',
			width: '100%',
			flashvars: {
				video: $('span.background').text()
			},
			wmode: 'opaque'
		});
		center_background();
	}
	else {	
		//bild laden
		$('#background').attr('src', $('.background').text()).load(function() {
			if(debug) { console.log('image ready'); }
			center_background();
		}); 
	}
		
	//und zentriert bleiben
	$(window).resize(function() {
		if(debug) { console.log('resize window'); }
		center_background();
		center_content();
	});
	
	//wenn das bild im cache liegt wird der load() befehl oben nicht ausgeführt. deshalb testen
	//window.setTimeout('test_background()', 100);
	
	/* schicke links */
	$('#menu a')
		.css({
			opacity: 0.7,
			background: 'white'
		})
		.hover(function() {
			$(this).stop().css('opacity', 1);
		}, function() {
			if(!$(this).is('.active')) {
				$(this).animate({opacity: 0.7}, 500);
			}
		})
	 .click(function() {
	 	$(this).closest('li').siblings().find('.active').removeClass('active').css('opacity', 0.7).end().end().end().addClass('active').css('opacity', 1);
	 })
	 .filter('.active').css('opacity', 1);
	 
	//autofocus
	$('input:first:visible').focus();
	
	//links in den teaserboxen verstecken
	//$('.tx-kiwiteaser-pi1 .teaser a').hide();
	
	//netter effekt für die startseite
	$('.tx-kiwiteaser-pi1>div:first').roundabout({
		childSelector: 'div.teaser'
	});
	//und automatisch abspielen
	autoPlayRoundabout();
	
});

var roundabout_timeout = '';
function autoPlayRoundabout() {
	roundabout_timeout = window.setTimeout(function() {
		$('.tx-kiwiteaser-pi1>div:first').roundabout_animateToNextChild();
		autoPlayRoundabout();
	}, 2000);
}

/* Silbentrennung */
Hyphenator.config({
	classname: 'bodytext'
});
Hyphenator.run();

function test_background() {
	if(!isLoaded) {
		$('#background').fadeIn();
	}
}
var i = 0;

function center_background() {
	isLoaded = true;
	var $bg = $('#background') /*.is('img') ? $('#background') : $('#background>:first')*/;
	var old = {w: $bg.width(), h: $bg.height()};
	var win = {w: $(window).width(), h: $(window).height(), q: 0};
	win.q = win.w/win.h;
	var now = {w: 0, h: 0, left: 0};
	
	if(old.width == 0) {
		i++;
		if(i < 5) window.setTimeout(function() { center_background(callback); }, 100);
		return false;
	}
	
	//größe setzen
	if(win.q < (old.w/old.h)) {
		now.h = win.h;
		now.w = old.w * (now.h / old.h);
	}
	else {
		now.w = win.w;
		now.h = old.h * (now.w / old.w);
	}
	
	//flash braucht die gesamte breite
	if(!$bg.is('img')) {
		now.w = win.w;
	}
	
	//zentrieren
	now.left = (win.w - now.w)/2+$(window).scrollLeft();
	
	//neue daten setzen
	$bg
		//.attr('width', now.w+'px')
		//.attr('height', now.h+'px')
		.css({
			width: now.w+'px',
			height: now.h+'px',
			left: now.left+'px',
			position: 'fixed',
			zIndex: 0,
			top:0 
		});
	
	//element einblenden
	if($bg.is(':hidden')) {
		window.setTimeout(function() { $bg.fadeIn(1000); }, 100);
	}
	
	return true;
}

function center_content() {
	var $content = $('#wrap');
	var left = ($(window).width() - $content.width())/2+$(window).scrollLeft();
	$content.css('left', left);
}

/*
function _center_background(callback) {
	isLoaded = true;
	$bg = $('#background');
	if(debug) { 
		console.log('center_background', $bg); 
		console.log('background_dimensions', $bg.width(), $bg.height(), ($(window).height() / $bg.height()));
		console.log('window_dimensions', $(window).width(), $(window).height());
	}
	if($bg.width() > 0) {
		if($(window).height() > $bg.height()) {
			//größe anpassen
			$bg
				.attr('width', )
				.css({
					height: $(window).height(),
					width: $bg.width()*($(window).width() / $bg.width()),
					position: 'fixed',
					zIndex: 0,
					top:0 
				});
		}
		
		//zentrieren
		$bg.css('left', ($(window).width() - $bg.width())/2+$(window).scrollLeft()+'px');
		
		if(typeof callback == 'function') {
			window.setTimeout(function() { callback.call() }, 100);
		}
	}
	else {
		i++;
		if(debug) { console.log('recal center_background '+i); }
		if(i < 5) {
			window.setTimeout(function() { center_background(callback); }, 100);
		}
	}
	return $bg;
}*/

