/* Fun */


$(function(){
	
	$(".section").each(function (i) {
		
		// starting item
		$(this).find(".item").hide();
		if( !$(this).hasClass("no-random-start") ) {
			var randItem = Math.ceil(( $(this).find(".item").length )*Math.random())-1;
			var currentItem = $(this).find(".item:eq("+randItem+")");
		} else {
			var currentItem = $(this).find(".item:first");
		}
		currentItem.show();
		
		// generate links to each item
		var htmlList = "";
		$(this).find(".item").each(function (j) {
			htmlList += "<li>"
			htmlList += "<a";
			if( $(this).find("h2").text() == currentItem.find("h2").text() ) htmlList += " class='active'";
			htmlList += " href='Javascript:selectItem("+i+", "+j+")'";
			htmlList +=	">";
			htmlList += (j+1) + ". ";
			htmlList += $(this).find("h2").text();
			htmlList += "</a>";
			htmlList += "</li>";
		});
		$(this).find(".item-selector").append(htmlList);
		
		//automatic slideshow
		if( !$(this).hasClass("no-rotate") ) {
			slideshow(i);
		}

    });
	
	//disable on rollover
	$(".section").hover(
      function () {
        $(this).toggleClass("disabled");
      }, 
      function () {
        $(this).toggleClass("disabled");
      }
    );
	
});


// on link clik make given item active
function selectItem( section, item ) {
	if( $(".section:eq("+section+") .item:eq("+item+"):visible").length > 0 ) {
		//alert( $(".section:eq("+section+") .item:eq("+item+") a").attr("href") );
		window.location.href = $(".section:eq("+section+") .item:eq("+item+") a").attr("href");
	} else {
		$(".section:eq("+section+") .item").hide();
		$(".section:eq("+section+") .item:eq("+item+")").show();
		$(".section:eq("+section+") .item-selector a").removeClass('active');
		$(".section:eq("+section+") .item-selector li:eq("+item+") a").addClass('active');
	}
}


//image slideshow
var fadeSpeed = 600;
var transitionSpeed = 7000;

function slideshow(i) {
	//setTimeout( fadeOut, transitionSpeed, [i] );
	setTimeout('fadeOut('+i+')',transitionSpeed);
}

function fadeOut(i) {
	if( !$(".section:eq("+i+")").hasClass("disabled") ) {
		$(".section:eq("+i+") .item:visible").fadeOut(fadeSpeed, fadeIn(i) );
	} else {
		fadeIn(i);
	}
}

function fadeIn(i) {
	var currItem;
	$(".section:eq("+i+") .item-selector a").each(function (j) { 
		if( $(this).hasClass("active") ) {
			currItem = j;
		}
	});
	if( !$(".section:eq("+i+")").hasClass("disabled") ) {
		if( currItem >= ($(".section:eq("+i+") .item").length)-1 ) {
			currItem = 0;
		} else {
			currItem++;
		}
		
		$(".section:eq("+i+") .item").hide();
		$(".section:eq("+i+") .item-selector a").removeClass('active');
		$(".section:eq("+i+") .item-selector li:eq("+currItem+") a").addClass('active');
	}
	$(".section:eq("+i+") .item:eq("+currItem+")").fadeIn(fadeSpeed, slideshow(i));
	
}
















