var HIDE_TIMEOUT = 2000;
var NEXT_TIMEOUT = 4000;

var baseHref = "";
var openedBox = true;
var overBanner = false;
var closeTimer = false;
var slideshowInterval = false;

$(function(){
	$("#footer_back").hover(
		function () {
			$(this).attr("src", baseHref + "/assets/images/back_OVER.png");
		}, 
		function () {
			$(this).attr("src", baseHref + "/assets/images/back.png");
		}
	);
	$("#footer_home").hover(
		function () {
			$(this).attr("src", baseHref + "/assets/images/home_OVER.png");
		}, 
		function () {
			$(this).attr("src", baseHref + "/assets/images/home.png");
		}
	);
	$("#footer_prev").hover(
		function () {
			$(this).attr("src", baseHref + "/assets/images/prev_OVER.png");
		}, 
		function () {
			$(this).attr("src", baseHref + "/assets/images/prev.png");
		}
	);
	$("#footer_next").hover(
		function () {
			$(this).attr("src", baseHref + "/assets/images/next_OVER.png");
		}, 
		function () {
			$(this).attr("src", baseHref + "/assets/images/next.png");
		}
	);
	
	/* HOVER MENU */
	function onMenuOver(num) {
		$("#linea_menu_" + num).attr("src", baseHref + "/assets/images/menu_linea_0" + num + "_OVER.gif");
		$("#img_menu_" + num).attr("src", baseHref + "/assets/images/0" + num + "_OVER.png");
	}
	function onMenuOut(num) {
		$("#linea_menu_" + num).attr("src", baseHref + "/assets/images/menu_linea.gif");
		$("#img_menu_" + num).attr("src", baseHref + "/assets/images/0" + num + ".png");
	}
	
	$("#anum_menu_1").hover(
		function () {
			onMenuOver(1);
		}, 
		function () {
			onMenuOut(1);
		}
	);
	
	$("#avoce_menu_1").hover(
		function () {
			onMenuOver(1);
		}, 
		function () {
			onMenuOut(1);
		}
	);
	
	$("#anum_menu_2").hover(
		function () {
			onMenuOver(2);
		}, 
		function () {
			onMenuOut(2);
		}
	);
	
	$("#avoce_menu_2").hover(
		function () {
			onMenuOver(2);
		}, 
		function () {
			onMenuOut(2);
		}
	);
	
	$("#anum_menu_3").hover(
		function () {
			onMenuOver(3);
		}, 
		function () {
			onMenuOut(3);
		}
	);
	
	$("#avoce_menu_3").hover(
		function () {
			onMenuOver(3);
		}, 
		function () {
			onMenuOut(3);
		}
	);
	
	$("#anum_menu_4").hover(
		function () {
			onMenuOver(4);
		}, 
		function () {
			onMenuOut(4);
		}
	);
	
	$("#avoce_menu_4").hover(
		function () {
			onMenuOver(4);
		}, 
		function () {
			onMenuOut(4);
		}
	);
	
	$("#anum_menu_5").hover(
		function () {
			onMenuOver(5);
		}, 
		function () {
			onMenuOut(5);
		}
	);
	
	$("#avoce_menu_5").hover(
		function () {
			onMenuOver(5);
		}, 
		function () {
			onMenuOut(5);
		}
	);
	
	$("#anum_menu_6").hover(
		function () {
			onMenuOver(6);
		}, 
		function () {
			onMenuOut(6);
		}
	);
	
	$("#avoce_menu_6").hover(
		function () {
			onMenuOver(6);
		}, 
		function () {
			onMenuOut(6);
		}
	);
	
	$("#anum_menu_7").hover(
		function () {
			onMenuOver(7);
		}, 
		function () {
			onMenuOut(7);
		}
	);
	
	$("#avoce_menu_7").hover(
		function () {
			onMenuOver(7);
		}, 
		function () {
			onMenuOut(7);
		}
	);
	
	$("#anum_menu_8").hover(
		function () {
			onMenuOver(8);
		}, 
		function () {
			onMenuOut(8);
		}
	);
	
	$("#avoce_menu_8").hover(
		function () {
			onMenuOver(8);
		}, 
		function () {
			onMenuOut(8);
		}
	);
	
	// OVER ABSTRACT IN LISTA
	function onAbstractOver(abstractContent) {
		if (abstractContent.hasClass("product_marker")) {
			abstractContent.css("background-color", "rgb(226, 32, 122)");
		} else if (abstractContent.hasClass("supplier_marker")) {
			abstractContent.css("background-color", "rgb(108, 52, 127)");
		} else if (abstractContent.hasClass("solution_marker")) {
			abstractContent.css("background-color", "rgb(136, 191, 13)");
		} else if (abstractContent.hasClass("news_marker")) {
			abstractContent.css("background-color", "rgb(242, 68, 5)");
		}
		$(".content_body", abstractContent).css("color", "rgb(255, 255, 255)");
		$(".content_body .content_title a", abstractContent).css("color", "rgb(255, 255, 255)");
	}
	function onAbstractOut(abstractContent) {
		abstractContent.css("background-color", "rgb(255, 255, 255)");
		$(".content_body", abstractContent).css("color", "rgb(61, 61, 65)");
		$(".content_body .content_title a", abstractContent).css("color", "rgb(61, 61, 65)");
	}
	
	$(".abstract_content").hover(
		function () {
			onAbstractOver($(this));
		}, 
		function () {
			onAbstractOut($(this));
		}
	);
	
	// OVER ABSTRACT IN HOME
	function onContentOver(abstractContent) {
		$(".content_selection", abstractContent).show();
	}
	function onContentOut(abstractContent) {
		$(".content_selection", abstractContent).hide();
	}
	
	$(".index_abstract_content").hover(
		function () {
			onContentOver($(this));
		}, 
		function () {
			onContentOut($(this));
		}
	);
	
	$(".index_abstract_small_content").hover(
		function () {
			onContentOver($(this));
		}, 
		function () {
			onContentOut($(this));
		}
	);
	
	$(".comearrivare").hover(
		function () {
			onContentOver($(this));
		}, 
		function () {
			onContentOut($(this));
		}
	);
	
	$(".gallery_item_content").hover(
		function () {
			onContentOver($(this));
		}, 
		function () {
			onContentOut($(this));
		}
	);
	
	$(".video_item_content").hover(
		function () {
			onContentOver($(this));
		}, 
		function () {
			onContentOut($(this));
		}
	);
	
	$("input#search").ezpz_hint();
	$("input#identity").ezpz_hint();
	$("input#password").ezpz_hint();
});

function getBannerOfObject(element) {
	return 1 + $("#banner_selector_content #selector #selector_choices li").index(element);
}

function showBannerBox() {
	openedBox = true;
	
	$("#banner_selector_content #selector").animate({ 
			bottom: "0px",
			opacity: 1.0
		}, 500 );
	$("#banner_selector_content #link").animate({ 
			bottom: "0px",
			opacity: 1.0
		}, 500 );

	// devo far partire un timer per la chiusura dei box
	if (!overBanner) {
		closeTimer = setTimeout(hideBannerBox, HIDE_TIMEOUT);
	}
}

function hideBannerBox() {
	openedBox = false;

	clearTimeout(closeTimer);
	closeTimer = false;
	
	$("#banner_selector_content #selector").animate({ 
			bottom: "-" + $("#banner_selector_content #selector").height() + "px",
			opacity: 0.0
		}, 500 );
	$("#banner_selector_content #link").animate({ 
			bottom: "-" + $("#banner_selector_content #link").height() + "px",
			opacity: 0.0
		}, 500 );
}

function showBanner(n) {
	stopSlideShow();
	
	// setto tutte le classi non attive
	$("#banner_selector_content #selector #selector_title a").addClass("not_active");
	$("#banner_selector_content #link a").addClass("not_active");

	// rimuovo le classi attive
	$("#banner_selector_content #selector #selector_title a").removeClass("active");
	$("#banner_selector_content #link a").removeClass("active");

	// seleziono le classi attive
	if (n > 0 && n < 6) {
		var title = $("#banner_selector_content #selector #selector_title a").get(n - 1);
		$(title).addClass("active");
		$(title).removeClass("not_active");
		var link = $("#banner_selector_content #link a").get(n - 1);
		$(link).addClass("active");
		$(link).removeClass("not_active");
	}

	$("#banner_selector_content #selector #selector_choices li").removeClass("selected_banner");
	$("#banner_selector_content #selector #selector_choices li").css("cursor", "pointer");
	
	// simulo mouse out di tutti i li con img
	var imgs = $("#banner_selector_content #selector #selector_choices li img");
	for (var i = 0; i < imgs.length; i++) {
		if (i == n - 1) {
			$(imgs[i]).attr("src", baseHref + "/assets/images/" + (i + 1) + "_OVER.png");
		} else {
			$(imgs[i]).attr("src", baseHref + "/assets/images/" + (i + 1) + ".png");
		}
	}
	
	$($("#banner_selector_content #selector #selector_choices li").get(n - 1)).addClass("selected_banner");
	$("#banner_selector_content #selector #selector_choices li.selected_banner").css("cursor", "auto");
	
	var $active = $("#banner_selector_content #banner div.active_banner");
	var $next = $($("#banner_selector_content #banner div").get(n - 1));
	
	$active.addClass("last_active_banner");
	$active.removeClass("active_banner");
	
	showBannerBox();
	
	$next.css({opacity: 0.0})
	    .addClass("active_banner")
	    .animate({opacity: 1.0}, 1000, function() {
	    		$active.removeClass("last_active_banner");
	    	});
	
	if (!overBanner) {
		startSlideShow();
	}
}

function initHomeBanner() {
	showBannerBox();
	
	$("body").mousemove(function(){
			if (!openedBox) {
				showBannerBox();	
			} else if (!overBanner) {
				clearTimeout(closeTimer);
				closeTimer = setTimeout(hideBannerBox, HIDE_TIMEOUT);
			}
		});
	
	$("#banner_selector_content").hover(function(){
			// stoppo il timer
			clearTimeout(closeTimer);
			closeTimer = false;
			stopSlideShow();
			overBanner = true;
		}, function () {
			// riavvio il timer
			closeTimer = setTimeout(hideBannerBox, HIDE_TIMEOUT);
			startSlideShow();
			overBanner = false;
		});

	$("#banner_selector_content #selector #selector_choices li").hover(function () {
			// over
			if (!$(this).hasClass("selected_banner")) {
				var i = getBannerOfObject(this);
				if (i > 0 && i < 6) {
					$("img", this).attr("src", baseHref + "/assets/images/" + i + "_OVER.png");
				}
			}
		}, function () {
			// out
			if (!$(this).hasClass("selected_banner")) {
				var i = getBannerOfObject(this);
				if (i > 0 && i < 6) {
					$("img", this).attr("src", baseHref + "/assets/images/" + i + ".png");
				}
			}
		});

	$("#banner_selector_content #selector #selector_choices li").click(function () {
			if (!$(this).hasClass("selected_banner")) {
				var i = getBannerOfObject(this);
				showBanner(i);
			}
		});

	$("#banner_selector_content #selector #selector_choices li").css("cursor", "pointer");
	$("#banner_selector_content #selector #selector_choices li.selected_banner").css("cursor", "auto");
	
	startSlideShow();
}

function startSlideShow() {
	slideshowInterval = setTimeout(nextSlide, NEXT_TIMEOUT);
}

function stopSlideShow() {
	clearTimeout(slideshowInterval);
	slideshowInterval = false;
}

function nextSlide() {
	var max = $("#banner_selector_content #selector #selector_choices li").length;
	
	if (max > 1) {
		var n = getBannerOfObject($("#banner_selector_content #selector #selector_choices li.selected_banner"));
		n++;
		
		if (n > max) {
			n = 1;
		}
		
		showBanner(n);
	}
}

function showContentBannerBox() {
	openedBox = true;
	
	$("#banner_box_content #banner_header").animate({ 
			bottom: "0px",
			opacity: 1.0
		}, 500 );

	// devo far partire un timer per la chiusura dei box
	closeTimer = setTimeout(hideContentBannerBox, HIDE_TIMEOUT);
}

function hideContentBannerBox() {
	openedBox = false;

	clearTimeout(closeTimer);
	closeTimer = false;
	
	$("#banner_box_content #banner_header").animate({ 
			bottom: "-" + (10 + $("#banner_box_content #banner_header").height()) + "px",
			opacity: 0.0
		}, 500 );
}

function initContentBanner() {
	showContentBannerBox();

	$("body").mousemove(function(){
			if (!openedBox) {
				showContentBannerBox();	
			} else if (!overBanner) {
				clearTimeout(closeTimer);
				closeTimer = setTimeout(hideContentBannerBox, HIDE_TIMEOUT);
			}
		});

	$("#banner_box_content").hover(function(){
			// stoppo il timer
			clearTimeout(closeTimer);
			closeTimer = false;
			overBanner = true;
		}, function () {
			// riavvio il timer
			closeTimer = setTimeout(hideContentBannerBox, HIDE_TIMEOUT);
			overBanner = false;
		});
}

function addRepeaterItem(repeater) {
	var repeaterCount = getRepeaterItemsCount(repeater);
	var template = getRepeaterItemTemplate(repeater); 
	var item = template.clone();

	// annullo lo stile invisibile con posizione assoluta
	item.removeAttr("style");
	
	// adesso sostituisco il numero 0 del template con il numero della riga attuale
	updateRepeaterItemIndex(repeater, item, repeaterCount);
	
	// rendo aggiunto il check dell'item
	$("[name='form." + repeater.attr("id") + "_" + repeaterCount + "_check_']", item).attr("checked", "true");
	$("[name='form." + repeater.attr("id") + "_" + repeaterCount + "_check']", item).val("true");
	
	// aggiorno il contatore
	var count = $("[name='form." + repeater.attr("id") + "_count']").val();
	$("[name='form." + repeater.attr("id") + "_count']").val(++count);
	
	// poi la copio e la metto in fondo alla table del repeater
	item.appendTo(repeater.children().first());
}

function moveUpRepeaterItem(itemElement) {
	var repeater = getRepeaterFromItemElement(itemElement);
	var item = getRepeaterItemFromItemElement(itemElement);
	var index = getRepeaterItemIndex(repeater, item);
	if (index > 1) {
		var item = getRepeaterItem(repeater, index);
		var item2 = getRepeaterItem(repeater, index - 1);
		
		// memorizzo lo stato di tutti i radio
		var checkeds = getCheckeds(item2);
		
		updateRepeaterItemIndex(repeater, item, index - 1);
		updateRepeaterItemIndex(repeater, item2, index);
		item.swap(item2);
		
		// ripristino lo stato di tutti i radio
		setCheckeds(item2, checkeds);
	}
}

function moveDownRepeaterItem(itemElement) {
	var repeater = getRepeaterFromItemElement(itemElement);
	var repeaterCount = getRepeaterItemsCount(repeater);
	var item = getRepeaterItemFromItemElement(itemElement);
	var index = getRepeaterItemIndex(repeater, item);
	if (index < repeaterCount - 1) {
		var item = getRepeaterItem(repeater, index);
		var item2 = getRepeaterItem(repeater, index + 1);
		
		// memorizzo lo stato di tutti i radio
		var checkeds = getCheckeds(item2);
		
		updateRepeaterItemIndex(repeater, item, index + 1);
		updateRepeaterItemIndex(repeater, item2, index);
		item.swap(item2);
		
		// ripristino lo stato di tutti i radio
		setCheckeds(item2, checkeds);
	}
}

function getCheckeds(item) {
    var repeater = getRepeaterFromItemElement(item);
    var index = getRepeaterItemIndex(repeater, item);
    var checkeds = new Array();
    $(":radio", item).each(function (i) {
		checkeds.push($(this).attr("checked"));
	});
    return checkeds;
}

function setCheckeds(item, checkeds) {
    var repeater = getRepeaterFromItemElement(item);
    var index = getRepeaterItemIndex(repeater, item);
    var i = 0;
    $(":radio", item).each(function (i) {
		$(this).attr("checked", checkeds[i++]);
	});
}

function updateRepeaterItemIndex(repeater, item, index) {
	
	// adesso sostituisco il numero 0 del template con il numero della riga attuale
	item.find(".repeater_count").html("#" + index);
	
	// sostituisco tutti gli input che hanno il nome che inizia per il nome del repeater_0_ con il nome del repeater_###_
	var key = "form." + repeater.attr("id") + "_";
	$("[name^='" + key + "']", item).each(function (i) {
		var oldName = $(this).attr("name");
		var first = oldName.substring(0, key.length);
		
		var second = oldName.substring(key.length);
		second = second.substring(second.indexOf("_"));
		
		$(this).attr("name", first + index + second);
	});	
}

jQuery.fn.swap = function(b) {
    b = jQuery(b)[0];
    var a = this[0];

    var t = a.parentNode.insertBefore(document.createTextNode(""), a);
    b.parentNode.insertBefore(a, b);
    t.parentNode.insertBefore(b, t);
    t.parentNode.removeChild(t);

    return this;
};

function swapRepeaterItems(a, b) {
    var aparent= a.parentNode;
    var asibling= a.nextSibling===b? a : a.nextSibling;
    b.parentNode.insertBefore(a, b);
    aparent.insertBefore(b, asibling);
}

function getRepeaterItem(repeater, index) {
	return $(repeater.children().first().children().get(index));
}

function getRepeaterItemIndex(repeater, item) {
	return item.index();
}

function getRepeaterFromItemElement(itemElement) {
	return itemElement.parents(".repeater");
}

function getRepeaterItemFromItemElement(itemElement) {
	return itemElement.parents(".repeater_item");
}

function getRepeaterItemsCount(repeater) {
	return repeater.children().first().children().length;
}

function getRepeaterItemTemplate(repeater) {
	return repeater.children().first().children().first();
}
