/**
 * @copyright Copyright (c) 2008 cron IT GmbH, All rights reserved
 * @author  Ernesto Baschny <ernst@cron-it.de>
 * @version $Id: 3cols.js,v 1.2 2009/01/22 20:55:07 ernst Exp $
 * @package cron_dpsa
 *
 * requires jquery
 */
 
function crossFade(src, dst, repeatTime, root) {
	if (src[0].tagName=='IMG') {
		root.data('isFading', 1);
		src.fadeOut(1200);
		dst.fadeIn(1200, function() {
			root.data('isFading', 0);
			if (repeatTime>0) {
				setTimeout(function() { crossFadeImages(repeatTime, root); }, repeatTime);
			}
		});	
	} else {
		src.removeClass('current').addClass('hidden');
		dst.removeClass('hidden').addClass('current');
		if (repeatTime>0) {
			setTimeout(function() { crossFadeImages(repeatTime, root); }, repeatTime);
		}
	}
	return;
}

function crossFadeImages(repeatTime, root) {
	if (root.data('isFading')) { return; }
	// Current element to fade;
	var fadeCurrent = parseInt(root.data('fadecurrent'));
	if (!fadeCurrent) {
		root.data('fadecurrent', 1);
		fadeCurrent = 1;
	}
	var fadeOut = root.find(".item-" + fadeCurrent);
	// Check if there is one more, or if we need to start from beginning
	var fadeIn = root.find(".item-" + (fadeCurrent+1));
	if (!fadeIn[0]) {
		fadeIn = root.find(".item-1");
		root.data('fadecurrent', 1);
	} else {
		root.data('fadecurrent', fadeCurrent+1);
	}
	if (fadeIn) {
		crossFade(fadeOut, fadeIn, repeatTime, root);
	}
	return;
}

function autoFadeElements(repeatTime) {
	$(".crossfade").each(function(i) {
		crossFadeImages(repeatTime, $(this));
	});
}

$(document).ready(function() {

	$(".highlightarea").mouseover(function(){
      $(this).addClass('highlightover');
    }).mouseout(function(){
      $(this).removeClass('highlightover');
    });

	setTimeout(function() { autoFadeElements(2000); }, 2000);

});