/* carrousel functions */
var EFFECTS_ENABLED = true;
var CARROUSEL_PAUSE = 4000;
var CARROUSEL_SPEED = 2000;
var CARROUSEL_ONCLICK_TIMEOUT = 200;

var now = new Date();
var carrousel_count;
var carrousel_current;
var carrousel_to;
var carrousel_timeout;
var carrousel_img_active;
var carrousel_img_inactive;


function initCarrousel() {
	carrousel_count = 3;
	carrousel_current = 1;
	carrousel_to = carrousel_current + 1;
	carrousel_timeout = setTimeout ('changeBg()', CARROUSEL_PAUSE);
	carrousel_img_active = 'images/slider-inactive.png';
	carrousel_img_inactive = 'images/slider-active.png';
}

function changeBg(to, speed) {
	if (!EFFECTS_ENABLED) return;
    if (to != undefined) {
    	trace('1: to is undefined');
    	carrousel_to = to;
    	trace('2: carrousel_to is set to ' + carrousel_to);
    	if (carrousel_timeout != undefined) clearTimeout(carrousel_timeout);
    	for (i=1; i<=carrousel_count; i++) {
    		$('#carrousel-back-' + i).stop();
	        $('#carrousel-back-' + i).hide();
	        $('#carrousel-back-' + i).css({ 'opacity' : 1 });
	        $('#carrousel-text-' + i).hide();
			$('#slider-'+ i).html('<img src="' + carrousel_img_inactive + '">');
	    }
		carrousel_current = getCarrouselNext(to-1);
		trace('3: carrousel_current is set to ' + carrousel_current);
    	$('#carrousel-text-'+ carrousel_to).show();
        $('#slider-'+ carrousel_to).html('<img src="' + carrousel_img_active + '">');
        $('#carrousel-back-' + carrousel_to).show();
        carrousel_to = getCarrouselNext(carrousel_to);
        trace('4: carrousel_to is set to ' + carrousel_to);
    	
        carrousel_timeout = setTimeout ('changeBg()', CARROUSEL_PAUSE + CARROUSEL_SPEED);
    	trace('4b: timeout set');
        return;
    }
    
    to = carrousel_to;
    if (speed == undefined) speed = CARROUSEL_SPEED;			    
    
    $('#carrousel-back-' + carrousel_current).fadeOut(speed, function() {
        $('#carrousel-text-' + carrousel_current).hide();
		for (i=1; i<=carrousel_count; i++) {
        	if (i!=to) {
		        $('#slider-'+ i).html('<img src="' + carrousel_img_inactive + '">');
        	}
        }
        carrousel_current = getCarrouselNext(to-1);
    	trace('5: carrousel_current is set to ' + carrousel_current);
      });

	$('#carrousel-back-' + carrousel_to).fadeIn(speed, function() {
        $('#carrousel-text-'+ carrousel_to).show();
        $('#slider-'+ carrousel_to).html('<img src="' + carrousel_img_active + '">');
        carrousel_to = getCarrouselNext(carrousel_to);
    	trace('6: carrousel_to is set to ' + carrousel_to);
    	carrousel_timeout = setTimeout ('changeBg()', CARROUSEL_PAUSE);
		trace('7: timeout set');
      });
      
    
}

function getCarrouselNext(item) {
	item == carrousel_count ? item = 1 : item++;
	return item;
}

function getCarrouselPrev(item) {
	if (--item == 0) item = carrousel_count;
	return item;
}


/* foto browser */
var fotobrowser_current;
var fotobrowser_count;
var fotobrowser_images;

function initFotoBrowser(name) {
	if (name == undefined) name = 'fotobrowser';
	fotobrowser_current = 1;
	fotobrowser_count = $('.' + name).length;
	fotobrowser_images = new Array();

	for (i = 0; i< fotobrowser_count; i++) {
		fotobrowser_images[i] = document.getElementById(name + '-' + (i + 1)).src;
	}
}

function getFotoBrowserNext(item) {
	item == fotobrowser_count ? item = 1 : item++;
	return item;
}

function getFotoBrowserPrev(item) {
	if (--item == 0) item = fotobrowser_count;
	return item;
}

function getFotoBrowserIndex(id) {
	res = (fotobrowser_current + id - 1) % fotobrowser_count;
	if (res == 0) res = fotobrowser_count;
	return res;
}

function changeFotoBrowser(to, name) {
	if (name == undefined) name = 'fotobrowser';
	$('#' + name + '-nav').fadeOut(250, function() {
		fotobrowser_current = to;
		for (i = 1; i <= fotobrowser_count; i++) {
			document.getElementById(name + '-' + i).src = fotobrowser_images[getFotoBrowserIndex(i) - 1];
		}
		document.getElementById(name + '-large-flyout').href = fotobrowser_images[getFotoBrowserIndex(i) - 1];
		document.getElementById(name + '-large').src = fotobrowser_images[fotobrowser_current - 1];
		$('#' + name + '-nav').fadeIn(250, function() {
		
		});
	});
}

function trace(val) {
	if ($('#trace') != undefined) {
		tt = new Date() - now;
		$('#trace').append(val + ' (' + tt + ')\r\n');	
	}
}
/* rollover functions */

$(function() {
    $('.rollover').hover(function() {
        var currentImg = $(this).attr('src');
        $(this).attr('src', $(this).attr('name'));
        $(this).attr('name', currentImg);
    }, function() {
        var currentImg = $(this).attr('src');
        $(this).attr('src', $(this).attr('name'));
        $(this).attr('name', currentImg);
    });
});

$(document).ready(function() {
	/* add spans to lists */

	$("#content ol li, #content ul li").wrapInner("<span></span>");

	/* add behaviour to lang selection */
	$(".lang-down").click(function() {
		$("#lang-dropdown").toggle();
	});
	
	$(".lang-hover").hover(function() {
		$("#lang-dropdown").toggle();
	});
	
	$("#lang-dropdown").hover(function() {
		$("#lang-dropdown").toggle();
	});
});

/* table styling */
$(document).ready(function() {
	$('#content table td:nth-child(1)').css( {padding: '0 0 0 20px'} );

	$('#content table.header-grijs th').css( {backgroundColor: '#eaece3'} );
	$('#content table.even-grijs th').css( {backgroundColor: '#eaece3'} );
	$('#content table.even-grijs tr:nth-child(odd)').css( {backgroundColor: '#eaece3'} );

	$('#content table.five-col th').css( {backgroundColor: '#eaece3'} );
	$('#content table.five-col tr:nth-child(odd)').css( {backgroundColor: '#eaece3'} );
	$('#content table.five-col td:nth-child(1n+0)').css( {width: '15%'} );
	$('#content table.five-col td:nth-child(1)').css( {width: '40%'} );

	$('#content table.two-col tr:nth-child(odd)').css( {backgroundColor: '#eaece3'} );
	$('#content table.two-col td:nth-child(1n+0)').css( {width: '50%'} );
	$('#content table.two-col td:nth-child(1)').css( {width: '50%'} );

	
	$('#content table.tree-col th').css( {backgroundColor: '#eaece3'} );
	$('#content table.tree-col tr:nth-child(odd)').css( {backgroundColor: '#eaece3'} );
	$('#content table.tree-col td:nth-child(1n+0)').css( {width: '43%'} );
	$('#content table.tree-col td:nth-child(1)').css( {width: '14%'} );

	$('#content table.four-col th').css( {backgroundColor: '#eaece3'} );
	$('#content table.four-col tr:nth-child(odd)').css( {backgroundColor: '#eaece3'} );
	$('#content table.four-col td:nth-child(1n+0)').css( {padding: '8px 0 8px 20px'} );
	$('#content table.four-col td:nth-child(1)').css( {width: '35%'} );
	$('#content table.four-col td:nth-child(3)').css( {width: '35%'} );
});
