//Showcase script

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

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

function fade(current_image, next_image) {
    current_identifier = next_image.identifier;
    
    el = $('.gallery');        
    step2 = function() {        
        el.css('background-image', 'url('+next_image.src+')');
        //el.css('opacity', 0);        
        el.animate({'opacity': 1, 'height': next_image.height+'px', 'width': next_image.width+'px'}, 1000);
    }    
    el.animate({'opacity': 0}, 100, step2);
}

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) {
        $('.gallery .next').hide();
    } else {
        $('.gallery .next').show();
    }
    if (images.length > 1) {        
        $('.gallery .previous').show();
    }
    
    fade(current_image, next_image);
}
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) {
        $('.gallery .previous').hide();
    } else {
        $('.gallery .previous').show();
    }
    if (images.length > 1) {        
        $('.gallery .next').show();
    }
    
    fade(current_image, previous_image);
}
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);
    }
}

// Same column height script

function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}
$(document).ready(function() {
	equalHeight($(".col-height")); // class .col-height
});