function randomToN(maxVal, floatVal) {
    var randVal = Math.random() * maxVal;
    return typeof floatVal == 'undefined' ? Math.round(randVal) : randVal.toFixed(floatVal);
}

// begin slideshow
$(document).ready(function() {
	var first = $('#divTeaserImage img:first');
	if (first != null)
		first.attr('src', first.attr('rel'));
	
    // get random number for starting slide
    var rand = 0;
    if($('#divTeaserImage > img').length > 0) {
		rand = randomToN($('#divTeaserImage > img').length, 0);
    }
		
    var fwd = true;
    $('#divTeaserImage').cycle({
        fx: 'fade',
        speed: 3000,
        timeout: 6000,
        startingSlide: rand,
        before: onBefore,
        after: onAfter
    });

    function onBefore(currSlide, nextSlide, opts) {
        // set description text
        $('#spanTeaserTitle').html($(nextSlide).attr('title'));
		$('#spanTeaserIntro').html($(nextSlide).attr('alt'));
		$('a.aTeaserLink').attr('href', $(nextSlide).attr('href'));
		
        // rel attr pretends image from downloading. At the time when the slide
        // is being loaded the rel attr will be replaced by the src attr and the
        // browser physically downloads the image.
        if ($(nextSlide).attr('src') === '' || $(nextSlide).attr('src') === undefined) {
            $(nextSlide).attr('src', $(nextSlide).attr('rel'));
        }
    }

    function onAfter(currSlide, nextSlide, opts) {
        // backward cycle
        if (!fwd && opts != null) {
            if (opts.currSlide == 0) {
                opts.nextSlide = opts.slideCount - 1;
            } else {
                opts.nextSlide = opts.currSlide - 1;
            }
        }
    }
});
// end slideshow

// Link list
$(document).ready(function() {
	$('.contentTable .opener').each(function(i){
		var container = $(this).nextAll('div:first');
		$(this).click(function(){
			if(!container.is(':visible')){
				container.show();
			}
			else{
				container.hide();
			}
		});
		if(i>0){
			container.hide();
		}
	});
});

function slideSwitch() {
	var $last = $('#project-slider div.project:last');
	var $active = $('#project-slider div.active');
    var $next = $active.next(); 
    
    if($active.attr('title') === $last.attr('title')) {
		$next = $('#project-slider div.project:first');
    }
       
    $next.addClass('active');
    $next.show();
    
    $active.removeClass('active');
    $active.hide();
}

$(document).ready(function() {
	$('#project-slider div.project').each(function(i){
		$(this).hide();
	});
	$('#project-slider div.project:first').show();
	$('#project-slider div.project:first').addClass("active");
	
	$(function() {
		setInterval("slideSwitch()", 5000);
	});
});




