// JavaScript Document

$(document).ready(function(){
	initMouldings();
});

var slideInterval = 2000;
var slideDocElements;
var highlightInterval;
var slidePos;

function initMouldings(){
	var numProfiles = $("#mouldings .profile").length;
	var profileWidth = $("#mouldings .profile").width() - 12;

	$("#mouldings .profile .name a").each(function(){
		$(this).parent().show();
		var width = $(this).width();
		$(this).parent().hide();
		$(this).parent().width(width + 1);
	});

	$("#mouldings .profile.right").each(function(){
		var name = $(this).find(".name a");
		name.parent().show();
		var width = name.width();
		name.parent().hide();
		if(width > profileWidth){
			name.parent().css("right", "6px");
			name.parent().css("left", "auto");
		}
	});

	$("#mouldings .profile").click(function(){
		window.location = $(this).find("a.read-more").attr("href");
	});

$("#mouldings .profile").hover(function(){
	slidePos = 0;
	slideDocElements = $(this).find(".images .hover");
    	if ($(slideDocElements).length > 1) {
		$(this).find(".images .standard").fadeOut();
		$(slideDocElements[slidePos]).fadeIn(250);
        	highlightInterval = setInterval("setupSlides()", slideInterval);
    	}
}, function(){
	if ($(slideDocElements).length > 1) {
		$(slideDocElements[slidePos]).fadeOut(250);
		$(this).find(".images .standard").fadeIn(250);
		window.clearInterval(highlightInterval);
	}
});
}

function setupSlides() {
    changeSlide(slideDocElements);
}

function changeSlide(elements) {
    var nextPos;

    if (slidePos < elements.length - 1)
        nextPos = slidePos + 1;
    else
        nextPos = 0;

    fadeSlide(elements, slidePos, nextPos);
}

function fadeSlide(elements, currentSlidePos, newSlidePos) {
    $(elements[currentSlidePos]).fadeOut(250);
    $(elements[newSlidePos]).fadeIn(250);
    slidePos = newSlidePos;
}
