//Showcase script

function Image(identifier, src, width, height, caption, eventType) {
    this.identifier = identifier;
    this.src = src;
    this.width = width;
    this.height = height;
	this.caption = caption;
	this.eventType = eventType;
}

var images = new Array();
var current_identifier = null;
var automatic = true;
var timeout_milliseconds = 6500;

function fade(current_image, next_image) {
    current_identifier = next_image.identifier;

    var el = $('#showcase');
    var step2 = function() {
        el.css('background-image', 'url('+next_image.src+')');
        //el.css('opacity', 0);
        el.animate({'opacity': 1, 'height': next_image.height+'px'}, 1300);
    };
    el.animate({'opacity': 0}, 100, step2);
}

function setEventLink(image) {
	if (image.eventType == 1) {
		$('#event_link').attr('href', '/training/workshops/');
	} else if (image.eventType == 2) {
		$('#event_link').attr('href', '/training/courses/');
	}
}

function forward() {
    current_pos = -1
    for (i=0; i<images.length; i++) {
        if (images[i].identifier == current_identifier) {
            current_pos = i;
            break;
        }
    }
    if (current_pos == -1)
        return
    next_pos = current_pos + 1;
    if (next_pos >= images.length) {
        next_pos = 0; // loop around
    }
    current_image = images[current_pos];
    next_image = images[next_pos];

    if (next_pos == images.length-1) {
        $('#showcase .next').hide();
    } else {
        $('#showcase .next').show();
    }
    if (images.length > 1) {
        $('#showcase .previous').show();
    }

	setEventLink(next_image);
    fade(current_image, next_image);
	$('#event_details').html(next_image.caption);
}
function backward() {
    current_pos = -1
    for (i=0; i<images.length; i++) {
        if (images[i].identifier == current_identifier) {
            current_pos = i;
            break;
        }
    }
    if (current_pos == -1)
        return
    previous_pos = current_pos - 1;
    if (previous_pos < 0) {
        previous_pos = images.length-1;
    }
    current_image = images[current_pos];
    previous_image = images[previous_pos];

    if (previous_pos == 0) {
        $('#showcase .previous').hide();
    } else {
        $('#showcase .previous').show();
    }
    if (images.length > 1) {
        $('#showcase .next').show();
    }

	setEventLink(previous_image);
    fade(current_image, previous_image);
	$('#event_details').html(previous_image.caption);
}
function next(event) {
    event.preventDefault();
    automatic = false;

    forward();
}
function previous(event) {
    event.preventDefault();
    automatic = false;

    backward();
}
function cycle() {
    if (automatic) {
        forward();
        setTimeout(cycle, timeout_milliseconds);
    }
}

jQuery.preloadImages = function() {
    for(var i = 0; i<arguments.length; i++) {
        jQuery("<img>").attr("src", arguments[i]);
    }
    if (images.length > 1) {
        setTimeout(cycle, timeout_milliseconds);
    }
}
