
$(document).ready(function() {	
	
	// Show Banner
	$(".main_image .desc").show();
	$(".main_image .block").animate({ opacity: 0.85 }, 1 );
	
	// Activate first item
	$(".image_thumb ul li:first").addClass('active');
	
	// Auto loop
	$(".image_thumb ul li").click(function(){ 
		showcaseItemClicked(this, 'auto');
	});
	
	// Manual click
	$(".image_thumb ul li").mouseup(function(){ 
		showcaseItemClicked(this, 'manual');
	});
	
	if (false) {
		// Hover
		$(".image_thumb ul li").hover(function(){
			showcaseItemClicked(this, 'manual');
			$(this).addClass('hover');
			}, function() {
			$(this).removeClass('hover');
		});
	}
	
	// Toggle Teaser
	$("a.collapse").click(function(){
		$(".main_image .block").slideToggle();
		$("a.collapse").toggleClass("show");
	});
	
	// Rotate items
	tRotate = setTimeout("showcaseNext();", delayRotate);
	
});

function showcaseItemClicked(el, mode) {
	// Stop rotate if click is manual
	if (mode == 'manual') {
		clearTimeout(tRotate);
		tRotate = setTimeout("showcaseNext();", delayStopped);
	}
	
	// Set Variables
	var imgAlt = $(el).find('img').attr("alt");
	var imgTitle = $(el).find('a').attr("href");
	var imgDesc = $(el).find('.block').html();
	var imgDescHeight = $(".main_image").find('.block').height();
	
	if ($(el).is(".active")) {
		return false;
	} else {
		// Animate the Teaser
		$(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() {
			$(".main_image .block").html(imgDesc).animate({ opacity: 0.85,	marginBottom: "0" }, 250 );
			$(".main_image img").fadeOut(300, function(){
				//$(".main_image .block").html(imgDesc).animate({ opacity: 0.85,	marginBottom: "0" }, 250 );
				$(this).attr({ src: imgTitle , alt: imgAlt});
				$(this).fadeIn(300);
			});
		});
	}
	
	$(".image_thumb ul li").removeClass('active');
	$(el).addClass('active');
	return false;
}

function showcaseNext() {
	var currentIndex 	= 0;
	var totalIndex 		= $(".image_thumb ul li").length;
	$(".image_thumb ul li").each(function(){
		if ($(this).hasClass("active")) {
			$(this).removeClass("active");
			return false;
		}
		currentIndex++;
	});
	
	// choose next index
	currentIndex++;
	if (currentIndex > totalIndex - 1)
		var nextIndex = 1;
	else
		var nextIndex = currentIndex + 1;
	
	// activate next index
	$(".image_thumb ul li:nth-child("+nextIndex+")").click();
	
	// Rotate items
	tRotate = setTimeout("showcaseNext();", delayRotate);
}


function addScript(url) {
	var obj = document.createElement("script");
	obj.type = "text/javascript";
	obj.src = url;
	document.body.appendChild(obj);
}

function addStyleSheet(url, id) {
	if (id) {
		// Update CSS
		var obj = document.getElementById(id);
		if (obj.href != baseurl+url)
			obj.href = url;
	} else {
		// Add CSS
		var obj = document.createElement("link");
		obj.type = "text/css";
		obj.rel = "stylesheet";
		obj.href = url;
		if (id)
			obj.id = id;
		document.body.appendChild(obj);
	}
}

