// on doc ready
$(document).ready(function(){
	footer();
	ctrl_search();
	//ctrl_language();
	slideshow(0,1);
	tabs();
	customer_page();
});

function customer_page() {
	if ( $("#kunde-login").length ) {
		$("#kunde-login #file-list").next().remove();
	}
}

function tabs() {
	if ( $(".tabs").length ) {
		
		$(".tabs li").click(function(){
			$(".tabs li.active").removeClass('active');									 
			$(this).addClass('active');
			$(".tabs-content").html( $(this).children('.content').html() );
		});
		
		// see if there is a hash
		var hash = window.location.hash;
		
		if (hash) {
			$("#select_"+hash.substring(1)).parent().click();
		} else {
			$(".tabs li:eq(0)").click();
		}
	}
}

// reset the footer height
function footer() {
	var height = $("#footer .content").get(0).offsetHeight;
	
	$("#footer").css({
		height: height,
		marginTop: -height
	});
	
	$("#main-content").css({
		paddingBottom: height+25
	});
}

// searchbox
function ctrl_search() {
	$("#ctrl_search input[name='keywords']").bind('keypress', function(evt){
		//$(this).parent().submit();
	});
}

// select language
function ctrl_language() {
	// select the proper language
	var current_language = $("html").attr('lang')
	$.each( $("#ctrl_language")[0].options, function(i,el){
		if (el.value == current_language) {
			$("#ctrl_language")[0].selectedIndex = i;
		}
	});
	
	// bind the event changer
	$("#ctrl_language").bind('change', function(){
		var val = this.options[this.selectedIndex].value;
		window.location = '/' + val + '/';
	});
}

// slideshow
function slideshow(current, next) {
	var images = $(".image-list li").length;
	
	setTimeout(function(){
		// fadeout the current image
		$(".image-list li:eq(" + current + ")").fadeOut('slow', function(){
			$(".image-list li:eq(" + next + ")").fadeIn('normal', function(){
				// see if we need to reset this thing
				if ( next == (images-1) ) {
					slideshow(next, 0);
				} else {
					slideshow(next, next+1);
				}
			});
		});
	}, 5000);
}
