var isScrolling = false;
var isOverShowcase = false;
var timeout = 7000;
var isHome = false;

function timerLoop() {
	$('.showcase').everyTime(timeout,"showcaseTimer",function() { loop()});
}

function showcase_prev() {
	$('#box1 div.tabs').tabs().prev();
	$('.showcase').oneTime(200, function() { $('#box2 div.tabs').tabs().prev(); });
	$('.showcase').oneTime(400, function() { $('#box3 div.tabs').tabs().prev(); });
	$('.showcase').oneTime(600, function() { $('#box4 div.tabs').tabs().prev(); });
	$('.showcase').oneTime(800, function() { $('#box5 div.tabs').tabs().prev(); });
	$('.showcase').oneTime(1000, function() { $('#box6 div.tabs').tabs().prev(); });
}

function showcase_next() {
	$('#box1 div.tabs').tabs().next();
	$('.showcase').oneTime(200, function() { $('#box2 div.tabs').tabs().next(); });
	$('.showcase').oneTime(400, function() { $('#box3 div.tabs').tabs().next(); });
	$('.showcase').oneTime(600, function() { $('#box4 div.tabs').tabs().next(); });
	$('.showcase').oneTime(800, function() { $('#box5 div.tabs').tabs().next(); });
	$('.showcase').oneTime(1000, function() { $('#box6 div.tabs').tabs().next(); });
}

function loop() {
	if (!isOverShowcase && !isScrolling) {
		showcase_next();	
	}
}

	
function setIsOverShowcase (boolValue) {
	isOverShowcase = boolValue;
}

$(function() {

	// jQuery tools http://flowplayer.org/tools/tabs/slideshow.html
	$("div.tabs").tabs(".images > div", {

		// enable "cross-fading" effect
		effect: 'fade',
		fadeOutSpeed: "slow",

		// start from the beginning after the last tab
		rotate: true

	});

	/** effetto click **/
	var prev = $(".showcase-nav li.prev");
	var next = $(".showcase-nav li.next");
	next.mousedown(function() {
		$(this).css('top','2px');
	});
	next.mouseup(function() {
		$(this).css('top','0');
	});
	prev.mousedown(function() {
		$(this).css('top','2px');
	});
	prev.mouseup(function() {
		$(this).css('top','0');
	});
	
	// ferma lo slideshow onmouseover prev e next
	next.hover(
		function() {		
			$(".showcase").stopTime("showcaseTimer");
		},
		function() {
			timerLoop();
		}
	);
	prev.hover(
		function() {
			$(".showcase").stopTime("showcaseTimer");
		},
		function() {
			timerLoop();
		}
	);
	
	next.click(function() {
		$(".showcase").stopTime("showcaseTimer");
		showcase_next();
	});
	prev.click(function() {
		$(".showcase").stopTime("showcaseTimer");
		showcase_prev();
	});
	
	
	$("a[href=#home]").click(function() {
		isHome = true;
		//alert("home");
	});

	/* Funzione per l'overlay della privacy notice */
	// if the function argument is given to overlay,
	// it is assumed to be the onBeforeLoad event listener
	$(".disclaimer a[rel]").overlay({

		expose: 'gray',
		effect: 'apple',

		onBeforeLoad: function() {

			// grab wrapper element inside content
			var wrap = this.getContent().find(".contentWrap");

			// load the page specified in the trigger
			wrap.load(this.getTrigger().attr("href"));
		}

	});
});

var images = new Array();

images[0] = "images/showcase/tdm-flash_01.jpg";
images[1] = "images/showcase/tdm-flash_02.jpg";
images[2] = "images/showcase/tdm-flash_03.jpg";
images[3] = "images/showcase/tdm-flash_04.jpg";
images[4] = "images/showcase/tdm-flash_05.jpg";
images[5] = "images/showcase/tdm-flash_06.jpg";
images[6] = "images/showcase/tdm-jquery_01.jpg";
images[7] = "images/showcase/tdm-jquery_02.jpg";
images[8] = "images/showcase/tdm-jquery_03.jpg";
images[9] = "images/showcase/tdm-jquery_04.jpg";
images[10] = "images/showcase/tdm-jquery_05.jpg";
images[11] = "images/showcase/tdm-jquery_06.jpg";
images[12] = "images/showcase/ipad_01.jpg";
images[13] = "images/showcase/ipad_02.jpg";
images[14] = "images/showcase/ipad_03.jpg";
images[15] = "images/showcase/ipad_04.jpg";
images[16] = "images/showcase/ipad_05.jpg";
images[17] = "images/showcase/ipad_06.jpg";
images[18] = "images/showcase/iphone_01.jpg";
images[19] = "images/showcase/iphone_02.jpg";
images[20] = "images/showcase/iphone_03.jpg";
images[21] = "images/showcase/iphone_04.jpg";
images[22] = "images/showcase/iphone_05.jpg";
images[23] = "images/showcase/iphone_06.jpg";

/**
 *	Preload function
 */
(function($) {
	var imgList = [];
	$.extend({
		preload: function(imgArr, option) {
			var setting = $.extend({
				init: function(loaded, total) {},
				loaded: function(img, loaded, total) {},
				loaded_all: function(loaded, total) {}
			}, option);
			var total = imgArr.length;
			var loaded = 0;
			
			setting.init(0, total);
			for(var i in imgArr) {
				imgList.push($("<img />")
					.attr("src", imgArr[i])
					.load(function() {
						loaded++;
						setting.loaded(this, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);
						}
					})
				);
			}
			
		}
	});

  $.preload(images,{
		init: function(loaded, total) {
			//$("#indicator").html("Loaded: "+loaded+"/"+total);
		},
		loaded: function(img, loaded, total) {
			//html += "Loaded: "+loaded+"/"+total+" "+img.src+"<br />";
			//$("#indicator").html(html);
		},
		loaded_all: function(loaded, total) {
			timerLoop();
		}
	});

})(jQuery);




